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()
|
2016-03-04 18:21:07 +00:00
|
|
|
switch {
|
|
|
|
case len(args) == 1:
|
2016-03-02 18:42:32 +00:00
|
|
|
path = args[0]
|
2016-03-04 18:21:07 +00:00
|
|
|
case len(args) == 2:
|
2017-09-05 04:00:00 +00:00
|
|
|
token, path = args[0], args[1]
|
2016-03-05 05:54:48 +00:00
|
|
|
default:
|
2017-09-05 04:00:00 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Too many arguments (expected 1-2, got %d)", len(args)))
|
2016-03-05 05:54:48 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-03-04 18:21:07 +00:00
|
|
|
var capabilities []string
|
2016-03-02 18:42:32 +00:00
|
|
|
if token == "" {
|
2016-03-04 18:21:07 +00:00
|
|
|
capabilities, err = client.Sys().CapabilitiesSelf(path)
|
2016-03-02 18:42:32 +00:00
|
|
|
} else {
|
2016-03-04 18:21:07 +00:00
|
|
|
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
|
|
|
|
}
|