2018-02-01 22:30:17 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2018-02-21 22:36:53 +00:00
|
|
|
"encoding/json"
|
2018-02-01 22:30:17 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-02-21 22:36:53 +00:00
|
|
|
"reflect"
|
2018-02-01 22:30:17 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-02-01 22:30:17 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
bplugin "github.com/hashicorp/vault/builtin/plugin"
|
|
|
|
"github.com/hashicorp/vault/helper/pluginutil"
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/plugin"
|
|
|
|
"github.com/hashicorp/vault/logical/plugin/mock"
|
2018-09-18 03:03:00 +00:00
|
|
|
"github.com/hashicorp/vault/physical"
|
2018-02-01 22:30:17 +00:00
|
|
|
"github.com/hashicorp/vault/physical/inmem"
|
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
func getPluginClusterAndCore(t testing.TB, logger log.Logger) (*vault.TestCluster, *vault.TestClusterCore) {
|
2018-09-18 03:03:00 +00:00
|
|
|
inm, err := inmem.NewTransactionalInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-04-03 00:46:59 +00:00
|
|
|
inmha, err := inmem.NewInmemHA(nil, logger)
|
2018-02-01 22:30:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
coreConfig := &vault.CoreConfig{
|
2018-09-18 03:03:00 +00:00
|
|
|
Physical: inm,
|
|
|
|
HAPhysical: inmha.(physical.HABackend),
|
2018-02-01 22:30:17 +00:00
|
|
|
LogicalBackends: map[string]logical.Factory{
|
|
|
|
"plugin": bplugin.Factory,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: Handler,
|
2018-04-03 00:46:59 +00:00
|
|
|
Logger: logger.Named("testclusteroptions"),
|
2018-02-01 22:30:17 +00:00
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
|
|
|
|
cores := cluster.Cores
|
|
|
|
core := cores[0]
|
|
|
|
|
|
|
|
os.Setenv(pluginutil.PluginCACertPEMEnv, cluster.CACertPEMFile)
|
|
|
|
|
|
|
|
vault.TestWaitActive(t, core.Core)
|
2018-09-20 17:50:29 +00:00
|
|
|
vault.TestAddTestPlugin(t, core.Core, "mock-plugin", "TestPlugin_PluginMain", []string{}, "")
|
2018-02-01 22:30:17 +00:00
|
|
|
|
|
|
|
// Mount the mock plugin
|
|
|
|
err = core.Client.Sys().Mount("mock", &api.MountInput{
|
|
|
|
Type: "plugin",
|
|
|
|
PluginName: "mock-plugin",
|
|
|
|
})
|
|
|
|
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}
|
|
|
|
|
|
|
|
apiClientMeta := &pluginutil.APIClientMeta{}
|
|
|
|
flags := apiClientMeta.FlagSet()
|
|
|
|
flags.Parse(args)
|
|
|
|
|
|
|
|
tlsConfig := apiClientMeta.GetTLSConfig()
|
|
|
|
tlsProviderFunc := pluginutil.VaultPluginTLSProvider(tlsConfig)
|
|
|
|
|
|
|
|
factoryFunc := mock.FactoryType(logical.TypeLogical)
|
|
|
|
|
|
|
|
err := plugin.Serve(&plugin.ServeOpts{
|
|
|
|
BackendFactoryFunc: factoryFunc,
|
|
|
|
TLSProviderFunc: tlsProviderFunc,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Fatal("Why are we here")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlugin_MockList(t *testing.T) {
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := log.New(&log.LoggerOptions{
|
2018-02-01 22:30:17 +00:00
|
|
|
Mutex: &sync.Mutex{},
|
2018-04-03 00:46:59 +00:00
|
|
|
})
|
2018-02-01 22:30:17 +00:00
|
|
|
cluster, core := getPluginClusterAndCore(t, logger)
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
_, err := core.Client.Logical().Write("mock/kv/foo", map[string]interface{}{
|
2018-02-21 22:36:53 +00:00
|
|
|
"value": "baz",
|
2018-02-01 22:30:17 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys, err := core.Client.Logical().List("mock/kv/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if keys.Data["keys"].([]interface{})[0].(string) != "foo" {
|
|
|
|
t.Fatal(keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = core.Client.Logical().Write("mock/kv/zoo", map[string]interface{}{
|
2018-02-21 22:36:53 +00:00
|
|
|
"value": "baz",
|
2018-02-01 22:30:17 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys, err = core.Client.Logical().List("mock/kv/")
|
|
|
|
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-03 00:46:59 +00:00
|
|
|
logger := log.New(&log.LoggerOptions{
|
2018-02-01 22:30:17 +00:00
|
|
|
Mutex: &sync.Mutex{},
|
2018-04-03 00:46:59 +00:00
|
|
|
})
|
2018-02-01 22:30:17 +00: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()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
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 22:36:53 +00:00
|
|
|
|
|
|
|
func TestPlugin_GetParams(t *testing.T) {
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := log.New(&log.LoggerOptions{
|
2018-02-21 22:36:53 +00:00
|
|
|
Mutex: &sync.Mutex{},
|
2018-04-03 00:46:59 +00:00
|
|
|
})
|
2018-02-21 22:36:53 +00:00
|
|
|
cluster, core := getPluginClusterAndCore(t, logger)
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
_, err := core.Client.Logical().Write("mock/kv/foo", map[string]interface{}{
|
|
|
|
"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)
|
|
|
|
}
|
|
|
|
}
|