open-vault/command/token_capabilities.go

115 lines
2.6 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
)
var (
_ cli.Command = (*TokenCapabilitiesCommand)(nil)
_ 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 {
CLI Enhancements (#3897) * Use Colored UI if stdout is a tty * Add format options to operator unseal * Add format test on operator unseal * Add -no-color output flag, and use BasicUi if no-color flag is provided * Move seal status formatting logic to OutputSealStatus * Apply no-color to warnings from DeprecatedCommands as well * Add OutputWithFormat to support arbitrary data, add format option to auth list * Add ability to output arbitrary list data on TableFormatter * Clear up switch logic on format * Add format option for list-related commands * Add format option to rest of commands that returns a client API response * Remove initOutputYAML and initOutputJSON, and use OutputWithFormat instead * Remove outputAsYAML and outputAsJSON, and use OutputWithFormat instead * Remove -no-color flag, use env var exclusively to toggle colored output * Fix compile * Remove -no-color flag in main.go * Add missing FlagSetOutputFormat * Fix generate-root/decode test * Migrate init functions to main.go * Add no-color flag back as hidden * Handle non-supported data types for TableFormatter.OutputList * Pull formatting much further up to remove the need to use c.flagFormat (#3950) * Pull formatting much further up to remove the need to use c.flagFormat Also remove OutputWithFormat as the logic can cause issues. * Use const for env var * Minor updates * Remove unnecessary check * Fix SSH output and some tests * Fix tests * Make race detector not run on generate root since it kills Travis these days * Update docs * Update docs * Address review feedback * Handle --format as well as -format
2018-02-12 23:12:16 +00:00
return c.flagSet(FlagSetHTTP | FlagSetOutputFormat)
2017-09-05 04:00:00 +00:00
}
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()
2018-09-13 15:23:36 +00:00
switch len(args) {
case 0:
c.UI.Error(fmt.Sprintf("Not enough arguments (expected 1-2, got 0)"))
return 1
case 1:
2016-03-02 18:42:32 +00:00
path = args[0]
2018-09-13 15:23:36 +00:00
case 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
}
if capabilities == nil {
c.UI.Error(fmt.Sprintf("No capabilities found"))
return 1
}
2016-03-02 18:42:32 +00:00
CLI Enhancements (#3897) * Use Colored UI if stdout is a tty * Add format options to operator unseal * Add format test on operator unseal * Add -no-color output flag, and use BasicUi if no-color flag is provided * Move seal status formatting logic to OutputSealStatus * Apply no-color to warnings from DeprecatedCommands as well * Add OutputWithFormat to support arbitrary data, add format option to auth list * Add ability to output arbitrary list data on TableFormatter * Clear up switch logic on format * Add format option for list-related commands * Add format option to rest of commands that returns a client API response * Remove initOutputYAML and initOutputJSON, and use OutputWithFormat instead * Remove outputAsYAML and outputAsJSON, and use OutputWithFormat instead * Remove -no-color flag, use env var exclusively to toggle colored output * Fix compile * Remove -no-color flag in main.go * Add missing FlagSetOutputFormat * Fix generate-root/decode test * Migrate init functions to main.go * Add no-color flag back as hidden * Handle non-supported data types for TableFormatter.OutputList * Pull formatting much further up to remove the need to use c.flagFormat (#3950) * Pull formatting much further up to remove the need to use c.flagFormat Also remove OutputWithFormat as the logic can cause issues. * Use const for env var * Minor updates * Remove unnecessary check * Fix SSH output and some tests * Fix tests * Make race detector not run on generate root since it kills Travis these days * Update docs * Update docs * Address review feedback * Handle --format as well as -format
2018-02-12 23:12:16 +00:00
switch Format(c.UI) {
case "table":
sort.Strings(capabilities)
c.UI.Output(strings.Join(capabilities, ", "))
return 0
default:
return OutputData(c.UI, capabilities)
}
2016-03-02 18:42:32 +00:00
}