Run all builtins as plugins (#5536)

This commit is contained in:
Becca Petrin
2018-11-06 17:21:24 -08:00
committed by GitHub
parent f63bcdf106
commit fb89af7cfa
88 changed files with 1474 additions and 778 deletions

View File

@@ -6,6 +6,7 @@ import (
"strings"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/helper/consts"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -23,15 +24,20 @@ func (c *PluginListCommand) Synopsis() string {
func (c *PluginListCommand) Help() string {
helpText := `
Usage: vault plugin list [options]
Usage: vault plugin list [options] [TYPE]
Lists available plugins registered in the catalog. This does not list whether
plugins are in use, but rather just their availability.
plugins are in use, but rather just their availability. The last argument of
type takes "auth", "database", or "secret".
List all available plugins in the catalog:
$ vault plugin list
List all available database plugins in the catalog:
$ vault plugin list database
` + c.Flags().Help()
return strings.TrimSpace(helpText)
@@ -58,32 +64,60 @@ func (c *PluginListCommand) Run(args []string) int {
}
args = f.Args()
if len(args) > 0 {
c.UI.Error(fmt.Sprintf("Too many arguments (expected 0, got %d)", len(args)))
switch {
case len(args) > 1:
c.UI.Error(fmt.Sprintf("Too many arguments (expected 0 or 1, got %d)", len(args)))
return 1
}
pluginType := consts.PluginTypeUnknown
if len(args) > 0 {
pluginTypeStr := strings.TrimSpace(args[0])
if pluginTypeStr != "" {
var err error
pluginType, err = consts.ParsePluginType(pluginTypeStr)
if err != nil {
c.UI.Error(fmt.Sprintf("Error parsing type: %s", err))
return 2
}
}
}
client, err := c.Client()
if err != nil {
c.UI.Error(err.Error())
return 2
}
resp, err := client.Sys().ListPlugins(&api.ListPluginsInput{})
resp, err := client.Sys().ListPlugins(&api.ListPluginsInput{
Type: pluginType,
})
if err != nil {
c.UI.Error(fmt.Sprintf("Error listing available plugins: %s", err))
return 2
}
pluginNames := resp.Names
sort.Strings(pluginNames)
switch Format(c.UI) {
case "table":
list := append([]string{"Plugins"}, pluginNames...)
var flattenedNames []string
namesAdded := make(map[string]bool)
for _, names := range resp.PluginsByType {
for _, name := range names {
if ok := namesAdded[name]; !ok {
flattenedNames = append(flattenedNames, name)
namesAdded[name] = true
}
}
sort.Strings(flattenedNames)
}
list := append([]string{"Plugins"}, flattenedNames...)
c.UI.Output(tableOutput(list, nil))
return 0
default:
return OutputData(c.UI, pluginNames)
res := make(map[string]interface{})
for k, v := range resp.PluginsByType {
res[k.String()] = v
}
return OutputData(c.UI, res)
}
}