open-vault/command/token_capabilities.go

101 lines
2.3 KiB
Go
Raw Normal View History

2016-03-02 18:42:32 +00:00
package command
import (
"fmt"
2017-09-05 04:00:00 +00:00
"sort"
2016-03-02 18:42:32 +00:00
"strings"
2016-04-01 17:16:05 +00:00
2017-09-05 04:00:00 +00:00
"github.com/mitchellh/cli"
"github.com/posener/complete"
2016-03-02 18:42:32 +00:00
)
2017-09-08 01:58:13 +00:00
var _ cli.Command = (*TokenCapabilitiesCommand)(nil)
var _ cli.CommandAutocomplete = (*TokenCapabilitiesCommand)(nil)
2017-09-05 04:00:00 +00:00
2017-09-08 01:58:13 +00:00
type TokenCapabilitiesCommand struct {
2017-09-05 04:00:00 +00:00
*BaseCommand
}
2017-09-08 01:58:13 +00:00
func (c *TokenCapabilitiesCommand) Synopsis() string {
return "Print capabilities of a token on a path"
2017-09-05 04:00:00 +00:00
}
2017-09-08 01:58:13 +00:00
func (c *TokenCapabilitiesCommand) Help() string {
2017-09-05 04:00:00 +00:00
helpText := `
2017-09-08 01:58:13 +00:00
Usage: vault token capabilities [options] [TOKEN] PATH
2017-09-05 04:00:00 +00:00
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
2017-09-08 01:58:13 +00:00
no TOKEN is provided, the "/sys/capabilities-self" endpoint and permission
2017-09-05 04:00:00 +00:00
is used with the locally authenticated token.
List capabilities for the local token on the "secret/foo" path:
2017-09-08 01:58:13 +00:00
$ vault token capabilities secret/foo
2017-09-05 04:00:00 +00:00
List capabilities for a token on the "cubbyhole/foo" path:
2017-09-08 01:58:13 +00:00
$ vault token capabilities 96ddf4bc-d217-f3ba-f9bd-017055595017 cubbyhole/foo
2017-09-05 04:00:00 +00:00
For a full list of examples, please see the documentation.
` + c.Flags().Help()
return strings.TrimSpace(helpText)
}
2017-09-08 01:58:13 +00:00
func (c *TokenCapabilitiesCommand) Flags() *FlagSets {
2017-09-05 04:00:00 +00:00
return c.flagSet(FlagSetHTTP)
}
2017-09-08 01:58:13 +00:00
func (c *TokenCapabilitiesCommand) AutocompleteArgs() complete.Predictor {
2017-09-05 04:00:00 +00:00
return nil
}
2017-09-08 01:58:13 +00:00
func (c *TokenCapabilitiesCommand) AutocompleteFlags() complete.Flags {
2017-09-05 04:00:00 +00:00
return c.Flags().Completions()
2016-03-02 18:42:32 +00:00
}
2017-09-08 01:58:13 +00:00
func (c *TokenCapabilitiesCommand) Run(args []string) int {
2017-09-05 04:00:00 +00:00
f := c.Flags()
2016-03-02 18:42:32 +00:00
2017-09-05 04:00:00 +00:00
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
2016-03-02 18:42:32 +00:00
return 1
}
2017-09-05 04:00:00 +00:00
token := ""
path := ""
args = f.Args()
switch {
case len(args) == 1:
2016-03-02 18:42:32 +00:00
path = args[0]
case len(args) == 2:
2017-09-05 04:00:00 +00:00
token, path = args[0], args[1]
default:
2017-09-05 04:00:00 +00:00
c.UI.Error(fmt.Sprintf("Too many arguments (expected 1-2, got %d)", len(args)))
return 1
2016-03-02 18:42:32 +00:00
}
client, err := c.Client()
if err != nil {
2017-09-05 04:00:00 +00:00
c.UI.Error(err.Error())
2016-03-02 18:42:32 +00:00
return 2
}
var capabilities []string
2016-03-02 18:42:32 +00:00
if token == "" {
capabilities, err = client.Sys().CapabilitiesSelf(path)
2016-03-02 18:42:32 +00:00
} else {
capabilities, err = client.Sys().Capabilities(token, path)
2016-03-02 18:42:32 +00:00
}
if err != nil {
2017-09-05 04:00:00 +00:00
c.UI.Error(fmt.Sprintf("Error listing capabilities: %s", err))
return 2
2016-03-02 18:42:32 +00:00
}
2017-09-05 04:00:00 +00:00
sort.Strings(capabilities)
c.UI.Output(strings.Join(capabilities, ", "))
2016-03-02 18:42:32 +00:00
return 0
}