2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-01-18 21:49:20 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2018-01-18 21:49:20 +00:00
|
|
|
"reflect"
|
2020-05-27 18:28:00 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2018-01-18 21:49:20 +00:00
|
|
|
|
2022-12-02 18:12:05 +00:00
|
|
|
"github.com/hashicorp/go-plugin"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
"github.com/hashicorp/vault/sdk/plugin/pb"
|
2020-05-27 18:28:00 +00:00
|
|
|
"google.golang.org/grpc"
|
2022-08-30 02:42:26 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2018-01-18 21:49:20 +00:00
|
|
|
)
|
|
|
|
|
2022-12-02 18:12:05 +00:00
|
|
|
func TestSystem_GRPC_ReturnsErrIfSystemViewNil(t *testing.T) {
|
|
|
|
_, err := new(gRPCSystemViewServer).ReplicationState(context.Background(), nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error when using server with no impl")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 21:49:20 +00:00
|
|
|
func TestSystem_GRPC_GRPC_impl(t *testing.T) {
|
|
|
|
var _ logical.SystemView = new(gRPCSystemViewClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystem_GRPC_defaultLeaseTTL(t *testing.T) {
|
|
|
|
sys := logical.TestSystemView()
|
|
|
|
client, _ := plugin.TestGRPCConn(t, func(s *grpc.Server) {
|
|
|
|
pb.RegisterSystemViewServer(s, &gRPCSystemViewServer{
|
|
|
|
impl: sys,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
defer client.Close()
|
|
|
|
testSystemView := newGRPCSystemView(client)
|
|
|
|
|
|
|
|
expected := sys.DefaultLeaseTTL()
|
|
|
|
actual := testSystemView.DefaultLeaseTTL()
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Fatalf("expected: %v, got: %v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystem_GRPC_maxLeaseTTL(t *testing.T) {
|
|
|
|
sys := logical.TestSystemView()
|
|
|
|
client, _ := plugin.TestGRPCConn(t, func(s *grpc.Server) {
|
|
|
|
pb.RegisterSystemViewServer(s, &gRPCSystemViewServer{
|
|
|
|
impl: sys,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
defer client.Close()
|
|
|
|
testSystemView := newGRPCSystemView(client)
|
|
|
|
|
|
|
|
expected := sys.MaxLeaseTTL()
|
|
|
|
actual := testSystemView.MaxLeaseTTL()
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Fatalf("expected: %v, got: %v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystem_GRPC_tainted(t *testing.T) {
|
|
|
|
sys := logical.TestSystemView()
|
|
|
|
sys.TaintedVal = true
|
|
|
|
client, _ := plugin.TestGRPCConn(t, func(s *grpc.Server) {
|
|
|
|
pb.RegisterSystemViewServer(s, &gRPCSystemViewServer{
|
|
|
|
impl: sys,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
defer client.Close()
|
|
|
|
testSystemView := newGRPCSystemView(client)
|
|
|
|
|
|
|
|
expected := sys.Tainted()
|
|
|
|
actual := testSystemView.Tainted()
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Fatalf("expected: %v, got: %v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystem_GRPC_cachingDisabled(t *testing.T) {
|
|
|
|
sys := logical.TestSystemView()
|
|
|
|
sys.CachingDisabledVal = true
|
|
|
|
client, _ := plugin.TestGRPCConn(t, func(s *grpc.Server) {
|
|
|
|
pb.RegisterSystemViewServer(s, &gRPCSystemViewServer{
|
|
|
|
impl: sys,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
defer client.Close()
|
|
|
|
testSystemView := newGRPCSystemView(client)
|
|
|
|
|
|
|
|
expected := sys.CachingDisabled()
|
|
|
|
actual := testSystemView.CachingDisabled()
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Fatalf("expected: %v, got: %v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystem_GRPC_replicationState(t *testing.T) {
|
|
|
|
sys := logical.TestSystemView()
|
|
|
|
sys.ReplicationStateVal = consts.ReplicationPerformancePrimary
|
|
|
|
client, _ := plugin.TestGRPCConn(t, func(s *grpc.Server) {
|
|
|
|
pb.RegisterSystemViewServer(s, &gRPCSystemViewServer{
|
|
|
|
impl: sys,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
defer client.Close()
|
|
|
|
testSystemView := newGRPCSystemView(client)
|
|
|
|
|
|
|
|
expected := sys.ReplicationState()
|
|
|
|
actual := testSystemView.ReplicationState()
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Fatalf("expected: %v, got: %v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystem_GRPC_lookupPlugin(t *testing.T) {
|
|
|
|
sys := logical.TestSystemView()
|
|
|
|
client, _ := plugin.TestGRPCConn(t, func(s *grpc.Server) {
|
|
|
|
pb.RegisterSystemViewServer(s, &gRPCSystemViewServer{
|
|
|
|
impl: sys,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
testSystemView := newGRPCSystemView(client)
|
|
|
|
|
2018-11-07 01:21:24 +00:00
|
|
|
if _, err := testSystemView.LookupPlugin(context.Background(), "foo", consts.PluginTypeDatabase); err == nil {
|
2018-01-18 21:49:20 +00:00
|
|
|
t.Fatal("LookPlugin(): expected error on due to unsupported call from plugin")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystem_GRPC_mlockEnabled(t *testing.T) {
|
|
|
|
sys := logical.TestSystemView()
|
|
|
|
sys.EnableMlock = true
|
|
|
|
client, _ := plugin.TestGRPCConn(t, func(s *grpc.Server) {
|
|
|
|
pb.RegisterSystemViewServer(s, &gRPCSystemViewServer{
|
|
|
|
impl: sys,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
testSystemView := newGRPCSystemView(client)
|
|
|
|
|
|
|
|
expected := sys.MlockEnabled()
|
|
|
|
actual := testSystemView.MlockEnabled()
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Fatalf("expected: %v, got: %v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-06-04 00:48:12 +00:00
|
|
|
|
|
|
|
func TestSystem_GRPC_entityInfo(t *testing.T) {
|
|
|
|
sys := logical.TestSystemView()
|
|
|
|
sys.EntityVal = &logical.Entity{
|
|
|
|
ID: "id",
|
|
|
|
Name: "name",
|
2018-07-23 16:45:06 +00:00
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
Aliases: []*logical.Alias{
|
2021-04-08 16:43:39 +00:00
|
|
|
{
|
2018-07-23 16:45:06 +00:00
|
|
|
MountType: "logical",
|
|
|
|
MountAccessor: "accessor",
|
|
|
|
Name: "name",
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"zip": "zap",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-28 21:31:50 +00:00
|
|
|
Disabled: true,
|
2018-06-04 00:48:12 +00:00
|
|
|
}
|
|
|
|
client, _ := plugin.TestGRPCConn(t, func(s *grpc.Server) {
|
|
|
|
pb.RegisterSystemViewServer(s, &gRPCSystemViewServer{
|
|
|
|
impl: sys,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
defer client.Close()
|
|
|
|
testSystemView := newGRPCSystemView(client)
|
|
|
|
|
|
|
|
actual, err := testSystemView.EntityInfo("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-07-23 16:45:06 +00:00
|
|
|
if !proto.Equal(sys.EntityVal, actual) {
|
2018-06-04 00:48:12 +00:00
|
|
|
t.Fatalf("expected: %v, got: %v", sys.EntityVal, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 16:32:17 +00:00
|
|
|
|
2020-01-06 18:16:52 +00:00
|
|
|
func TestSystem_GRPC_groupsForEntity(t *testing.T) {
|
|
|
|
sys := logical.TestSystemView()
|
|
|
|
sys.GroupsVal = []*logical.Group{
|
|
|
|
{
|
|
|
|
ID: "group1-id",
|
|
|
|
Name: "group1",
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"group-metadata": "metadata-value",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
client, _ := plugin.TestGRPCConn(t, func(s *grpc.Server) {
|
|
|
|
pb.RegisterSystemViewServer(s, &gRPCSystemViewServer{
|
|
|
|
impl: sys,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
defer client.Close()
|
|
|
|
testSystemView := newGRPCSystemView(client)
|
|
|
|
|
|
|
|
actual, err := testSystemView.GroupsForEntity("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !proto.Equal(sys.GroupsVal[0], actual[0]) {
|
|
|
|
t.Fatalf("expected: %v, got: %v", sys.GroupsVal, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 16:32:17 +00:00
|
|
|
func TestSystem_GRPC_pluginEnv(t *testing.T) {
|
|
|
|
sys := logical.TestSystemView()
|
|
|
|
sys.PluginEnvironment = &logical.PluginEnvironment{
|
2022-04-05 05:31:01 +00:00
|
|
|
VaultVersion: "0.10.42",
|
|
|
|
VaultVersionPrerelease: "dev",
|
2022-05-06 13:09:42 +00:00
|
|
|
VaultVersionMetadata: "ent",
|
2018-08-03 16:32:17 +00:00
|
|
|
}
|
|
|
|
client, _ := plugin.TestGRPCConn(t, func(s *grpc.Server) {
|
|
|
|
pb.RegisterSystemViewServer(s, &gRPCSystemViewServer{
|
|
|
|
impl: sys,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
testSystemView := newGRPCSystemView(client)
|
|
|
|
|
|
|
|
expected, err := sys.PluginEnv(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := testSystemView.PluginEnv(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !proto.Equal(expected, actual) {
|
|
|
|
t.Fatalf("expected: %v, got: %v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2020-05-27 18:28:00 +00:00
|
|
|
|
|
|
|
func TestSystem_GRPC_GeneratePasswordFromPolicy(t *testing.T) {
|
|
|
|
policyName := "testpolicy"
|
2020-06-17 20:24:38 +00:00
|
|
|
expectedPassword := "87354qtnjgrehiogd9u0t43"
|
|
|
|
passGen := func() (password string, err error) {
|
|
|
|
return expectedPassword, nil
|
2020-05-27 18:28:00 +00:00
|
|
|
}
|
|
|
|
sys := &logical.StaticSystemView{
|
2020-06-17 20:24:38 +00:00
|
|
|
PasswordPolicies: map[string]logical.PasswordGenerator{
|
|
|
|
policyName: passGen,
|
2020-05-27 18:28:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client, server := plugin.TestGRPCConn(t, func(s *grpc.Server) {
|
|
|
|
pb.RegisterSystemViewServer(s, &gRPCSystemViewServer{
|
|
|
|
impl: sys,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
defer server.Stop()
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
testSystemView := newGRPCSystemView(client)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
password, err := testSystemView.GeneratePasswordFromPolicy(ctx, policyName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("no error expected, got: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-06-17 20:24:38 +00:00
|
|
|
if password != expectedPassword {
|
|
|
|
t.Fatalf("Actual password: %s\nExpected password: %s", password, expectedPassword)
|
2020-05-27 18:28:00 +00:00
|
|
|
}
|
|
|
|
}
|