Skip to content

Commit

Permalink
adds ACL token delete
Browse files Browse the repository at this point in the history
  • Loading branch information
chelseakomlo committed Sep 16, 2017
1 parent 58ac12a commit 730c5ad
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
75 changes: 75 additions & 0 deletions command/acl_token_delete.go
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
}
58 changes: 58 additions & 0 deletions command/acl_token_delete_test.go
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)
}
}
2 changes: 2 additions & 0 deletions website/source/docs/commands/acl.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ subcommands are available:
* [`acl bootstrap`][bootstrap] - Bootstrap the initial ACL token
* [`acl policy apply`][policyapply] - Create or update ACL policies
* [`acl token create`][tokencreate] - Create new ACL token
* [`acl token delete`][tokendelete] - Delete an existing ACL token

[bootstrap]: /docs/commands/acl/bootstrap.html
[policyapply]: /docs/commands/acl/policy-apply.html
[tokencreate]: /docs/commands/acl/token-create.html
[tokendelete]: /docs/commands/acl/token-delete.html

33 changes: 33 additions & 0 deletions website/source/docs/commands/acl/token-delete.html.md.erb
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
```

0 comments on commit 730c5ad

Please sign in to comment.