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