2015-04-08 05:42:04 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2017-02-16 16:37:27 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2015-04-08 05:42:04 +00:00
|
|
|
"github.com/hashicorp/vault/helper/kv-builder"
|
2016-04-01 17:16:05 +00:00
|
|
|
"github.com/hashicorp/vault/meta"
|
2015-04-08 05:42:04 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AuditEnableCommand is a Command that mounts a new mount.
|
|
|
|
type AuditEnableCommand struct {
|
2016-04-01 17:16:05 +00:00
|
|
|
meta.Meta
|
2015-04-08 05:42:04 +00:00
|
|
|
|
|
|
|
// A test stdin that can be used for tests
|
|
|
|
testStdin io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuditEnableCommand) Run(args []string) int {
|
2016-03-14 21:15:07 +00:00
|
|
|
var desc, path string
|
2017-02-16 16:37:27 +00:00
|
|
|
var local bool
|
2016-04-01 17:16:05 +00:00
|
|
|
flags := c.Meta.FlagSet("audit-enable", meta.FlagSetDefault)
|
2015-04-08 05:42:04 +00:00
|
|
|
flags.StringVar(&desc, "description", "", "")
|
2016-03-14 21:15:07 +00:00
|
|
|
flags.StringVar(&path, "path", "", "")
|
2017-02-16 16:37:27 +00:00
|
|
|
flags.BoolVar(&local, "local", false, "")
|
2015-04-08 05:42:04 +00:00
|
|
|
flags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = flags.Args()
|
|
|
|
if len(args) < 1 {
|
|
|
|
flags.Usage()
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"\naudit-enable expects at least one argument: the type to enable"))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
auditType := args[0]
|
2016-03-14 21:15:07 +00:00
|
|
|
if path == "" {
|
|
|
|
path = auditType
|
2015-04-08 05:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the options
|
|
|
|
var stdin io.Reader = os.Stdin
|
|
|
|
if c.testStdin != nil {
|
|
|
|
stdin = c.testStdin
|
|
|
|
}
|
|
|
|
builder := &kvbuilder.Builder{Stdin: stdin}
|
|
|
|
if err := builder.Add(args[1:]...); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error parsing options: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var opts map[string]string
|
|
|
|
if err := mapstructure.WeakDecode(builder.Map(), &opts); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error parsing options: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-02-16 16:37:27 +00:00
|
|
|
err = client.Sys().EnableAuditWithOptions(path, &api.EnableAuditOptions{
|
2017-05-19 12:34:17 +00:00
|
|
|
Type: auditType,
|
2017-02-16 16:37:27 +00:00
|
|
|
Description: desc,
|
|
|
|
Options: opts,
|
|
|
|
Local: local,
|
|
|
|
})
|
2015-04-08 05:42:04 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error enabling audit backend: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(fmt.Sprintf(
|
2016-03-14 21:15:07 +00:00
|
|
|
"Successfully enabled audit backend '%s' with path '%s'!", auditType, path))
|
2015-04-08 05:42:04 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuditEnableCommand) Synopsis() string {
|
|
|
|
return "Enable an audit backend"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuditEnableCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: vault audit-enable [options] type [config...]
|
|
|
|
|
|
|
|
Enable an audit backend.
|
|
|
|
|
|
|
|
This command enables an audit backend of type "type". Additional
|
|
|
|
options for configuring the audit backend can be specified after the
|
|
|
|
type in the same format as the "vault write" command in key/value pairs.
|
2016-09-02 00:14:29 +00:00
|
|
|
|
|
|
|
For example, to configure the file audit backend to write audit logs at
|
|
|
|
the path /var/log/audit.log:
|
|
|
|
|
|
|
|
$ vault audit-enable file file_path=/var/log/audit.log
|
|
|
|
|
|
|
|
For information on available configuration options, please see the
|
|
|
|
documentation.
|
2015-04-08 05:42:04 +00:00
|
|
|
|
|
|
|
General Options:
|
2016-04-01 20:50:12 +00:00
|
|
|
` + meta.GeneralOptionsUsage() + `
|
2015-04-08 05:42:04 +00:00
|
|
|
Audit Enable Options:
|
|
|
|
|
|
|
|
-description=<desc> A human-friendly description for the backend. This
|
|
|
|
shows up only when querying the enabled backends.
|
|
|
|
|
2016-03-14 21:15:07 +00:00
|
|
|
-path=<path> Specify a unique path for this audit backend. This
|
2015-04-08 05:42:04 +00:00
|
|
|
is purely for referencing this audit backend. By
|
|
|
|
default this will be the backend type.
|
|
|
|
|
2017-02-16 16:37:27 +00:00
|
|
|
-local Mark the mount as a local mount. Local mounts
|
|
|
|
are not replicated nor (if a secondary)
|
|
|
|
removed by replication.
|
2015-04-08 05:42:04 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|