Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create API tokens with team slug #980

Merged
merged 2 commits into from
Jul 14, 2023
Merged
Changes from 1 commit
Commits
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
25 changes: 16 additions & 9 deletions command/api/token/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func createCommand() cli.Command {
Name: "create",
Action: cli.ActionFunc(createAction),
Usage: "create a new token",
UsageText: `**step api token create** <team-id> <crt-file> <key-file>
UsageText: `**step api token create** <team-id>|<team-slug> <crt-file> <key-file>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use just and use the text in the positional arguments to actually describe what that argument means.

Suggested change
UsageText: `**step api token create** <team-id>|<team-slug> <crt-file> <key-file>
UsageText: `**step api token create** <team> <crt-file> <key-file>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[**--api-url**=<url>] [**--audience**=<name>]
`,
Flags: []cli.Flag{
Expand All @@ -32,8 +32,8 @@ func createCommand() cli.Command {

## POSITIONAL ARGUMENTS

<team-id>
: UUID of the team the API token will be issued for. This is available in the Smallstep dashboard.
<team-id>|<team-slug>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<team-id>|<team-slug>
<team>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: UUID or slug of the team the API token will be issued for. This is available in the Smallstep dashboard.

<crt-file>
: File to read the certificate (PEM format). This certificate must be signed by a trusted root configured in the Smallstep dashboard.
Expand All @@ -45,12 +45,19 @@ func createCommand() cli.Command {
Use a certificate to get a new API token:
'''
$ step api token create ff98be70-7cc3-4df5-a5db-37f5d3c96e23 internal.crt internal.key
'''`,
'''

Get a token using the team slug:
'''
$ step api token create teamfoo internal.crt internal.key
'''
`,
}
}

type createTokenReq struct {
TeamID string `json:"teamID"`
TeamSlug string `json:"teamSlug"`
Bundle [][]byte `json:"bundle"`
Audience string `json:"audience,omitempty"`
}
Expand Down Expand Up @@ -78,20 +85,20 @@ func createAction(ctx *cli.Context) (err error) {
parsedURL.Path = path.Join(parsedURL.Path, "api/auth")
apiURL := parsedURL.String()

if _, err := uuid.Parse(teamID); err != nil {
return fmt.Errorf("team-id argument must be a valid UUID")
}

clientCert, err := tls.LoadX509KeyPair(crtFile, keyFile)
if err != nil {
return err
}
b := &bytes.Buffer{}
r := &createTokenReq{
TeamID: teamID,
Bundle: clientCert.Certificate,
Audience: ctx.String("audience"),
}
if _, err := uuid.Parse(teamID); err == nil {
r.TeamID = teamID
} else {
r.TeamSlug = teamID
}
err = json.NewEncoder(b).Encode(r)
if err != nil {
return err
Expand Down