Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2a103b3
add tokens switch
Kira-Pilot Feb 23, 2023
6402745
reorged TokensPage
Kira-Pilot Feb 23, 2023
340a9d1
using Trans component for description
Kira-Pilot Feb 23, 2023
ff2ac68
using Trans component on DeleteDialog
Kira-Pilot Feb 23, 2023
ade9548
add owner col
Kira-Pilot Feb 23, 2023
dd587e8
simplify hook return
Kira-Pilot Feb 23, 2023
2ce518c
lint
Kira-Pilot Feb 24, 2023
740c4a2
type for response
Kira-Pilot Feb 24, 2023
ef09103
added flag for name
Kira-Pilot Feb 27, 2023
20fc87c
fixed auth
Kira-Pilot Feb 27, 2023
f399fb8
lint, prettier, tests
Kira-Pilot Feb 27, 2023
eda2702
Merge remote-tracking branch 'origin/main' into name-tokens/kira-pilot
Kira-Pilot Feb 28, 2023
5963708
added unique index for login type token
Kira-Pilot Feb 28, 2023
187f812
remove tokens by name
Kira-Pilot Feb 28, 2023
3609057
better check for unique constraint
Kira-Pilot Feb 28, 2023
b824bca
docs
Kira-Pilot Mar 1, 2023
621ee44
Merge remote-tracking branch 'origin/main' into name-tokens/kira-pilot
Kira-Pilot Mar 1, 2023
269f2ba
test: Fix dbfake to insert token name
Emyrk Mar 1, 2023
dd01f53
fix doc tests
Kira-Pilot Mar 1, 2023
e8d519f
Update cli/tokens.go
Kira-Pilot Mar 2, 2023
4c974c4
Update coderd/database/migrations/000102_add_apikey_name.down.sql
Kira-Pilot Mar 2, 2023
4bf374d
add more specificity to IsUniqueViolation check
Kira-Pilot Mar 2, 2023
07acbff
fix tests
Kira-Pilot Mar 2, 2023
aa779d9
Fix AutorizeAllEndpoints
Emyrk Mar 2, 2023
c33361b
Merge remote-tracking branch 'origin/main' into name-tokens/kira-pilot
Kira-Pilot Mar 2, 2023
6cd9329
rename migration
Kira-Pilot Mar 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/testdata/coder_tokens_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Get Started:
$ coder tokens rm WuoWs4ZsMX

Commands:
create Create a tokens
create Create a token
list List tokens
remove Delete a token

Expand Down
3 changes: 2 additions & 1 deletion cli/testdata/coder_tokens_create_--help.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Create a tokens
Create a token

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

Global Flags:
--global-config coder Path to the global coder config directory.
Expand Down
6 changes: 3 additions & 3 deletions cli/testdata/coder_tokens_list_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Aliases:
Flags:
-a, --all Specifies whether all users' tokens will be listed or not (must have
Owner role to see all tokens).
-c, --column strings Columns to display in table output. Available columns: id, last used,
expires at, created at, owner (default [id,last used,expires
at,created at])
-c, --column strings Columns to display in table output. Available columns: id, name, last
used, expires at, created at, owner (default [id,name,last
used,expires at,created at])
-h, --help help for list
-o, --output string Output format. Available formats: table, json (default "table")

Expand Down
2 changes: 1 addition & 1 deletion cli/testdata/coder_tokens_remove_--help.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Delete a token

Usage:
coder tokens remove [id] [flags]
coder tokens remove [name] [flags]

Aliases:
remove, rm
Expand Down
24 changes: 18 additions & 6 deletions cli/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,22 @@ func tokens() *cobra.Command {
}

func createToken() *cobra.Command {
var tokenLifetime time.Duration
var (
tokenLifetime time.Duration
name string
)
cmd := &cobra.Command{
Use: "create",
Short: "Create a tokens",
Short: "Create a token",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := CreateClient(cmd)
if err != nil {
return xerrors.Errorf("create codersdk client: %w", err)
}

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

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

return cmd
}
Expand All @@ -92,6 +97,7 @@ type tokenListRow struct {

// For table format:
ID string `json:"-" table:"id,default_sort"`
TokenName string `json:"token_name" table:"name"`
LastUsed time.Time `json:"-" table:"last used"`
ExpiresAt time.Time `json:"-" table:"expires at"`
CreatedAt time.Time `json:"-" table:"created at"`
Expand All @@ -102,6 +108,7 @@ func tokenListRowFromToken(token codersdk.APIKeyWithOwner) tokenListRow {
return tokenListRow{
APIKey: token.APIKey,
ID: token.ID,
TokenName: token.TokenName,
LastUsed: token.LastUsed,
ExpiresAt: token.ExpiresAt,
CreatedAt: token.CreatedAt,
Expand All @@ -111,7 +118,7 @@ func tokenListRowFromToken(token codersdk.APIKeyWithOwner) tokenListRow {

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

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

err = client.DeleteAPIKey(cmd.Context(), codersdk.Me, args[0])
token, err := client.APIKeyByName(cmd.Context(), codersdk.Me, args[0])
if err != nil {
return xerrors.Errorf("fetch api key by name %s: %w", args[0], err)
}

err = client.DeleteAPIKey(cmd.Context(), codersdk.Me, token.ID)
if err != nil {
return xerrors.Errorf("delete api key: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cli/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestTokens(t *testing.T) {
res := buf.String()
require.Contains(t, res, "tokens found")

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

cmd, root = clitest.New(t, "tokens", "rm", id)
cmd, root = clitest.New(t, "tokens", "rm", "token-one")
clitest.SetupConfig(t, client, root)
buf = new(bytes.Buffer)
cmd.SetOut(buf)
Expand Down
53 changes: 51 additions & 2 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 47 additions & 2 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading