From 485b331d6affa00fe2890e2f13e2f16fefa3f250 Mon Sep 17 00:00:00 2001 From: Brian Kassouf Date: Tue, 4 Apr 2017 17:12:02 -0700 Subject: [PATCH] Add a cli command to run builtin plugins --- cli/commands.go | 6 ++++ command/plugin-exec.go | 71 +++++++++++++++++++++++++++++++++++++++++ vault/plugin_catalog.go | 3 +- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 command/plugin-exec.go diff --git a/cli/commands.go b/cli/commands.go index 13f7c8b25a..e7545ca906 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -331,5 +331,11 @@ func Commands(metaPtr *meta.Meta) map[string]cli.CommandFactory { Ui: metaPtr.Ui, }, nil }, + + "plugin-exec": func() (cli.Command, error) { + return &command.PluginExec{ + Meta: *metaPtr, + }, nil + }, } } diff --git a/command/plugin-exec.go b/command/plugin-exec.go new file mode 100644 index 0000000000..18dc3e1453 --- /dev/null +++ b/command/plugin-exec.go @@ -0,0 +1,71 @@ +package command + +import ( + "fmt" + "strings" + + "github.com/hashicorp/vault/meta" +) + +type PluginExec struct { + meta.Meta +} + +var builtinFactories = map[string]func() error{ +// "mysql-database-plugin": mysql.Factory, +// "postgres-database-plugin": postgres.Factory, +} + +func (c *PluginExec) Run(args []string) int { + flags := c.Meta.FlagSet("plugin-exec", meta.FlagSetDefault) + 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( + "\nplugin-exec expects one argument: the plugin to execute.")) + return 1 + } + + pluginName := args[0] + + factory, ok := builtinFactories[pluginName] + if !ok { + c.Ui.Error(fmt.Sprintf( + "No plugin with the name %s found", pluginName)) + return 1 + } + + err := factory() + if err != nil { + c.Ui.Error(fmt.Sprintf( + "Error running plugin: %s", err)) + return 1 + } + + return 0 +} + +func (c *PluginExec) Synopsis() string { + return "Force the Vault node to give up active duty" +} + +func (c *PluginExec) Help() string { + helpText := ` +Usage: vault step-down [options] + + Force the Vault node to step down from active duty. + + This causes the indicated node to give up active status. Note that while the + affected node will have a short delay before attempting to grab the lock + again, if no other node grabs the lock beforehand, it is possible for the + same node to re-grab the lock and become active again. + +General Options: +` + meta.GeneralOptionsUsage() + return strings.TrimSpace(helpText) +} diff --git a/vault/plugin_catalog.go b/vault/plugin_catalog.go index 88265a2452..eccac2bd1b 100644 --- a/vault/plugin_catalog.go +++ b/vault/plugin_catalog.go @@ -25,8 +25,7 @@ type PluginCatalog struct { vaultCommand string vaultSHA256 []byte - lock sync.RWMutex - builtin map[string]*pluginutil.PluginRunner + lock sync.RWMutex } func (c *Core) setupPluginCatalog() error {