open-vault/command/audit_enable.go

157 lines
3.4 KiB
Go
Raw Normal View History

2015-04-08 05:42:04 +00:00
package command
import (
"fmt"
"io"
"os"
"strings"
"github.com/hashicorp/vault/api"
2017-09-05 03:59:03 +00:00
"github.com/mitchellh/cli"
"github.com/posener/complete"
2015-04-08 05:42:04 +00:00
)
var (
_ cli.Command = (*AuditEnableCommand)(nil)
_ cli.CommandAutocomplete = (*AuditEnableCommand)(nil)
)
2017-09-05 03:59:03 +00:00
2015-04-08 05:42:04 +00:00
type AuditEnableCommand struct {
2017-09-05 03:59:03 +00:00
*BaseCommand
2015-04-08 05:42:04 +00:00
2017-09-05 03:59:03 +00:00
flagDescription string
flagPath string
flagLocal bool
2015-04-08 05:42:04 +00:00
2017-09-05 03:59:03 +00:00
testStdin io.Reader // For tests
2015-04-08 05:42:04 +00:00
}
func (c *AuditEnableCommand) Synopsis() string {
2017-09-08 01:57:32 +00:00
return "Enables an audit device"
2015-04-08 05:42:04 +00:00
}
func (c *AuditEnableCommand) Help() string {
helpText := `
2017-09-08 01:57:32 +00:00
Usage: vault audit enable [options] TYPE [CONFIG K=V...]
2015-04-08 05:42:04 +00:00
2017-09-08 01:57:32 +00:00
Enables an audit device at a given path.
2015-04-08 05:42:04 +00:00
2017-09-08 01:57:32 +00:00
This command enables an audit device of TYPE. Additional options for
configuring the audit device can be specified after the type in the same
format as the "vault write" command in key/value pairs.
2017-09-08 01:57:32 +00:00
For example, to configure the file audit device to write audit logs at the
path "/var/log/audit.log":
2017-09-08 01:57:32 +00:00
$ vault audit enable file file_path=/var/log/audit.log
2015-04-08 05:42:04 +00:00
2017-09-05 03:59:03 +00:00
` + c.Flags().Help()
2015-04-08 05:42:04 +00:00
2017-09-05 03:59:03 +00:00
return strings.TrimSpace(helpText)
}
2015-04-08 05:42:04 +00:00
2017-09-05 03:59:03 +00:00
func (c *AuditEnableCommand) Flags() *FlagSets {
set := c.flagSet(FlagSetHTTP)
2015-04-08 05:42:04 +00:00
2017-09-05 03:59:03 +00:00
f := set.NewFlagSet("Command Options")
f.StringVar(&StringVar{
Name: "description",
Target: &c.flagDescription,
Default: "",
EnvVar: "",
Completion: complete.PredictAnything,
Usage: "Human-friendly description for the purpose of this audit " +
2017-09-08 01:57:32 +00:00
"device.",
2017-09-05 03:59:03 +00:00
})
f.StringVar(&StringVar{
Name: "path",
Target: &c.flagPath,
Default: "", // The default is complex, so we have to manually document
EnvVar: "",
Completion: complete.PredictAnything,
2017-09-08 01:57:32 +00:00
Usage: "Place where the audit device will be accessible. This must be " +
"unique across all audit devices. This defaults to the \"type\" of the " +
"audit device.",
2017-09-05 03:59:03 +00:00
})
f.BoolVar(&BoolVar{
Name: "local",
Target: &c.flagLocal,
Default: false,
EnvVar: "",
2017-09-08 01:57:32 +00:00
Usage: "Mark the audit device as a local-only device. Local devices " +
"are not replicated or removed by replication.",
2017-09-05 03:59:03 +00:00
})
return set
2015-04-08 05:42:04 +00:00
}
func (c *AuditEnableCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictSet(
"file",
"syslog",
"socket",
)
}
func (c *AuditEnableCommand) AutocompleteFlags() complete.Flags {
2017-09-05 03:59:03 +00:00
return c.Flags().Completions()
}
func (c *AuditEnableCommand) 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) < 1 {
c.UI.Error("Missing TYPE!")
return 1
}
// Grab the type
auditType := strings.TrimSpace(args[0])
auditPath := c.flagPath
if auditPath == "" {
auditPath = auditType
}
auditPath = ensureTrailingSlash(auditPath)
// Pull our fake stdin if needed
stdin := (io.Reader)(os.Stdin)
if c.testStdin != nil {
stdin = c.testStdin
}
options, err := parseArgsDataString(stdin, args[1:])
if err != nil {
c.UI.Error(fmt.Sprintf("Failed to parse K=V data: %s", err))
return 1
}
2017-09-05 03:59:03 +00:00
client, err := c.Client()
if err != nil {
c.UI.Error(err.Error())
return 2
}
if err := client.Sys().EnableAuditWithOptions(auditPath, &api.EnableAuditOptions{
Type: auditType,
Description: c.flagDescription,
Options: options,
Local: c.flagLocal,
}); err != nil {
2017-09-08 01:57:32 +00:00
c.UI.Error(fmt.Sprintf("Error enabling audit device: %s", err))
2017-09-05 03:59:03 +00:00
return 2
}
2017-09-08 01:57:32 +00:00
c.UI.Output(fmt.Sprintf("Success! Enabled the %s audit device at: %s", auditType, auditPath))
2017-09-05 03:59:03 +00:00
return 0
}