2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-05-17 16:41:26 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2022-10-07 14:28:15 +00:00
|
|
|
"reflect"
|
2022-05-17 16:41:26 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-02-06 14:41:56 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/strutil"
|
2022-05-17 16:41:26 +00:00
|
|
|
)
|
|
|
|
|
2022-08-31 18:23:05 +00:00
|
|
|
func TestRegisterPlugin(t *testing.T) {
|
|
|
|
mockVaultServer := httptest.NewServer(http.HandlerFunc(mockVaultHandlerRegister))
|
|
|
|
defer mockVaultServer.Close()
|
|
|
|
|
|
|
|
cfg := DefaultConfig()
|
|
|
|
cfg.Address = mockVaultServer.URL
|
|
|
|
client, err := NewClient(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.Sys().RegisterPluginWithContext(context.Background(), &RegisterPluginInput{
|
|
|
|
Version: "v1.0.0",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 16:41:26 +00:00
|
|
|
func TestListPlugins(t *testing.T) {
|
2022-08-31 18:23:05 +00:00
|
|
|
mockVaultServer := httptest.NewServer(http.HandlerFunc(mockVaultHandlerList))
|
2022-05-17 16:41:26 +00:00
|
|
|
defer mockVaultServer.Close()
|
|
|
|
|
|
|
|
cfg := DefaultConfig()
|
|
|
|
cfg.Address = mockVaultServer.URL
|
|
|
|
client, err := NewClient(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-09-29 17:22:33 +00:00
|
|
|
for name, tc := range map[string]struct {
|
|
|
|
input ListPluginsInput
|
2023-02-06 14:41:56 +00:00
|
|
|
expectedPlugins map[PluginType][]string
|
2022-09-29 17:22:33 +00:00
|
|
|
}{
|
|
|
|
"no type specified": {
|
|
|
|
input: ListPluginsInput{},
|
2023-02-06 14:41:56 +00:00
|
|
|
expectedPlugins: map[PluginType][]string{
|
|
|
|
PluginTypeCredential: {"alicloud"},
|
|
|
|
PluginTypeDatabase: {"cassandra-database-plugin"},
|
|
|
|
PluginTypeSecrets: {"ad", "alicloud"},
|
2022-09-29 17:22:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"only auth plugins": {
|
2023-02-06 14:41:56 +00:00
|
|
|
input: ListPluginsInput{Type: PluginTypeCredential},
|
|
|
|
expectedPlugins: map[PluginType][]string{
|
|
|
|
PluginTypeCredential: {"alicloud"},
|
2022-09-29 17:22:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"only database plugins": {
|
2023-02-06 14:41:56 +00:00
|
|
|
input: ListPluginsInput{Type: PluginTypeDatabase},
|
|
|
|
expectedPlugins: map[PluginType][]string{
|
|
|
|
PluginTypeDatabase: {"cassandra-database-plugin"},
|
2022-09-29 17:22:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"only secret plugins": {
|
2023-02-06 14:41:56 +00:00
|
|
|
input: ListPluginsInput{Type: PluginTypeSecrets},
|
|
|
|
expectedPlugins: map[PluginType][]string{
|
|
|
|
PluginTypeSecrets: {"ad", "alicloud"},
|
2022-09-29 17:22:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
resp, err := client.Sys().ListPluginsWithContext(context.Background(), &tc.input)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-05-17 16:41:26 +00:00
|
|
|
|
2022-09-29 17:22:33 +00:00
|
|
|
for pluginType, expected := range tc.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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, expectedPlugin := range expected {
|
|
|
|
found := false
|
|
|
|
for _, plugin := range resp.Details {
|
|
|
|
if plugin.Type == pluginType.String() && plugin.Name == expectedPlugin {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Errorf("Expected to find %s plugin %s but not found in details: %#v", pluginType.String(), expectedPlugin, resp.Details)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-17 16:41:26 +00:00
|
|
|
|
2022-09-29 17:22:33 +00:00
|
|
|
for _, actual := range resp.Details {
|
2023-02-06 14:41:56 +00:00
|
|
|
pluginType, err := ParsePluginType(actual.Type)
|
2022-09-29 17:22:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strutil.StrListContains(tc.expectedPlugins[pluginType], actual.Name) {
|
|
|
|
t.Errorf("Did not expect to find %s in details", actual.Name)
|
|
|
|
}
|
2022-05-17 16:41:26 +00:00
|
|
|
}
|
2022-09-29 17:22:33 +00:00
|
|
|
})
|
2022-05-17 16:41:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 14:28:15 +00:00
|
|
|
func TestGetPlugin(t *testing.T) {
|
|
|
|
for name, tc := range map[string]struct {
|
|
|
|
version string
|
|
|
|
body string
|
|
|
|
expected GetPluginResponse
|
|
|
|
}{
|
|
|
|
"builtin": {
|
|
|
|
body: getResponse,
|
|
|
|
expected: GetPluginResponse{
|
|
|
|
Args: nil,
|
|
|
|
Builtin: true,
|
|
|
|
Command: "",
|
|
|
|
Name: "azure",
|
|
|
|
SHA256: "",
|
|
|
|
DeprecationStatus: "supported",
|
|
|
|
Version: "v0.14.0+builtin",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"external": {
|
|
|
|
version: "v1.0.0",
|
|
|
|
body: getResponseExternal,
|
|
|
|
expected: GetPluginResponse{
|
|
|
|
Args: []string{},
|
|
|
|
Builtin: false,
|
|
|
|
Command: "azure-plugin",
|
|
|
|
Name: "azure",
|
|
|
|
SHA256: "8ba442dba253803685b05e35ad29dcdebc48dec16774614aa7a4ebe53c1e90e1",
|
|
|
|
DeprecationStatus: "",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"old server": {
|
|
|
|
body: getResponseOldServerVersion,
|
|
|
|
expected: GetPluginResponse{
|
|
|
|
Args: nil,
|
|
|
|
Builtin: true,
|
|
|
|
Command: "",
|
|
|
|
Name: "azure",
|
|
|
|
SHA256: "",
|
|
|
|
DeprecationStatus: "",
|
|
|
|
Version: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
mockVaultServer := httptest.NewServer(http.HandlerFunc(mockVaultHandlerInfo(tc.body)))
|
|
|
|
defer mockVaultServer.Close()
|
|
|
|
|
|
|
|
cfg := DefaultConfig()
|
|
|
|
cfg.Address = mockVaultServer.URL
|
|
|
|
client, err := NewClient(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
input := GetPluginInput{
|
|
|
|
Name: "azure",
|
2023-02-06 14:41:56 +00:00
|
|
|
Type: PluginTypeSecrets,
|
2022-10-07 14:28:15 +00:00
|
|
|
}
|
|
|
|
if tc.version != "" {
|
|
|
|
input.Version = tc.version
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := client.Sys().GetPluginWithContext(context.Background(), &input)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tc.expected, *info) {
|
|
|
|
t.Errorf("expected: %#v\ngot: %#v", tc.expected, info)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mockVaultHandlerInfo(body string) func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
_, _ = w.Write([]byte(body))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getResponse = `{
|
|
|
|
"request_id": "e93d3f93-8e4f-8443-a803-f1c97c495241",
|
|
|
|
"lease_id": "",
|
|
|
|
"renewable": false,
|
|
|
|
"lease_duration": 0,
|
|
|
|
"data": {
|
|
|
|
"args": null,
|
|
|
|
"builtin": true,
|
|
|
|
"command": "",
|
|
|
|
"deprecation_status": "supported",
|
|
|
|
"name": "azure",
|
|
|
|
"sha256": "",
|
|
|
|
"version": "v0.14.0+builtin"
|
|
|
|
},
|
|
|
|
"wrap_info": null,
|
|
|
|
"warnings": null,
|
|
|
|
"auth": null
|
|
|
|
}`
|
|
|
|
|
|
|
|
const getResponseExternal = `{
|
|
|
|
"request_id": "e93d3f93-8e4f-8443-a803-f1c97c495241",
|
|
|
|
"lease_id": "",
|
|
|
|
"renewable": false,
|
|
|
|
"lease_duration": 0,
|
|
|
|
"data": {
|
|
|
|
"args": [],
|
|
|
|
"builtin": false,
|
|
|
|
"command": "azure-plugin",
|
|
|
|
"name": "azure",
|
|
|
|
"sha256": "8ba442dba253803685b05e35ad29dcdebc48dec16774614aa7a4ebe53c1e90e1",
|
|
|
|
"version": "v1.0.0"
|
|
|
|
},
|
|
|
|
"wrap_info": null,
|
|
|
|
"warnings": null,
|
|
|
|
"auth": null
|
|
|
|
}`
|
|
|
|
|
|
|
|
const getResponseOldServerVersion = `{
|
|
|
|
"request_id": "e93d3f93-8e4f-8443-a803-f1c97c495241",
|
|
|
|
"lease_id": "",
|
|
|
|
"renewable": false,
|
|
|
|
"lease_duration": 0,
|
|
|
|
"data": {
|
|
|
|
"args": null,
|
|
|
|
"builtin": true,
|
|
|
|
"command": "",
|
|
|
|
"name": "azure",
|
|
|
|
"sha256": ""
|
|
|
|
},
|
|
|
|
"wrap_info": null,
|
|
|
|
"warnings": null,
|
|
|
|
"auth": null
|
|
|
|
}`
|
|
|
|
|
2022-08-31 18:23:05 +00:00
|
|
|
func mockVaultHandlerList(w http.ResponseWriter, _ *http.Request) {
|
2022-05-17 16:41:26 +00:00
|
|
|
_, _ = 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
|
|
|
|
}
|
2022-09-29 17:22:33 +00:00
|
|
|
],
|
|
|
|
"detailed": [
|
|
|
|
{
|
|
|
|
"type": "auth",
|
|
|
|
"name": "alicloud",
|
|
|
|
"version": "v0.13.0+builtin",
|
|
|
|
"builtin": true,
|
|
|
|
"deprecation_status": "supported"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "database",
|
|
|
|
"name": "cassandra-database-plugin",
|
|
|
|
"version": "v1.13.0+builtin.vault",
|
|
|
|
"builtin": true,
|
|
|
|
"deprecation_status": "supported"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "secret",
|
|
|
|
"name": "ad",
|
|
|
|
"version": "v0.14.0+builtin",
|
|
|
|
"builtin": true,
|
|
|
|
"deprecation_status": "supported"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "secret",
|
|
|
|
"name": "alicloud",
|
|
|
|
"version": "v0.13.0+builtin",
|
|
|
|
"builtin": true,
|
|
|
|
"deprecation_status": "supported"
|
|
|
|
}
|
2022-05-17 16:41:26 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
"wrap_info": null,
|
|
|
|
"warnings": null,
|
|
|
|
"auth": null
|
|
|
|
}`
|
2022-08-31 18:23:05 +00:00
|
|
|
|
|
|
|
func mockVaultHandlerRegister(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
_, _ = w.Write([]byte(registerResponse))
|
|
|
|
}
|
|
|
|
|
|
|
|
const registerResponse = `{}`
|