2017-04-13 20:48:32 +00:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
2017-12-14 22:03:11 +00:00
|
|
|
"context"
|
2017-04-13 20:48:32 +00:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-04-22 16:26:10 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/docker"
|
2019-04-15 18:10:07 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/database/dbplugin"
|
2018-07-11 21:49:13 +00:00
|
|
|
"github.com/ory/dockertest"
|
2017-04-13 20:48:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testPostgresImagePull sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
func preparePostgresTestContainer(t *testing.T) (cleanup func(), retURL string) {
|
|
|
|
if os.Getenv("PG_URL") != "" {
|
|
|
|
return func() {}, os.Getenv("PG_URL")
|
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := dockertest.NewPool("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resource, err := pool.Run("postgres", "latest", []string{"POSTGRES_PASSWORD=secret", "POSTGRES_DB=database"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start local PostgreSQL docker container: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup = func() {
|
2019-04-22 16:26:10 +00:00
|
|
|
docker.CleanupResource(t, pool, resource)
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
retURL = fmt.Sprintf("postgres://postgres:secret@localhost:%s/database?sslmode=disable", resource.GetPort("5432/tcp"))
|
|
|
|
|
|
|
|
// exponential backoff-retry
|
|
|
|
if err = pool.Retry(func() error {
|
|
|
|
var err error
|
|
|
|
var db *sql.DB
|
|
|
|
db, err = sql.Open("postgres", retURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-26 15:28:58 +00:00
|
|
|
defer db.Close()
|
2017-04-13 20:48:32 +00:00
|
|
|
return db.Ping()
|
|
|
|
}); err != nil {
|
2018-08-07 16:56:33 +00:00
|
|
|
cleanup()
|
2017-04-13 20:48:32 +00:00
|
|
|
t.Fatalf("Could not connect to PostgreSQL docker container: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostgreSQL_Initialize(t *testing.T) {
|
|
|
|
cleanup, connURL := preparePostgresTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
2017-06-15 01:59:27 +00:00
|
|
|
"connection_url": connURL,
|
|
|
|
"max_open_connections": 5,
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
if !db.Initialized {
|
2019-03-19 13:32:45 +00:00
|
|
|
t.Fatal("Database should be initialized")
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-06-15 01:59:27 +00:00
|
|
|
|
|
|
|
// Test decoding a string value for max_open_connections
|
|
|
|
connectionDetails = map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"max_open_connections": "5",
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
_, err = db.Init(context.Background(), connectionDetails, true)
|
2017-06-15 01:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostgreSQL_CreateUser(t *testing.T) {
|
|
|
|
cleanup, connURL := preparePostgresTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
usernameConfig := dbplugin.UsernameConfig{
|
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// Test with no configured Creation Statement
|
2017-12-14 22:03:11 +00:00
|
|
|
_, _, err = db.CreateUser(context.Background(), dbplugin.Statements{}, usernameConfig, time.Now().Add(time.Minute))
|
2017-04-13 20:48:32 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error when no creation statement is provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
statements := dbplugin.Statements{
|
2018-03-21 19:05:56 +00:00
|
|
|
Creation: []string{testPostgresRole},
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err := db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(time.Minute))
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
statements.Creation = []string{testPostgresReadOnlyRole}
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err = db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(time.Minute))
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-11-30 14:35:47 +00:00
|
|
|
// Sleep to make sure we haven't expired if granularity is only down to the second
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2017-04-13 20:48:32 +00:00
|
|
|
if err = testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostgreSQL_RenewUser(t *testing.T) {
|
|
|
|
cleanup, connURL := preparePostgresTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
statements := dbplugin.Statements{
|
2018-03-21 19:05:56 +00:00
|
|
|
Creation: []string{testPostgresRole},
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
usernameConfig := dbplugin.UsernameConfig{
|
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err := db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(2*time.Second))
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
err = db.RenewUser(context.Background(), statements, username, time.Now().Add(time.Minute))
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-03-19 13:32:45 +00:00
|
|
|
// Sleep longer than the initial expiration time
|
2017-04-13 20:48:32 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
if err = testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
statements.Renewal = []string{defaultPostgresRenewSQL}
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err = db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(2*time.Second))
|
2017-06-01 20:18:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
err = db.RenewUser(context.Background(), statements, username, time.Now().Add(time.Minute))
|
2017-06-01 20:18:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-03-19 13:32:45 +00:00
|
|
|
// Sleep longer than the initial expiration time
|
2017-06-01 20:18:16 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
if err = testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
func TestPostgreSQL_RotateRootCredentials(t *testing.T) {
|
|
|
|
cleanup, connURL := preparePostgresTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connURL = strings.Replace(connURL, "postgres:secret", `{{username}}:{{password}}`, -1)
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"max_open_connections": 5,
|
|
|
|
"username": "postgres",
|
|
|
|
"password": "secret",
|
|
|
|
}
|
|
|
|
|
|
|
|
db := new()
|
|
|
|
|
|
|
|
connProducer := db.SQLConnectionProducer
|
|
|
|
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !connProducer.Initialized {
|
2019-03-19 13:32:45 +00:00
|
|
|
t.Fatal("Database should be initialized")
|
2018-03-21 19:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newConf, err := db.RotateRootCredentials(context.Background(), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if newConf["password"] == "secret" {
|
|
|
|
t.Fatal("password was not updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:48:32 +00:00
|
|
|
func TestPostgreSQL_RevokeUser(t *testing.T) {
|
|
|
|
cleanup, connURL := preparePostgresTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
statements := dbplugin.Statements{
|
2018-03-21 19:05:56 +00:00
|
|
|
Creation: []string{testPostgresRole},
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
usernameConfig := dbplugin.UsernameConfig{
|
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err := db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(2*time.Second))
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// Test default revoke statements
|
2017-12-14 22:03:11 +00:00
|
|
|
err = db.RevokeUser(context.Background(), statements, username)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := testCredsExist(t, connURL, username, password); err == nil {
|
|
|
|
t.Fatal("Credentials were not revoked")
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err = db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(2*time.Second))
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test custom revoke statements
|
2018-03-21 19:05:56 +00:00
|
|
|
statements.Revocation = []string{defaultPostgresRevocationSQL}
|
2017-12-14 22:03:11 +00:00
|
|
|
err = db.RevokeUser(context.Background(), statements, username)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := testCredsExist(t, connURL, username, password); err == nil {
|
|
|
|
t.Fatal("Credentials were not revoked")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 TestPostgresSQL_SetCredentials(t *testing.T) {
|
|
|
|
cleanup, connURL := preparePostgresTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
|
|
|
|
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
password, err := db.GenerateCredentials(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
usernameConfig := dbplugin.StaticUserConfig{
|
|
|
|
Username: "test",
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with no configured Creation Statement
|
|
|
|
username, password, err := db.SetCredentials(context.Background(), dbplugin.Statements{}, usernameConfig)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
statements := dbplugin.Statements{
|
|
|
|
Creation: []string{testPostgresStaticRole},
|
|
|
|
}
|
|
|
|
// User should not exist, make sure we can create
|
|
|
|
username, password, err = db.SetCredentials(context.Background(), statements, usernameConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// call SetCredentials again, the user will already exist, password will
|
|
|
|
// change. Without rotation statements, this should use the defaults
|
|
|
|
newPassword, _ := db.GenerateCredentials(context.Background())
|
|
|
|
usernameConfig.Password = newPassword
|
|
|
|
username, password, err = db.SetCredentials(context.Background(), statements, usernameConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if password != newPassword {
|
|
|
|
t.Fatal("passwords should have changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate a new password and supply owr own rotation statements
|
|
|
|
newPassword2, _ := db.GenerateCredentials(context.Background())
|
|
|
|
usernameConfig.Password = newPassword2
|
|
|
|
statements.Rotation = []string{testPostgresStaticRoleRotate, testPostgresStaticRoleGrant}
|
|
|
|
username, password, err = db.SetCredentials(context.Background(), statements, usernameConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if password != newPassword2 {
|
|
|
|
t.Fatal("passwords should have changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:48:32 +00:00
|
|
|
func testCredsExist(t testing.TB, connURL, username, password string) error {
|
2018-03-21 19:05:56 +00:00
|
|
|
t.Helper()
|
2017-04-13 20:48:32 +00:00
|
|
|
// Log in with the new creds
|
|
|
|
connURL = strings.Replace(connURL, "postgres:secret", fmt.Sprintf("%s:%s", username, password), 1)
|
|
|
|
db, err := sql.Open("postgres", connURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
return db.Ping()
|
|
|
|
}
|
|
|
|
|
|
|
|
const testPostgresRole = `
|
|
|
|
CREATE ROLE "{{name}}" WITH
|
|
|
|
LOGIN
|
|
|
|
PASSWORD '{{password}}'
|
|
|
|
VALID UNTIL '{{expiration}}';
|
|
|
|
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "{{name}}";
|
|
|
|
`
|
|
|
|
|
|
|
|
const testPostgresReadOnlyRole = `
|
|
|
|
CREATE ROLE "{{name}}" WITH
|
|
|
|
LOGIN
|
|
|
|
PASSWORD '{{password}}'
|
|
|
|
VALID UNTIL '{{expiration}}';
|
|
|
|
GRANT SELECT ON ALL TABLES IN SCHEMA public TO "{{name}}";
|
|
|
|
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO "{{name}}";
|
|
|
|
`
|
|
|
|
|
|
|
|
const testPostgresBlockStatementRole = `
|
|
|
|
DO $$
|
|
|
|
BEGIN
|
|
|
|
IF NOT EXISTS (SELECT * FROM pg_catalog.pg_roles WHERE rolname='foo-role') THEN
|
|
|
|
CREATE ROLE "foo-role";
|
|
|
|
CREATE SCHEMA IF NOT EXISTS foo AUTHORIZATION "foo-role";
|
|
|
|
ALTER ROLE "foo-role" SET search_path = foo;
|
|
|
|
GRANT TEMPORARY ON DATABASE "postgres" TO "foo-role";
|
|
|
|
GRANT ALL PRIVILEGES ON SCHEMA foo TO "foo-role";
|
|
|
|
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA foo TO "foo-role";
|
|
|
|
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA foo TO "foo-role";
|
|
|
|
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA foo TO "foo-role";
|
|
|
|
END IF;
|
|
|
|
END
|
|
|
|
$$
|
|
|
|
|
|
|
|
CREATE ROLE "{{name}}" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}';
|
|
|
|
GRANT "foo-role" TO "{{name}}";
|
|
|
|
ALTER ROLE "{{name}}" SET search_path = foo;
|
|
|
|
GRANT CONNECT ON DATABASE "postgres" TO "{{name}}";
|
|
|
|
`
|
|
|
|
|
|
|
|
var testPostgresBlockStatementRoleSlice = []string{
|
|
|
|
`
|
|
|
|
DO $$
|
|
|
|
BEGIN
|
|
|
|
IF NOT EXISTS (SELECT * FROM pg_catalog.pg_roles WHERE rolname='foo-role') THEN
|
|
|
|
CREATE ROLE "foo-role";
|
|
|
|
CREATE SCHEMA IF NOT EXISTS foo AUTHORIZATION "foo-role";
|
|
|
|
ALTER ROLE "foo-role" SET search_path = foo;
|
|
|
|
GRANT TEMPORARY ON DATABASE "postgres" TO "foo-role";
|
|
|
|
GRANT ALL PRIVILEGES ON SCHEMA foo TO "foo-role";
|
|
|
|
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA foo TO "foo-role";
|
|
|
|
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA foo TO "foo-role";
|
|
|
|
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA foo TO "foo-role";
|
|
|
|
END IF;
|
|
|
|
END
|
|
|
|
$$
|
|
|
|
`,
|
|
|
|
`CREATE ROLE "{{name}}" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}';`,
|
|
|
|
`GRANT "foo-role" TO "{{name}}";`,
|
|
|
|
`ALTER ROLE "{{name}}" SET search_path = foo;`,
|
|
|
|
`GRANT CONNECT ON DATABASE "postgres" TO "{{name}}";`,
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultPostgresRevocationSQL = `
|
|
|
|
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}}";
|
|
|
|
`
|
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
|
|
|
|
|
|
|
const testPostgresStaticRole = `
|
|
|
|
CREATE ROLE "{{name}}" WITH
|
|
|
|
LOGIN
|
|
|
|
PASSWORD '{{password}}';
|
|
|
|
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "{{name}}";
|
|
|
|
`
|
|
|
|
|
|
|
|
const testPostgresStaticRoleRotate = `
|
|
|
|
ALTER ROLE "{{name}}" WITH PASSWORD '{{password}}';
|
|
|
|
`
|
|
|
|
|
|
|
|
const testPostgresStaticRoleGrant = `
|
|
|
|
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "{{name}}";
|
|
|
|
`
|