open-vault/builtin/logical/database/dbplugin/plugin_test.go

221 lines
5 KiB
Go
Raw Normal View History

2017-04-10 21:12:28 +00:00
package dbplugin_test
2017-03-23 22:54:15 +00:00
import (
"errors"
"net"
"os"
"testing"
"time"
2017-04-10 21:12:28 +00:00
"github.com/hashicorp/vault/builtin/logical/database/dbplugin"
2017-03-23 22:54:15 +00:00
"github.com/hashicorp/vault/helper/pluginutil"
"github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/vault"
2017-03-28 19:20:17 +00:00
log "github.com/mgutz/logxi/v1"
2017-03-23 22:54:15 +00:00
)
type mockPlugin struct {
users map[string][]string
}
2017-04-12 23:41:06 +00:00
func (m *mockPlugin) Type() (string, error) { return "mock", nil }
2017-04-10 21:12:28 +00:00
func (m *mockPlugin) CreateUser(statements dbplugin.Statements, usernamePrefix string, expiration time.Time) (username string, password string, err error) {
err = errors.New("err")
if usernamePrefix == "" || expiration.IsZero() {
return "", "", err
2017-03-23 22:54:15 +00:00
}
2017-04-10 21:12:28 +00:00
if _, ok := m.users[usernamePrefix]; ok {
return "", "", err
2017-03-23 22:54:15 +00:00
}
2017-04-10 21:12:28 +00:00
m.users[usernamePrefix] = []string{password}
2017-03-23 22:54:15 +00:00
2017-04-10 21:12:28 +00:00
return usernamePrefix, "test", nil
2017-03-23 22:54:15 +00:00
}
2017-04-10 21:12:28 +00:00
func (m *mockPlugin) RenewUser(statements dbplugin.Statements, username string, expiration time.Time) error {
2017-03-23 22:54:15 +00:00
err := errors.New("err")
2017-04-10 21:12:28 +00:00
if username == "" || expiration.IsZero() {
2017-03-23 22:54:15 +00:00
return err
}
if _, ok := m.users[username]; !ok {
return err
}
return nil
}
2017-04-10 21:12:28 +00:00
func (m *mockPlugin) RevokeUser(statements dbplugin.Statements, username string) error {
2017-03-23 22:54:15 +00:00
err := errors.New("err")
if username == "" {
return err
}
if _, ok := m.users[username]; !ok {
return err
}
delete(m.users, username)
return nil
}
2017-04-12 23:41:06 +00:00
func (m *mockPlugin) Initialize(conf map[string]interface{}, _ bool) error {
2017-03-23 22:54:15 +00:00
err := errors.New("err")
if len(conf) != 1 {
return err
}
return nil
}
func (m *mockPlugin) Close() error {
m.users = nil
return nil
}
func getCore(t *testing.T) (*vault.Core, net.Listener, logical.SystemView) {
core, _, _, ln := vault.TestCoreUnsealedWithListener(t)
http.TestServerWithListener(t, ln, "", core)
sys := vault.TestDynamicSystemView(core)
2017-04-10 21:12:28 +00:00
vault.TestAddTestPlugin(t, core, "test-plugin", "TestPlugin_Main")
2017-03-23 22:54:15 +00:00
return core, ln, sys
}
2017-03-27 18:46:20 +00:00
// This is not an actual test case, it's a helper function that will be executed
// by the go-plugin client via an exec call.
2017-03-23 22:54:15 +00:00
func TestPlugin_Main(t *testing.T) {
if os.Getenv(pluginutil.PluginUnwrapTokenEnv) == "" {
return
}
plugin := &mockPlugin{
2017-04-10 21:12:28 +00:00
users: make(map[string][]string),
2017-03-23 22:54:15 +00:00
}
2017-04-10 21:12:28 +00:00
dbplugin.NewPluginServer(plugin)
2017-03-23 22:54:15 +00:00
}
func TestPlugin_Initialize(t *testing.T) {
_, ln, sys := getCore(t)
defer ln.Close()
2017-04-10 21:12:28 +00:00
dbRaw, err := dbplugin.PluginFactory("test-plugin", sys, &log.NullLogger{})
2017-03-23 22:54:15 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
2017-04-10 21:12:28 +00:00
connectionDetails := map[string]interface{}{
"test": 1,
}
2017-04-12 23:41:06 +00:00
err = dbRaw.Initialize(connectionDetails, true)
2017-03-23 22:54:15 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
err = dbRaw.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
}
func TestPlugin_CreateUser(t *testing.T) {
_, ln, sys := getCore(t)
defer ln.Close()
2017-04-10 21:12:28 +00:00
db, err := dbplugin.PluginFactory("test-plugin", sys, &log.NullLogger{})
2017-03-23 22:54:15 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
defer db.Close()
2017-04-10 21:12:28 +00:00
connectionDetails := map[string]interface{}{
"test": 1,
2017-03-23 22:54:15 +00:00
}
2017-04-12 23:41:06 +00:00
err = db.Initialize(connectionDetails, true)
2017-03-23 22:54:15 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
2017-04-10 21:12:28 +00:00
us, pw, err := db.CreateUser(dbplugin.Statements{}, "test", time.Now().Add(time.Minute))
2017-03-23 22:54:15 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
2017-04-10 21:12:28 +00:00
if us != "test" || pw != "test" {
t.Fatal("expected username and password to be 'test'")
2017-03-23 22:54:15 +00:00
}
// try and save the same user again to verify it saved the first time, this
// should return an error
2017-04-10 21:12:28 +00:00
_, _, err = db.CreateUser(dbplugin.Statements{}, "test", time.Now().Add(time.Minute))
2017-03-23 22:54:15 +00:00
if err == nil {
t.Fatal("expected an error, user wasn't created correctly")
}
}
func TestPlugin_RenewUser(t *testing.T) {
_, ln, sys := getCore(t)
defer ln.Close()
2017-04-10 21:12:28 +00:00
db, err := dbplugin.PluginFactory("test-plugin", sys, &log.NullLogger{})
2017-03-23 22:54:15 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
defer db.Close()
2017-04-10 21:12:28 +00:00
connectionDetails := map[string]interface{}{
"test": 1,
2017-03-23 22:54:15 +00:00
}
2017-04-12 23:41:06 +00:00
err = db.Initialize(connectionDetails, true)
2017-03-23 22:54:15 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
2017-04-10 21:12:28 +00:00
us, _, err := db.CreateUser(dbplugin.Statements{}, "test", time.Now().Add(time.Minute))
2017-03-23 22:54:15 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
2017-04-10 21:12:28 +00:00
err = db.RenewUser(dbplugin.Statements{}, us, time.Now().Add(time.Minute))
2017-03-23 22:54:15 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
}
func TestPlugin_RevokeUser(t *testing.T) {
_, ln, sys := getCore(t)
defer ln.Close()
2017-04-10 21:12:28 +00:00
db, err := dbplugin.PluginFactory("test-plugin", sys, &log.NullLogger{})
2017-03-23 22:54:15 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
defer db.Close()
2017-04-10 21:12:28 +00:00
connectionDetails := map[string]interface{}{
"test": 1,
2017-03-23 22:54:15 +00:00
}
2017-04-12 23:41:06 +00:00
err = db.Initialize(connectionDetails, true)
2017-03-23 22:54:15 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
2017-04-10 21:12:28 +00:00
us, _, err := db.CreateUser(dbplugin.Statements{}, "test", time.Now().Add(time.Minute))
2017-03-23 22:54:15 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
// Test default revoke statememts
2017-04-10 21:12:28 +00:00
err = db.RevokeUser(dbplugin.Statements{}, us)
2017-03-23 22:54:15 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
// Try adding the same username back so we can verify it was removed
2017-04-10 21:12:28 +00:00
_, _, err = db.CreateUser(dbplugin.Statements{}, "test", time.Now().Add(time.Minute))
2017-03-23 22:54:15 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
}