Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database gRPC plugins #3666

Merged
merged 22 commits into from
Dec 14, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tests
Brian Kassouf committed Dec 8, 2017
commit 1726422900d6f628b79b7a6ae8b5c3a27e28fe64
29 changes: 29 additions & 0 deletions builtin/logical/database/backend_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package database

import (
"context"
"database/sql"
"fmt"
"log"
@@ -52,6 +53,7 @@ func preparePostgresTestContainer(t *testing.T, s logical.Storage, b logical.Bac
if err = pool.Retry(func() error {
// This will cause a validation to run
resp, err := b.HandleRequest(&logical.Request{
Context: context.Background(),
Storage: s,
Operation: logical.UpdateOperation,
Path: "config/postgresql",
@@ -140,6 +142,7 @@ func TestBackend_config_connection(t *testing.T) {
}

configReq := &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "config/plugin-test",
Storage: config.StorageView,
@@ -206,6 +209,7 @@ func TestBackend_basic(t *testing.T) {
"allowed_roles": []string{"plugin-role-test"},
}
req := &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "config/plugin-test",
Storage: config.StorageView,
@@ -224,6 +228,7 @@ func TestBackend_basic(t *testing.T) {
"max_ttl": "10m",
}
req = &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "roles/plugin-role-test",
Storage: config.StorageView,
@@ -237,6 +242,7 @@ func TestBackend_basic(t *testing.T) {
// Get creds
data = map[string]interface{}{}
req = &logical.Request{
Context: context.Background(),
Operation: logical.ReadOperation,
Path: "creds/plugin-role-test",
Storage: config.StorageView,
@@ -253,6 +259,7 @@ func TestBackend_basic(t *testing.T) {

// Revoke creds
resp, err = b.HandleRequest(&logical.Request{
Context: context.Background(),
Operation: logical.RevokeOperation,
Storage: config.StorageView,
Secret: &logical.Secret{
@@ -297,6 +304,7 @@ func TestBackend_connectionCrud(t *testing.T) {
"verify_connection": false,
}
req := &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "config/plugin-test",
Storage: config.StorageView,
@@ -316,6 +324,7 @@ func TestBackend_connectionCrud(t *testing.T) {
"max_ttl": "10m",
}
req = &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "roles/plugin-role-test",
Storage: config.StorageView,
@@ -333,6 +342,7 @@ func TestBackend_connectionCrud(t *testing.T) {
"allowed_roles": []string{"plugin-role-test"},
}
req = &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "config/plugin-test",
Storage: config.StorageView,
@@ -365,6 +375,7 @@ func TestBackend_connectionCrud(t *testing.T) {
// Reset Connection
data = map[string]interface{}{}
req = &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "reset/plugin-test",
Storage: config.StorageView,
@@ -378,6 +389,7 @@ func TestBackend_connectionCrud(t *testing.T) {
// Get creds
data = map[string]interface{}{}
req = &logical.Request{
Context: context.Background(),
Operation: logical.ReadOperation,
Path: "creds/plugin-role-test",
Storage: config.StorageView,
@@ -395,6 +407,7 @@ func TestBackend_connectionCrud(t *testing.T) {
// Delete Connection
data = map[string]interface{}{}
req = &logical.Request{
Context: context.Background(),
Operation: logical.DeleteOperation,
Path: "config/plugin-test",
Storage: config.StorageView,
@@ -441,6 +454,7 @@ func TestBackend_roleCrud(t *testing.T) {
"plugin_name": "postgresql-database-plugin",
}
req := &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "config/plugin-test",
Storage: config.StorageView,
@@ -460,6 +474,7 @@ func TestBackend_roleCrud(t *testing.T) {
"max_ttl": "10m",
}
req = &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "roles/plugin-role-test",
Storage: config.StorageView,
@@ -473,6 +488,7 @@ func TestBackend_roleCrud(t *testing.T) {
// Read the role
data = map[string]interface{}{}
req = &logical.Request{
Context: context.Background(),
Operation: logical.ReadOperation,
Path: "roles/plugin-role-test",
Storage: config.StorageView,
@@ -500,6 +516,7 @@ func TestBackend_roleCrud(t *testing.T) {
// Delete the role
data = map[string]interface{}{}
req = &logical.Request{
Context: context.Background(),
Operation: logical.DeleteOperation,
Path: "roles/plugin-role-test",
Storage: config.StorageView,
@@ -513,6 +530,7 @@ func TestBackend_roleCrud(t *testing.T) {
// Read the role
data = map[string]interface{}{}
req = &logical.Request{
Context: context.Background(),
Operation: logical.ReadOperation,
Path: "roles/plugin-role-test",
Storage: config.StorageView,
@@ -551,6 +569,7 @@ func TestBackend_allowedRoles(t *testing.T) {
"plugin_name": "postgresql-database-plugin",
}
req := &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "config/plugin-test",
Storage: config.StorageView,
@@ -569,6 +588,7 @@ func TestBackend_allowedRoles(t *testing.T) {
"max_ttl": "10m",
}
req = &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "roles/denied",
Storage: config.StorageView,
@@ -586,6 +606,7 @@ func TestBackend_allowedRoles(t *testing.T) {
"max_ttl": "10m",
}
req = &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "roles/allowed",
Storage: config.StorageView,
@@ -599,6 +620,7 @@ func TestBackend_allowedRoles(t *testing.T) {
// Get creds from denied role, should fail
data = map[string]interface{}{}
req = &logical.Request{
Context: context.Background(),
Operation: logical.ReadOperation,
Path: "creds/denied",
Storage: config.StorageView,
@@ -616,6 +638,7 @@ func TestBackend_allowedRoles(t *testing.T) {
"allowed_roles": "allow*",
}
req = &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "config/plugin-test",
Storage: config.StorageView,
@@ -629,6 +652,7 @@ func TestBackend_allowedRoles(t *testing.T) {
// Get creds, should work.
data = map[string]interface{}{}
req = &logical.Request{
Context: context.Background(),
Operation: logical.ReadOperation,
Path: "creds/allowed",
Storage: config.StorageView,
@@ -650,6 +674,7 @@ func TestBackend_allowedRoles(t *testing.T) {
"allowed_roles": "*",
}
req = &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "config/plugin-test",
Storage: config.StorageView,
@@ -663,6 +688,7 @@ func TestBackend_allowedRoles(t *testing.T) {
// Get creds, should work.
data = map[string]interface{}{}
req = &logical.Request{
Context: context.Background(),
Operation: logical.ReadOperation,
Path: "creds/allowed",
Storage: config.StorageView,
@@ -684,6 +710,7 @@ func TestBackend_allowedRoles(t *testing.T) {
"allowed_roles": "allow, allowed",
}
req = &logical.Request{
Context: context.Background(),
Operation: logical.UpdateOperation,
Path: "config/plugin-test",
Storage: config.StorageView,
@@ -697,6 +724,7 @@ func TestBackend_allowedRoles(t *testing.T) {
// Get creds from denied role, should fail
data = map[string]interface{}{}
req = &logical.Request{
Context: context.Background(),
Operation: logical.ReadOperation,
Path: "creds/denied",
Storage: config.StorageView,
@@ -710,6 +738,7 @@ func TestBackend_allowedRoles(t *testing.T) {
// Get creds from allowed role, should work.
data = map[string]interface{}{}
req = &logical.Request{
Context: context.Background(),
Operation: logical.ReadOperation,
Path: "creds/allowed",
Storage: config.StorageView,
31 changes: 16 additions & 15 deletions builtin/logical/database/dbplugin/plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dbplugin_test

import (
"context"
"errors"
"os"
"testing"
@@ -20,7 +21,7 @@ type mockPlugin struct {
}

func (m *mockPlugin) Type() (string, error) { return "mock", nil }
func (m *mockPlugin) CreateUser(statements dbplugin.Statements, usernameConf dbplugin.UsernameConfig, expiration time.Time) (username string, password string, err error) {
func (m *mockPlugin) CreateUser(_ context.Context, statements dbplugin.Statements, usernameConf dbplugin.UsernameConfig, expiration time.Time) (username string, password string, err error) {
err = errors.New("err")
if usernameConf.DisplayName == "" || expiration.IsZero() {
return "", "", err
@@ -34,7 +35,7 @@ func (m *mockPlugin) CreateUser(statements dbplugin.Statements, usernameConf dbp

return usernameConf.DisplayName, "test", nil
}
func (m *mockPlugin) RenewUser(statements dbplugin.Statements, username string, expiration time.Time) error {
func (m *mockPlugin) RenewUser(_ context.Context, statements dbplugin.Statements, username string, expiration time.Time) error {
err := errors.New("err")
if username == "" || expiration.IsZero() {
return err
@@ -46,7 +47,7 @@ func (m *mockPlugin) RenewUser(statements dbplugin.Statements, username string,

return nil
}
func (m *mockPlugin) RevokeUser(statements dbplugin.Statements, username string) error {
func (m *mockPlugin) RevokeUser(_ context.Context, statements dbplugin.Statements, username string) error {
err := errors.New("err")
if username == "" {
return err
@@ -59,7 +60,7 @@ func (m *mockPlugin) RevokeUser(statements dbplugin.Statements, username string)
delete(m.users, username)
return nil
}
func (m *mockPlugin) Initialize(conf map[string]interface{}, _ bool) error {
func (m *mockPlugin) Initialize(_ context.Context, conf map[string]interface{}, _ bool) error {
err := errors.New("err")
if len(conf) != 1 {
return err
@@ -118,7 +119,7 @@ func TestPlugin_Initialize(t *testing.T) {
"test": 1,
}

err = dbRaw.Initialize(connectionDetails, true)
err = dbRaw.Initialize(context.Background(), connectionDetails, true)
if err != nil {
t.Fatalf("err: %s", err)
}
@@ -143,7 +144,7 @@ func TestPlugin_CreateUser(t *testing.T) {
"test": 1,
}

err = db.Initialize(connectionDetails, true)
err = db.Initialize(context.Background(), connectionDetails, true)
if err != nil {
t.Fatalf("err: %s", err)
}
@@ -153,7 +154,7 @@ func TestPlugin_CreateUser(t *testing.T) {
RoleName: "test",
}

us, pw, err := db.CreateUser(dbplugin.Statements{}, usernameConf, time.Now().Add(time.Minute))
us, pw, err := db.CreateUser(context.Background(), dbplugin.Statements{}, usernameConf, time.Now().Add(time.Minute))
if err != nil {
t.Fatalf("err: %s", err)
}
@@ -163,7 +164,7 @@ func TestPlugin_CreateUser(t *testing.T) {

// try and save the same user again to verify it saved the first time, this
// should return an error
_, _, err = db.CreateUser(dbplugin.Statements{}, usernameConf, time.Now().Add(time.Minute))
_, _, err = db.CreateUser(context.Background(), dbplugin.Statements{}, usernameConf, time.Now().Add(time.Minute))
if err == nil {
t.Fatal("expected an error, user wasn't created correctly")
}
@@ -182,7 +183,7 @@ func TestPlugin_RenewUser(t *testing.T) {
connectionDetails := map[string]interface{}{
"test": 1,
}
err = db.Initialize(connectionDetails, true)
err = db.Initialize(context.Background(), connectionDetails, true)
if err != nil {
t.Fatalf("err: %s", err)
}
@@ -192,12 +193,12 @@ func TestPlugin_RenewUser(t *testing.T) {
RoleName: "test",
}

us, _, err := db.CreateUser(dbplugin.Statements{}, usernameConf, time.Now().Add(time.Minute))
us, _, err := db.CreateUser(context.Background(), dbplugin.Statements{}, usernameConf, time.Now().Add(time.Minute))
if err != nil {
t.Fatalf("err: %s", err)
}

err = db.RenewUser(dbplugin.Statements{}, us, time.Now().Add(time.Minute))
err = db.RenewUser(context.Background(), dbplugin.Statements{}, us, time.Now().Add(time.Minute))
if err != nil {
t.Fatalf("err: %s", err)
}
@@ -216,7 +217,7 @@ func TestPlugin_RevokeUser(t *testing.T) {
connectionDetails := map[string]interface{}{
"test": 1,
}
err = db.Initialize(connectionDetails, true)
err = db.Initialize(context.Background(), connectionDetails, true)
if err != nil {
t.Fatalf("err: %s", err)
}
@@ -226,19 +227,19 @@ func TestPlugin_RevokeUser(t *testing.T) {
RoleName: "test",
}

us, _, err := db.CreateUser(dbplugin.Statements{}, usernameConf, time.Now().Add(time.Minute))
us, _, err := db.CreateUser(context.Background(), dbplugin.Statements{}, usernameConf, time.Now().Add(time.Minute))
if err != nil {
t.Fatalf("err: %s", err)
}

// Test default revoke statememts
err = db.RevokeUser(dbplugin.Statements{}, us)
err = db.RevokeUser(context.Background(), dbplugin.Statements{}, us)
if err != nil {
t.Fatalf("err: %s", err)
}

// Try adding the same username back so we can verify it was removed
_, _, err = db.CreateUser(dbplugin.Statements{}, usernameConf, time.Now().Add(time.Minute))
_, _, err = db.CreateUser(context.Background(), dbplugin.Statements{}, usernameConf, time.Now().Add(time.Minute))
if err != nil {
t.Fatalf("err: %s", err)
}
Loading