Skip to content

Commit

Permalink
Merge branch 'main' into yx/gn
Browse files Browse the repository at this point in the history
  • Loading branch information
yux0 authored Nov 8, 2023
2 parents 3126fa6 + a2d4cae commit ff87dd3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
18 changes: 13 additions & 5 deletions app/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/gogo/protobuf/types"
"github.com/temporalio/tcld/protogen/api/auth/v1"
"github.com/temporalio/tcld/protogen/api/authservice/v1"
"github.com/temporalio/tcld/utils"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -151,9 +152,9 @@ func NewAPIKeyCommand(getAPIKeyClientFn GetAPIKeyClientFn) (CommandOut, error) {
Usage: "the description of the apikey",
Aliases: []string{"desc"},
},
&cli.DurationFlag{
&cli.StringFlag{
Name: "duration",
Usage: "the duration from now when the apikey will expire, will be ignored if expiry flag is set, example: '24h'",
Usage: "the duration from now when the apikey will expire, will be ignored if expiry flag is set, examples: '2.5y', '30d', '4d12h'",
Aliases: []string{"d"},
},
&cli.TimestampFlag{
Expand All @@ -167,11 +168,18 @@ func NewAPIKeyCommand(getAPIKeyClientFn GetAPIKeyClientFn) (CommandOut, error) {
Action: func(ctx *cli.Context) error {
expiry := ctx.Timestamp("expiry")
if expiry == nil || expiry.IsZero() {
expiryPeriod := ctx.Duration("duration")
if expiryPeriod == 0 {
expiryPeriod := ctx.String("duration")
if expiryPeriod == "" {
return fmt.Errorf("no expiry was set")
}
e := time.Now().UTC().Add(expiryPeriod)
d, err := utils.ParseDuration(expiryPeriod)
if err != nil {
return fmt.Errorf("failed to parse duration: %w", err)
}
if d <= 0 {
return fmt.Errorf("expiration must be positive: %s", expiryPeriod)
}
e := time.Now().UTC().Add(d)
expiry = &e
}
return c.createAPIKey(
Expand Down
6 changes: 4 additions & 2 deletions app/apikey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,18 @@ func (s *APIKeyTestSuite) TestList() {
func (s *APIKeyTestSuite) TestCreate() {
s.Error(s.RunCmd("apikey", "create"))
s.Error(s.RunCmd("apikey", "create", "--name", "test1"))
s.Error(s.RunCmd("apikey", "create", "--name", "test1", "--duration", "-24h"))
s.Error(s.RunCmd("apikey", "create", "--name", "test1", "--duration", "0d"))
s.mockAuthService.EXPECT().CreateAPIKey(gomock.Any(), gomock.Any()).Return(nil, errors.New("create apikey error")).Times(1)
s.Error(s.RunCmd("apikey", "create", "--name", "test1", "--duration", "1h"))
s.Error(s.RunCmd("apikey", "create", "--name", "test1", "--duration", "30d"))
s.mockAuthService.EXPECT().CreateAPIKey(gomock.Any(), gomock.Any()).Return(&authservice.CreateAPIKeyResponse{
Id: "id1",
SecretKey: "secret1",
RequestStatus: &request.RequestStatus{
RequestId: "rid",
},
}, nil).Times(1)
s.NoError(s.RunCmd("apikey", "create", "--name", "test1", "--duration", "1h"))
s.NoError(s.RunCmd("apikey", "create", "--name", "test1", "--duration", "30d"))
s.mockAuthService.EXPECT().CreateAPIKey(gomock.Any(), gomock.Any()).Return(&authservice.CreateAPIKeyResponse{
Id: "id1",
SecretKey: "secret1",
Expand Down

0 comments on commit ff87dd3

Please sign in to comment.