2017-04-07 22:50:03 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2017-04-07 22:50:03 +00:00
|
|
|
"database/sql"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
2018-03-21 19:05:56 +00:00
|
|
|
"strings"
|
2017-04-07 22:50:03 +00:00
|
|
|
"testing"
|
2018-01-18 06:43:38 +00:00
|
|
|
"time"
|
2017-04-07 22:50:03 +00:00
|
|
|
|
2018-06-19 15:24:28 +00:00
|
|
|
"github.com/go-test/deep"
|
2020-10-22 23:20:17 +00:00
|
|
|
mongodbatlas "github.com/hashicorp/vault-plugin-database-mongodbatlas"
|
2018-09-18 03:03:00 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2020-09-15 14:01:26 +00:00
|
|
|
postgreshelper "github.com/hashicorp/vault/helper/testhelpers/postgresql"
|
2017-07-31 15:28:06 +00:00
|
|
|
vaulthttp "github.com/hashicorp/vault/http"
|
2019-07-05 18:57:01 +00:00
|
|
|
"github.com/hashicorp/vault/plugins/database/mongodb"
|
2017-04-21 01:46:41 +00:00
|
|
|
"github.com/hashicorp/vault/plugins/database/postgresql"
|
2020-10-22 21:43:19 +00:00
|
|
|
v4 "github.com/hashicorp/vault/sdk/database/dbplugin"
|
|
|
|
v5 "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
|
2019-04-15 18:10:07 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/database/helper/dbutil"
|
2019-04-13 02:05:01 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/pluginutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-04-07 22:50:03 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
"github.com/lib/pq"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2017-07-03 18:54:01 +00:00
|
|
|
func getCluster(t *testing.T) (*vault.TestCluster, logical.SystemView) {
|
2017-05-02 21:40:11 +00:00
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
LogicalBackends: map[string]logical.Factory{
|
|
|
|
"database": Factory,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: vaulthttp.Handler,
|
|
|
|
})
|
|
|
|
cluster.Start()
|
2017-07-03 18:54:01 +00:00
|
|
|
cores := cluster.Cores
|
2020-10-16 16:35:55 +00:00
|
|
|
vault.TestWaitActive(t, cores[0].Core)
|
2017-07-31 15:28:06 +00:00
|
|
|
|
2017-07-31 19:09:09 +00:00
|
|
|
os.Setenv(pluginutil.PluginCACertPEMEnv, cluster.CACertPEMFile)
|
2017-05-02 21:40:11 +00:00
|
|
|
|
2017-07-03 18:54:01 +00:00
|
|
|
sys := vault.TestDynamicSystemView(cores[0].Core)
|
Combined Database Backend: Static Accounts (#6834)
* Add priority queue to sdk
* fix issue of storing pointers and now copy
* update to use copy structure
* Remove file, put Item struct def. into other file
* add link
* clean up docs
* refactor internal data structure to hide heap method implementations. Other cleanup after feedback
* rename PushItem and PopItem to just Push/Pop, after encapsulating the heap methods
* updates after feedback
* refactoring/renaming
* guard against pushing a nil item
* minor updates after feedback
* Add SetCredentials, GenerateCredentials gRPC methods to combined database backend gPRC
* Initial Combined database backend implementation of static accounts and automatic rotation
* vendor updates
* initial implementation of static accounts with Combined database backend, starting with PostgreSQL implementation
* add lock and setup of rotation queue
* vendor the queue
* rebase on new method signature of queue
* remove mongo tests for now
* update default role sql
* gofmt after rebase
* cleanup after rebasing to remove checks for ErrNotFound error
* rebase cdcr-priority-queue
* vendor dependencies with 'go mod vendor'
* website database docs for Static Role support
* document the rotate-role API endpoint
* postgres specific static role docs
* use constants for paths
* updates from review
* remove dead code
* combine and clarify error message for older plugins
* Update builtin/logical/database/backend.go
Co-Authored-By: Jim Kalafut <jim@kalafut.net>
* cleanups from feedback
* code and comment cleanups
* move db.RLock higher to protect db.GenerateCredentials call
* Return output with WALID if we failed to delete the WAL
* Update builtin/logical/database/path_creds_create.go
Co-Authored-By: Jim Kalafut <jim@kalafut.net>
* updates after running 'make fmt'
* update after running 'make proto'
* Update builtin/logical/database/path_roles.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* Update builtin/logical/database/path_roles.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* update comment and remove and rearrange some dead code
* Update website/source/api/secret/databases/index.html.md
Co-Authored-By: Jim Kalafut <jim@kalafut.net>
* cleanups after review
* Update sdk/database/dbplugin/grpc_transport.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* code cleanup after feedback
* remove PasswordLastSet; it's not used
* document GenerateCredentials and SetCredentials
* Update builtin/logical/database/path_rotate_credentials.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* wrap pop and popbykey in backend methods to protect against nil cred rotation queue
* use strings.HasPrefix instead of direct equality check for path
* Forgot to commit this
* updates after feedback
* re-purpose an outdated test to now check that static and dynamic roles cannot share a name
* check for unique name across dynamic and static roles
* refactor loadStaticWALs to return a map of name/setCredentialsWAL struct to consolidate where we're calling set credentials
* remove commented out code
* refactor to have loadstaticwals filter out wals for roles that no longer exist
* return error if nil input given
* add nil check for input into setStaticAccount
* Update builtin/logical/database/path_roles.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* add constant for queue tick time in seconds, used for comparrison in updates
* Update builtin/logical/database/path_roles.go
Co-Authored-By: Jim Kalafut <jim@kalafut.net>
* code cleanup after review
* remove misplaced code comment
* remove commented out code
* create a queue in the Factory method, even if it's never used
* update path_roles to use a common set of fields, with specific overrides for dynamic/static roles by type
* document new method
* move rotation things into a specific file
* rename test file and consolidate some static account tests
* Update builtin/logical/database/path_roles.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* Update builtin/logical/database/rotation.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* Update builtin/logical/database/rotation.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* Update builtin/logical/database/rotation.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* Update builtin/logical/database/rotation.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* Update builtin/logical/database/rotation.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* update code comments, method names, and move more methods into rotation.go
* update comments to be capitalized
* remove the item from the queue before we try to destroy it
* findStaticWAL returns an error
* use lowercase keys when encoding WAL entries
* small cleanups
* remove vestigial static account check
* remove redundant DeleteWAL call in populate queue
* if we error on loading role, push back to queue with 10 second backoff
* poll in initqueue to make sure the backend is setup and can write/delete data
* add revoke_user_on_delete flag to allow users to opt-in to revoking the static database user on delete of the Vault role. Default false
* add code comments on read-only loop
* code comment updates
* re-push if error returned from find static wal
* add locksutil and acquire locks when pop'ing from the queue
* grab exclusive locks for updating static roles
* Add SetCredentials and GenerateCredentials stubs to mockPlugin
* add a switch in initQueue to listen for cancelation
* remove guard on zero time, it should have no affect
* create a new context in Factory to pass on and use for closing the backend queue
* restore master copy of vendor dir
2019-06-19 19:45:39 +00:00
|
|
|
vault.TestAddTestPlugin(t, cores[0].Core, "postgresql-database-plugin", consts.PluginTypeDatabase, "TestBackend_PluginMain_Postgres", []string{}, "")
|
2019-07-05 18:57:01 +00:00
|
|
|
vault.TestAddTestPlugin(t, cores[0].Core, "mongodb-database-plugin", consts.PluginTypeDatabase, "TestBackend_PluginMain_Mongo", []string{}, "")
|
2020-10-22 23:20:17 +00:00
|
|
|
vault.TestAddTestPlugin(t, cores[0].Core, "mongodbatlas-database-plugin", consts.PluginTypeDatabase, "TestBackend_PluginMain_MongoAtlas", []string{}, "")
|
2017-04-07 22:50:03 +00:00
|
|
|
|
2017-07-03 18:54:01 +00:00
|
|
|
return cluster, sys
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
|
Combined Database Backend: Static Accounts (#6834)
* Add priority queue to sdk
* fix issue of storing pointers and now copy
* update to use copy structure
* Remove file, put Item struct def. into other file
* add link
* clean up docs
* refactor internal data structure to hide heap method implementations. Other cleanup after feedback
* rename PushItem and PopItem to just Push/Pop, after encapsulating the heap methods
* updates after feedback
* refactoring/renaming
* guard against pushing a nil item
* minor updates after feedback
* Add SetCredentials, GenerateCredentials gRPC methods to combined database backend gPRC
* Initial Combined database backend implementation of static accounts and automatic rotation
* vendor updates
* initial implementation of static accounts with Combined database backend, starting with PostgreSQL implementation
* add lock and setup of rotation queue
* vendor the queue
* rebase on new method signature of queue
* remove mongo tests for now
* update default role sql
* gofmt after rebase
* cleanup after rebasing to remove checks for ErrNotFound error
* rebase cdcr-priority-queue
* vendor dependencies with 'go mod vendor'
* website database docs for Static Role support
* document the rotate-role API endpoint
* postgres specific static role docs
* use constants for paths
* updates from review
* remove dead code
* combine and clarify error message for older plugins
* Update builtin/logical/database/backend.go
Co-Authored-By: Jim Kalafut <jim@kalafut.net>
* cleanups from feedback
* code and comment cleanups
* move db.RLock higher to protect db.GenerateCredentials call
* Return output with WALID if we failed to delete the WAL
* Update builtin/logical/database/path_creds_create.go
Co-Authored-By: Jim Kalafut <jim@kalafut.net>
* updates after running 'make fmt'
* update after running 'make proto'
* Update builtin/logical/database/path_roles.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* Update builtin/logical/database/path_roles.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* update comment and remove and rearrange some dead code
* Update website/source/api/secret/databases/index.html.md
Co-Authored-By: Jim Kalafut <jim@kalafut.net>
* cleanups after review
* Update sdk/database/dbplugin/grpc_transport.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* code cleanup after feedback
* remove PasswordLastSet; it's not used
* document GenerateCredentials and SetCredentials
* Update builtin/logical/database/path_rotate_credentials.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* wrap pop and popbykey in backend methods to protect against nil cred rotation queue
* use strings.HasPrefix instead of direct equality check for path
* Forgot to commit this
* updates after feedback
* re-purpose an outdated test to now check that static and dynamic roles cannot share a name
* check for unique name across dynamic and static roles
* refactor loadStaticWALs to return a map of name/setCredentialsWAL struct to consolidate where we're calling set credentials
* remove commented out code
* refactor to have loadstaticwals filter out wals for roles that no longer exist
* return error if nil input given
* add nil check for input into setStaticAccount
* Update builtin/logical/database/path_roles.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* add constant for queue tick time in seconds, used for comparrison in updates
* Update builtin/logical/database/path_roles.go
Co-Authored-By: Jim Kalafut <jim@kalafut.net>
* code cleanup after review
* remove misplaced code comment
* remove commented out code
* create a queue in the Factory method, even if it's never used
* update path_roles to use a common set of fields, with specific overrides for dynamic/static roles by type
* document new method
* move rotation things into a specific file
* rename test file and consolidate some static account tests
* Update builtin/logical/database/path_roles.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* Update builtin/logical/database/rotation.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* Update builtin/logical/database/rotation.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* Update builtin/logical/database/rotation.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* Update builtin/logical/database/rotation.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* Update builtin/logical/database/rotation.go
Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com>
* update code comments, method names, and move more methods into rotation.go
* update comments to be capitalized
* remove the item from the queue before we try to destroy it
* findStaticWAL returns an error
* use lowercase keys when encoding WAL entries
* small cleanups
* remove vestigial static account check
* remove redundant DeleteWAL call in populate queue
* if we error on loading role, push back to queue with 10 second backoff
* poll in initqueue to make sure the backend is setup and can write/delete data
* add revoke_user_on_delete flag to allow users to opt-in to revoking the static database user on delete of the Vault role. Default false
* add code comments on read-only loop
* code comment updates
* re-push if error returned from find static wal
* add locksutil and acquire locks when pop'ing from the queue
* grab exclusive locks for updating static roles
* Add SetCredentials and GenerateCredentials stubs to mockPlugin
* add a switch in initQueue to listen for cancelation
* remove guard on zero time, it should have no affect
* create a new context in Factory to pass on and use for closing the backend queue
* restore master copy of vendor dir
2019-06-19 19:45:39 +00:00
|
|
|
func TestBackend_PluginMain_Postgres(t *testing.T) {
|
2020-10-22 21:43:19 +00:00
|
|
|
if os.Getenv(pluginutil.PluginVaultVersionEnv) == "" {
|
2017-04-07 22:50:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:43:19 +00:00
|
|
|
dbType, err := postgresql.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to initialize postgres: %s", err)
|
2017-07-31 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 21:43:19 +00:00
|
|
|
v5.Serve(dbType.(v5.Database))
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 18:57:01 +00:00
|
|
|
func TestBackend_PluginMain_Mongo(t *testing.T) {
|
2020-10-22 21:43:19 +00:00
|
|
|
if os.Getenv(pluginutil.PluginVaultVersionEnv) == "" {
|
2019-07-05 18:57:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:43:19 +00:00
|
|
|
dbType, err := mongodb.New()
|
2019-07-05 18:57:01 +00:00
|
|
|
if err != nil {
|
2020-10-22 21:43:19 +00:00
|
|
|
t.Fatalf("Failed to initialize mongodb: %s", err)
|
2019-07-05 18:57:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 21:43:19 +00:00
|
|
|
v5.Serve(dbType.(v5.Database))
|
2020-05-29 18:21:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 23:20:17 +00:00
|
|
|
func TestBackend_PluginMain_MongoAtlas(t *testing.T) {
|
|
|
|
if os.Getenv(pluginutil.PluginUnwrapTokenEnv) == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dbType, err := mongodbatlas.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to initialize mongodbatlas: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
v5.Serve(dbType.(v5.Database))
|
|
|
|
}
|
2020-10-22 21:43:19 +00:00
|
|
|
|
2017-12-19 03:38:47 +00:00
|
|
|
func TestBackend_RoleUpgrade(t *testing.T) {
|
|
|
|
storage := &logical.InmemStorage{}
|
|
|
|
backend := &databaseBackend{}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
roleExpected := &roleEntry{
|
2020-10-22 21:43:19 +00:00
|
|
|
Statements: v4.Statements{
|
2017-12-19 03:38:47 +00:00
|
|
|
CreationStatements: "test",
|
2018-03-21 19:05:56 +00:00
|
|
|
Creation: []string{"test"},
|
2017-12-19 03:38:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("role/test", &roleEntry{
|
2020-10-22 21:43:19 +00:00
|
|
|
Statements: v4.Statements{
|
2018-03-21 19:05:56 +00:00
|
|
|
CreationStatements: "test",
|
|
|
|
},
|
|
|
|
})
|
2017-12-19 03:38:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := storage.Put(context.Background(), entry); err != nil {
|
2017-12-19 03:38:47 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
role, err := backend.Role(context.Background(), storage, "test")
|
2017-12-19 03:38:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
if !reflect.DeepEqual(role, roleExpected) {
|
|
|
|
t.Fatalf("bad role %#v, %#v", role, roleExpected)
|
2017-12-19 03:38:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrade case
|
|
|
|
badJSON := `{"statments":{"creation_statments":"test","revocation_statements":"","rollback_statements":"","renew_statements":""}}`
|
|
|
|
entry = &logical.StorageEntry{
|
|
|
|
Key: "role/test",
|
|
|
|
Value: []byte(badJSON),
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := storage.Put(context.Background(), entry); err != nil {
|
2017-12-19 03:38:47 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
role, err = backend.Role(context.Background(), storage, "test")
|
2017-12-19 03:38:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
if !reflect.DeepEqual(role, roleExpected) {
|
|
|
|
t.Fatalf("bad role %#v, %#v", role, roleExpected)
|
2017-12-19 03:38:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 22:50:03 +00:00
|
|
|
func TestBackend_config_connection(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
2017-07-03 18:54:01 +00:00
|
|
|
|
|
|
|
cluster, sys := getCluster(t)
|
2017-07-31 15:28:06 +00:00
|
|
|
defer cluster.Cleanup()
|
2017-04-07 22:50:03 +00:00
|
|
|
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
|
|
|
config.System = sys
|
2018-06-19 15:24:28 +00:00
|
|
|
lb, err := Factory(context.Background(), config)
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-19 15:24:28 +00:00
|
|
|
b, ok := lb.(*databaseBackend)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("could not convert to database backend")
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
defer b.Cleanup(context.Background())
|
2017-04-07 22:50:03 +00:00
|
|
|
|
2018-06-19 15:24:28 +00:00
|
|
|
// Test creation
|
|
|
|
{
|
|
|
|
configData := map[string]interface{}{
|
|
|
|
"connection_url": "sample_connection_url",
|
|
|
|
"someotherdata": "testing",
|
|
|
|
"plugin_name": "postgresql-database-plugin",
|
|
|
|
"verify_connection": false,
|
|
|
|
"allowed_roles": []string{"*"},
|
|
|
|
"name": "plugin-test",
|
|
|
|
}
|
2017-04-07 22:50:03 +00:00
|
|
|
|
2018-06-19 15:24:28 +00:00
|
|
|
configReq := &logical.Request{
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "config/plugin-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: configData,
|
|
|
|
}
|
2017-04-07 22:50:03 +00:00
|
|
|
|
2018-06-19 15:24:28 +00:00
|
|
|
exists, err := b.connectionExistenceCheck()(context.Background(), configReq, &framework.FieldData{
|
|
|
|
Raw: configData,
|
|
|
|
Schema: pathConfigurePluginConnection(b).Fields,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
t.Fatal("expected not exists")
|
|
|
|
}
|
|
|
|
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), configReq)
|
2018-06-19 15:24:28 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"plugin_name": "postgresql-database-plugin",
|
|
|
|
"connection_details": map[string]interface{}{
|
|
|
|
"connection_url": "sample_connection_url",
|
|
|
|
"someotherdata": "testing",
|
|
|
|
},
|
|
|
|
"allowed_roles": []string{"*"},
|
|
|
|
"root_credentials_rotate_statements": []string{},
|
2020-09-18 21:10:54 +00:00
|
|
|
"password_policy": "",
|
2018-06-19 15:24:28 +00:00
|
|
|
}
|
|
|
|
configReq.Operation = logical.ReadOperation
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), configReq)
|
2018-06-19 15:24:28 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(resp.Data["connection_details"].(map[string]interface{}), "name")
|
|
|
|
if !reflect.DeepEqual(expected, resp.Data) {
|
|
|
|
t.Fatalf("bad: expected:%#v\nactual:%#v\n", expected, resp.Data)
|
|
|
|
}
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
2018-06-19 15:24:28 +00:00
|
|
|
|
|
|
|
// Test existence check and an update to a single connection detail parameter
|
|
|
|
{
|
|
|
|
configData := map[string]interface{}{
|
|
|
|
"connection_url": "sample_convection_url",
|
|
|
|
"verify_connection": false,
|
|
|
|
"name": "plugin-test",
|
|
|
|
}
|
|
|
|
|
|
|
|
configReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/plugin-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: configData,
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := b.connectionExistenceCheck()(context.Background(), configReq, &framework.FieldData{
|
|
|
|
Raw: configData,
|
|
|
|
Schema: pathConfigurePluginConnection(b).Fields,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
t.Fatal("expected exists")
|
|
|
|
}
|
|
|
|
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), configReq)
|
2018-06-19 15:24:28 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"plugin_name": "postgresql-database-plugin",
|
|
|
|
"connection_details": map[string]interface{}{
|
|
|
|
"connection_url": "sample_convection_url",
|
|
|
|
"someotherdata": "testing",
|
|
|
|
},
|
|
|
|
"allowed_roles": []string{"*"},
|
|
|
|
"root_credentials_rotate_statements": []string{},
|
2020-09-18 21:10:54 +00:00
|
|
|
"password_policy": "",
|
2018-06-19 15:24:28 +00:00
|
|
|
}
|
|
|
|
configReq.Operation = logical.ReadOperation
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), configReq)
|
2018-06-19 15:24:28 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(resp.Data["connection_details"].(map[string]interface{}), "name")
|
|
|
|
if !reflect.DeepEqual(expected, resp.Data) {
|
|
|
|
t.Fatalf("bad: expected:%#v\nactual:%#v\n", expected, resp.Data)
|
|
|
|
}
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 15:24:28 +00:00
|
|
|
// Test an update to a non-details value
|
|
|
|
{
|
|
|
|
configData := map[string]interface{}{
|
|
|
|
"verify_connection": false,
|
|
|
|
"allowed_roles": []string{"flu", "barre"},
|
|
|
|
"name": "plugin-test",
|
|
|
|
}
|
|
|
|
|
|
|
|
configReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/plugin-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: configData,
|
|
|
|
}
|
|
|
|
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), configReq)
|
2018-06-19 15:24:28 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"plugin_name": "postgresql-database-plugin",
|
|
|
|
"connection_details": map[string]interface{}{
|
|
|
|
"connection_url": "sample_convection_url",
|
|
|
|
"someotherdata": "testing",
|
|
|
|
},
|
|
|
|
"allowed_roles": []string{"flu", "barre"},
|
|
|
|
"root_credentials_rotate_statements": []string{},
|
2020-09-18 21:10:54 +00:00
|
|
|
"password_policy": "",
|
2018-06-19 15:24:28 +00:00
|
|
|
}
|
|
|
|
configReq.Operation = logical.ReadOperation
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), configReq)
|
2018-06-19 15:24:28 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(resp.Data["connection_details"].(map[string]interface{}), "name")
|
|
|
|
if !reflect.DeepEqual(expected, resp.Data) {
|
|
|
|
t.Fatalf("bad: expected:%#v\nactual:%#v\n", expected, resp.Data)
|
|
|
|
}
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
2017-06-07 14:03:17 +00:00
|
|
|
|
2018-06-19 15:24:28 +00:00
|
|
|
req := &logical.Request{
|
|
|
|
Operation: logical.ListOperation,
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Path: "config/",
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-06-07 14:03:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
keys := resp.Data["keys"].([]string)
|
|
|
|
key := keys[0]
|
|
|
|
if key != "plugin-test" {
|
|
|
|
t.Fatalf("bad key: %q", key)
|
|
|
|
}
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
func TestBackend_BadConnectionString(t *testing.T) {
|
|
|
|
cluster, sys := getCluster(t)
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
|
|
|
config.System = sys
|
|
|
|
|
|
|
|
b, err := Factory(context.Background(), config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer b.Cleanup(context.Background())
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, _ := postgreshelper.PrepareTestContainer(t, "latest")
|
2018-03-21 19:05:56 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
respCheck := func(req *logical.Request) {
|
|
|
|
t.Helper()
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err := b.HandleRequest(namespace.RootContext(nil), req)
|
2018-03-21 19:05:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected error, resp:%#v", resp)
|
|
|
|
}
|
|
|
|
err = resp.Error()
|
|
|
|
if strings.Contains(err.Error(), "localhost") {
|
|
|
|
t.Fatalf("error should not contain connection info")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure a connection
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"connection_url": "postgresql://:pw@[localhost",
|
|
|
|
"plugin_name": "postgresql-database-plugin",
|
|
|
|
"allowed_roles": []string{"plugin-role-test"},
|
|
|
|
}
|
|
|
|
req := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/plugin-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
respCheck(req)
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
|
2017-04-07 22:50:03 +00:00
|
|
|
func TestBackend_basic(t *testing.T) {
|
2017-07-03 18:54:01 +00:00
|
|
|
cluster, sys := getCluster(t)
|
2017-07-31 15:28:06 +00:00
|
|
|
defer cluster.Cleanup()
|
2017-04-07 22:50:03 +00:00
|
|
|
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
|
|
|
config.System = sys
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
defer b.Cleanup(context.Background())
|
2017-04-07 22:50:03 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "latest")
|
2017-04-07 22:50:03 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Configure a connection
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"plugin_name": "postgresql-database-plugin",
|
2017-04-25 18:48:24 +00:00
|
|
|
"allowed_roles": []string{"plugin-role-test"},
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
req := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/plugin-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err := b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a role
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"db_name": "plugin-test",
|
|
|
|
"creation_statements": testRole,
|
|
|
|
"max_ttl": "10m",
|
|
|
|
}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "roles/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
// Get creds
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
credsResp, err := b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil || (credsResp != nil && credsResp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, credsResp)
|
|
|
|
}
|
2018-01-18 06:43:38 +00:00
|
|
|
// Update the role with no max ttl
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"db_name": "plugin-test",
|
|
|
|
"creation_statements": testRole,
|
|
|
|
"default_ttl": "5m",
|
|
|
|
"max_ttl": 0,
|
|
|
|
}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "roles/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-01-18 06:43:38 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
// Get creds
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
credsResp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-01-18 06:43:38 +00:00
|
|
|
if err != nil || (credsResp != nil && credsResp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, credsResp)
|
|
|
|
}
|
|
|
|
// Test for #3812
|
|
|
|
if credsResp.Secret.TTL != 5*time.Minute {
|
|
|
|
t.Fatalf("unexpected TTL of %d", credsResp.Secret.TTL)
|
|
|
|
}
|
|
|
|
// Update the role with a max ttl
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"db_name": "plugin-test",
|
|
|
|
"creation_statements": testRole,
|
|
|
|
"default_ttl": "5m",
|
|
|
|
"max_ttl": "10m",
|
|
|
|
}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "roles/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-01-18 06:43:38 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
2017-04-07 22:50:03 +00:00
|
|
|
|
2018-06-19 15:24:28 +00:00
|
|
|
// Get creds and revoke when the role stays in existence
|
|
|
|
{
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
credsResp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-06-19 15:24:28 +00:00
|
|
|
if err != nil || (credsResp != nil && credsResp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, credsResp)
|
|
|
|
}
|
|
|
|
// Test for #3812
|
|
|
|
if credsResp.Secret.TTL != 5*time.Minute {
|
|
|
|
t.Fatalf("unexpected TTL of %d", credsResp.Secret.TTL)
|
|
|
|
}
|
|
|
|
if !testCredsExist(t, credsResp, connURL) {
|
|
|
|
t.Fatalf("Creds should exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revoke creds
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), &logical.Request{
|
2018-06-19 15:24:28 +00:00
|
|
|
Operation: logical.RevokeOperation,
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Secret: &logical.Secret{
|
|
|
|
InternalData: map[string]interface{}{
|
|
|
|
"secret_type": "creds",
|
|
|
|
"username": credsResp.Data["username"],
|
|
|
|
"role": "plugin-role-test",
|
|
|
|
},
|
2017-04-07 22:50:03 +00:00
|
|
|
},
|
2018-06-19 15:24:28 +00:00
|
|
|
})
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if testCredsExist(t, credsResp, connURL) {
|
|
|
|
t.Fatalf("Creds should not exist")
|
|
|
|
}
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 15:24:28 +00:00
|
|
|
// Get creds and revoke using embedded revocation data
|
|
|
|
{
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
credsResp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-06-19 15:24:28 +00:00
|
|
|
if err != nil || (credsResp != nil && credsResp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, credsResp)
|
|
|
|
}
|
|
|
|
if !testCredsExist(t, credsResp, connURL) {
|
|
|
|
t.Fatalf("Creds should exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete role, forcing us to rely on embedded data
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.DeleteOperation,
|
|
|
|
Path: "roles/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-06-19 15:24:28 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revoke creds
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), &logical.Request{
|
2018-06-19 15:24:28 +00:00
|
|
|
Operation: logical.RevokeOperation,
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Secret: &logical.Secret{
|
|
|
|
InternalData: map[string]interface{}{
|
|
|
|
"secret_type": "creds",
|
|
|
|
"username": credsResp.Data["username"],
|
|
|
|
"role": "plugin-role-test",
|
|
|
|
"db_name": "plugin-test",
|
2018-09-05 15:41:37 +00:00
|
|
|
"revocation_statements": nil,
|
2018-06-19 15:24:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if testCredsExist(t, credsResp, connURL) {
|
|
|
|
t.Fatalf("Creds should not exist")
|
|
|
|
}
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:16 +00:00
|
|
|
func TestBackend_connectionCrud(t *testing.T) {
|
2017-07-03 18:54:01 +00:00
|
|
|
cluster, sys := getCluster(t)
|
2017-07-31 15:28:06 +00:00
|
|
|
defer cluster.Cleanup()
|
2017-04-07 22:50:03 +00:00
|
|
|
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
|
|
|
config.System = sys
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
defer b.Cleanup(context.Background())
|
2017-04-07 22:50:03 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "latest")
|
2017-04-07 22:50:03 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Configure a connection
|
|
|
|
data := map[string]interface{}{
|
2017-04-10 17:35:16 +00:00
|
|
|
"connection_url": "test",
|
|
|
|
"plugin_name": "postgresql-database-plugin",
|
|
|
|
"verify_connection": false,
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
req := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/plugin-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err := b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a role
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"db_name": "plugin-test",
|
|
|
|
"creation_statements": testRole,
|
|
|
|
"revocation_statements": defaultRevocationSQL,
|
|
|
|
"default_ttl": "5m",
|
|
|
|
"max_ttl": "10m",
|
|
|
|
}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "roles/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:16 +00:00
|
|
|
// Update the connection
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"plugin_name": "postgresql-database-plugin",
|
2017-04-25 18:48:24 +00:00
|
|
|
"allowed_roles": []string{"plugin-role-test"},
|
2018-04-07 15:06:04 +00:00
|
|
|
"username": "postgres",
|
|
|
|
"password": "secret",
|
2020-11-19 10:58:55 +00:00
|
|
|
"private_key": "PRIVATE_KEY",
|
2017-04-10 17:35:16 +00:00
|
|
|
}
|
2017-04-07 22:50:03 +00:00
|
|
|
req = &logical.Request{
|
2017-04-10 17:35:16 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/plugin-test",
|
2017-04-07 22:50:03 +00:00
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
2018-04-07 15:06:04 +00:00
|
|
|
if len(resp.Warnings) != 1 {
|
|
|
|
t.Fatalf("expected warning about password in url %s, resp:%#v\n", connURL, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Operation = logical.ReadOperation
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-04-07 15:06:04 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
2020-11-19 10:58:55 +00:00
|
|
|
returnedConnectionDetails := resp.Data["connection_details"].(map[string]interface{})
|
|
|
|
if strings.Contains(returnedConnectionDetails["connection_url"].(string), "secret") {
|
2018-04-07 15:06:04 +00:00
|
|
|
t.Fatal("password should not be found in the connection url")
|
|
|
|
}
|
2020-11-19 10:58:55 +00:00
|
|
|
// Covered by the filled out `expected` value below, but be explicit about this requirement.
|
|
|
|
if _, exists := returnedConnectionDetails["password"]; exists {
|
|
|
|
t.Fatal("password should NOT be found in the returned config")
|
|
|
|
}
|
|
|
|
if _, exists := returnedConnectionDetails["private_key"]; exists {
|
|
|
|
t.Fatal("private_key should NOT be found in the returned config")
|
|
|
|
}
|
2018-04-07 15:06:04 +00:00
|
|
|
|
|
|
|
// Replace connection url with templated version
|
|
|
|
req.Operation = logical.UpdateOperation
|
|
|
|
connURL = strings.Replace(connURL, "postgres:secret", "{{username}}:{{password}}", -1)
|
|
|
|
data["connection_url"] = connURL
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-04-07 15:06:04 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
2017-04-07 22:50:03 +00:00
|
|
|
|
2017-04-10 17:35:16 +00:00
|
|
|
// Read connection
|
|
|
|
expected := map[string]interface{}{
|
2018-04-07 15:06:04 +00:00
|
|
|
"plugin_name": "postgresql-database-plugin",
|
|
|
|
"connection_details": map[string]interface{}{
|
|
|
|
"username": "postgres",
|
|
|
|
"connection_url": connURL,
|
|
|
|
},
|
2018-03-21 19:05:56 +00:00
|
|
|
"allowed_roles": []string{"plugin-role-test"},
|
2018-06-19 15:24:28 +00:00
|
|
|
"root_credentials_rotate_statements": []string(nil),
|
2020-09-18 21:10:54 +00:00
|
|
|
"password_policy": "",
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
2017-04-10 17:35:16 +00:00
|
|
|
req.Operation = logical.ReadOperation
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-10 17:35:16 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:16 +00:00
|
|
|
delete(resp.Data["connection_details"].(map[string]interface{}), "name")
|
2018-06-19 15:24:28 +00:00
|
|
|
if diff := deep.Equal(resp.Data, expected); diff != nil {
|
|
|
|
t.Fatal(diff)
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:16 +00:00
|
|
|
// Reset Connection
|
2017-04-07 22:50:03 +00:00
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
2017-04-10 17:35:16 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "reset/plugin-test",
|
2017-04-07 22:50:03 +00:00
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:16 +00:00
|
|
|
// Get creds
|
2017-04-07 22:50:03 +00:00
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
2017-04-10 17:35:16 +00:00
|
|
|
Path: "creds/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
credsResp, err := b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-10 17:35:16 +00:00
|
|
|
if err != nil || (credsResp != nil && credsResp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, credsResp)
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:06:04 +00:00
|
|
|
credCheckURL := dbutil.QueryHelper(connURL, map[string]string{
|
|
|
|
"username": "postgres",
|
|
|
|
"password": "secret",
|
|
|
|
})
|
|
|
|
if !testCredsExist(t, credsResp, credCheckURL) {
|
2017-04-10 17:35:16 +00:00
|
|
|
t.Fatalf("Creds should exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete Connection
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.DeleteOperation,
|
|
|
|
Path: "config/plugin-test",
|
2017-04-07 22:50:03 +00:00
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:16 +00:00
|
|
|
// Read connection
|
|
|
|
req.Operation = logical.ReadOperation
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-10 17:35:16 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
2017-04-07 22:50:03 +00:00
|
|
|
// Should be empty
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("Expected response to be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:16 +00:00
|
|
|
func TestBackend_roleCrud(t *testing.T) {
|
2017-07-03 18:54:01 +00:00
|
|
|
cluster, sys := getCluster(t)
|
2017-07-31 15:28:06 +00:00
|
|
|
defer cluster.Cleanup()
|
2017-04-07 22:50:03 +00:00
|
|
|
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
|
|
|
config.System = sys
|
|
|
|
|
2018-06-19 15:24:28 +00:00
|
|
|
lb, err := Factory(context.Background(), config)
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-19 15:24:28 +00:00
|
|
|
b, ok := lb.(*databaseBackend)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("could not convert to db backend")
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
defer b.Cleanup(context.Background())
|
2017-04-07 22:50:03 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "latest")
|
2017-04-07 22:50:03 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Configure a connection
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"plugin_name": "postgresql-database-plugin",
|
|
|
|
}
|
|
|
|
req := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/plugin-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err := b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
2018-06-19 15:24:28 +00:00
|
|
|
// Test role creation
|
|
|
|
{
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"db_name": "plugin-test",
|
|
|
|
"creation_statements": testRole,
|
|
|
|
"revocation_statements": defaultRevocationSQL,
|
|
|
|
"default_ttl": "5m",
|
|
|
|
"max_ttl": "10m",
|
|
|
|
}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "roles/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-06-19 15:24:28 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
2017-04-07 22:50:03 +00:00
|
|
|
|
2018-06-19 15:24:28 +00:00
|
|
|
// Read the role
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "roles/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-06-19 15:24:28 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:43:19 +00:00
|
|
|
expected := v4.Statements{
|
2018-06-19 15:24:28 +00:00
|
|
|
Creation: []string{strings.TrimSpace(testRole)},
|
|
|
|
Revocation: []string{strings.TrimSpace(defaultRevocationSQL)},
|
2018-12-14 14:12:26 +00:00
|
|
|
Rollback: []string{},
|
|
|
|
Renewal: []string{},
|
2018-06-19 15:24:28 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 21:43:19 +00:00
|
|
|
actual := v4.Statements{
|
2018-06-19 15:24:28 +00:00
|
|
|
Creation: resp.Data["creation_statements"].([]string),
|
|
|
|
Revocation: resp.Data["revocation_statements"].([]string),
|
|
|
|
Rollback: resp.Data["rollback_statements"].([]string),
|
|
|
|
Renewal: resp.Data["renew_statements"].([]string),
|
|
|
|
}
|
2017-04-10 17:35:16 +00:00
|
|
|
|
2018-12-14 14:12:26 +00:00
|
|
|
if diff := deep.Equal(expected, actual); diff != nil {
|
|
|
|
t.Fatal(diff)
|
2018-06-19 15:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if diff := deep.Equal(resp.Data["db_name"], "plugin-test"); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
if diff := deep.Equal(resp.Data["default_ttl"], float64(300)); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
if diff := deep.Equal(resp.Data["max_ttl"], float64(600)); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 16:00:52 +00:00
|
|
|
// Test role modification of TTL
|
2018-06-19 15:24:28 +00:00
|
|
|
{
|
|
|
|
data = map[string]interface{}{
|
2018-08-10 16:00:52 +00:00
|
|
|
"name": "plugin-role-test",
|
|
|
|
"max_ttl": "7m",
|
2018-06-19 15:24:28 +00:00
|
|
|
}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "roles/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-06-19 15:24:28 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the role
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "roles/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-06-19 15:24:28 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:43:19 +00:00
|
|
|
expected := v4.Statements{
|
2018-06-19 15:24:28 +00:00
|
|
|
Creation: []string{strings.TrimSpace(testRole)},
|
|
|
|
Revocation: []string{strings.TrimSpace(defaultRevocationSQL)},
|
2018-12-14 14:12:26 +00:00
|
|
|
Rollback: []string{},
|
|
|
|
Renewal: []string{},
|
2018-06-19 15:24:28 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 21:43:19 +00:00
|
|
|
actual := v4.Statements{
|
2018-06-19 15:24:28 +00:00
|
|
|
Creation: resp.Data["creation_statements"].([]string),
|
|
|
|
Revocation: resp.Data["revocation_statements"].([]string),
|
|
|
|
Rollback: resp.Data["rollback_statements"].([]string),
|
|
|
|
Renewal: resp.Data["renew_statements"].([]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Fatalf("Statements did not match, expected %#v, got %#v", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := deep.Equal(resp.Data["db_name"], "plugin-test"); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
if diff := deep.Equal(resp.Data["default_ttl"], float64(300)); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
if diff := deep.Equal(resp.Data["max_ttl"], float64(420)); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 16:00:52 +00:00
|
|
|
// Test role modification of statements
|
|
|
|
{
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"name": "plugin-role-test",
|
|
|
|
"creation_statements": []string{testRole, testRole},
|
|
|
|
"revocation_statements": []string{defaultRevocationSQL, defaultRevocationSQL},
|
|
|
|
"rollback_statements": testRole,
|
|
|
|
"renew_statements": defaultRevocationSQL,
|
|
|
|
}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "roles/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
resp, err = b.HandleRequest(context.Background(), req)
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the role
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "roles/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
resp, err = b.HandleRequest(context.Background(), req)
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:43:19 +00:00
|
|
|
expected := v4.Statements{
|
2018-08-10 16:00:52 +00:00
|
|
|
Creation: []string{strings.TrimSpace(testRole), strings.TrimSpace(testRole)},
|
|
|
|
Rollback: []string{strings.TrimSpace(testRole)},
|
|
|
|
Revocation: []string{strings.TrimSpace(defaultRevocationSQL), strings.TrimSpace(defaultRevocationSQL)},
|
|
|
|
Renewal: []string{strings.TrimSpace(defaultRevocationSQL)},
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:43:19 +00:00
|
|
|
actual := v4.Statements{
|
2018-08-10 16:00:52 +00:00
|
|
|
Creation: resp.Data["creation_statements"].([]string),
|
|
|
|
Revocation: resp.Data["revocation_statements"].([]string),
|
|
|
|
Rollback: resp.Data["rollback_statements"].([]string),
|
|
|
|
Renewal: resp.Data["renew_statements"].([]string),
|
|
|
|
}
|
|
|
|
|
2018-12-14 14:12:26 +00:00
|
|
|
if diff := deep.Equal(expected, actual); diff != nil {
|
|
|
|
t.Fatal(diff)
|
2018-08-10 16:00:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if diff := deep.Equal(resp.Data["db_name"], "plugin-test"); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
if diff := deep.Equal(resp.Data["default_ttl"], float64(300)); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
if diff := deep.Equal(resp.Data["max_ttl"], float64(420)); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:16 +00:00
|
|
|
// Delete the role
|
2017-04-07 22:50:03 +00:00
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
2017-04-10 17:35:16 +00:00
|
|
|
Operation: logical.DeleteOperation,
|
|
|
|
Path: "roles/plugin-role-test",
|
2017-04-07 22:50:03 +00:00
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-10 17:35:16 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:16 +00:00
|
|
|
// Read the role
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "roles/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-10 17:35:16 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:16 +00:00
|
|
|
// Should be empty
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("Expected response to be nil")
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 16:43:39 +00:00
|
|
|
|
2017-04-13 17:33:34 +00:00
|
|
|
func TestBackend_allowedRoles(t *testing.T) {
|
2017-07-03 18:54:01 +00:00
|
|
|
cluster, sys := getCluster(t)
|
2017-07-31 15:28:06 +00:00
|
|
|
defer cluster.Cleanup()
|
2017-04-13 17:33:34 +00:00
|
|
|
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
|
|
|
config.System = sys
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2017-04-13 17:33:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
defer b.Cleanup(context.Background())
|
2017-04-13 17:33:34 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "latest")
|
2017-04-13 17:33:34 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Configure a connection
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"plugin_name": "postgresql-database-plugin",
|
|
|
|
}
|
|
|
|
req := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/plugin-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err := b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-13 17:33:34 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a denied and an allowed role
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"db_name": "plugin-test",
|
|
|
|
"creation_statements": testRole,
|
|
|
|
"default_ttl": "5m",
|
|
|
|
"max_ttl": "10m",
|
|
|
|
}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "roles/denied",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-13 17:33:34 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"db_name": "plugin-test",
|
|
|
|
"creation_statements": testRole,
|
|
|
|
"default_ttl": "5m",
|
|
|
|
"max_ttl": "10m",
|
|
|
|
}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "roles/allowed",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-13 17:33:34 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get creds from denied role, should fail
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/denied",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
credsResp, err := b.HandleRequest(namespace.RootContext(nil), req)
|
2019-02-07 19:16:23 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error because role is denied")
|
2017-04-13 17:33:34 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 20:24:25 +00:00
|
|
|
// update connection with glob allowed roles connection
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"plugin_name": "postgresql-database-plugin",
|
|
|
|
"allowed_roles": "allow*",
|
|
|
|
}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/plugin-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-10-30 20:24:25 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get creds, should work.
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/allowed",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
credsResp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-10-30 20:24:25 +00:00
|
|
|
if err != nil || (credsResp != nil && credsResp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, credsResp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !testCredsExist(t, credsResp, connURL) {
|
|
|
|
t.Fatalf("Creds should exist")
|
|
|
|
}
|
|
|
|
|
2017-04-25 18:48:24 +00:00
|
|
|
// update connection with * allowed roles connection
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"plugin_name": "postgresql-database-plugin",
|
|
|
|
"allowed_roles": "*",
|
|
|
|
}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/plugin-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-25 18:48:24 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get creds, should work.
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/allowed",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
credsResp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-25 18:48:24 +00:00
|
|
|
if err != nil || (credsResp != nil && credsResp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, credsResp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !testCredsExist(t, credsResp, connURL) {
|
|
|
|
t.Fatalf("Creds should exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
// update connection with allowed roles
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"plugin_name": "postgresql-database-plugin",
|
|
|
|
"allowed_roles": "allow, allowed",
|
|
|
|
}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/plugin-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-25 18:48:24 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get creds from denied role, should fail
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/denied",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
credsResp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2019-02-07 19:16:23 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error because role is denied")
|
2017-04-25 18:48:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 17:33:34 +00:00
|
|
|
// Get creds from allowed role, should work.
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/allowed",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
credsResp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2017-04-13 17:33:34 +00:00
|
|
|
if err != nil || (credsResp != nil && credsResp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, credsResp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !testCredsExist(t, credsResp, connURL) {
|
|
|
|
t.Fatalf("Creds should exist")
|
|
|
|
}
|
|
|
|
}
|
2017-04-07 22:50:03 +00:00
|
|
|
|
2018-04-08 17:34:59 +00:00
|
|
|
func TestBackend_RotateRootCredentials(t *testing.T) {
|
|
|
|
cluster, sys := getCluster(t)
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
|
|
|
config.System = sys
|
|
|
|
|
|
|
|
b, err := Factory(context.Background(), config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer b.Cleanup(context.Background())
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "latest")
|
2018-04-08 17:34:59 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connURL = strings.Replace(connURL, "postgres:secret", "{{username}}:{{password}}", -1)
|
|
|
|
|
|
|
|
// Configure a connection
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"plugin_name": "postgresql-database-plugin",
|
|
|
|
"allowed_roles": []string{"plugin-role-test"},
|
|
|
|
"username": "postgres",
|
|
|
|
"password": "secret",
|
|
|
|
}
|
|
|
|
req := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/plugin-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err := b.HandleRequest(namespace.RootContext(nil), req)
|
2018-04-08 17:34:59 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a role
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"db_name": "plugin-test",
|
|
|
|
"creation_statements": testRole,
|
|
|
|
"max_ttl": "10m",
|
|
|
|
}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "roles/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-04-08 17:34:59 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
// Get creds
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
credsResp, err := b.HandleRequest(namespace.RootContext(nil), req)
|
2018-04-08 17:34:59 +00:00
|
|
|
if err != nil || (credsResp != nil && credsResp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, credsResp)
|
|
|
|
}
|
|
|
|
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "rotate-root/plugin-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-04-08 17:34:59 +00:00
|
|
|
if err != nil || (credsResp != nil && credsResp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, credsResp)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbConfig, err := b.(*databaseBackend).DatabaseConfig(context.Background(), config.StorageView, "plugin-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %#v", err)
|
|
|
|
}
|
|
|
|
if dbConfig.ConnectionDetails["password"].(string) == "secret" {
|
|
|
|
t.Fatal("root credentials not rotated")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get creds to make sure it still works
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/plugin-role-test",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: data,
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
credsResp, err = b.HandleRequest(namespace.RootContext(nil), req)
|
2018-04-08 17:34:59 +00:00
|
|
|
if err != nil || (credsResp != nil && credsResp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, credsResp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:16 +00:00
|
|
|
func testCredsExist(t *testing.T, resp *logical.Response, connURL string) bool {
|
2018-04-07 15:06:04 +00:00
|
|
|
t.Helper()
|
2017-04-07 22:50:03 +00:00
|
|
|
var d struct {
|
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
log.Printf("[TRACE] Generated credentials: %v", d)
|
|
|
|
conn, err := pq.ParseURL(connURL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn += " timezone=utc"
|
|
|
|
|
|
|
|
db, err := sql.Open("postgres", conn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
returnedRows := func() int {
|
|
|
|
stmt, err := db.Prepare("SELECT DISTINCT schemaname FROM pg_tables WHERE has_table_privilege($1, 'information_schema.role_column_grants', 'select');")
|
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
rows, err := stmt.Query(d.Username)
|
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for rows.Next() {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:16 +00:00
|
|
|
return returnedRows() == 2
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const testRole = `
|
|
|
|
CREATE ROLE "{{name}}" WITH
|
|
|
|
LOGIN
|
|
|
|
PASSWORD '{{password}}'
|
|
|
|
VALID UNTIL '{{expiration}}';
|
|
|
|
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "{{name}}";
|
|
|
|
`
|
|
|
|
|
|
|
|
const defaultRevocationSQL = `
|
|
|
|
REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM {{name}};
|
|
|
|
REVOKE ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public FROM {{name}};
|
|
|
|
REVOKE USAGE ON SCHEMA public FROM {{name}};
|
|
|
|
|
|
|
|
DROP ROLE IF EXISTS {{name}};
|
|
|
|
`
|