Skip to content

Commit

Permalink
feat: add api key owner listing flag
Browse files Browse the repository at this point in the history
fixes #395
  • Loading branch information
gabizou committed Oct 16, 2024
1 parent 6a6af68 commit f7c8ef2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ tcld apikey create --name <api-key-name> --description <api-key-description> --d
```
tcld apikey list
```

### List API Keys for a specific account (ServiceAccount or User):
```
tcld apikey list --owner-id <account-id>
```

### Delete an API Key:
```
tcld apikey delete --id <api-key-id>
Expand All @@ -75,6 +81,8 @@ tcld apikey enable --id <api-key-id>
```

### Performing an API Key rotation:

#### Current User Specific Rotation
1. Generate the new API key to rotate to.
```
tcld apikey create --name <api-key-name> --description <api-key-description> --duration <api-key-duration>
Expand All @@ -85,6 +93,17 @@ tcld apikey create --name <api-key-name> --description <api-key-description> --d
tcld apikey delete --id <api-key-id>
```

#### Service Account Specific Rotation
1. Generate the new API key to rotate to.
```
tcld apikey create --name <api-key-name> --description <api-key-description> --duration <api-key-duration> --service-account-id <service-account-id>
```
2. Update temporal clients to use the new API key and monitor deployments to make sure all old API key usage is gone.
3. Delete the old API key.
```
tcld apikey delete --id <api-key-id>
```

# Namespace Management

### List namespaces user has access to:
Expand Down
19 changes: 16 additions & 3 deletions app/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"github.com/urfave/cli/v2"
)

const (
ownerIDFlagName = "owner-id"
)

type (
APIKeyClient struct {
client authservice.AuthServiceClient
Expand Down Expand Up @@ -85,12 +89,13 @@ func (s *APIKeyClient) createServiceAccountAPIKey(
return PrintProto(resp)
}

func (s *APIKeyClient) listAPIKey() error {
func (s *APIKeyClient) listAPIKey(ownerId string) error {

totalRes := &authservice.GetAPIKeysResponse{}
pageToken := ""
for {
resp, err := s.client.GetAPIKeys(s.ctx, &authservice.GetAPIKeysRequest{
OwnerId: ownerId,
PageToken: pageToken,
})
if err != nil {
Expand Down Expand Up @@ -258,9 +263,17 @@ func NewAPIKeyCommand(getAPIKeyClientFn GetAPIKeyClientFn) (CommandOut, error) {
Name: "list",
Usage: "List apikeys",
Aliases: []string{"l"},
Flags: []cli.Flag{},
Flags: []cli.Flag{
&cli.StringFlag{
Name: ownerIDFlagName,
Usage: "The owner id of the API Keys to list",
Aliases: []string{"o"},
},
},
Action: func(ctx *cli.Context) error {
return c.listAPIKey()
return c.listAPIKey(
ctx.String(ownerIDFlagName),
)
},
},
{
Expand Down
11 changes: 11 additions & 0 deletions app/apikey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ func (s *APIKeyTestSuite) TestList() {
s.NoError(s.RunCmd("apikey", "list"))
}

func (s *APIKeyTestSuite) TestOwnerIdList() {
s.mockAuthService.EXPECT().GetAPIKeys(gomock.Any(), gomock.Any()).Return(&authservice.GetAPIKeysResponse{
ApiKeys: []*auth.APIKey{
{
Id: "test-apikey-id-1",
},
},
}, nil).Times(1)
s.NoError(s.RunCmd("apikey", "list", "--owner-id", "ownerID"))
}

func (s *APIKeyTestSuite) TestCreate() {
s.Error(s.RunCmd("apikey", "create"))
s.Error(s.RunCmd("apikey", "create", "--name", "test1"))
Expand Down

0 comments on commit f7c8ef2

Please sign in to comment.