Skip to content

Commit 4057db8

Browse files
Frankonlyzjshen14
authored andcommitted
Add secureConnect config for ioctl (iotexproject#1061)
1 parent a89900b commit 4057db8

File tree

12 files changed

+62
-43
lines changed

12 files changed

+62
-43
lines changed

cli/ioctl/cmd/account/account.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ var AccountCmd = &cobra.Command{
4141
Args: cobra.MinimumNArgs(1),
4242
}
4343

44+
var insecure bool
45+
4446
func init() {
4547
AccountCmd.AddCommand(accountBalanceCmd)
4648
AccountCmd.AddCommand(accountCreateCmd)
@@ -54,8 +56,8 @@ func init() {
5456
AccountCmd.AddCommand(accountUpdateCmd)
5557
AccountCmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint",
5658
config.ReadConfig.Endpoint, "set endpoint for once")
57-
AccountCmd.PersistentFlags().BoolVar(&config.IsInsecure, "insecure",
58-
false, "connect endpoint with insecure option")
59+
AccountCmd.PersistentFlags().BoolVar(&config.Insecure, "insecure", config.Insecure,
60+
"insecure connection for once")
5961
}
6062

6163
// KsAccountToPrivateKey generates our PrivateKey interface from Keystore account
@@ -82,7 +84,7 @@ func KsAccountToPrivateKey(signer, password string) (keypair.PrivateKey, error)
8284

8385
// GetAccountMeta gets account metadata
8486
func GetAccountMeta(addr string) (*iotextypes.AccountMeta, error) {
85-
conn, err := util.ConnectToEndpoint(config.IsInsecure)
87+
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
8688
if err != nil {
8789
return nil, err
8890
}

cli/ioctl/cmd/action/action.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ var ActionCmd = &cobra.Command{
4545
Args: cobra.MinimumNArgs(1),
4646
}
4747

48+
var insecure bool
49+
4850
func init() {
4951
ActionCmd.AddCommand(actionHashCmd)
5052
ActionCmd.AddCommand(actionTransferCmd)
@@ -54,10 +56,10 @@ func init() {
5456
ActionCmd.AddCommand(actionDepositCmd)
5557
ActionCmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint",
5658
config.ReadConfig.Endpoint, "set endpoint for once")
59+
ActionCmd.PersistentFlags().BoolVar(&config.Insecure, "insecure", config.Insecure,
60+
"insecure connection for once")
5761
setActionFlags(actionTransferCmd, actionDeployCmd, actionInvokeCmd, actionClaimCmd,
5862
actionDepositCmd)
59-
ActionCmd.PersistentFlags().BoolVar(&config.IsInsecure, "insecure",
60-
false, "connect endpoint with insecure option")
6163
}
6264

6365
func setActionFlags(cmds ...*cobra.Command) {
@@ -78,7 +80,7 @@ func setActionFlags(cmds ...*cobra.Command) {
7880

7981
// GetGasPrice gets the suggest gas price
8082
func GetGasPrice() (*big.Int, error) {
81-
conn, err := util.ConnectToEndpoint(config.IsInsecure)
83+
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
8284
if err != nil {
8385
return nil, err
8486
}
@@ -128,7 +130,7 @@ func sendAction(elp action.Envelope) (string, error) {
128130
fmt.Println()
129131

130132
request := &iotexapi.SendActionRequest{Action: selp}
131-
conn, err := util.ConnectToEndpoint(config.IsInsecure)
133+
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
132134
if err != nil {
133135
return "", err
134136
}

cli/ioctl/cmd/action/actionhash.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var actionHashCmd = &cobra.Command{
4646
// getActionByHash gets action of IoTeX Blockchain by hash
4747
func getActionByHash(args []string) (string, error) {
4848
hash := args[0]
49-
conn, err := util.ConnectToEndpoint(config.IsInsecure)
49+
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
5050
if err != nil {
5151
return "", err
5252
}

cli/ioctl/cmd/bc/bc.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@ var BCCmd = &cobra.Command{
2626
Args: cobra.ExactArgs(1),
2727
}
2828

29+
var insecure bool
30+
2931
func init() {
3032
BCCmd.AddCommand(bcBlockCmd)
3133
BCCmd.AddCommand(bcInfoCmd)
3234
BCCmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint",
3335
config.ReadConfig.Endpoint, "set endpoint for once")
34-
BCCmd.PersistentFlags().BoolVar(&config.IsInsecure, "insecure",
35-
false, "connect endpoint with insecure option")
36+
BCCmd.PersistentFlags().BoolVar(&config.Insecure, "insecure", config.Insecure,
37+
"insecure connection for once")
3638
}
3739

3840
// GetChainMeta gets block chain metadata
3941
func GetChainMeta() (*iotextypes.ChainMeta, error) {
40-
conn, err := util.ConnectToEndpoint(config.IsInsecure)
42+
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
4143
if err != nil {
4244
return nil, err
4345
}

cli/ioctl/cmd/bc/bcblock.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func getBlock(args []string) (string, error) {
8484

8585
// GetBlockMetaByHeight gets block metadata by height
8686
func GetBlockMetaByHeight(height uint64) (*iotextypes.BlockMeta, error) {
87-
conn, err := util.ConnectToEndpoint(config.IsInsecure)
87+
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
8888
if err != nil {
8989
return nil, err
9090
}
@@ -115,7 +115,7 @@ func GetBlockMetaByHeight(height uint64) (*iotextypes.BlockMeta, error) {
115115

116116
// GetBlockMetaByHash gets block metadata by hash
117117
func GetBlockMetaByHash(hash string) (*iotextypes.BlockMeta, error) {
118-
conn, err := util.ConnectToEndpoint(config.IsInsecure)
118+
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
119119
if err != nil {
120120
return nil, err
121121
}

cli/ioctl/cmd/config/config.go

+20-16
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,17 @@ var ConfigCmd = &cobra.Command{
4343

4444
// Config defines the config schema
4545
type Config struct {
46-
Endpoint string `yaml:"endpoint"`
47-
Wallet string `yaml:"wallet"`
48-
Aliases map[string]string `yaml:"aliases"`
46+
Wallet string `yaml:"wallet"`
47+
Endpoint string `yaml:"endpoint"`
48+
SecureConnect bool `yaml:"secureConnect"`
49+
Aliases map[string]string `yaml:"aliases"`
4950
}
5051

5152
var (
5253
// ReadConfig represents the current config read from local
5354
ReadConfig Config
54-
// IsInsecure represents the connect option of grpc dial
55-
IsInsecure bool
55+
// Insecure represents the insecure connect option of grpc dial, default is false
56+
Insecure = false
5657
)
5758

5859
func init() {
@@ -63,19 +64,22 @@ func init() {
6364
DefaultConfigFile = ConfigDir + "/config.default"
6465
var err error
6566
ReadConfig, err = LoadConfig()
66-
if err != nil || ReadConfig.Wallet == "" {
67-
if !os.IsNotExist(err) || ReadConfig.Wallet == "" {
68-
ReadConfig.Wallet = ConfigDir
69-
out, err := yaml.Marshal(&ReadConfig)
70-
if err != nil {
71-
log.L().Panic(err.Error())
72-
}
73-
if err := ioutil.WriteFile(DefaultConfigFile, out, 0600); err != nil {
74-
log.L().Panic(fmt.Sprintf("Failed to write to config file %s.", DefaultConfigFile))
75-
}
76-
} else {
67+
if err != nil || len(ReadConfig.Wallet) == 0 {
68+
if err != nil && !os.IsNotExist(err) {
69+
log.L().Panic(err.Error()) // Config file exists but error occurs
70+
}
71+
ReadConfig.Wallet = ConfigDir
72+
if os.IsNotExist(err) {
73+
ReadConfig.SecureConnect = true
74+
}
75+
out, err := yaml.Marshal(&ReadConfig)
76+
if err != nil {
7777
log.L().Panic(err.Error())
7878
}
79+
if err := ioutil.WriteFile(DefaultConfigFile, out, 0600); err != nil {
80+
log.L().Panic(fmt.Sprintf("Failed to write to config file %s.", DefaultConfigFile))
81+
}
82+
7983
}
8084
ConfigCmd.AddCommand(configGetCmd)
8185
ConfigCmd.AddCommand(configSetCmd)

cli/ioctl/cmd/config/configsetget.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ var configSetCmd = &cobra.Command{
6262
},
6363
}
6464

65+
func init() {
66+
configSetCmd.Flags().BoolVar(&Insecure, "insecure", false,
67+
"set insecure connection as default")
68+
}
69+
6570
// Get gets config variable
6671
func Get(arg string) (string, error) {
6772
switch arg {
@@ -71,10 +76,10 @@ func Get(arg string) (string, error) {
7176
if ReadConfig.Endpoint == "" {
7277
return "", ErrEmptyEndpoint
7378
}
74-
return ReadConfig.Endpoint, nil
79+
return fmt.Sprint(ReadConfig.Endpoint, " secure connect(TLS):",
80+
ReadConfig.SecureConnect), nil
7581
case "wallet":
7682
return ReadConfig.Wallet, nil
77-
7883
}
7984
}
8085

@@ -85,9 +90,9 @@ func set(args []string) (string, error) {
8590
return "", ErrConfigNotMatch
8691
case "endpoint":
8792
ReadConfig.Endpoint = args[1]
93+
ReadConfig.SecureConnect = !Insecure
8894
case "wallet":
8995
ReadConfig.Wallet = args[1]
90-
9196
}
9297
out, err := yaml.Marshal(&ReadConfig)
9398
if err != nil {

cli/ioctl/cmd/node/node.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ var NodeCmd = &cobra.Command{
1818
Args: cobra.ExactArgs(1),
1919
}
2020

21+
var insecure bool
22+
2123
func init() {
2224
NodeCmd.AddCommand(nodeDelegateCmd)
2325
NodeCmd.AddCommand(nodeRewardCmd)
2426
NodeCmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint",
2527
config.ReadConfig.Endpoint, "set endpoint for once")
26-
NodeCmd.PersistentFlags().BoolVar(&config.IsInsecure, "insecure",
27-
false, "connect endpoint with insecure option")
28+
NodeCmd.PersistentFlags().BoolVar(&config.Insecure, "insecure", config.Insecure,
29+
"insecure connection for once")
2830
}

cli/ioctl/cmd/node/nodedelegate.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func delegates() (string, error) {
6969
}
7070
epochNum = chainMeta.Epoch.Num
7171
}
72-
conn, err := util.ConnectToEndpoint(config.IsInsecure)
72+
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
7373
if err != nil {
7474
return "", err
7575
}
@@ -126,7 +126,7 @@ func nextDelegates() (string, error) {
126126
return "", err
127127
}
128128
epochNum = chainMeta.Epoch.Num + 1
129-
conn, err := util.ConnectToEndpoint(config.IsInsecure)
129+
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
130130
if err != nil {
131131
return "", err
132132
}

cli/ioctl/cmd/node/nodereward.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var nodeRewardCmd = &cobra.Command{
4343
}
4444

4545
func rewardPool() (string, error) {
46-
conn, err := util.ConnectToEndpoint(config.IsInsecure)
46+
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
4747
if err != nil {
4848
return "", err
4949
}
@@ -92,7 +92,7 @@ func reward(args []string) (string, error) {
9292
if err != nil {
9393
return "", err
9494
}
95-
conn, err := util.ConnectToEndpoint(config.IsInsecure)
95+
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
9696
if err != nil {
9797
return "", err
9898
}

cli/ioctl/cmd/version/version.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ var VersionCmd = &cobra.Command{
3535
},
3636
}
3737

38+
var insecure bool
39+
3840
func init() {
3941
VersionCmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint",
4042
config.ReadConfig.Endpoint, "set endpoint for once")
41-
VersionCmd.PersistentFlags().BoolVar(&config.IsInsecure, "insecure",
42-
false, "connect endpoint with insecure option")
43+
VersionCmd.PersistentFlags().BoolVar(&config.Insecure, "insecure", config.Insecure,
44+
"insecure connection for once")
4345
}
4446

4547
func version() (string, error) {
@@ -51,7 +53,7 @@ func version() (string, error) {
5153
BuildTime: ver.BuildTime,
5254
}
5355
fmt.Printf("Client:\n%+v\n\n", versionInfo)
54-
conn, err := util.ConnectToEndpoint(config.IsInsecure)
56+
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
5557
if err != nil {
5658
return "", err
5759
}

cli/ioctl/util/util.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ const (
3030
)
3131

3232
// ConnectToEndpoint starts a new connection
33-
func ConnectToEndpoint(isInsecure bool) (*grpc.ClientConn, error) {
33+
func ConnectToEndpoint(secure bool) (*grpc.ClientConn, error) {
3434
endpoint := config.ReadConfig.Endpoint
3535
if endpoint == "" {
3636
return nil, fmt.Errorf(`use "ioctl config set endpoint" to config endpoint first`)
3737
}
38-
if isInsecure {
38+
if !secure {
3939
return grpc.Dial(endpoint, grpc.WithInsecure())
4040
}
4141
return grpc.Dial(endpoint, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))

0 commit comments

Comments
 (0)