Skip to content

Commit

Permalink
[ioctl] add checking --insecure in newcmd (#3781)
Browse files Browse the repository at this point in the history
* add insecure error message

* error variable for invalid endpoint or insecure

---------

Co-authored-by: dustinxie <dahuaxie@gmail.com>
  • Loading branch information
huof6829 and dustinxie committed Feb 6, 2023
1 parent 6fe6a17 commit 425db42
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 48 deletions.
5 changes: 5 additions & 0 deletions ioctl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions ioctl/newcmd/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions ioctl/newcmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down
16 changes: 10 additions & 6 deletions ioctl/newcmd/action/actionhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
32 changes: 20 additions & 12 deletions ioctl/newcmd/bc/bc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
24 changes: 14 additions & 10 deletions ioctl/newcmd/bc/bcblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions ioctl/newcmd/bc/bcbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions ioctl/newcmd/bc/bcinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions ioctl/newcmd/node/nodedelegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
19 changes: 13 additions & 6 deletions ioctl/newcmd/node/nodereward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions ioctl/newcmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 425db42

Please sign in to comment.