Skip to content

Commit

Permalink
support listing at parent plugin catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
Becca Petrin committed Nov 7, 2018
1 parent 8d32f46 commit f6d2ee9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
17 changes: 17 additions & 0 deletions api/sys_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type ListPluginsInput struct {
type ListPluginsResponse struct {
// PluginsByType is the list of plugins by type.
PluginsByType map[consts.PluginType][]string `json:"types"`

// NamesDeprecated is the list of names of the plugins.
NamesDeprecated []string `json:"names"`
}

// ListPlugins lists all plugins in the catalog and returns their names as a
Expand Down Expand Up @@ -53,6 +56,20 @@ func (c *Sys) ListPlugins(i *ListPluginsInput) (*ListPluginsResponse, error) {
return nil, errors.New("data from server response is empty")
}

if resp.StatusCode == 405 {
// We received an Unsupported Operation response from Vault, indicating
// Vault of an older version that doesn't support the READ method yet.
var result struct {
Data struct {
Keys []string `json:"keys"`
} `json:"data"`
}
if err := resp.DecodeJSON(&result); err != nil {
return nil, err
}
return &ListPluginsResponse{NamesDeprecated: result.Data.Keys}, nil
}

result := &ListPluginsResponse{
PluginsByType: make(map[consts.PluginType][]string),
}
Expand Down
22 changes: 21 additions & 1 deletion vault/logical_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ func (b *SystemBackend) handleTidyLeases(ctx context.Context, req *logical.Reque
}

func (b *SystemBackend) handlePluginCatalogTypedList(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
pluginType, err := consts.ParsePluginType(d.Get("type").(string))
pluginTypeStr := d.Get("type").(string)
pluginType, err := consts.ParsePluginType(pluginTypeStr)
if err != nil {
return nil, err
}
Expand All @@ -268,6 +269,25 @@ func (b *SystemBackend) handlePluginCatalogTypedList(ctx context.Context, req *l
return logical.ListResponse(plugins), nil
}

func (b *SystemBackend) handlePluginsListDeprecated(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
var plugins []string
pluginsAdded := make(map[string]bool)
for _, pluginType := range consts.PluginTypes {
names, err := b.Core.pluginCatalog.List(ctx, pluginType)
if err != nil {
return nil, err
}
for _, name := range names {
if _, found := pluginsAdded[name]; !found {
plugins = append(plugins, name)
pluginsAdded[name] = true
}
}
}
sort.Strings(plugins)
return logical.ListResponse(plugins), nil
}

func (b *SystemBackend) handlePluginCatalogUntypedList(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
pluginsByType := make(map[string]interface{})
for _, pluginType := range consts.PluginTypes {
Expand Down
20 changes: 20 additions & 0 deletions vault/logical_system_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,26 @@ func (b *SystemBackend) sealPaths() []*framework.Path {

func (b *SystemBackend) pluginsCatalogPaths() []*framework.Path {
return []*framework.Path{
{
Pattern: "plugins/catalog/?$",

This comment has been minimized.

Copy link
@briankassouf

briankassouf Nov 7, 2018

Contributor

This pattern already exists below, i think we can just add the list operation there.

This comment has been minimized.

Copy link
@tyrannosaurus-becks

tyrannosaurus-becks Nov 7, 2018

Contributor

I ended up reverting this commit.


Fields: map[string]*framework.FieldSchema{
"type": &framework.FieldSchema{

This comment has been minimized.

Copy link
@briankassouf

briankassouf Nov 7, 2018

Contributor

I don't think we need the type field here

This comment has been minimized.

Copy link
@tyrannosaurus-becks

tyrannosaurus-becks Nov 7, 2018

Contributor

LOL you're totally right, I reverted it. Added this instead: #5717

Type: framework.TypeString,
Description: strings.TrimSpace(sysHelp["plugin-catalog_type"][0]),
},
},

Operations: map[logical.Operation]framework.OperationHandler{
logical.ListOperation: &framework.PathOperation{
Callback: b.handlePluginsListDeprecated,
Summary: "List the plugins in the catalog.",
},
},

HelpSynopsis: strings.TrimSpace(sysHelp["plugin-catalog"][0]),
HelpDescription: strings.TrimSpace(sysHelp["plugin-catalog"][1]),
},
{
Pattern: "plugins/catalog/(?P<type>auth|database|secret)/?$",

Expand Down

0 comments on commit f6d2ee9

Please sign in to comment.