From 425db42a567cef694c9bb0a79a7bc55441405de9 Mon Sep 17 00:00:00 2001 From: huofei <68298506@qq.com> Date: Tue, 7 Feb 2023 03:04:31 +0800 Subject: [PATCH] [ioctl] add checking --insecure in newcmd (#3781) * add insecure error message * error variable for invalid endpoint or insecure --------- Co-authored-by: dustinxie --- ioctl/client.go | 5 +++++ ioctl/newcmd/account/account.go | 7 +++++-- ioctl/newcmd/action/action.go | 7 +++++-- ioctl/newcmd/action/actionhash.go | 16 ++++++++++------ ioctl/newcmd/bc/bc.go | 32 +++++++++++++++++++------------ ioctl/newcmd/bc/bcblock.go | 24 +++++++++++++---------- ioctl/newcmd/bc/bcbucket.go | 7 +++++-- ioctl/newcmd/bc/bcinfo.go | 1 + ioctl/newcmd/node/nodedelegate.go | 19 ++++++++++-------- ioctl/newcmd/node/nodereward.go | 19 ++++++++++++------ ioctl/newcmd/version/version.go | 4 ++++ 11 files changed, 93 insertions(+), 48 deletions(-) diff --git a/ioctl/client.go b/ioctl/client.go index f5640813c2..004c1e2db4 100644 --- a/ioctl/client.go +++ b/ioctl/client.go @@ -37,6 +37,11 @@ const ( _urlPattern = `[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)` ) +var ( + //ErrInvalidEndpointOrInsecure represents that endpoint or insecure is invalid + ErrInvalidEndpointOrInsecure = errors.New("check endpoint or secureConnect in ~/.config/ioctl/default/config.default or cmd flag value if has") +) + type ( // Client defines the interface of an ioctl client Client interface { diff --git a/ioctl/newcmd/account/account.go b/ioctl/newcmd/account/account.go index e42db4973c..e1163e60a9 100644 --- a/ioctl/newcmd/account/account.go +++ b/ioctl/newcmd/account/account.go @@ -24,6 +24,7 @@ import ( "github.com/iotexproject/iotex-proto/golang/iotextypes" "github.com/pkg/errors" "github.com/spf13/cobra" + "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "github.com/iotexproject/iotex-core/ioctl" @@ -178,8 +179,10 @@ func Meta(client ioctl.Client, addr string) (*iotextypes.AccountMeta, error) { response, err := apiServiceClient.GetAccount(ctx, &iotexapi.GetAccountRequest{Address: addr}) if err != nil { - sta, ok := status.FromError(err) - if ok { + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.Unavailable { + return nil, ioctl.ErrInvalidEndpointOrInsecure + } return nil, errors.New(sta.Message()) } return nil, errors.Wrap(err, "failed to invoke GetAccount api") diff --git a/ioctl/newcmd/action/action.go b/ioctl/newcmd/action/action.go index f62e7f93d2..430d984ee3 100644 --- a/ioctl/newcmd/action/action.go +++ b/ioctl/newcmd/action/action.go @@ -18,6 +18,7 @@ import ( "github.com/iotexproject/iotex-proto/golang/iotextypes" "github.com/pkg/errors" "github.com/spf13/cobra" + "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/proto" @@ -206,8 +207,10 @@ func GetWriteCommandFlag(cmd *cobra.Command) (gasPrice, signer, password string, } func handleClientRequestError(err error, apiName string) error { - sta, ok := status.FromError(err) - if ok { + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.Unavailable { + return ioctl.ErrInvalidEndpointOrInsecure + } return errors.New(sta.Message()) } return errors.Wrapf(err, "failed to invoke %s api", apiName) diff --git a/ioctl/newcmd/action/actionhash.go b/ioctl/newcmd/action/actionhash.go index d38844c910..b4c36c453e 100644 --- a/ioctl/newcmd/action/actionhash.go +++ b/ioctl/newcmd/action/actionhash.go @@ -84,8 +84,10 @@ func NewActionHashCmd(client ioctl.Client) *cobra.Command { } response, err := apiServiceClient.GetActions(ctx, requestGetAction) if err != nil { - sta, ok := status.FromError(err) - if ok { + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.Unavailable { + return ioctl.ErrInvalidEndpointOrInsecure + } return errors.New(sta.Message()) } return errors.Wrap(err, "failed to invoke GetActions api") @@ -98,10 +100,12 @@ func NewActionHashCmd(client ioctl.Client) *cobra.Command { requestGetReceipt := &iotexapi.GetReceiptByActionRequest{ActionHash: hash} responseReceipt, err := apiServiceClient.GetReceiptByAction(ctx, requestGetReceipt) if err != nil { - sta, ok := status.FromError(err) - if ok && sta.Code() == codes.NotFound { - message.State = Pending - } else if ok { + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.NotFound { + message.State = Pending + } else if sta.Code() == codes.Unavailable { + return ioctl.ErrInvalidEndpointOrInsecure + } return errors.New(sta.Message()) } return errors.Wrap(err, "failed to invoke GetReceiptByAction api") diff --git a/ioctl/newcmd/bc/bc.go b/ioctl/newcmd/bc/bc.go index 333a6f7c03..157b6a7402 100644 --- a/ioctl/newcmd/bc/bc.go +++ b/ioctl/newcmd/bc/bc.go @@ -60,9 +60,11 @@ func GetChainMeta(client ioctl.Client) (*iotextypes.ChainMeta, error) { } chainMetaResponse, err := apiServiceClient.GetChainMeta(ctx, &iotexapi.GetChainMetaRequest{}) if err != nil { - sta, ok := status.FromError(err) - if ok { - return nil, errors.Wrap(nil, sta.Message()) + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.Unavailable { + return nil, ioctl.ErrInvalidEndpointOrInsecure + } + return nil, errors.New(sta.Message()) } return nil, errors.Wrap(err, "failed to invoke GetChainMeta api") } @@ -82,9 +84,11 @@ func GetEpochMeta(client ioctl.Client, epochNum uint64) (*iotexapi.GetEpochMetaR } epochMetaresponse, err := apiServiceClient.GetEpochMeta(ctx, &iotexapi.GetEpochMetaRequest{EpochNumber: epochNum}) if err != nil { - sta, ok := status.FromError(err) - if ok { - return nil, errors.Wrap(nil, sta.Message()) + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.Unavailable { + return nil, ioctl.ErrInvalidEndpointOrInsecure + } + return nil, errors.New(sta.Message()) } return nil, errors.Wrap(err, "failed to invoke GetEpochMeta api") } @@ -111,10 +115,12 @@ func GetProbationList(client ioctl.Client, epochNum uint64, epochStartHeight uin response, err := apiServiceClient.ReadState(ctx, request) if err != nil { - sta, ok := status.FromError(err) - if ok && sta.Code() == codes.NotFound { - return nil, nil - } else if ok { + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.NotFound { + return nil, nil + } else if sta.Code() == codes.Unavailable { + return nil, ioctl.ErrInvalidEndpointOrInsecure + } return nil, errors.New(sta.Message()) } return nil, errors.Wrap(err, "failed to invoke ReadState api") @@ -152,8 +158,10 @@ func GetBucketList( response, err := apiServiceClient.ReadState(ctx, request) if err != nil { - sta, ok := status.FromError(err) - if ok { + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.Unavailable { + return nil, ioctl.ErrInvalidEndpointOrInsecure + } return nil, errors.New(sta.Message()) } return nil, errors.Wrap(err, "failed to invoke ReadState api") diff --git a/ioctl/newcmd/bc/bcblock.go b/ioctl/newcmd/bc/bcblock.go index c26f27057c..d729cca278 100644 --- a/ioctl/newcmd/bc/bcblock.go +++ b/ioctl/newcmd/bc/bcblock.go @@ -10,19 +10,19 @@ import ( "fmt" "strconv" - "google.golang.org/grpc/status" - "github.com/grpc-ecosystem/go-grpc-middleware/util/metautils" + "github.com/iotexproject/go-pkgs/hash" + "github.com/iotexproject/iotex-proto/golang/iotexapi" + "github.com/iotexproject/iotex-proto/golang/iotextypes" "github.com/pkg/errors" "github.com/spf13/cobra" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" - "github.com/iotexproject/go-pkgs/hash" "github.com/iotexproject/iotex-core/ioctl" "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/util" "github.com/iotexproject/iotex-core/ioctl/validator" - "github.com/iotexproject/iotex-proto/golang/iotexapi" - "github.com/iotexproject/iotex-proto/golang/iotextypes" ) // Multi-language support @@ -117,7 +117,7 @@ func NewBCBlockCmd(c ioctl.Client) *cobra.Command { type blockMessage struct { Node string `json:"node"` Block *iotextypes.BlockMeta `json:"block"` - Actions []actionInfo `json:actions` + Actions []actionInfo `json:"actions"` } type actionInfo struct { @@ -147,8 +147,10 @@ func getActionInfoWithinBlock(cli *iotexapi.APIServiceClient, height uint64, cou response, err := (*cli).GetRawBlocks(ctx, &request) if err != nil { - sta, ok := status.FromError(err) - if ok { + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.Unavailable { + return nil, ioctl.ErrInvalidEndpointOrInsecure + } return nil, errors.New(sta.Message()) } return nil, errors.Wrap(err, "failed to invoke GetRawBlocks api") @@ -171,8 +173,10 @@ func getBlockMeta(cli *iotexapi.APIServiceClient, request *iotexapi.GetBlockMeta response, err := (*cli).GetBlockMetas(ctx, request) if err != nil { - sta, ok := status.FromError(err) - if ok { + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.Unavailable { + return nil, ioctl.ErrInvalidEndpointOrInsecure + } return nil, errors.New(sta.Message()) } return nil, errors.Wrap(err, "failed to invoke GetBlockMetas api") diff --git a/ioctl/newcmd/bc/bcbucket.go b/ioctl/newcmd/bc/bcbucket.go index 39facd15be..cad999be96 100644 --- a/ioctl/newcmd/bc/bcbucket.go +++ b/ioctl/newcmd/bc/bcbucket.go @@ -18,6 +18,7 @@ import ( "github.com/iotexproject/iotex-proto/golang/iotextypes" "github.com/pkg/errors" "github.com/spf13/cobra" + "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/proto" @@ -227,8 +228,10 @@ func getBuckets(client ioctl.Client, method *iotexapi.ReadStakingDataMethod, rea response, err = apiClient.ReadState(ctx, request) if err != nil { - sta, ok := status.FromError(err) - if ok { + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.Unavailable { + return nil, ioctl.ErrInvalidEndpointOrInsecure + } return nil, errors.New(sta.Message()) } return nil, errors.Wrap(err, "failed to invoke ReadState api") diff --git a/ioctl/newcmd/bc/bcinfo.go b/ioctl/newcmd/bc/bcinfo.go index b01542871e..dbaa88eece 100644 --- a/ioctl/newcmd/bc/bcinfo.go +++ b/ioctl/newcmd/bc/bcinfo.go @@ -38,6 +38,7 @@ func NewBCInfoCmd(client ioctl.Client) *cobra.Command { Short: bcInfoCmdShort, Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true chainMeta, err := GetChainMeta(client) if err != nil { return err diff --git a/ioctl/newcmd/node/nodedelegate.go b/ioctl/newcmd/node/nodedelegate.go index a2e3cde2af..d07908684d 100644 --- a/ioctl/newcmd/node/nodedelegate.go +++ b/ioctl/newcmd/node/nodedelegate.go @@ -115,12 +115,13 @@ func NewNodeDelegateCmd(client ioctl.Client) *cobra.Command { }, ) if err != nil { - sta, ok := status.FromError(err) - if ok && sta.Code() == codes.NotFound { - message.Determined = false - cmd.Println(message.String(epochNum)) - return nil - } else if ok { + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.NotFound { + cmd.Println(message.String(epochNum)) + return nil + } else if sta.Code() == codes.Unavailable { + return ioctl.ErrInvalidEndpointOrInsecure + } return errors.New(sta.Message()) } return errors.Wrap(err, "failed to invoke ReadState api") @@ -140,8 +141,10 @@ func NewNodeDelegateCmd(client ioctl.Client) *cobra.Command { }, ) if err != nil { - sta, ok := status.FromError(err) - if ok { + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.Unavailable { + return ioctl.ErrInvalidEndpointOrInsecure + } return errors.New(sta.Message()) } return errors.Wrap(err, "failed to invoke ReadState api") diff --git a/ioctl/newcmd/node/nodereward.go b/ioctl/newcmd/node/nodereward.go index e16617b067..dca90beeb5 100644 --- a/ioctl/newcmd/node/nodereward.go +++ b/ioctl/newcmd/node/nodereward.go @@ -13,6 +13,7 @@ import ( "github.com/iotexproject/iotex-proto/golang/iotexapi" "github.com/pkg/errors" "github.com/spf13/cobra" + "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "github.com/iotexproject/iotex-core/ioctl" @@ -95,8 +96,10 @@ func rewardPool(client ioctl.Client) (string, string, string, error) { }, ) if err != nil { - sta, ok := status.FromError(err) - if ok { + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.Unavailable { + return "", "", "", ioctl.ErrInvalidEndpointOrInsecure + } return "", "", "", errors.New(sta.Message()) } return "", "", "", errors.Wrap(err, "failed to invoke ReadState api") @@ -115,8 +118,10 @@ func rewardPool(client ioctl.Client) (string, string, string, error) { }, ) if err != nil { - sta, ok := status.FromError(err) - if ok { + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.Unavailable { + return "", "", "", ioctl.ErrInvalidEndpointOrInsecure + } return "", "", "", errors.New(sta.Message()) } return "", "", "", errors.Wrap(err, "failed to invoke ReadState api") @@ -158,8 +163,10 @@ func reward(client ioctl.Client, arg string) (string, string, error) { }, ) if err != nil { - sta, ok := status.FromError(err) - if ok { + if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.Unavailable { + return "", "", ioctl.ErrInvalidEndpointOrInsecure + } return "", "", errors.New(sta.Message()) } return "", "", errors.Wrap(err, "failed to get version from server") diff --git a/ioctl/newcmd/version/version.go b/ioctl/newcmd/version/version.go index 1ee55245a2..5c6fa06451 100644 --- a/ioctl/newcmd/version/version.go +++ b/ioctl/newcmd/version/version.go @@ -13,6 +13,7 @@ import ( "github.com/iotexproject/iotex-proto/golang/iotextypes" "github.com/pkg/errors" "github.com/spf13/cobra" + "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "github.com/iotexproject/iotex-core/ioctl" @@ -61,6 +62,9 @@ func NewVersionCmd(cli ioctl.Client) *cobra.Command { ) if err != nil { if sta, ok := status.FromError(err); ok { + if sta.Code() == codes.Unavailable { + return ioctl.ErrInvalidEndpointOrInsecure + } return errors.New(sta.Message()) } return errors.Wrap(err, "failed to get version from server")