60732577f5
* 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
167 lines
3.3 KiB
Go
167 lines
3.3 KiB
Go
package command
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
"github.com/mitchellh/cli"
|
|
"github.com/posener/complete"
|
|
)
|
|
|
|
var _ cli.Command = (*AuditListCommand)(nil)
|
|
var _ cli.CommandAutocomplete = (*AuditListCommand)(nil)
|
|
|
|
type AuditListCommand struct {
|
|
*BaseCommand
|
|
|
|
flagDetailed bool
|
|
}
|
|
|
|
func (c *AuditListCommand) Synopsis() string {
|
|
return "Lists enabled audit devices"
|
|
}
|
|
|
|
func (c *AuditListCommand) Help() string {
|
|
helpText := `
|
|
Usage: vault audit list [options]
|
|
|
|
Lists the enabled audit devices in the Vault server. The output lists the
|
|
enabled audit devices and the options for those devices.
|
|
|
|
List all audit devices:
|
|
|
|
$ vault audit list
|
|
|
|
List detailed output about the audit devices:
|
|
|
|
$ vault audit list -detailed
|
|
|
|
` + c.Flags().Help()
|
|
|
|
return strings.TrimSpace(helpText)
|
|
}
|
|
|
|
func (c *AuditListCommand) Flags() *FlagSets {
|
|
set := c.flagSet(FlagSetHTTP | FlagSetOutputFormat)
|
|
|
|
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 " +
|
|
"status about each auth device.",
|
|
})
|
|
|
|
return set
|
|
}
|
|
|
|
func (c *AuditListCommand) AutocompleteArgs() complete.Predictor {
|
|
return nil
|
|
}
|
|
|
|
func (c *AuditListCommand) AutocompleteFlags() complete.Flags {
|
|
return c.Flags().Completions()
|
|
}
|
|
|
|
func (c *AuditListCommand) 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
|
|
}
|
|
|
|
audits, err := client.Sys().ListAudit()
|
|
if err != nil {
|
|
c.UI.Error(fmt.Sprintf("Error listing audits: %s", err))
|
|
return 2
|
|
}
|
|
|
|
if len(audits) == 0 {
|
|
c.UI.Output(fmt.Sprintf("No audit devices are enabled."))
|
|
return 0
|
|
}
|
|
|
|
switch Format(c.UI) {
|
|
case "table":
|
|
if c.flagDetailed {
|
|
c.UI.Output(tableOutput(c.detailedAudits(audits), nil))
|
|
return 0
|
|
}
|
|
c.UI.Output(tableOutput(c.simpleAudits(audits), nil))
|
|
return 0
|
|
default:
|
|
return OutputData(c.UI, audits)
|
|
}
|
|
}
|
|
|
|
func (c *AuditListCommand) simpleAudits(audits map[string]*api.Audit) []string {
|
|
paths := make([]string, 0, len(audits))
|
|
for path, _ := range audits {
|
|
paths = append(paths, path)
|
|
}
|
|
sort.Strings(paths)
|
|
|
|
columns := []string{"Path | Type | Description"}
|
|
for _, path := range paths {
|
|
audit := audits[path]
|
|
columns = append(columns, fmt.Sprintf("%s | %s | %s",
|
|
audit.Path,
|
|
audit.Type,
|
|
audit.Description,
|
|
))
|
|
}
|
|
|
|
return columns
|
|
}
|
|
|
|
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)
|
|
|
|
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)
|
|
}
|
|
|
|
replication := "replicated"
|
|
if audit.Local {
|
|
replication = "local"
|
|
}
|
|
|
|
columns = append(columns, fmt.Sprintf("%s | %s | %s | %s | %s",
|
|
audit.Path,
|
|
audit.Type,
|
|
audit.Description,
|
|
replication,
|
|
strings.Join(opts, " "),
|
|
))
|
|
}
|
|
|
|
return columns
|
|
}
|