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

Add -json and -t flag for nomad acl token create command #16055

Merged
merged 6 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions .changelog/16055.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
cli: Add `-json` and `-t` flag to `nomad acl token create` command
```
24 changes: 22 additions & 2 deletions command/acl_token_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ Create Options:
Specifies the time-to-live of the created ACL token. This takes the form of
a time duration such as "5m" and "1h". By default, tokens will be created
without a TTL and therefore never expire.

-json
Output the ACL token information in JSON format.

-t
Format and display the ACL token information using a Go template.
`
return strings.TrimSpace(helpText)
}
Expand All @@ -67,6 +73,8 @@ func (c *ACLTokenCreateCommand) AutocompleteFlags() complete.Flags {
"role-id": complete.PredictAnything,
"role-name": complete.PredictAnything,
"ttl": complete.PredictAnything,
"-json": complete.PredictNothing,
"-t": complete.PredictAnything,
})
}

Expand All @@ -81,15 +89,17 @@ func (c *ACLTokenCreateCommand) Synopsis() string {
func (c *ACLTokenCreateCommand) Name() string { return "acl token create" }

func (c *ACLTokenCreateCommand) Run(args []string) int {
var name, tokenType, ttl string
var global bool
var name, tokenType, ttl, tmpl string
var global, json bool
var policies []string
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.StringVar(&name, "name", "", "")
flags.StringVar(&tokenType, "type", "client", "")
flags.BoolVar(&global, "global", false, "")
flags.StringVar(&ttl, "ttl", "", "")
flags.BoolVar(&json, "json", false, "")
flags.StringVar(&tmpl, "t", "", "")
flags.Var((funcVar)(func(s string) error {
policies = append(policies, s)
return nil
Expand Down Expand Up @@ -148,6 +158,16 @@ func (c *ACLTokenCreateCommand) Run(args []string) int {
return 1
}

if json || len(tmpl) > 0 {
out, err := Format(json, tmpl, token)
if err != nil {
c.Ui.Error(err.Error())
return 1
}

c.Ui.Output(out)
return 0
}
// Format the output
outputACLToken(c.Ui, token)
return 0
Expand Down
39 changes: 39 additions & 0 deletions command/acl_token_create_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"encoding/json"
"testing"

"github.com/hashicorp/nomad/api"
Expand Down Expand Up @@ -45,12 +46,50 @@ func TestACLTokenCreateCommand(t *testing.T) {
ui.OutputWriter.Reset()
ui.ErrorWriter.Reset()

// Test with a no-expiry token and -json/-t flag
testCasesNoTTL := []string{"-json", "-t='{{ .Policies }}'"}
var jsonMap map[string]interface{}
for _, outputFormatFlag := range testCasesNoTTL {
code = cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID, "-policy=foo", "-type=client", outputFormatFlag})
require.Equal(t, 0, code)

// Check the output
out = ui.OutputWriter.String()
require.Contains(t, out, "foo")
if outputFormatFlag == "-json" {
err := json.Unmarshal([]byte(out), &jsonMap)
require.Nil(t, err, "Output not in JSON format")
}

ui.OutputWriter.Reset()
ui.ErrorWriter.Reset()
}

// Create a new token that has an expiry TTL set and check the response.
code = cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID, "-type=management", "-ttl=10m"})
require.Equal(t, 0, code)

out = ui.OutputWriter.String()
require.NotContains(t, out, "Expiry Time = <none>")
ui.OutputWriter.Reset()
ui.ErrorWriter.Reset()

// Test with a token that has expiry TTL set and -json/-t flag
testCasesWithTTL := [][]string{{"-json", "ExpirationTTL"}, {"-t='{{ .ExpirationTTL }}'", "10m0s"}}
for _, outputFormatFlag := range testCasesWithTTL {
code = cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID, "-type=management", "-ttl=10m", outputFormatFlag[0]})
require.Equal(t, 0, code)

// Check the output
out = ui.OutputWriter.String()
if outputFormatFlag[0] == "-json" {
err := json.Unmarshal([]byte(out), &jsonMap)
require.Nil(t, err, "Output not in JSON format")
}
require.Contains(t, out, outputFormatFlag[1])
ui.OutputWriter.Reset()
ui.ErrorWriter.Reset()
}
}

func Test_generateACLTokenRoleLinks(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions website/content/docs/commands/acl/token/create.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ The `acl token create` command requires no arguments.
form of a time duration such as "5m" and "1h". By default, tokens will be
created without a TTL and therefore never expire.

- `-json`:Output the ACL token information in JSON format.

- `-t`: Format and display the ACL token information using a Go template.

## Examples

Create a new ACL token linked to an ACL Policy and Role:
Expand Down