Add token as a subcommand

This commit is contained in:
Seth Vargo
2017-09-07 21:58:13 -04:00
parent 98b356d7f1
commit b50d7d69bd
7 changed files with 87 additions and 51 deletions

46
command/token.go Normal file
View File

@@ -0,0 +1,46 @@
package command
import (
"strings"
"github.com/mitchellh/cli"
)
var _ cli.Command = (*TokenCommand)(nil)
type TokenCommand struct {
*BaseCommand
}
func (c *TokenCommand) Synopsis() string {
return "Interact with tokens"
}
func (c *TokenCommand) Help() string {
helpText := `
Usage: vault token <subcommand> [options] [args]
This command groups subcommands for interacting with tokens. Users can
create, lookup, renew, and revoke tokens.
Create a new token:
$ vault token create
Revoke a token:
$ vault token revoke 96ddf4bc-d217-f3ba-f9bd-017055595017
Renew a token:
$ vault token renew 96ddf4bc-d217-f3ba-f9bd-017055595017
Please see the individual subcommand help for detailed usage information.
`
return strings.TrimSpace(helpText)
}
func (c *TokenCommand) Run(args []string) int {
return cli.RunResultHelp
}

View File

@@ -9,22 +9,20 @@ import (
"github.com/posener/complete" "github.com/posener/complete"
) )
// Ensure we are implementing the right interfaces. var _ cli.Command = (*TokenCapabilitiesCommand)(nil)
var _ cli.Command = (*CapabilitiesCommand)(nil) var _ cli.CommandAutocomplete = (*TokenCapabilitiesCommand)(nil)
var _ cli.CommandAutocomplete = (*CapabilitiesCommand)(nil)
// CapabilitiesCommand is a Command that enables a new endpoint. type TokenCapabilitiesCommand struct {
type CapabilitiesCommand struct {
*BaseCommand *BaseCommand
} }
func (c *CapabilitiesCommand) Synopsis() string { func (c *TokenCapabilitiesCommand) Synopsis() string {
return "Fetchs the capabilities of a token" return "Print capabilities of a token on a path"
} }
func (c *CapabilitiesCommand) Help() string { func (c *TokenCapabilitiesCommand) Help() string {
helpText := ` helpText := `
Usage: vault capabilities [options] [TOKEN] PATH Usage: vault token capabilities [options] [TOKEN] PATH
Fetches the capabilities of a token for a given path. If a TOKEN is provided Fetches the capabilities of a token for a given path. If a TOKEN is provided
as an argument, the "/sys/capabilities" endpoint and permission is used. If as an argument, the "/sys/capabilities" endpoint and permission is used. If
@@ -33,11 +31,11 @@ Usage: vault capabilities [options] [TOKEN] PATH
List capabilities for the local token on the "secret/foo" path: List capabilities for the local token on the "secret/foo" path:
$ vault capabilities secret/foo $ vault token capabilities secret/foo
List capabilities for a token on the "cubbyhole/foo" path: List capabilities for a token on the "cubbyhole/foo" path:
$ vault capabilities 96ddf4bc-d217-f3ba-f9bd-017055595017 cubbyhole/foo $ vault token capabilities 96ddf4bc-d217-f3ba-f9bd-017055595017 cubbyhole/foo
For a full list of examples, please see the documentation. For a full list of examples, please see the documentation.
@@ -46,19 +44,19 @@ Usage: vault capabilities [options] [TOKEN] PATH
return strings.TrimSpace(helpText) return strings.TrimSpace(helpText)
} }
func (c *CapabilitiesCommand) Flags() *FlagSets { func (c *TokenCapabilitiesCommand) Flags() *FlagSets {
return c.flagSet(FlagSetHTTP) return c.flagSet(FlagSetHTTP)
} }
func (c *CapabilitiesCommand) AutocompleteArgs() complete.Predictor { func (c *TokenCapabilitiesCommand) AutocompleteArgs() complete.Predictor {
return nil return nil
} }
func (c *CapabilitiesCommand) AutocompleteFlags() complete.Flags { func (c *TokenCapabilitiesCommand) AutocompleteFlags() complete.Flags {
return c.Flags().Completions() return c.Flags().Completions()
} }
func (c *CapabilitiesCommand) Run(args []string) int { func (c *TokenCapabilitiesCommand) Run(args []string) int {
f := c.Flags() f := c.Flags()
if err := f.Parse(args); err != nil { if err := f.Parse(args); err != nil {

View File

@@ -8,18 +8,18 @@ import (
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func testCapabilitiesCommand(tb testing.TB) (*cli.MockUi, *CapabilitiesCommand) { func testTokenCapabilitiesCommand(tb testing.TB) (*cli.MockUi, *TokenCapabilitiesCommand) {
tb.Helper() tb.Helper()
ui := cli.NewMockUi() ui := cli.NewMockUi()
return ui, &CapabilitiesCommand{ return ui, &TokenCapabilitiesCommand{
BaseCommand: &BaseCommand{ BaseCommand: &BaseCommand{
UI: ui, UI: ui,
}, },
} }
} }
func TestCapabilitiesCommand_Run(t *testing.T) { func TestTokenCapabilitiesCommand_Run(t *testing.T) {
t.Parallel() t.Parallel()
cases := []struct { cases := []struct {
@@ -42,7 +42,7 @@ func TestCapabilitiesCommand_Run(t *testing.T) {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
t.Parallel() t.Parallel()
ui, cmd := testCapabilitiesCommand(t) ui, cmd := testTokenCapabilitiesCommand(t)
code := cmd.Run(tc.args) code := cmd.Run(tc.args)
if code != tc.code { if code != tc.code {
@@ -79,7 +79,7 @@ func TestCapabilitiesCommand_Run(t *testing.T) {
} }
token := secret.Auth.ClientToken token := secret.Auth.ClientToken
ui, cmd := testCapabilitiesCommand(t) ui, cmd := testTokenCapabilitiesCommand(t)
cmd.client = client cmd.client = client
code := cmd.Run([]string{ code := cmd.Run([]string{
@@ -121,7 +121,7 @@ func TestCapabilitiesCommand_Run(t *testing.T) {
client.SetToken(token) client.SetToken(token)
ui, cmd := testCapabilitiesCommand(t) ui, cmd := testTokenCapabilitiesCommand(t)
cmd.client = client cmd.client = client
code := cmd.Run([]string{ code := cmd.Run([]string{
@@ -144,7 +144,7 @@ func TestCapabilitiesCommand_Run(t *testing.T) {
client, closer := testVaultServerBad(t) client, closer := testVaultServerBad(t)
defer closer() defer closer()
ui, cmd := testCapabilitiesCommand(t) ui, cmd := testTokenCapabilitiesCommand(t)
cmd.client = client cmd.client = client
code := cmd.Run([]string{ code := cmd.Run([]string{
@@ -164,7 +164,7 @@ func TestCapabilitiesCommand_Run(t *testing.T) {
t.Run("no_tabs", func(t *testing.T) { t.Run("no_tabs", func(t *testing.T) {
t.Parallel() t.Parallel()
_, cmd := testCapabilitiesCommand(t) _, cmd := testTokenCapabilitiesCommand(t)
assertNoTabs(t, cmd) assertNoTabs(t, cmd)
}) })
} }

View File

@@ -10,11 +10,9 @@ import (
"github.com/posener/complete" "github.com/posener/complete"
) )
// Ensure we are implementing the right interfaces.
var _ cli.Command = (*TokenCreateCommand)(nil) var _ cli.Command = (*TokenCreateCommand)(nil)
var _ cli.CommandAutocomplete = (*TokenCreateCommand)(nil) var _ cli.CommandAutocomplete = (*TokenCreateCommand)(nil)
// TokenCreateCommand is a Command that mounts a new mount.
type TokenCreateCommand struct { type TokenCreateCommand struct {
*BaseCommand *BaseCommand
@@ -36,12 +34,12 @@ type TokenCreateCommand struct {
} }
func (c *TokenCreateCommand) Synopsis() string { func (c *TokenCreateCommand) Synopsis() string {
return "Creates a new token" return "Create a new token"
} }
func (c *TokenCreateCommand) Help() string { func (c *TokenCreateCommand) Help() string {
helpText := ` helpText := `
Usage: vault token-create [options] Usage: vault token create [options]
Creates a new token that can be used for authentication. This token will be Creates a new token that can be used for authentication. This token will be
created as a child of the currently authenticated token. The generated token created as a child of the currently authenticated token. The generated token
@@ -159,7 +157,7 @@ func (c *TokenCreateCommand) Flags() *FlagSets {
Name: "metadata", Name: "metadata",
Target: &c.flagMetadata, Target: &c.flagMetadata,
Completion: complete.PredictAnything, Completion: complete.PredictAnything,
Usage: "Arbitary key=value metadata to associate with the token. " + Usage: "Arbitrary key=value metadata to associate with the token. " +
"This metadata will show in the audit log when the token is used. " + "This metadata will show in the audit log when the token is used. " +
"This can be specified multiple times to add multiple pieces of " + "This can be specified multiple times to add multiple pieces of " +
"metadata.", "metadata.",

View File

@@ -9,11 +9,9 @@ import (
"github.com/posener/complete" "github.com/posener/complete"
) )
// Ensure we are implementing the right interfaces.
var _ cli.Command = (*TokenLookupCommand)(nil) var _ cli.Command = (*TokenLookupCommand)(nil)
var _ cli.CommandAutocomplete = (*TokenLookupCommand)(nil) var _ cli.CommandAutocomplete = (*TokenLookupCommand)(nil)
// TokenLookupCommand is a Command that outputs details about the provided.
type TokenLookupCommand struct { type TokenLookupCommand struct {
*BaseCommand *BaseCommand
@@ -21,12 +19,12 @@ type TokenLookupCommand struct {
} }
func (c *TokenLookupCommand) Synopsis() string { func (c *TokenLookupCommand) Synopsis() string {
return "Displays information about a token" return "Display information about a token"
} }
func (c *TokenLookupCommand) Help() string { func (c *TokenLookupCommand) Help() string {
helpText := ` helpText := `
Usage: vault token-lookup [options] [TOKEN | ACCESSOR] Usage: vault token lookup [options] [TOKEN | ACCESSOR]
Displays information about a token or accessor. If a TOKEN is not provided, Displays information about a token or accessor. If a TOKEN is not provided,
the locally authenticated token is used. the locally authenticated token is used.
@@ -34,16 +32,16 @@ Usage: vault token-lookup [options] [TOKEN | ACCESSOR]
Get information about the locally authenticated token (this uses the Get information about the locally authenticated token (this uses the
/auth/token/lookup-self endpoint and permission): /auth/token/lookup-self endpoint and permission):
$ vault token-lookup $ vault token lookup
Get information about a particular token (this uses the /auth/token/lookup Get information about a particular token (this uses the /auth/token/lookup
endpoint and permission): endpoint and permission):
$ vault token-lookup 96ddf4bc-d217-f3ba-f9bd-017055595017 $ vault token lookup 96ddf4bc-d217-f3ba-f9bd-017055595017
Get information about a token via its accessor: Get information about a token via its accessor:
$ vault token-lookup -accessor 9793c9b3-e04a-46f3-e7b8-748d7da248da $ vault token lookup -accessor 9793c9b3-e04a-46f3-e7b8-748d7da248da
For a full list of examples, please see the documentation. For a full list of examples, please see the documentation.
@@ -63,7 +61,7 @@ func (c *TokenLookupCommand) Flags() *FlagSets {
Default: false, Default: false,
EnvVar: "", EnvVar: "",
Completion: complete.PredictNothing, Completion: complete.PredictNothing,
Usage: "Treat the argument as an accessor intead of a token. When " + Usage: "Treat the argument as an accessor instead of a token. When " +
"this option is selected, the output will NOT include the token.", "this option is selected, the output will NOT include the token.",
}) })

View File

@@ -10,11 +10,9 @@ import (
"github.com/posener/complete" "github.com/posener/complete"
) )
// Ensure we are implementing the right interfaces.
var _ cli.Command = (*TokenRenewCommand)(nil) var _ cli.Command = (*TokenRenewCommand)(nil)
var _ cli.CommandAutocomplete = (*TokenRenewCommand)(nil) var _ cli.CommandAutocomplete = (*TokenRenewCommand)(nil)
// TokenRenewCommand is a Command that mounts a new mount.
type TokenRenewCommand struct { type TokenRenewCommand struct {
*BaseCommand *BaseCommand
@@ -22,12 +20,12 @@ type TokenRenewCommand struct {
} }
func (c *TokenRenewCommand) Synopsis() string { func (c *TokenRenewCommand) Synopsis() string {
return "Renews token leases" return "Renew a token lease"
} }
func (c *TokenRenewCommand) Help() string { func (c *TokenRenewCommand) Help() string {
helpText := ` helpText := `
Usage: vault token-renew [options] [TOKEN] Usage: vault token renew [options] [TOKEN]
Renews a token's lease, extending the amount of time it can be used. If a Renews a token's lease, extending the amount of time it can be used. If a
TOKEN is not provided, the locally authenticated token is used. Lease renewal TOKEN is not provided, the locally authenticated token is used. Lease renewal
@@ -36,16 +34,16 @@ Usage: vault token-renew [options] [TOKEN]
Renew a token (this uses the /auth/token/renew endpoint and permission): Renew a token (this uses the /auth/token/renew endpoint and permission):
$ vault token-renew 96ddf4bc-d217-f3ba-f9bd-017055595017 $ vault token renew 96ddf4bc-d217-f3ba-f9bd-017055595017
Renew the currently authenticated token (this uses the /auth/token/renew-self Renew the currently authenticated token (this uses the /auth/token/renew-self
endpoint and permission): endpoint and permission):
$ vault token-renew $ vault token renew
Renew a token requesting a specific increment value: Renew a token requesting a specific increment value:
$ vault token-renew -increment 30m 96ddf4bc-d217-f3ba-f9bd-017055595017 $ vault token renew -increment=30m 96ddf4bc-d217-f3ba-f9bd-017055595017
For a full list of examples, please see the documentation. For a full list of examples, please see the documentation.

View File

@@ -8,11 +8,9 @@ import (
"github.com/posener/complete" "github.com/posener/complete"
) )
// Ensure we are implementing the right interfaces.
var _ cli.Command = (*TokenRevokeCommand)(nil) var _ cli.Command = (*TokenRevokeCommand)(nil)
var _ cli.CommandAutocomplete = (*TokenRevokeCommand)(nil) var _ cli.CommandAutocomplete = (*TokenRevokeCommand)(nil)
// TokenRevokeCommand is a Command that mounts a new mount.
type TokenRevokeCommand struct { type TokenRevokeCommand struct {
*BaseCommand *BaseCommand
@@ -22,12 +20,12 @@ type TokenRevokeCommand struct {
} }
func (c *TokenRevokeCommand) Synopsis() string { func (c *TokenRevokeCommand) Synopsis() string {
return "Revokes tokens and their children" return "Revoke a token and its children"
} }
func (c *TokenRevokeCommand) Help() string { func (c *TokenRevokeCommand) Help() string {
helpText := ` helpText := `
Usage: vault token-revoke [options] [TOKEN | ACCESSOR] Usage: vault token revoke [options] [TOKEN | ACCESSOR]
Revokes authentication tokens and their children. If a TOKEN is not provided, Revokes authentication tokens and their children. If a TOKEN is not provided,
the locally authenticated token is used. The "-mode" flag can be used to the locally authenticated token is used. The "-mode" flag can be used to
@@ -36,15 +34,15 @@ Usage: vault token-revoke [options] [TOKEN | ACCESSOR]
Revoke a token and all the token's children: Revoke a token and all the token's children:
$ vault token-revoke 96ddf4bc-d217-f3ba-f9bd-017055595017 $ vault token revoke 96ddf4bc-d217-f3ba-f9bd-017055595017
Revoke a token leaving the token's children: Revoke a token leaving the token's children:
$ vault token-revoke -mode=orphan 96ddf4bc-d217-f3ba-f9bd-017055595017 $ vault token revoke -mode=orphan 96ddf4bc-d217-f3ba-f9bd-017055595017
Revoke a token by accessor: Revoke a token by accessor:
$ vault token-revoke -accessor 9793c9b3-e04a-46f3-e7b8-748d7da248da $ vault token revoke -accessor 9793c9b3-e04a-46f3-e7b8-748d7da248da
For a full list of examples, please see the documentation. For a full list of examples, please see the documentation.