Skip to content

Commit c3295c0

Browse files
authored
ioctl connects endpoint with TLS by default and add insecure flag (iotexproject#1051)
1 parent 5550ce1 commit c3295c0

File tree

11 files changed

+44
-16
lines changed

11 files changed

+44
-16
lines changed

cli/ioctl/cmd/account/account.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func init() {
5454
AccountCmd.AddCommand(accountUpdateCmd)
5555
AccountCmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint",
5656
config.ReadConfig.Endpoint, "set endpoint for once")
57+
AccountCmd.PersistentFlags().BoolVar(&config.IsInsecure, "insecure",
58+
false, "connect endpoint with insecure option")
5759
}
5860

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

8183
// GetAccountMeta gets account metadata
8284
func GetAccountMeta(addr string) (*iotextypes.AccountMeta, error) {
83-
conn, err := util.ConnectToEndpoint()
85+
conn, err := util.ConnectToEndpoint(config.IsInsecure)
8486
if err != nil {
8587
return nil, err
8688
}

cli/ioctl/cmd/action/action.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ func init() {
5656
config.ReadConfig.Endpoint, "set endpoint for once")
5757
setActionFlags(actionTransferCmd, actionDeployCmd, actionInvokeCmd, actionClaimCmd,
5858
actionDepositCmd)
59+
ActionCmd.PersistentFlags().BoolVar(&config.IsInsecure, "insecure",
60+
false, "connect endpoint with insecure option")
5961
}
6062

