2015-03-16 04:28:31 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
2015-09-25 13:46:20 +00:00
|
|
|
"strconv"
|
2015-03-16 04:28:31 +00:00
|
|
|
"strings"
|
2015-04-02 00:01:10 +00:00
|
|
|
|
2017-09-05 04:02:45 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/posener/complete"
|
2015-03-16 04:28:31 +00:00
|
|
|
)
|
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
var _ cli.Command = (*SecretsListCommand)(nil)
|
|
|
|
var _ cli.CommandAutocomplete = (*SecretsListCommand)(nil)
|
2017-09-05 04:02:45 +00:00
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
type SecretsListCommand struct {
|
2017-09-05 04:02:45 +00:00
|
|
|
*BaseCommand
|
|
|
|
|
|
|
|
flagDetailed bool
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
func (c *SecretsListCommand) Synopsis() string {
|
|
|
|
return "List enabled secrets engines"
|
2017-09-05 04:02:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
func (c *SecretsListCommand) Help() string {
|
2017-09-05 04:02:45 +00:00
|
|
|
helpText := `
|
2017-09-08 02:01:31 +00:00
|
|
|
Usage: vault secrets list [options]
|
2017-09-05 04:02:45 +00:00
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
Lists the enabled secret engines on the Vault server. This command also
|
|
|
|
outputs information about the enabled path including configured TTLs and
|
2017-09-05 04:02:45 +00:00
|
|
|
human-friendly descriptions. A TTL of "system" indicates that the system
|
|
|
|
default is in use.
|
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
List all enabled secrets engines:
|
2017-09-05 04:02:45 +00:00
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
$ vault secrets list
|
2017-09-05 04:02:45 +00:00
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
List all enabled secrets engines with detailed output:
|
2017-09-05 04:02:45 +00:00
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
$ vault secrets list -detailed
|
2017-09-05 04:02:45 +00:00
|
|
|
|
|
|
|
` + c.Flags().Help()
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
func (c *SecretsListCommand) Flags() *FlagSets {
|
2018-02-12 23:12:16 +00:00
|
|
|
set := c.flagSet(FlagSetHTTP | FlagSetOutputFormat)
|
2017-09-05 04:02:45 +00:00
|
|
|
|
|
|
|
f := set.NewFlagSet("Command Options")
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "detailed",
|
|
|
|
Target: &c.flagDetailed,
|
|
|
|
Default: false,
|
|
|
|
Usage: "Print detailed information such as TTLs and replication status " +
|
2017-09-08 02:01:31 +00:00
|
|
|
"about each secrets engine.",
|
2017-09-05 04:02:45 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
func (c *SecretsListCommand) AutocompleteArgs() complete.Predictor {
|
2017-09-05 04:02:45 +00:00
|
|
|
return c.PredictVaultFiles()
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
func (c *SecretsListCommand) AutocompleteFlags() complete.Flags {
|
2017-09-05 04:02:45 +00:00
|
|
|
return c.Flags().Completions()
|
2015-03-16 04:28:31 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
func (c *SecretsListCommand) Run(args []string) int {
|
2017-09-05 04:02:45 +00:00
|
|
|
f := c.Flags()
|
|
|
|
|
|
|
|
if err := f.Parse(args); err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = f.Args()
|
|
|
|
if len(args) > 0 {
|
|
|
|
c.UI.Error(fmt.Sprintf("Too many arguments (expected 0, got %d)", len(args)))
|
2015-03-16 04:28:31 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
2017-09-05 04:02:45 +00:00
|
|
|
c.UI.Error(err.Error())
|
2015-03-16 04:28:31 +00:00
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
mounts, err := client.Sys().ListMounts()
|
|
|
|
if err != nil {
|
2017-09-08 02:01:31 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error listing secrets engines: %s", err))
|
2015-03-16 04:28:31 +00:00
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
switch Format(c.UI) {
|
|
|
|
case "table":
|
|
|
|
if c.flagDetailed {
|
|
|
|
c.UI.Output(tableOutput(c.detailedMounts(mounts), nil))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
c.UI.Output(tableOutput(c.simpleMounts(mounts), nil))
|
2017-09-05 04:02:45 +00:00
|
|
|
return 0
|
2018-02-12 23:12:16 +00:00
|
|
|
default:
|
|
|
|
return OutputData(c.UI, mounts)
|
2017-09-05 04:02:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
func (c *SecretsListCommand) simpleMounts(mounts map[string]*api.MountOutput) []string {
|
2015-03-16 04:28:31 +00:00
|
|
|
paths := make([]string, 0, len(mounts))
|
2015-09-02 19:56:58 +00:00
|
|
|
for path := range mounts {
|
2015-03-16 04:28:31 +00:00
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
|
|
|
sort.Strings(paths)
|
|
|
|
|
2017-09-05 04:02:45 +00:00
|
|
|
out := []string{"Path | Type | Description"}
|
2015-03-16 04:28:31 +00:00
|
|
|
for _, path := range paths {
|
|
|
|
mount := mounts[path]
|
2017-09-05 04:02:45 +00:00
|
|
|
out = append(out, fmt.Sprintf("%s | %s | %s", path, mount.Type, mount.Description))
|
|
|
|
}
|
2017-10-23 21:39:21 +00:00
|
|
|
|
2017-09-05 04:02:45 +00:00
|
|
|
return out
|
|
|
|
}
|
2017-10-23 21:39:21 +00:00
|
|
|
|
2017-09-08 02:01:31 +00:00
|
|
|
func (c *SecretsListCommand) detailedMounts(mounts map[string]*api.MountOutput) []string {
|
2017-09-05 04:02:45 +00:00
|
|
|
paths := make([]string, 0, len(mounts))
|
|
|
|
for path := range mounts {
|
|
|
|
paths = append(paths, path)
|
2015-03-16 04:28:31 +00:00
|
|
|
}
|
2017-09-05 04:02:45 +00:00
|
|
|
sort.Strings(paths)
|
2015-03-16 04:28:31 +00:00
|
|
|
|
2017-09-05 04:02:45 +00:00
|
|
|
calcTTL := func(typ string, ttl int) string {
|
|
|
|
switch {
|
|
|
|
case typ == "system", typ == "cubbyhole":
|
|
|
|
return ""
|
|
|
|
case ttl != 0:
|
|
|
|
return strconv.Itoa(ttl)
|
|
|
|
default:
|
|
|
|
return "system"
|
|
|
|
}
|
|
|
|
}
|
2015-03-16 04:28:31 +00:00
|
|
|
|
2018-04-09 19:42:18 +00:00
|
|
|
out := []string{"Path | Type | Accessor | Plugin | Default TTL | Max TTL | Force No Cache | Replication | Seal Wrap | Options | Description"}
|
2017-09-05 04:02:45 +00:00
|
|
|
for _, path := range paths {
|
|
|
|
mount := mounts[path]
|
2015-03-16 04:28:31 +00:00
|
|
|
|
2017-09-05 04:02:45 +00:00
|
|
|
defaultTTL := calcTTL(mount.Type, mount.Config.DefaultLeaseTTL)
|
|
|
|
maxTTL := calcTTL(mount.Type, mount.Config.MaxLeaseTTL)
|
2015-03-16 04:28:31 +00:00
|
|
|
|
2017-09-05 04:02:45 +00:00
|
|
|
replication := "replicated"
|
|
|
|
if mount.Local {
|
|
|
|
replication = "local"
|
|
|
|
}
|
2015-03-16 04:28:31 +00:00
|
|
|
|
2018-04-09 19:42:18 +00:00
|
|
|
out = append(out, fmt.Sprintf("%s | %s | %s | %s | %s | %s | %t | %s | %t | %v | %s",
|
2017-09-05 04:02:45 +00:00
|
|
|
path,
|
|
|
|
mount.Type,
|
|
|
|
mount.Accessor,
|
|
|
|
mount.Config.PluginName,
|
|
|
|
defaultTTL,
|
|
|
|
maxTTL,
|
|
|
|
mount.Config.ForceNoCache,
|
|
|
|
replication,
|
2018-01-03 19:02:31 +00:00
|
|
|
mount.SealWrap,
|
2018-04-09 19:42:18 +00:00
|
|
|
mount.Options,
|
2017-09-05 04:02:45 +00:00
|
|
|
mount.Description,
|
|
|
|
))
|
|
|
|
}
|
2015-03-16 04:28:31 +00:00
|
|
|
|
2017-09-05 04:02:45 +00:00
|
|
|
return out
|
2015-03-16 04:28:31 +00:00
|
|
|
}
|