2017-04-10 21:12:28 +00:00
|
|
|
package dbplugin_test
|
2017-03-23 22:54:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-05-02 21:40:11 +00:00
|
|
|
stdhttp "net/http"
|
2017-03-23 22:54:15 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-05-02 21:40:11 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
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"
|
2017-05-02 21:40:11 +00:00
|
|
|
"github.com/hashicorp/vault/plugins"
|
2017-03-23 22:54:15 +00:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2017-05-02 21:40:11 +00:00
|
|
|
func getCore(t *testing.T) ([]*vault.TestClusterCore, logical.SystemView) {
|
|
|
|
coreConfig := &vault.CoreConfig{}
|
2017-03-23 22:54:15 +00:00
|
|
|
|
2017-05-02 21:40:11 +00:00
|
|
|
handler1 := stdhttp.NewServeMux()
|
|
|
|
handler2 := stdhttp.NewServeMux()
|
|
|
|
handler3 := stdhttp.NewServeMux()
|
|
|
|
|
|
|
|
// Chicken-and-egg: Handler needs a core. So we create handlers first, then
|
|
|
|
// add routes chained to a Handler-created handler.
|
|
|
|
cores := vault.TestCluster(t, []stdhttp.Handler{handler1, handler2, handler3}, coreConfig, false)
|
|
|
|
handler1.Handle("/", http.Handler(cores[0].Core))
|
|
|
|
handler2.Handle("/", http.Handler(cores[1].Core))
|
|
|
|
handler3.Handle("/", http.Handler(cores[2].Core))
|
|
|
|
|
|
|
|
core := cores[0]
|
|
|
|
|
|
|
|
sys := vault.TestDynamicSystemView(core.Core)
|
|
|
|
vault.TestAddTestPlugin(t, core.Core, "test-plugin", "TestPlugin_Main")
|
|
|
|
|
|
|
|
return cores, sys
|
2017-03-23 22:54:15 +00:00
|
|
|
}
|
|
|
|
|
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-05-02 21:40:11 +00:00
|
|
|
plugins.Serve(plugin, &api.TLSConfig{Insecure: true})
|
2017-03-23 22:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlugin_Initialize(t *testing.T) {
|
2017-05-02 21:40:11 +00:00
|
|
|
cores, sys := getCore(t)
|
|
|
|
for _, core := range cores {
|
|
|
|
defer core.CloseListeners()
|
|
|
|
}
|
2017-03-23 22:54:15 +00:00
|
|
|
|
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) {
|
2017-05-02 21:40:11 +00:00
|
|
|
cores, sys := getCore(t)
|
|
|
|
for _, core := range cores {
|
|
|
|
defer core.CloseListeners()
|
|
|
|
}
|
2017-03-23 22:54:15 +00:00
|
|
|
|
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) {
|
2017-05-02 21:40:11 +00:00
|
|
|
cores, sys := getCore(t)
|
|
|
|
for _, core := range cores {
|
|
|
|
defer core.CloseListeners()
|
|
|
|
}
|
2017-03-23 22:54:15 +00:00
|
|
|
|
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) {
|
2017-05-02 21:40:11 +00:00
|
|
|
cores, sys := getCore(t)
|
|
|
|
for _, core := range cores {
|
|
|
|
defer core.CloseListeners()
|
|
|
|
}
|
2017-03-23 22:54:15 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|