6163
func setActionFlags(cmds ...*cobra.Command) {
@@ -76,7 +78,7 @@ func setActionFlags(cmds ...*cobra.Command) {
7678

7779
// GetGasPrice gets the suggest gas price
7880
func GetGasPrice() (*big.Int, error) {
79-
conn, err := util.ConnectToEndpoint()
81+
conn, err := util.ConnectToEndpoint(config.IsInsecure)
8082
if err != nil {
8183
return nil, err
8284
}
@@ -126,7 +128,7 @@ func sendAction(elp action.Envelope) (string, error) {
126128
fmt.Println()
127129

128130
request := &iotexapi.SendActionRequest{Action: selp}
129-
conn, err := util.ConnectToEndpoint()
131+
conn, err := util.ConnectToEndpoint(config.IsInsecure)
130132
if err != nil {
131133
return "", err
132134
}

cli/ioctl/cmd/action/actionhash.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/iotexproject/iotex-address/address"
2222
"github.com/iotexproject/iotex-core/cli/ioctl/cmd/alias"
23+
"github.com/iotexproject/iotex-core/cli/ioctl/cmd/config"
2324
"github.com/iotexproject/iotex-core/cli/ioctl/util"
2425
"github.com/iotexproject/iotex-core/pkg/keypair"
2526
"github.com/iotexproject/iotex-core/pkg/log"
@@ -45,7 +46,7 @@ var actionHashCmd = &cobra.Command{
4546
// getActionByHash gets action of IoTeX Blockchain by hash
4647
func getActionByHash(args []string) (string, error) {
4748
hash := args[0]
48-
conn, err := util.ConnectToEndpoint()
49+
conn, err := util.ConnectToEndpoint(config.IsInsecure)
4950
if err != nil {
5051
return "", err
5152
}

cli/ioctl/cmd/bc/bc.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ func init() {
3131
BCCmd.AddCommand(bcInfoCmd)
3232
BCCmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint",
3333
config.ReadConfig.Endpoint, "set endpoint for once")
34+
BCCmd.PersistentFlags().BoolVar(&config.IsInsecure, "insecure",
35+
false, "connect endpoint with insecure option")
3436
}
3537

3638
// GetChainMeta gets block chain metadata
3739
func GetChainMeta() (*iotextypes.ChainMeta, error) {
38-
conn, err := util.ConnectToEndpoint()
40+
conn, err := util.ConnectToEndpoint(config.IsInsecure)
3941
if err != nil {
4042
return nil, err
4143
}

cli/ioctl/cmd/bc/bcblock.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"google.golang.org/grpc/status"
1717

1818
"github.com/iotexproject/iotex-core/cli/ioctl/cmd/action"
19+
"github.com/iotexproject/iotex-core/cli/ioctl/cmd/config"
1920
"github.com/iotexproject/iotex-core/cli/ioctl/util"
2021
"github.com/iotexproject/iotex-core/cli/ioctl/validator"
2122
"github.com/iotexproject/iotex-core/protogen/iotexapi"
@@ -83,7 +84,7 @@ func getBlock(args []string) (string, error) {
8384

8485
// GetBlockMetaByHeight gets block metadata by height
8586
func GetBlockMetaByHeight(height uint64) (*iotextypes.BlockMeta, error) {
86-
conn, err := util.ConnectToEndpoint()
87+
conn, err := util.ConnectToEndpoint(config.IsInsecure)
8788
if err != nil {
8889
return nil, err
8990
}
@@ -114,7 +115,7 @@ func GetBlockMetaByHeight(height uint64) (*iotextypes.BlockMeta, error) {
114115

115116
// GetBlockMetaByHash gets block metadata by hash
116117
func GetBlockMetaByHash(hash string) (*iotextypes.BlockMeta, error) {
117-
conn, err := util.ConnectToEndpoint()
118+
conn, err := util.ConnectToEndpoint(config.IsInsecure)
118119
if err != nil {
119120
return nil, err
120121
}

cli/ioctl/cmd/config/config.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ type Config struct {
4848
Aliases map[string]string `yaml:"aliases"`
4949
}
5050

51-
// ReadConfig represents the current config read from local
52-
var ReadConfig Config
51+
var (
52+
// ReadConfig represents the current config read from local
53+
ReadConfig Config
54+
// IsInsecure represents the connect option of grpc dial
55+
IsInsecure bool
56+
)
5357

5458
func init() {
5559
ConfigDir = os.Getenv("HOME") + "/.config/ioctl/default"

cli/ioctl/cmd/node/node.go

+2
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ func init() {
2323
NodeCmd.AddCommand(nodeRewardCmd)
2424
NodeCmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint",
2525
config.ReadConfig.Endpoint, "set endpoint for once")
26+
NodeCmd.PersistentFlags().BoolVar(&config.IsInsecure, "insecure",
27+
false, "connect endpoint with insecure option")
2628
}

cli/ioctl/cmd/node/nodedelegate.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/iotexproject/iotex-core/action/protocol/poll"
2121
"github.com/iotexproject/iotex-core/cli/ioctl/cmd/alias"
2222
"github.com/iotexproject/iotex-core/cli/ioctl/cmd/bc"
23+
"github.com/iotexproject/iotex-core/cli/ioctl/cmd/config"
2324
"github.com/iotexproject/iotex-core/cli/ioctl/util"
2425
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
2526
"github.com/iotexproject/iotex-core/protogen/iotexapi"
@@ -68,7 +69,7 @@ func delegates() (string, error) {
6869
}
6970
epochNum = chainMeta.Epoch.Num
7071
}
71-
conn, err := util.ConnectToEndpoint()
72+
conn, err := util.ConnectToEndpoint(config.IsInsecure)
7273
if err != nil {
7374
return "", err
7475
}
@@ -125,7 +126,7 @@ func nextDelegates() (string, error) {
125126
return "", err
126127
}
127128
epochNum = chainMeta.Epoch.Num + 1
128-
conn, err := util.ConnectToEndpoint()
129+
conn, err := util.ConnectToEndpoint(config.IsInsecure)
129130
if err != nil {
130131
return "", err
131132
}

cli/ioctl/cmd/node/nodereward.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/iotexproject/iotex-core/action/protocol/rewarding"
1818
"github.com/iotexproject/iotex-core/cli/ioctl/cmd/alias"
19+
"github.com/iotexproject/iotex-core/cli/ioctl/cmd/config"
1920
"github.com/iotexproject/iotex-core/cli/ioctl/util"
2021
"github.com/iotexproject/iotex-core/protogen/iotexapi"
2122
)
@@ -42,7 +43,7 @@ var nodeRewardCmd = &cobra.Command{
4243
}
4344

4445
func rewardPool() (string, error) {
45-
conn, err := util.ConnectToEndpoint()
46+
conn, err := util.ConnectToEndpoint(config.IsInsecure)
4647
if err != nil {
4748
return "", err
4849
}
@@ -91,7 +92,7 @@ func reward(args []string) (string, error) {
9192
if err != nil {
9293
return "", err
9394
}
94-
conn, err := util.ConnectToEndpoint()
95+
conn, err := util.ConnectToEndpoint(config.IsInsecure)
9596
if err != nil {
9697
return "", err
9798
}

cli/ioctl/cmd/version/version.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ var VersionCmd = &cobra.Command{
3535
},
3636
}
3737

38+
func init() {
39+
VersionCmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint",
40+
config.ReadConfig.Endpoint, "set endpoint for once")
41+
VersionCmd.PersistentFlags().BoolVar(&config.IsInsecure, "insecure",
42+
false, "connect endpoint with insecure option")
43+
}
44+
3845
func version() (string, error) {
3946
versionInfo := &iotextypes.ServerMeta{
4047
PackageVersion: ver.PackageVersion,
@@ -44,7 +51,7 @@ func version() (string, error) {
4451
BuildTime: ver.BuildTime,
4552
}
4653
fmt.Printf("Client:\n%+v\n\n", versionInfo)
47-
conn, err := util.ConnectToEndpoint()
54+
conn, err := util.ConnectToEndpoint(config.IsInsecure)
4855
if err != nil {
4956
return "", err
5057
}

cli/ioctl/util/util.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
package util
88

99
import (
10+
"crypto/tls"
1011
"fmt"
1112
"math/big"
1213
"strings"
1314

1415
"github.com/ethereum/go-ethereum/common"
1516
"google.golang.org/grpc"
17+
"google.golang.org/grpc/credentials"
1618

1719
"github.com/iotexproject/iotex-address/address"
1820
"github.com/iotexproject/iotex-core/cli/ioctl/cmd/config"
@@ -28,12 +30,15 @@ const (
2830
)
2931

3032
// ConnectToEndpoint starts a new connection
31-
func ConnectToEndpoint() (*grpc.ClientConn, error) {
33+
func ConnectToEndpoint(isInsecure bool) (*grpc.ClientConn, error) {
3234
endpoint := config.ReadConfig.Endpoint
3335
if endpoint == "" {
3436
return nil, fmt.Errorf(`use "ioctl config set endpoint" to config endpoint first`)
3537
}
36-
return grpc.Dial(endpoint, grpc.WithInsecure())
38+
if isInsecure {
39+
return grpc.Dial(endpoint, grpc.WithInsecure())
40+
}
41+
return grpc.Dial(endpoint, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
3742
}
3843

3944
// StringToRau converts different unit string into Rau big int

0 commit comments

Comments
 (0)