forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58ac12a
commit 730c5ad
Showing
4 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,75 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/posener/complete" | ||
) | ||
|
||
type ACLTokenDeleteCommand struct { | ||
Meta | ||
} | ||
|
||
func (c *ACLTokenDeleteCommand) Help() string { | ||
helpText := ` | ||
Usage: nomad acl token delete [options] | ||
Delete is used to delete existing ACL tokens. Requires a management token. | ||
General Options: | ||
` + generalOptionsUsage() | ||
|
||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
func (c *ACLTokenDeleteCommand) AutocompleteFlags() complete.Flags { | ||
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), | ||
complete.Flags{}) | ||
} | ||
|
||
func (c *ACLTokenDeleteCommand) AutocompleteArgs() complete.Predictor { | ||
return complete.PredictNothing | ||
} | ||
|
||
func (c *ACLTokenDeleteCommand) Synopsis() string { | ||
return "Delete an existing ACL token" | ||
} | ||
|
||
func (c *ACLTokenDeleteCommand) Run(args []string) int { | ||
flags := c.Meta.FlagSet("acl token delete", FlagSetClient) | ||
flags.Usage = func() { c.Ui.Output(c.Help()) } | ||
|
||
if err := flags.Parse(args); err != nil { | ||
return 1 | ||
} | ||
|
||
// Check that the last argument is the token to delete. Return error if no | ||
// such token was provided. | ||
args = flags.Args() | ||
if l := len(args); l != 1 { | ||
c.Ui.Error(c.Help()) | ||
return 1 | ||
} | ||
|
||
tokenAccessorID := args[0] | ||
|
||
// Get the HTTP client | ||
client, err := c.Meta.Client() | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) | ||
return 1 | ||
} | ||
|
||
// Delete the specified token | ||
_, err = client.ACLTokens().Delete(tokenAccessorID, nil) | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error deleting token: %s", err)) | ||
return 1 | ||
} | ||
|
||
// Format the output | ||
c.Ui.Output(fmt.Sprintf("Token %s successfully deleted", tokenAccessorID)) | ||
return 0 | ||
} |
This file contains 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,58 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/acl" | ||
"github.com/hashicorp/nomad/command/agent" | ||
"github.com/hashicorp/nomad/nomad/mock" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/mitchellh/cli" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestACLTokenDeleteCommand_ViaEnvVariable(t *testing.T) { | ||
assert := assert.New(t) | ||
t.Parallel() | ||
config := func(c *agent.Config) { | ||
c.ACL.Enabled = true | ||
} | ||
|
||
srv, _, url := testServer(t, true, config) | ||
defer srv.Shutdown() | ||
|
||
// Bootstrap an initial ACL token | ||
token := srv.Token | ||
assert.NotNil(token, "failed to bootstrap ACL token") | ||
|
||
ui := new(cli.MockUi) | ||
cmd := &ACLTokenDeleteCommand{Meta: Meta{Ui: ui, flagAddress: url}} | ||
state := srv.Agent.Server().State() | ||
|
||
// Create a valid token | ||
mockToken := mock.ACLToken() | ||
token.Policies = []string{acl.PolicyWrite} | ||
token.SetHash() | ||
assert.Nil(state.UpsertACLTokens(1000, []*structs.ACLToken{token})) | ||
|
||
// Attempt to delete a token without providing a valid token with delete | ||
// permissions | ||
os.Setenv("NOMAD_TOKEN", "foo") | ||
code := cmd.Run([]string{"-address=" + url, mockToken.AccessorID}) | ||
assert.Equal(1, code) | ||
|
||
// Delete a token using a valid management token set via an environment | ||
// variable | ||
os.Setenv("NOMAD_TOKEN", token.SecretID) | ||
code = cmd.Run([]string{"-address=" + url, mockToken.AccessorID}) | ||
assert.Equal(0, code) | ||
|
||
// Check the output | ||
out := ui.OutputWriter.String() | ||
if !strings.Contains(out, fmt.Sprintf("Token %s successfully deleted", mockToken.AccessorID)) { | ||
t.Fatalf("bad: %v", out) | ||
} | ||
} |
This file contains 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
This file contains 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,33 @@ | ||
--- | ||
layout: "docs" | ||
page_title: "Commands: acl token delete" | ||
sidebar_current: "docs-commands-acl-token-delete" | ||
description: > | ||
The token create command is used to delete existing ACL tokens. | ||
--- | ||
|
||
# Command: acl token delete | ||
|
||
The `acl token delete` command is used to delete existing ACL tokens. | ||
|
||
## Usage | ||
|
||
``` | ||
nomad acl token delete <token_accessor_id> | ||
``` | ||
|
||
The `acl token delete` command requires an existing token's AccessorID. | ||
|
||
## General Options | ||
|
||
<%= partial "docs/commands/_general_options" %> | ||
|
||
## Examples | ||
|
||
Delete an existing ACL token: | ||
|
||
``` | ||
$ nomad acl token delete d532c40a-30f1-695c-19e5-c35b882b0efd | ||
|
||
Token d532c40a-30f1-695c-19e5-c35b882b0efd successfully deleted | ||
``` |