open-vault/sdk/plugin/logger_test.go
John-Michael Faircloth b6c05fae33
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

263 lines
5.7 KiB
Go

package plugin
import (
"bufio"
"bytes"
"io/ioutil"
"net/rpc"
"strings"
"testing"
hclog "github.com/hashicorp/go-hclog"
plugin "github.com/hashicorp/go-plugin"
"github.com/hashicorp/vault/sdk/helper/logging"
)
func TestLogger_levels(t *testing.T) {
client, server := plugin.TestRPCConn(t)
defer client.Close()
var buf bytes.Buffer
writer := bufio.NewWriter(&buf)
l := logging.NewVaultLoggerWithWriter(writer, hclog.Trace)
server.RegisterName("Plugin", &LoggerServer{
logger: l,
})
expected := "foobar"
testLogger := &deprecatedLoggerClient{client: client}
// Test trace
testLogger.Trace(expected)
if err := writer.Flush(); err != nil {
t.Fatal(err)
}
result := buf.String()
buf.Reset()
if !strings.Contains(result, expected) {
t.Fatalf("expected log to contain %s, got %s", expected, result)
}
// Test debug
testLogger.Debug(expected)
if err := writer.Flush(); err != nil {
t.Fatal(err)
}
result = buf.String()
buf.Reset()
if !strings.Contains(result, expected) {
t.Fatalf("expected log to contain %s, got %s", expected, result)
}
// Test debug
testLogger.Info(expected)
if err := writer.Flush(); err != nil {
t.Fatal(err)
}
result = buf.String()
buf.Reset()
if !strings.Contains(result, expected) {
t.Fatalf("expected log to contain %s, got %s", expected, result)
}
// Test warn
testLogger.Warn(expected)
if err := writer.Flush(); err != nil {
t.Fatal(err)
}
result = buf.String()
buf.Reset()
if !strings.Contains(result, expected) {
t.Fatalf("expected log to contain %s, got %s", expected, result)
}
// Test error
testLogger.Error(expected)
if err := writer.Flush(); err != nil {
t.Fatal(err)
}
result = buf.String()
buf.Reset()
if !strings.Contains(result, expected) {
t.Fatalf("expected log to contain %s, got %s", expected, result)
}
// Test fatal
testLogger.Fatal(expected)
if err := writer.Flush(); err != nil {
t.Fatal(err)
}
result = buf.String()
buf.Reset()
if result != "" {
t.Fatalf("expected log Fatal() to be no-op, got %s", result)
}
}
func TestLogger_isLevels(t *testing.T) {
client, server := plugin.TestRPCConn(t)
defer client.Close()
l := logging.NewVaultLoggerWithWriter(ioutil.Discard, hclog.Trace)
server.RegisterName("Plugin", &LoggerServer{
logger: l,
})
testLogger := &deprecatedLoggerClient{client: client}
if !testLogger.IsDebug() || !testLogger.IsInfo() || !testLogger.IsTrace() || !testLogger.IsWarn() {
t.Fatal("expected logger to return true for all logger level checks")
}
}
func TestLogger_log(t *testing.T) {
client, server := plugin.TestRPCConn(t)
defer client.Close()
var buf bytes.Buffer
writer := bufio.NewWriter(&buf)
l := logging.NewVaultLoggerWithWriter(writer, hclog.Trace)
server.RegisterName("Plugin", &LoggerServer{
logger: l,
})
expected := "foobar"
testLogger := &deprecatedLoggerClient{client: client}
// Test trace 6 = logxi.LevelInfo
testLogger.Log(6, expected, nil)
if err := writer.Flush(); err != nil {
t.Fatal(err)
}
result := buf.String()
if !strings.Contains(result, expected) {
t.Fatalf("expected log to contain %s, got %s", expected, result)
}
}
func TestLogger_setLevel(t *testing.T) {
client, server := plugin.TestRPCConn(t)
defer client.Close()
l := hclog.New(&hclog.LoggerOptions{Output: ioutil.Discard})
server.RegisterName("Plugin", &LoggerServer{
logger: l,
})
testLogger := &deprecatedLoggerClient{client: client}
testLogger.SetLevel(4) // 4 == logxi.LevelWarn
if !testLogger.IsWarn() {
t.Fatal("expected logger to support warn level")
}
}
type deprecatedLoggerClient struct {
client *rpc.Client
}
func (l *deprecatedLoggerClient) Trace(msg string, args ...interface{}) {
cArgs := &LoggerArgs{
Msg: msg,
Args: args,
}
l.client.Call("Plugin.Trace", cArgs, &struct{}{})
}
func (l *deprecatedLoggerClient) Debug(msg string, args ...interface{}) {
cArgs := &LoggerArgs{
Msg: msg,
Args: args,
}
l.client.Call("Plugin.Debug", cArgs, &struct{}{})
}
func (l *deprecatedLoggerClient) Info(msg string, args ...interface{}) {
cArgs := &LoggerArgs{
Msg: msg,
Args: args,
}
l.client.Call("Plugin.Info", cArgs, &struct{}{})
}
func (l *deprecatedLoggerClient) Warn(msg string, args ...interface{}) error {
var reply LoggerReply
cArgs := &LoggerArgs{
Msg: msg,
Args: args,
}
err := l.client.Call("Plugin.Warn", cArgs, &reply)
if err != nil {
return err
}
if reply.Error != nil {
return reply.Error
}
return nil
}
func (l *deprecatedLoggerClient) Error(msg string, args ...interface{}) error {
var reply LoggerReply
cArgs := &LoggerArgs{
Msg: msg,
Args: args,
}
err := l.client.Call("Plugin.Error", cArgs, &reply)
if err != nil {
return err
}
if reply.Error != nil {
return reply.Error
}
return nil
}
func (l *deprecatedLoggerClient) Fatal(msg string, args ...interface{}) {
// NOOP since it's not actually used within vault
}
func (l *deprecatedLoggerClient) Log(level int, msg string, args []interface{}) {
cArgs := &LoggerArgs{
Level: level,
Msg: msg,
Args: args,
}
l.client.Call("Plugin.Log", cArgs, &struct{}{})
}
func (l *deprecatedLoggerClient) SetLevel(level int) {
l.client.Call("Plugin.SetLevel", level, &struct{}{})
}
func (l *deprecatedLoggerClient) IsTrace() bool {
var reply LoggerReply
l.client.Call("Plugin.IsTrace", new(interface{}), &reply)
return reply.IsTrue
}
func (l *deprecatedLoggerClient) IsDebug() bool {
var reply LoggerReply
l.client.Call("Plugin.IsDebug", new(interface{}), &reply)
return reply.IsTrue
}
func (l *deprecatedLoggerClient) IsInfo() bool {
var reply LoggerReply
l.client.Call("Plugin.IsInfo", new(interface{}), &reply)
return reply.IsTrue
}
func (l *deprecatedLoggerClient) IsWarn() bool {
var reply LoggerReply
l.client.Call("Plugin.IsWarn", new(interface{}), &reply)
return reply.IsTrue
}