2016-03-02 18:42:32 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CapabilitiesCommand is a Command that enables a new endpoint.
|
|
|
|
type CapabilitiesCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CapabilitiesCommand) Run(args []string) int {
|
|
|
|
flags := c.Meta.FlagSet("capabilities", FlagSetDefault)
|
|
|
|
flags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = flags.Args()
|
|
|
|
if len(args) > 2 {
|
|
|
|
flags.Usage()
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"\ncapabilities expects at most two arguments"))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var token string
|
|
|
|
var path string
|
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:
|
2016-03-02 18:42:32 +00:00
|
|
|
token = args[0]
|
|
|
|
path = args[1]
|
2016-03-05 05:54:48 +00:00
|
|
|
default:
|
|
|
|
flags.Usage()
|
|
|
|
c.Ui.Error(fmt.Sprintf("\ncapabilities expects at least one argument"))
|
|
|
|
return 1
|
2016-03-02 18:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error initializing client: %s", err))
|
|
|
|
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 {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error retrieving capabilities: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-03-04 18:21:07 +00:00
|
|
|
c.Ui.Output(fmt.Sprintf("Capabilities: %s", capabilities))
|
2016-03-02 18:42:32 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CapabilitiesCommand) Synopsis() string {
|
2016-03-04 18:21:07 +00:00
|
|
|
return "Fetch the capabilities of a token on a given path"
|
2016-03-02 18:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CapabilitiesCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: vault capabilities [options] [token] path
|
|
|
|
|
|
|
|
Fetch the capabilities of a token on a given path.
|
2016-03-07 23:36:26 +00:00
|
|
|
If a token is provided as an argument, the '/sys/capabilities' endpoint will be invoked
|
|
|
|
with the given token; otherwise the '/sys/capabilities-self' endpoint will be invoked
|
2016-03-05 05:54:48 +00:00
|
|
|
with the client token.
|
2016-03-02 18:42:32 +00:00
|
|
|
|
2016-03-05 05:54:48 +00:00
|
|
|
If a token does not have any capability on a given path, or if any of the policies
|
2016-03-07 23:36:26 +00:00
|
|
|
belonging to the token explicitly have ["deny"] capability, or if the argument path
|
2016-03-05 05:54:48 +00:00
|
|
|
is invalid, this command will respond with a ["deny"].
|
2016-03-04 18:21:07 +00:00
|
|
|
|
2016-03-02 18:42:32 +00:00
|
|
|
General Options:
|
|
|
|
|
|
|
|
` + generalOptionsUsage()
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|