This repository was archived by the owner on Aug 30, 2024. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork18
Add new tokens command for managing API tokens#170
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
a28b7e4
Add new tokens command for managing API tokens
cmoog933bfd0
fixup! Add new tokens command for managing API tokens
cmoog9f4e472
fixup! Add new tokens command for managing API tokens
cmooge98dfae
fixup! Add new tokens command for managing API tokens
cmoog7bf7d01
fixup! Add new tokens command for managing API tokens
cmoog6a8c9f2
Add integration test for CODER_TOKEN static auth
cmoogccf3893
fixup! Add integration test for CODER_TOKEN static auth
cmoog3b1f2f8
fixup! Add integration test for CODER_TOKEN static auth
cmoogFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletionsci/integration/statictokens_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package integration | ||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
"cdr.dev/coder-cli/pkg/tcli" | ||
) | ||
func TestStaticAuth(t *testing.T) { | ||
t.Parallel() | ||
t.Skip() | ||
run(t, "static-auth-test", func(t *testing.T, ctx context.Context, c *tcli.ContainerRunner) { | ||
headlessLogin(ctx, t, c) | ||
c.Run(ctx, "coder tokens ls").Assert(t, | ||
tcli.Success(), | ||
) | ||
var result *tcli.CommandResult | ||
tokenName := randString(5) | ||
c.Run(ctx, "coder tokens create "+tokenName).Assert(t, | ||
tcli.Success(), | ||
tcli.GetResult(&result), | ||
) | ||
// remove loging credentials | ||
c.Run(ctx, "rm -rf ~/.config/coder").Assert(t, | ||
tcli.Success(), | ||
) | ||
// make requests with token environment variable authentication | ||
cmd := exec.CommandContext(ctx, "sh", "-c", | ||
fmt.Sprintf("export CODER_URL=%s && export CODER_TOKEN=$(cat) && coder envs ls", os.Getenv("CODER_URL")), | ||
) | ||
cmd.Stdin = strings.NewReader(string(result.Stdout)) | ||
c.RunCmd(cmd).Assert(t, | ||
tcli.Success(), | ||
) | ||
// should error when the environment variabels aren't set | ||
c.Run(ctx, "coder envs ls").Assert(t, | ||
tcli.Error(), | ||
) | ||
}) | ||
} |
2 changes: 1 addition & 1 deletioncoder-sdk/request.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletionscoder-sdk/tokens.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package coder | ||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
) | ||
type APIToken struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Application bool `json:"application"` | ||
UserID string `json:"user_id"` | ||
LastUsed time.Time `json:"last_used"` | ||
} | ||
type CreateAPITokenReq struct { | ||
Name string `json:"name"` | ||
} | ||
type createAPITokenResp struct { | ||
Key string `json:"key"` | ||
} | ||
func (c Client) CreateAPIToken(ctx context.Context, userID string, req CreateAPITokenReq) (token string, _ error) { | ||
var resp createAPITokenResp | ||
err := c.requestBody(ctx, http.MethodPost, "/api/api-keys/"+userID, req, &resp) | ||
if err != nil { | ||
return "", err | ||
} | ||
return resp.Key, nil | ||
} | ||
func (c Client) APITokens(ctx context.Context, userID string) ([]APIToken, error) { | ||
var tokens []APIToken | ||
if err := c.requestBody(ctx, http.MethodGet, "/api/api-keys/"+userID, nil, &tokens); err != nil { | ||
return nil, err | ||
} | ||
return tokens, nil | ||
} | ||
func (c Client) APITokenByID(ctx context.Context, userID, tokenID string) (*APIToken, error) { | ||
var token APIToken | ||
if err := c.requestBody(ctx, http.MethodGet, "/api/api-keys/"+userID+"/"+tokenID, nil, &token); err != nil { | ||
return nil, err | ||
} | ||
return &token, nil | ||
} | ||
func (c Client) DeleteAPIToken(ctx context.Context, userID, tokenID string) error { | ||
return c.requestBody(ctx, http.MethodDelete, "/api/api-keys/"+userID+"/"+tokenID, nil, nil) | ||
} | ||
func (c Client) RegenerateAPIToken(ctx context.Context, userID, tokenID string) (token string, _ error) { | ||
var resp createAPITokenResp | ||
if err := c.requestBody(ctx, http.MethodPost, "/api/api-keys/"+userID+"/"+tokenID+"/regen", nil, &resp); err != nil { | ||
return "", err | ||
} | ||
return resp.Key, nil | ||
} |
26 changes: 19 additions & 7 deletionsinternal/cmd/auth.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionsinternal/cmd/cmd.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletionsinternal/cmd/tokens.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package cmd | ||
import ( | ||
"fmt" | ||
"cdr.dev/coder-cli/coder-sdk" | ||
"cdr.dev/coder-cli/pkg/tablewriter" | ||
"github.com/spf13/cobra" | ||
) | ||
func tokensCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "tokens", | ||
Short: "manage Coder API tokens for the active user", | ||
Hidden: true, | ||
Long: "Create and manage API Tokens for authenticating the CLI.\n" + | ||
"Statically authenticate using the token value with the " + "`" + "CODER_TOKEN" + "`" + " and " + "`" + "CODER_URL" + "`" + " environment variables.", | ||
} | ||
cmd.AddCommand( | ||
lsTokensCmd(), | ||
createTokensCmd(), | ||
rmTokenCmd(), | ||
regenTokenCmd(), | ||
) | ||
return cmd | ||
} | ||
func lsTokensCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "ls", | ||
Short: "show the user's active API tokens", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
client, err := newClient(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
tokens, err := client.APITokens(ctx, coder.Me) | ||
if err != nil { | ||
return err | ||
} | ||
err = tablewriter.WriteTable(len(tokens), func(i int) interface{} { return tokens[i] }) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
} | ||
func createTokensCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "create [token_name]", | ||
Short: "create generates a new API token and prints it to stdout", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
client, err := newClient(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
token, err := client.CreateAPIToken(ctx, coder.Me, coder.CreateAPITokenReq{ | ||
Name: args[0], | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(token) | ||
return nil | ||
}, | ||
} | ||
} | ||
func rmTokenCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "rm [token_id]", | ||
Short: "remove an API token by its unique ID", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
client, err := newClient(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
if err = client.DeleteAPIToken(ctx, coder.Me, args[0]); err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
} | ||
func regenTokenCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "regen [token_id]", | ||
Short: "regenerate an API token by its unique ID and print the new token to stdout", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
client, err := newClient(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
token, err := client.RegenerateAPIToken(ctx, coder.Me, args[0]) | ||
if err != nil { | ||
return nil | ||
} | ||
fmt.Println(token) | ||
return nil | ||
}, | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.