2017-09-05 03:59:52 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ cli.Command = (*AuthListCommand)(nil)
|
|
|
|
var _ cli.CommandAutocomplete = (*AuthListCommand)(nil)
|
|
|
|
|
|
|
|
type AuthListCommand struct {
|
|
|
|
*BaseCommand
|
|
|
|
|
|
|
|
flagDetailed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuthListCommand) Synopsis() string {
|
2017-09-08 01:56:39 +00:00
|
|
|
return "Lists enabled auth methods"
|
2017-09-05 03:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuthListCommand) Help() string {
|
|
|
|
helpText := `
|
2017-09-08 01:56:39 +00:00
|
|
|
Usage: vault auth list [options]
|
2017-09-05 03:59:52 +00:00
|
|
|
|
2017-09-08 01:56:39 +00:00
|
|
|
Lists the enabled auth methods on the Vault server. This command also outputs
|
|
|
|
information about the method including configuration and human-friendly
|
|
|
|
descriptions. A TTL of "system" indicates that the system default is in use.
|
2017-09-05 03:59:52 +00:00
|
|
|
|
2017-09-08 01:56:39 +00:00
|
|
|
List all enabled auth methods:
|
2017-09-05 03:59:52 +00:00
|
|
|
|
2017-09-08 01:56:39 +00:00
|
|
|
$ vault auth list
|
2017-09-05 03:59:52 +00:00
|
|
|
|
2017-09-08 01:56:39 +00:00
|
|
|
List all enabled auth methods with detailed output:
|
2017-09-05 03:59:52 +00:00
|
|
|
|
2017-09-08 01:56:39 +00:00
|
|
|
$ vault auth list -detailed
|
2017-09-05 03:59:52 +00:00
|
|
|
|
|
|
|
` + c.Flags().Help()
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuthListCommand) Flags() *FlagSets {
|
2018-02-12 23:12:16 +00:00
|
|
|
set := c.flagSet(FlagSetHTTP | FlagSetOutputFormat)
|
2017-09-05 03:59:52 +00:00
|
|
|
|
|
|
|
f := set.NewFlagSet("Command Options")
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "detailed",
|
|
|
|
Target: &c.flagDetailed,
|
|
|
|
Default: false,
|
|
|
|
Usage: "Print detailed information such as configuration and replication " +
|
2018-02-12 23:12:16 +00:00
|
|
|
"status about each auth method. This option is only applicable to " +
|
|
|
|
"table-formatted output.",
|
2017-09-05 03:59:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuthListCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuthListCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return c.Flags().Completions()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuthListCommand) Run(args []string) int {
|
|
|
|
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)))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
auths, err := client.Sys().ListAuth()
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error listing enabled authentications: %s", err))
|
|
|
|
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(auths), nil))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
c.UI.Output(tableOutput(c.simpleMounts(auths), nil))
|
2017-09-05 03:59:52 +00:00
|
|
|
return 0
|
2018-02-12 23:12:16 +00:00
|
|
|
default:
|
|
|
|
return OutputData(c.UI, auths)
|
2017-09-05 03:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuthListCommand) simpleMounts(auths map[string]*api.AuthMount) []string {
|
|
|
|
paths := make([]string, 0, len(auths))
|
|
|
|
for path := range auths {
|
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
|
|
|
sort.Strings(paths)
|
|
|
|
|
2018-06-01 14:20:09 +00:00
|
|
|
out := []string{"Path | Type | Accessor | Description"}
|
2017-09-05 03:59:52 +00:00
|
|
|
for _, path := range paths {
|
|
|
|
mount := auths[path]
|
2018-06-01 14:20:09 +00:00
|
|
|
out = append(out, fmt.Sprintf("%s | %s | %s | %s", path, mount.Type, mount.Accessor, mount.Description))
|
2017-09-05 03:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuthListCommand) detailedMounts(auths map[string]*api.AuthMount) []string {
|
|
|
|
paths := make([]string, 0, len(auths))
|
|
|
|
for path := range auths {
|
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
|
|
|
sort.Strings(paths)
|
|
|
|
|
|
|
|
calcTTL := func(typ string, ttl int) string {
|
|
|
|
switch {
|
|
|
|
case typ == "system", typ == "cubbyhole":
|
|
|
|
return ""
|
|
|
|
case ttl != 0:
|
|
|
|
return strconv.Itoa(ttl)
|
|
|
|
default:
|
|
|
|
return "system"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 19:27:43 +00:00
|
|
|
out := []string{"Path | Plugin | Accessor | Default TTL | Max TTL | Token Type | Replication | Seal Wrap | Options | Description | UUID"}
|
2017-09-05 03:59:52 +00:00
|
|
|
for _, path := range paths {
|
|
|
|
mount := auths[path]
|
|
|
|
|
|
|
|
defaultTTL := calcTTL(mount.Type, mount.Config.DefaultLeaseTTL)
|
|
|
|
maxTTL := calcTTL(mount.Type, mount.Config.MaxLeaseTTL)
|
|
|
|
|
|
|
|
replication := "replicated"
|
|
|
|
if mount.Local {
|
|
|
|
replication = "local"
|
|
|
|
}
|
|
|
|
|
2018-11-20 00:41:17 +00:00
|
|
|
pluginName := mount.Type
|
|
|
|
if pluginName == "plugin" {
|
|
|
|
pluginName = mount.Config.PluginName
|
|
|
|
}
|
|
|
|
|
2019-04-24 19:27:43 +00:00
|
|
|
out = append(out, fmt.Sprintf("%s | %s | %s | %s | %s | %s | %s | %t | %v | %s | %s",
|
2017-09-05 03:59:52 +00:00
|
|
|
path,
|
2018-11-20 00:41:17 +00:00
|
|
|
pluginName,
|
2017-09-05 03:59:52 +00:00
|
|
|
mount.Accessor,
|
|
|
|
defaultTTL,
|
|
|
|
maxTTL,
|
2018-10-15 16:56:24 +00:00
|
|
|
mount.Config.TokenType,
|
2017-09-05 03:59:52 +00:00
|
|
|
replication,
|
2018-01-03 19:02:31 +00:00
|
|
|
mount.SealWrap,
|
2018-04-09 19:42:18 +00:00
|
|
|
mount.Options,
|
2017-09-05 03:59:52 +00:00
|
|
|
mount.Description,
|
2019-04-24 19:27:43 +00:00
|
|
|
mount.UUID,
|
2017-09-05 03:59:52 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|