vault/sdk/database/dbplugin/v5/plugin_client_test.go
John-Michael Faircloth 07927e036c
feature: secrets/auth plugin multiplexing (#14946)
* enable registering backend muxed plugins in plugin catalog

* set the sysview on the pluginconfig to allow enabling secrets/auth plugins

* store backend instances in map

* store single implementations in the instances map

cleanup instance map and ensure we don't deadlock

* fix system backend unit tests

move GetMultiplexIDFromContext to pluginutil package

fix pluginutil test

fix dbplugin ut

* return error(s) if we can't get the plugin client

update comments

* refactor/move GetMultiplexIDFromContext test

* add changelog

* remove unnecessary field on pluginClient

* add unit tests to PluginCatalog for secrets/auth plugins

* fix comment

* return pluginClient from TestRunTestPlugin

* add multiplexed backend test

* honor metadatamode value in newbackend pluginconfig

* check that connection exists on cleanup

* add automtls to secrets/auth plugins

* don't remove apiclientmeta parsing

* use formatting directive for fmt.Errorf

* fix ut: remove tls provider func

* remove tlsproviderfunc from backend plugin tests

* use env var to prevent test plugin from running as a unit test

* WIP: remove lazy loading

* move non lazy loaded backend to new package

* use version wrapper for backend plugin factory

* remove backendVersionWrapper type

* implement getBackendPluginType for plugin catalog

* handle backend plugin v4 registration

* add plugin automtls env guard

* modify plugin factory to determine the backend to use

* remove old pluginsets from v5 and log pid in plugin catalog

* add reload mechanism via context

* readd v3 and v4 to pluginset

* call cleanup from reload if non-muxed

* move v5 backend code to new package

* use context reload for for ErrPluginShutdown case

* add wrapper on v5 backend

* fix run config UTs

* fix unit tests

- use v4/v5 mapping for plugin versions
- fix test build err
- add reload method on fakePluginClient
- add multiplexed cases for integration tests

* remove comment and update AutoMTLS field in test

* remove comment

* remove errwrap and unused context

* only support metadatamode false for v5 backend plugins

* update plugin catalog errors

* use const for env variables

* rename locks and remove unused

* remove unneeded nil check

* improvements based on staticcheck recommendations

* use const for single implementation string

* use const for context key

* use info default log level

* move pid to pluginClient struct

* remove v3 and v4 from multiplexed plugin set

* return from reload when non-multiplexed

* update automtls env string

* combine getBackend and getBrokeredClient

* update comments for plugin reload, Backend return val and log

* revert Backend return type

* allow non-muxed plugins to serve v5

* move v5 code to existing sdk plugin package

* do next export sdk fields now that we have removed extra plugin pkg

* set TLSProvider in ServeMultiplex for backwards compat

* use bool to flag multiplexing support on grpc backend server

* revert userpass main.go

* refactor plugin sdk

- update comments
- make use of multiplexing boolean and single implementation ID const

* update comment and use multierr

* attempt v4 if dispense fails on getPluginTypeForUnknown

* update comments on sdk plugin backend
2022-08-29 21:42:26 -05:00

151 lines
3.9 KiB
Go

package dbplugin
import (
"context"
"errors"
"reflect"
"testing"
"time"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/database/dbplugin/v5/proto"
"github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/sdk/helper/pluginutil"
"github.com/hashicorp/vault/sdk/helper/wrapping"
"github.com/stretchr/testify/mock"
"google.golang.org/grpc"
)
func TestNewPluginClient(t *testing.T) {
type testCase struct {
config pluginutil.PluginClientConfig
pluginClient pluginutil.PluginClient
expectedResp *DatabasePluginClient
expectedErr error
}
tests := map[string]testCase{
"happy path": {
config: testPluginClientConfig(),
pluginClient: &fakePluginClient{
connResp: nil,
dispenseResp: gRPCClient{client: fakeClient{}},
dispenseErr: nil,
},
expectedResp: &DatabasePluginClient{
client: &fakePluginClient{
connResp: nil,
dispenseResp: gRPCClient{client: fakeClient{}},
dispenseErr: nil,
},
Database: gRPCClient{proto.NewDatabaseClient(nil), context.Context(nil)},
},
expectedErr: nil,
},
"dispense error": {
config: testPluginClientConfig(),
pluginClient: &fakePluginClient{
connResp: nil,
dispenseResp: gRPCClient{},
dispenseErr: errors.New("dispense error"),
},
expectedResp: nil,
expectedErr: errors.New("dispense error"),
},
"error unsupported client type": {
config: testPluginClientConfig(),
pluginClient: &fakePluginClient{
connResp: nil,
dispenseResp: nil,
dispenseErr: nil,
},
expectedResp: nil,
expectedErr: errors.New("unsupported client type"),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
mockWrapper := new(mockRunnerUtil)
mockWrapper.On("NewPluginClient", ctx, mock.Anything).
Return(test.pluginClient, nil)
defer mockWrapper.AssertNumberOfCalls(t, "NewPluginClient", 1)
resp, err := NewPluginClient(ctx, mockWrapper, test.config)
if test.expectedErr != nil && err == nil {
t.Fatalf("err expected, got nil")
}
if test.expectedErr == nil && err != nil {
t.Fatalf("no error expected, got: %s", err)
}
if test.expectedErr == nil && !reflect.DeepEqual(resp, test.expectedResp) {
t.Fatalf("Actual response: %#v\nExpected response: %#v", resp, test.expectedResp)
}
})
}
}
func testPluginClientConfig() pluginutil.PluginClientConfig {
return pluginutil.PluginClientConfig{
Name: "test-plugin",
PluginSets: PluginSets,
PluginType: consts.PluginTypeDatabase,
HandshakeConfig: HandshakeConfig,
Logger: log.NewNullLogger(),
IsMetadataMode: true,
AutoMTLS: true,
}
}
var _ pluginutil.PluginClient = &fakePluginClient{}
type fakePluginClient struct {
connResp grpc.ClientConnInterface
dispenseResp interface{}
dispenseErr error
}
func (f *fakePluginClient) Conn() grpc.ClientConnInterface {
return nil
}
func (f *fakePluginClient) Reload() error {
return nil
}
func (f *fakePluginClient) Dispense(name string) (interface{}, error) {
return f.dispenseResp, f.dispenseErr
}
func (f *fakePluginClient) Ping() error {
return nil
}
func (f *fakePluginClient) Close() error {
return nil
}
var _ pluginutil.RunnerUtil = &mockRunnerUtil{}
type mockRunnerUtil struct {
mock.Mock
}
func (m *mockRunnerUtil) NewPluginClient(ctx context.Context, config pluginutil.PluginClientConfig) (pluginutil.PluginClient, error) {
args := m.Called(ctx, config)
return args.Get(0).(pluginutil.PluginClient), args.Error(1)
}
func (m *mockRunnerUtil) ResponseWrapData(ctx context.Context, data map[string]interface{}, ttl time.Duration, jwt bool) (*wrapping.ResponseWrapInfo, error) {
args := m.Called(ctx, data, ttl, jwt)
return args.Get(0).(*wrapping.ResponseWrapInfo), args.Error(1)
}
func (m *mockRunnerUtil) MlockEnabled() bool {
args := m.Called()
return args.Bool(0)
}