2015-04-08 01:19:44 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2017-09-05 03:59:13 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/posener/complete"
|
2015-04-08 01:19:44 +00:00
|
|
|
)
|
|
|
|
|
2017-09-05 03:59:13 +00:00
|
|
|
var _ cli.Command = (*AuditListCommand)(nil)
|
|
|
|
var _ cli.CommandAutocomplete = (*AuditListCommand)(nil)
|
|
|
|
|
2015-04-08 01:19:44 +00:00
|
|
|
type AuditListCommand struct {
|
2017-09-05 03:59:13 +00:00
|
|
|
*BaseCommand
|
|
|
|
|
|
|
|
flagDetailed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuditListCommand) Synopsis() string {
|
2017-09-08 01:57:32 +00:00
|
|
|
return "Lists enabled audit devices"
|
2017-09-05 03:59:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuditListCommand) Help() string {
|
|
|
|
helpText := `
|
2017-09-08 01:57:32 +00:00
|
|
|
Usage: vault audit list [options]
|
2017-09-05 03:59:13 +00:00
|
|
|
|
2017-09-08 01:57:32 +00:00
|
|
|
Lists the enabled audit devices in the Vault server. The output lists the
|
|
|
|
enabled audit devices and the options for those devices.
|
2017-09-05 03:59:13 +00:00
|
|
|
|
2017-09-08 01:57:32 +00:00
|
|
|
List all audit devices:
|
2017-09-05 03:59:13 +00:00
|
|
|
|
2017-09-08 01:57:32 +00:00
|
|
|
$ vault audit list
|
2017-09-05 03:59:13 +00:00
|
|
|
|
2017-09-08 01:57:32 +00:00
|
|
|
List detailed output about the audit devices:
|
2017-09-05 03:59:13 +00:00
|
|
|
|
2017-09-08 01:57:32 +00:00
|
|
|
$ vault audit list -detailed
|
2017-09-05 03:59:13 +00:00
|
|
|
|
|
|
|
` + c.Flags().Help()
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuditListCommand) Flags() *FlagSets {
|
|
|
|
set := c.flagSet(FlagSetHTTP)
|
|
|
|
|
|
|
|
f := set.NewFlagSet("Command Options")
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "detailed",
|
|
|
|
Target: &c.flagDetailed,
|
|
|
|
Default: false,
|
|
|
|
EnvVar: "",
|
|
|
|
Usage: "Print detailed information such as options and replication " +
|
2017-09-08 01:57:32 +00:00
|
|
|
"status about each auth device.",
|
2017-09-05 03:59:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuditListCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuditListCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return c.Flags().Completions()
|
2015-04-08 01:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuditListCommand) Run(args []string) int {
|
2017-09-05 03:59:13 +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-04-08 01:19:44 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
2017-09-05 03:59:13 +00:00
|
|
|
c.UI.Error(err.Error())
|
2015-04-08 01:19:44 +00:00
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
audits, err := client.Sys().ListAudit()
|
|
|
|
if err != nil {
|
2017-09-05 03:59:13 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error listing audits: %s", err))
|
2015-04-08 01:19:44 +00:00
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(audits) == 0 {
|
2017-09-08 01:57:32 +00:00
|
|
|
c.UI.Output(fmt.Sprintf("No audit devices are enabled."))
|
2017-09-05 03:59:13 +00:00
|
|
|
return 0
|
2015-04-08 01:19:44 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:59:13 +00:00
|
|
|
if c.flagDetailed {
|
2017-09-08 01:57:32 +00:00
|
|
|
c.UI.Output(tableOutput(c.detailedAudits(audits), nil))
|
2017-09-05 03:59:13 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-09-08 01:57:32 +00:00
|
|
|
c.UI.Output(tableOutput(c.simpleAudits(audits), nil))
|
2017-09-05 03:59:13 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuditListCommand) simpleAudits(audits map[string]*api.Audit) []string {
|
2015-04-08 01:19:44 +00:00
|
|
|
paths := make([]string, 0, len(audits))
|
|
|
|
for path, _ := range audits {
|
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
|
|
|
sort.Strings(paths)
|
|
|
|
|
2017-09-05 03:59:13 +00:00
|
|
|
columns := []string{"Path | Type | Description"}
|
2015-04-08 01:19:44 +00:00
|
|
|
for _, path := range paths {
|
|
|
|
audit := audits[path]
|
2017-09-05 03:59:13 +00:00
|
|
|
columns = append(columns, fmt.Sprintf("%s | %s | %s",
|
|
|
|
audit.Path,
|
|
|
|
audit.Type,
|
|
|
|
audit.Description,
|
|
|
|
))
|
2015-04-08 01:19:44 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:59:13 +00:00
|
|
|
return columns
|
2015-04-08 01:19:44 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:59:13 +00:00
|
|
|
func (c *AuditListCommand) detailedAudits(audits map[string]*api.Audit) []string {
|
|
|
|
paths := make([]string, 0, len(audits))
|
|
|
|
for path, _ := range audits {
|
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
|
|
|
sort.Strings(paths)
|
2015-04-08 01:19:44 +00:00
|
|
|
|
2017-09-05 03:59:13 +00:00
|
|
|
columns := []string{"Path | Type | Description | Replication | Options"}
|
|
|
|
for _, path := range paths {
|
|
|
|
audit := audits[path]
|
|
|
|
|
|
|
|
opts := make([]string, 0, len(audit.Options))
|
|
|
|
for k, v := range audit.Options {
|
|
|
|
opts = append(opts, k+"="+v)
|
|
|
|
}
|
2015-04-08 01:19:44 +00:00
|
|
|
|
2017-09-05 03:59:13 +00:00
|
|
|
replication := "replicated"
|
|
|
|
if audit.Local {
|
|
|
|
replication = "local"
|
|
|
|
}
|
2015-04-08 01:19:44 +00:00
|
|
|
|
2017-09-05 03:59:13 +00:00
|
|
|
columns = append(columns, fmt.Sprintf("%s | %s | %s | %s | %s",
|
|
|
|
audit.Path,
|
|
|
|
audit.Type,
|
|
|
|
audit.Description,
|
|
|
|
replication,
|
|
|
|
strings.Join(opts, " "),
|
|
|
|
))
|
|
|
|
}
|
2015-04-08 01:19:44 +00:00
|
|
|
|
2017-09-05 03:59:13 +00:00
|
|
|
return columns
|
2015-04-08 01:19:44 +00:00
|
|
|
}
|