Skip to content

Commit 71d1e63

Browse files
Kira-PilotEmyrk
andauthored
feat: add ability to name tokens (#6365)
* add tokens switch * reorged TokensPage * using Trans component for description * using Trans component on DeleteDialog * add owner col * simplify hook return * lint * type for response * added flag for name * fixed auth * lint, prettier, tests * added unique index for login type token * remove tokens by name * better check for unique constraint * docs * test: Fix dbfake to insert token name * fix doc tests * Update cli/tokens.go Co-authored-by: Steven Masley <Emyrk@users.noreply.github.com> * Update coderd/database/migrations/000102_add_apikey_name.down.sql Co-authored-by: Steven Masley <Emyrk@users.noreply.github.com> * add more specificity to IsUniqueViolation check * fix tests * Fix AutorizeAllEndpoints * rename migration --------- Co-authored-by: Steven Masley <stevenmasley@coder.com> Co-authored-by: Steven Masley <Emyrk@users.noreply.github.com>
1 parent e3a4861 commit 71d1e63

37 files changed

+447
-63
lines changed

cli/testdata/coder_tokens_--help.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Get Started:
2222
$ coder tokens rm WuoWs4ZsMX
2323

2424
Commands:
25-
create Create a tokens
25+
create Create a token
2626
list List tokens
2727
remove Delete a token
2828

cli/testdata/coder_tokens_create_--help.golden

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Create a tokens
1+
Create a token
22

33
Usage:
44
coder tokens create [flags]
@@ -7,6 +7,7 @@ Flags:
77
-h, --help help for create
88
--lifetime duration Specify a duration for the lifetime of the token.
99
Consumes $CODER_TOKEN_LIFETIME (default 720h0m0s)
10+
-n, --name string Specify a human-readable name.
1011

1112
Global Flags:
1213
--global-config coder Path to the global coder config directory.

cli/testdata/coder_tokens_list_--help.golden

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Aliases:
99
Flags:
1010
-a, --all Specifies whether all users' tokens will be listed or not (must have
1111
Owner role to see all tokens).
12-
-c, --column strings Columns to display in table output. Available columns: id, last used,
13-
expires at, created at, owner (default [id,last used,expires
14-
at,created at])
12+
-c, --column strings Columns to display in table output. Available columns: id, name, last
13+
used, expires at, created at, owner (default [id,name,last
14+
used,expires at,created at])
1515
-h, --help help for list
1616
-o, --output string Output format. Available formats: table, json (default "table")
1717

cli/testdata/coder_tokens_remove_--help.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Delete a token
22

33
Usage:
4-
coder tokens remove [id] [flags]
4+
coder tokens remove [name] [flags]
55

66
Aliases:
77
remove, rm

cli/tokens.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,22 @@ func tokens() *cobra.Command {
4949
}
5050

5151
func createToken() *cobra.Command {
52-
var tokenLifetime time.Duration
52+
var (
53+
tokenLifetime time.Duration
54+
name string
55+
)
5356
cmd := &cobra.Command{
5457
Use: "create",
55-
Short: "Create a tokens",
58+
Short: "Create a token",
5659
RunE: func(cmd *cobra.Command, args []string) error {
5760
client, err := CreateClient(cmd)
5861
if err != nil {
5962
return xerrors.Errorf("create codersdk client: %w", err)
6063
}
6164

6265
res, err := client.CreateToken(cmd.Context(), codersdk.Me, codersdk.CreateTokenRequest{
63-
Lifetime: tokenLifetime,
66+
Lifetime: tokenLifetime,
67+
TokenName: name,
6468
})
6569
if err != nil {
6670
return xerrors.Errorf("create tokens: %w", err)
@@ -81,6 +85,7 @@ func createToken() *cobra.Command {
8185
}
8286

8387
cliflag.DurationVarP(cmd.Flags(), &tokenLifetime, "lifetime", "", "CODER_TOKEN_LIFETIME", 30*24*time.Hour, "Specify a duration for the lifetime of the token.")
88+
cmd.Flags().StringVarP(&name, "name", "n", "", "Specify a human-readable name.")
8489

8590
return cmd
8691
}
@@ -92,6 +97,7 @@ type tokenListRow struct {
9297

9398
// For table format:
9499
ID string `json:"-" table:"id,default_sort"`
100+
TokenName string `json:"token_name" table:"name"`
95101
LastUsed time.Time `json:"-" table:"last used"`
96102
ExpiresAt time.Time `json:"-" table:"expires at"`
97103
CreatedAt time.Time `json:"-" table:"created at"`
@@ -102,6 +108,7 @@ func tokenListRowFromToken(token codersdk.APIKeyWithOwner) tokenListRow {
102108
return tokenListRow{
103109
APIKey: token.APIKey,
104110
ID: token.ID,
111+
TokenName: token.TokenName,
105112
LastUsed: token.LastUsed,
106113
ExpiresAt: token.ExpiresAt,
107114
CreatedAt: token.CreatedAt,
@@ -111,7 +118,7 @@ func tokenListRowFromToken(token codersdk.APIKeyWithOwner) tokenListRow {
111118

112119
func listTokens() *cobra.Command {
113120
// we only display the 'owner' column if the --all argument is passed in
114-
defaultCols := []string{"id", "last used", "expires at", "created at"}
121+
defaultCols := []string{"id", "name", "last used", "expires at", "created at"}
115122
if slices.Contains(os.Args, "-a") || slices.Contains(os.Args, "--all") {
116123
defaultCols = append(defaultCols, "owner")
117124
}
@@ -172,7 +179,7 @@ func listTokens() *cobra.Command {
172179

173180
func removeToken() *cobra.Command {
174181
cmd := &cobra.Command{
175-
Use: "remove [id]",
182+
Use: "remove [name]",
176183
Aliases: []string{"rm"},
177184
Short: "Delete a token",
178185
Args: cobra.ExactArgs(1),
@@ -182,7 +189,12 @@ func removeToken() *cobra.Command {
182189
return xerrors.Errorf("create codersdk client: %w", err)
183190
}
184191

185-
err = client.DeleteAPIKey(cmd.Context(), codersdk.Me, args[0])
192+
token, err := client.APIKeyByName(cmd.Context(), codersdk.Me, args[0])
193+
if err != nil {
194+
return xerrors.Errorf("fetch api key by name %s: %w", args[0], err)
195+
}
196+
197+
err = client.DeleteAPIKey(cmd.Context(), codersdk.Me, token.ID)
186198
if err != nil {
187199
return xerrors.Errorf("delete api key: %w", err)
188200
}

cli/tokens_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestTokens(t *testing.T) {
3333
res := buf.String()
3434
require.Contains(t, res, "tokens found")
3535

36-
cmd, root = clitest.New(t, "tokens", "create")
36+
cmd, root = clitest.New(t, "tokens", "create", "--name", "token-one")
3737
clitest.SetupConfig(t, client, root)
3838
buf = new(bytes.Buffer)
3939
cmd.SetOut(buf)
@@ -73,7 +73,7 @@ func TestTokens(t *testing.T) {
7373
require.Len(t, tokens, 1)
7474
require.Equal(t, id, tokens[0].ID)
7575

76-
cmd, root = clitest.New(t, "tokens", "rm", id)
76+
cmd, root = clitest.New(t, "tokens", "rm", "token-one")
7777
clitest.SetupConfig(t, client, root)
7878
buf = new(bytes.Buffer)
7979
cmd.SetOut(buf)

coderd/apidoc/docs.go

+51-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+47-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)