- Notifications
You must be signed in to change notification settings - Fork928
feat: tokens#4380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
feat: tokens#4380
Changes fromall commits
Commits
Show all changes
17 commits Select commitHold shift + click to select a range
75e26af
feat: machine keys
f0ssel2689c10
fix migration conflict
f0sseld19d864
make gen
f0sselcace329
fmt
f0sselfd3617b
fix down migration
f0ssele4f498e
add cli commands
f0ssel7e299d7
add cli commands
f0ssela097819
add tests
f0sself2c5e13
lint
f0ssel8d99b76
fix cliui test
f0ssel07d1d79
fix alignment
f0ssel5a94dc8
migration conflict
f0ssel8b10c26
new migration
f0ssel0db2bf6
rename to tokens
f0sself4dbe39
rename to tokens
f0ssel0537ad2
more renames
f0ssel8226464
more renames
f0sselFile 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
4 changes: 2 additions & 2 deletionscli/cliui/table.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
22 changes: 11 additions & 11 deletionscli/cliui/table_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
1 change: 1 addition & 0 deletionscli/root.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 |
---|---|---|
@@ -93,6 +93,7 @@ func Core() []*cobra.Command { | ||
users(), | ||
versionCmd(), | ||
workspaceAgent(), | ||
tokens(), | ||
} | ||
} | ||
155 changes: 155 additions & 0 deletionscli/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,155 @@ | ||
package cli | ||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
func tokens() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "tokens", | ||
Short: "Manage personal access tokens", | ||
Long: "Tokens are used to authenticate automated clients to Coder.", | ||
Aliases: []string{"token"}, | ||
Example: formatExamples( | ||
example{ | ||
Description: "Create a token for automation", | ||
Command: "coder tokens create", | ||
}, | ||
example{ | ||
Description: "List your tokens", | ||
Command: "coder tokens ls", | ||
}, | ||
example{ | ||
Description: "Remove a token by ID", | ||
Command: "coder tokens rm WuoWs4ZsMX", | ||
}, | ||
), | ||
} | ||
cmd.AddCommand( | ||
createToken(), | ||
listTokens(), | ||
removeToken(), | ||
) | ||
return cmd | ||
} | ||
func createToken() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "create", | ||
Short: "Create a tokens", | ||
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) | ||
if err != nil { | ||
return xerrors.Errorf("create tokens: %w", err) | ||
} | ||
cmd.Println(cliui.Styles.Wrap.Render( | ||
"Here is your token. 🪄", | ||
)) | ||
cmd.Println() | ||
cmd.Println(cliui.Styles.Code.Render(strings.TrimSpace(res.Key))) | ||
cmd.Println() | ||
cmd.Println(cliui.Styles.Wrap.Render( | ||
fmt.Sprintf("You can use this token by setting the --%s CLI flag, the %s environment variable, or the %q HTTP header.", varToken, envSessionToken, codersdk.SessionTokenKey), | ||
)) | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} | ||
type tokenRow struct { | ||
ID string `table:"ID"` | ||
LastUsed time.Time `table:"Last Used"` | ||
ExpiresAt time.Time `table:"Expires At"` | ||
CreatedAt time.Time `table:"Created At"` | ||
} | ||
func listTokens() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Aliases: []string{"ls"}, | ||
Short: "List tokens", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client, err := CreateClient(cmd) | ||
if err != nil { | ||
return xerrors.Errorf("create codersdk client: %w", err) | ||
} | ||
keys, err := client.GetTokens(cmd.Context(), codersdk.Me) | ||
if err != nil { | ||
return xerrors.Errorf("create tokens: %w", err) | ||
} | ||
if len(keys) == 0 { | ||
cmd.Println(cliui.Styles.Wrap.Render( | ||
"No tokens found.", | ||
)) | ||
} | ||
var rows []tokenRow | ||
for _, key := range keys { | ||
rows = append(rows, tokenRow{ | ||
ID: key.ID, | ||
LastUsed: key.LastUsed, | ||
ExpiresAt: key.ExpiresAt, | ||
CreatedAt: key.CreatedAt, | ||
}) | ||
} | ||
out, err := cliui.DisplayTable(rows, "", nil) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = fmt.Fprintln(cmd.OutOrStdout(), out) | ||
return err | ||
}, | ||
} | ||
return cmd | ||
} | ||
func removeToken() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "remove [id]", | ||
Aliases: []string{"rm"}, | ||
Short: "Delete a token", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client, err := CreateClient(cmd) | ||
if err != nil { | ||
return xerrors.Errorf("create codersdk client: %w", err) | ||
} | ||
err = client.DeleteAPIKey(cmd.Context(), codersdk.Me, args[0]) | ||
if err != nil { | ||
return xerrors.Errorf("delete api key: %w", err) | ||
} | ||
cmd.Println(cliui.Styles.Wrap.Render( | ||
"Token has been deleted.", | ||
)) | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} |
66 changes: 66 additions & 0 deletionscli/tokens_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,66 @@ | ||
package cli_test | ||
import ( | ||
"bytes" | ||
"regexp" | ||
"testing" | ||
"github.com/stretchr/testify/require" | ||
"github.com/coder/coder/cli/clitest" | ||
"github.com/coder/coder/coderd/coderdtest" | ||
) | ||
func TestTokens(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t, nil) | ||
_ = coderdtest.CreateFirstUser(t, client) | ||
// helpful empty response | ||
cmd, root := clitest.New(t, "tokens", "ls") | ||
clitest.SetupConfig(t, client, root) | ||
buf := new(bytes.Buffer) | ||
cmd.SetOut(buf) | ||
err := cmd.Execute() | ||
require.NoError(t, err) | ||
res := buf.String() | ||
require.Contains(t, res, "tokens found") | ||
cmd, root = clitest.New(t, "tokens", "create") | ||
clitest.SetupConfig(t, client, root) | ||
buf = new(bytes.Buffer) | ||
cmd.SetOut(buf) | ||
err = cmd.Execute() | ||
require.NoError(t, err) | ||
res = buf.String() | ||
require.NotEmpty(t, res) | ||
// find API key in format "XXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXX" | ||
r := regexp.MustCompile("[a-zA-Z0-9]{10}-[a-zA-Z0-9]{22}") | ||
require.Regexp(t, r, res) | ||
key := r.FindString(res) | ||
id := key[:10] | ||
cmd, root = clitest.New(t, "tokens", "ls") | ||
clitest.SetupConfig(t, client, root) | ||
buf = new(bytes.Buffer) | ||
cmd.SetOut(buf) | ||
err = cmd.Execute() | ||
require.NoError(t, err) | ||
res = buf.String() | ||
require.NotEmpty(t, res) | ||
require.Contains(t, res, "ID") | ||
require.Contains(t, res, "EXPIRES AT") | ||
require.Contains(t, res, "CREATED AT") | ||
require.Contains(t, res, "LAST USED") | ||
require.Contains(t, res, id) | ||
cmd, root = clitest.New(t, "tokens", "rm", id) | ||
clitest.SetupConfig(t, client, root) | ||
buf = new(bytes.Buffer) | ||
cmd.SetOut(buf) | ||
err = cmd.Execute() | ||
require.NoError(t, err) | ||
res = buf.String() | ||
require.NotEmpty(t, res) | ||
require.Contains(t, res, "deleted") | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.