2023-03-15 12:00:52 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-10 21:14:03 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-15 12:00:52 -04:00
|
|
|
|
2018-02-01 17:30:17 -05:00
|
|
|
package http
|
|
|
|
|
|
|
|
|
|
import (
|
2018-02-21 17:36:53 -05:00
|
|
|
"encoding/json"
|
2024-01-08 07:21:13 -05:00
|
|
|
"fmt"
|
2024-07-30 09:18:24 -04:00
|
|
|
"io"
|
2018-02-01 17:30:17 -05:00
|
|
|
"os"
|
2018-02-21 17:36:53 -05:00
|
|
|
"reflect"
|
2018-02-01 17:30:17 -05:00
|
|
|
"sync"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2018-04-02 20:46:59 -04:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-02-01 17:30:17 -05:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
|
bplugin "github.com/hashicorp/vault/builtin/plugin"
|
2023-12-07 07:36:17 -05:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/corehelpers"
|
2019-04-12 17:54:35 -04:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/pluginutil"
|
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
|
|
|
|
"github.com/hashicorp/vault/sdk/physical/inmem"
|
2019-04-13 03:44:06 -04:00
|
|
|
"github.com/hashicorp/vault/sdk/plugin"
|
|
|
|
|
"github.com/hashicorp/vault/sdk/plugin/mock"
|
2018-02-01 17:30:17 -05:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
|
)
|
|
|
|
|
|
2023-12-07 07:36:17 -05:00
|
|
|
func getPluginClusterAndCore(t *testing.T, logger log.Logger) (*vault.TestCluster, *vault.TestClusterCore) {
|
|
|
|
|
t.Helper()
|
2018-09-17 23:03:00 -04:00
|
|
|
inm, err := inmem.NewTransactionalInmem(nil, logger)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2018-04-02 20:46:59 -04:00
|
|
|
inmha, err := inmem.NewInmemHA(nil, logger)
|
2018-02-01 17:30:17 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-07 07:36:17 -05:00
|
|
|
pluginDir := corehelpers.MakeTestPluginDir(t)
|
2018-02-01 17:30:17 -05:00
|
|
|
coreConfig := &vault.CoreConfig{
|
2018-09-17 23:03:00 -04:00
|
|
|
Physical: inm,
|
|
|
|
|
HAPhysical: inmha.(physical.HABackend),
|
2018-02-01 17:30:17 -05:00
|
|
|
LogicalBackends: map[string]logical.Factory{
|
|
|
|
|
"plugin": bplugin.Factory,
|
|
|
|
|
},
|
2023-12-07 07:36:17 -05:00
|
|
|
PluginDirectory: pluginDir,
|
2018-02-01 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
|
2024-07-04 11:09:41 -04:00
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
2018-02-01 17:30:17 -05:00
|
|
|
HandlerFunc: Handler,
|
|
|
|
|
})
|
|
|
|
|
cluster.Start()
|
|
|
|
|
|
|
|
|
|
cores := cluster.Cores
|
|
|
|
|
core := cores[0]
|
|
|
|
|
|
2024-07-04 11:09:41 -04:00
|
|
|
vault.TestWaitActive(t, core.Core)
|
|
|
|
|
vault.TestAddTestPlugin(t, core.Core, "mock-plugin", consts.PluginTypeSecrets, "", "TestPlugin_PluginMain",
|
2024-01-08 07:21:13 -05:00
|
|
|
[]string{fmt.Sprintf("%s=%s", pluginutil.PluginCACertPEMEnv, cluster.CACertPEMFile)})
|
2018-02-01 17:30:17 -05:00
|
|
|
|
|
|
|
|
// Mount the mock plugin
|
2022-04-07 15:12:58 -04:00
|
|
|
err = core.Client.Sys().Mount("mock", &api.MountInput{
|
2018-11-06 20:21:24 -05:00
|
|
|
Type: "mock-plugin",
|
2018-02-01 17:30:17 -05:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cluster, core
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPlugin_PluginMain(t *testing.T) {
|
|
|
|
|
if os.Getenv(pluginutil.PluginVaultVersionEnv) == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
caPEM := os.Getenv(pluginutil.PluginCACertPEMEnv)
|
|
|
|
|
if caPEM == "" {
|
|
|
|
|
t.Fatal("CA cert not passed in")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args := []string{"--ca-cert=" + caPEM}
|
|
|
|
|
|
2019-04-15 14:10:07 -04:00
|
|
|
apiClientMeta := &api.PluginAPIClientMeta{}
|
2018-02-01 17:30:17 -05:00
|
|
|
flags := apiClientMeta.FlagSet()
|
|
|
|
|
flags.Parse(args)
|
|
|
|
|
|
|
|
|
|
factoryFunc := mock.FactoryType(logical.TypeLogical)
|
|
|
|
|
|
|
|
|
|
err := plugin.Serve(&plugin.ServeOpts{
|
|
|
|
|
BackendFactoryFunc: factoryFunc,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
t.Fatal("Why are we here")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPlugin_MockList(t *testing.T) {
|
2018-04-02 20:46:59 -04:00
|
|
|
logger := log.New(&log.LoggerOptions{
|
2018-02-01 17:30:17 -05:00
|
|
|
Mutex: &sync.Mutex{},
|
2018-04-02 20:46:59 -04:00
|
|
|
})
|
2018-02-01 17:30:17 -05:00
|
|
|
cluster, core := getPluginClusterAndCore(t, logger)
|
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
2022-04-07 15:12:58 -04:00
|
|
|
_, err := core.Client.Logical().Write("mock/kv/foo", map[string]interface{}{
|
2018-02-21 17:36:53 -05:00
|
|
|
"value": "baz",
|
2018-02-01 17:30:17 -05:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 15:12:58 -04:00
|
|
|
keys, err := core.Client.Logical().List("mock/kv/")
|
2018-02-01 17:30:17 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if keys.Data["keys"].([]interface{})[0].(string) != "foo" {
|
|
|
|
|
t.Fatal(keys)
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 15:12:58 -04:00
|
|
|
_, err = core.Client.Logical().Write("mock/kv/zoo", map[string]interface{}{
|
2018-02-21 17:36:53 -05:00
|
|
|
"value": "baz",
|
2018-02-01 17:30:17 -05:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 15:12:58 -04:00
|
|
|
keys, err = core.Client.Logical().List("mock/kv/")
|
2018-02-01 17:30:17 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if keys.Data["keys"].([]interface{})[0].(string) != "foo" || keys.Data["keys"].([]interface{})[1].(string) != "zoo" {
|
|
|
|
|
t.Fatal(keys)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPlugin_MockRawResponse(t *testing.T) {
|
2018-04-02 20:46:59 -04:00
|
|
|
logger := log.New(&log.LoggerOptions{
|
2018-02-01 17:30:17 -05:00
|
|
|
Mutex: &sync.Mutex{},
|
2018-04-02 20:46:59 -04:00
|
|
|
})
|
2018-02-01 17:30:17 -05:00
|
|
|
cluster, core := getPluginClusterAndCore(t, logger)
|
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
|
|
resp, err := core.Client.RawRequest(core.Client.NewRequest("GET", "/v1/mock/raw"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
2024-07-30 09:18:24 -04:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2018-02-01 17:30:17 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if string(body[:]) != "Response" {
|
|
|
|
|
t.Fatal("bad body")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
|
t.Fatal("bad status")
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-21 17:36:53 -05:00
|
|
|
|
|
|
|
|
func TestPlugin_GetParams(t *testing.T) {
|
2018-04-02 20:46:59 -04:00
|
|
|
logger := log.New(&log.LoggerOptions{
|
2018-02-21 17:36:53 -05:00
|
|
|
Mutex: &sync.Mutex{},
|
2018-04-02 20:46:59 -04:00
|
|
|
})
|
2018-02-21 17:36:53 -05:00
|
|
|
cluster, core := getPluginClusterAndCore(t, logger)
|
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
2022-04-07 15:12:58 -04:00
|
|
|
_, err := core.Client.Logical().Write("mock/kv/foo", map[string]interface{}{
|
2018-02-21 17:36:53 -05:00
|
|
|
"value": "baz",
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r := core.Client.NewRequest("GET", "/v1/mock/kv/foo")
|
|
|
|
|
r.Params.Add("version", "12")
|
|
|
|
|
resp, err := core.Client.RawRequest(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
secret, err := api.ParseSecret(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
|
"value": "baz",
|
|
|
|
|
"version": json.Number("12"),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(secret.Data, expected) {
|
|
|
|
|
t.Fatal(secret.Data)
|
|
|
|
|
}
|
|
|
|
|
}
|