api: make ListPlugins parse only known plugin types (#15434)
This commit is contained in:
parent
d3629ab49d
commit
ab0b0c96ca
|
@ -100,23 +100,24 @@ func (c *Sys) ListPluginsWithContext(ctx context.Context, i *ListPluginsInput) (
|
||||||
PluginsByType: make(map[consts.PluginType][]string),
|
PluginsByType: make(map[consts.PluginType][]string),
|
||||||
}
|
}
|
||||||
if i.Type == consts.PluginTypeUnknown {
|
if i.Type == consts.PluginTypeUnknown {
|
||||||
for pluginTypeStr, pluginsRaw := range secret.Data {
|
for _, pluginType := range consts.PluginTypes {
|
||||||
pluginType, err := consts.ParsePluginType(pluginTypeStr)
|
pluginsRaw, ok := secret.Data[pluginType.String()]
|
||||||
if err != nil {
|
if !ok {
|
||||||
return nil, err
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginsIfc, ok := pluginsRaw.([]interface{})
|
pluginsIfc, ok := pluginsRaw.([]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unable to parse plugins for %q type", pluginTypeStr)
|
return nil, fmt.Errorf("unable to parse plugins for %q type", pluginType.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
plugins := make([]string, len(pluginsIfc))
|
plugins := make([]string, 0, len(pluginsIfc))
|
||||||
for i, nameIfc := range pluginsIfc {
|
for _, nameIfc := range pluginsIfc {
|
||||||
name, ok := nameIfc.(string)
|
name, ok := nameIfc.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
plugins[i] = name
|
plugins = append(plugins, name)
|
||||||
}
|
}
|
||||||
result.PluginsByType[pluginType] = plugins
|
result.PluginsByType[pluginType] = plugins
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestListPlugins(t *testing.T) {
|
||||||
|
mockVaultServer := httptest.NewServer(http.HandlerFunc(mockVaultHandler))
|
||||||
|
defer mockVaultServer.Close()
|
||||||
|
|
||||||
|
cfg := DefaultConfig()
|
||||||
|
cfg.Address = mockVaultServer.URL
|
||||||
|
client, err := NewClient(cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Sys().ListPluginsWithContext(context.Background(), &ListPluginsInput{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedPlugins := map[consts.PluginType][]string{
|
||||||
|
consts.PluginTypeCredential: {"alicloud"},
|
||||||
|
consts.PluginTypeDatabase: {"cassandra-database-plugin"},
|
||||||
|
consts.PluginTypeSecrets: {"ad", "alicloud"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for pluginType, expected := range expectedPlugins {
|
||||||
|
actualPlugins := resp.PluginsByType[pluginType]
|
||||||
|
if len(expected) != len(actualPlugins) {
|
||||||
|
t.Fatal("Wrong number of plugins", expected, actualPlugins)
|
||||||
|
}
|
||||||
|
for i := range actualPlugins {
|
||||||
|
if expected[i] != actualPlugins[i] {
|
||||||
|
t.Fatalf("Expected %q but got %q", expected[i], actualPlugins[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mockVaultHandler(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
_, _ = w.Write([]byte(listUntypedResponse))
|
||||||
|
}
|
||||||
|
|
||||||
|
const listUntypedResponse = `{
|
||||||
|
"request_id": "82601a91-cd7a-718f-feca-f573449cc1bb",
|
||||||
|
"lease_id": "",
|
||||||
|
"renewable": false,
|
||||||
|
"lease_duration": 0,
|
||||||
|
"data": {
|
||||||
|
"auth": [
|
||||||
|
"alicloud"
|
||||||
|
],
|
||||||
|
"database": [
|
||||||
|
"cassandra-database-plugin"
|
||||||
|
],
|
||||||
|
"secret": [
|
||||||
|
"ad",
|
||||||
|
"alicloud"
|
||||||
|
],
|
||||||
|
"some_other_unexpected_key": [
|
||||||
|
{
|
||||||
|
"objectKey": "objectValue"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"arbitraryData": 7
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"wrap_info": null,
|
||||||
|
"warnings": null,
|
||||||
|
"auth": null
|
||||||
|
}`
|
|
@ -0,0 +1,3 @@
|
||||||
|
```release-note:improvement
|
||||||
|
api: make ListPlugins parse only known plugin types
|
||||||
|
```
|
Loading…
Reference in New Issue