2015-04-19 00:34:36 +00:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-10-29 20:59:08 +00:00
|
|
|
"database/sql"
|
2016-08-03 19:34:50 +00:00
|
|
|
"encoding/json"
|
2015-04-19 04:59:59 +00:00
|
|
|
"fmt"
|
2015-04-19 01:37:27 +00:00
|
|
|
"log"
|
2016-07-19 15:55:49 +00:00
|
|
|
"path"
|
2016-06-11 15:48:40 +00:00
|
|
|
"reflect"
|
2015-04-19 00:34:36 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-09-15 22:26:56 +00:00
|
|
|
logicaltest "github.com/hashicorp/vault/helper/testhelpers/logical"
|
2020-05-29 18:21:23 +00:00
|
|
|
postgreshelper "github.com/hashicorp/vault/helper/testhelpers/postgresql"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-04-19 01:37:27 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-04-19 00:34:36 +00:00
|
|
|
)
|
|
|
|
|
2016-06-11 15:48:40 +00:00
|
|
|
func TestBackend_config_connection(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2016-06-11 15:48:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configData := map[string]interface{}{
|
|
|
|
"connection_url": "sample_connection_url",
|
|
|
|
"max_open_connections": 9,
|
|
|
|
"max_idle_connections": 7,
|
|
|
|
"verify_connection": false,
|
|
|
|
}
|
|
|
|
|
|
|
|
configReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/connection",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: configData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), configReq)
|
2016-06-11 15:48:40 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
configReq.Operation = logical.ReadOperation
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), configReq)
|
2016-06-11 15:48:40 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
2016-07-19 15:55:49 +00:00
|
|
|
delete(configData, "verify_connection")
|
2018-03-30 14:17:39 +00:00
|
|
|
delete(configData, "connection_url")
|
2016-06-11 15:48:40 +00:00
|
|
|
if !reflect.DeepEqual(configData, resp.Data) {
|
|
|
|
t.Fatalf("bad: expected:%#v\nactual:%#v\n", configData, resp.Data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 00:34:36 +00:00
|
|
|
func TestBackend_basic(t *testing.T) {
|
2016-06-30 17:46:39 +00:00
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2016-06-30 17:46:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-29 20:59:08 +00:00
|
|
|
|
2020-05-29 18:21:23 +00:00
|
|
|
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "")
|
2018-08-07 16:56:33 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2016-06-30 17:46:39 +00:00
|
|
|
connData := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
2016-02-19 02:03:47 +00:00
|
|
|
}
|
2015-04-19 00:34:36 +00:00
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2018-11-07 01:21:24 +00:00
|
|
|
LogicalBackend: b,
|
2015-04-19 00:34:36 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
2016-06-30 17:46:39 +00:00
|
|
|
testAccStepConfig(t, connData, false),
|
2016-08-03 19:34:50 +00:00
|
|
|
testAccStepCreateRole(t, "web", testRole, false),
|
2016-07-01 16:00:21 +00:00
|
|
|
testAccStepReadCreds(t, b, config.StorageView, "web", connURL),
|
2015-04-19 00:34:36 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-19 04:59:59 +00:00
|
|
|
func TestBackend_roleCrud(t *testing.T) {
|
2016-06-30 17:46:39 +00:00
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2016-06-30 17:46:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-29 18:21:23 +00:00
|
|
|
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "")
|
2018-08-07 16:56:33 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2016-06-30 17:46:39 +00:00
|
|
|
connData := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
2016-02-19 02:03:47 +00:00
|
|
|
}
|
2015-04-19 04:59:59 +00:00
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2018-11-07 01:21:24 +00:00
|
|
|
LogicalBackend: b,
|
2015-04-19 04:59:59 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
2016-06-30 17:46:39 +00:00
|
|
|
testAccStepConfig(t, connData, false),
|
2016-08-03 19:34:50 +00:00
|
|
|
testAccStepCreateRole(t, "web", testRole, false),
|
2015-04-19 04:59:59 +00:00
|
|
|
testAccStepReadRole(t, "web", testRole),
|
|
|
|
testAccStepDeleteRole(t, "web"),
|
|
|
|
testAccStepReadRole(t, "web", ""),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-03 19:34:50 +00:00
|
|
|
func TestBackend_BlockStatements(t *testing.T) {
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2016-08-03 19:34:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-29 18:21:23 +00:00
|
|
|
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "")
|
2018-08-07 16:56:33 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2016-08-03 19:34:50 +00:00
|
|
|
connData := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
|
|
|
jsonBlockStatement, err := json.Marshal(testBlockStatementRoleSlice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2018-11-07 01:21:24 +00:00
|
|
|
LogicalBackend: b,
|
2016-08-03 19:34:50 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepConfig(t, connData, false),
|
|
|
|
// This will also validate the query
|
|
|
|
testAccStepCreateRole(t, "web-block", testBlockStatementRole, true),
|
|
|
|
testAccStepCreateRole(t, "web-block", string(jsonBlockStatement), false),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-30 10:53:34 +00:00
|
|
|
func TestBackend_roleReadOnly(t *testing.T) {
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2016-06-30 10:53:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-29 18:21:23 +00:00
|
|
|
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "")
|
2018-08-07 16:56:33 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2016-06-30 10:53:34 +00:00
|
|
|
connData := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2018-11-07 01:21:24 +00:00
|
|
|
LogicalBackend: b,
|
2016-06-30 10:53:34 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepConfig(t, connData, false),
|
2016-08-03 19:34:50 +00:00
|
|
|
testAccStepCreateRole(t, "web", testRole, false),
|
|
|
|
testAccStepCreateRole(t, "web-readonly", testReadOnlyRole, false),
|
2016-06-30 10:53:34 +00:00
|
|
|
testAccStepReadRole(t, "web-readonly", testReadOnlyRole),
|
2016-07-11 11:35:35 +00:00
|
|
|
testAccStepCreateTable(t, b, config.StorageView, "web", connURL),
|
|
|
|
testAccStepReadCreds(t, b, config.StorageView, "web-readonly", connURL),
|
|
|
|
testAccStepDropTable(t, b, config.StorageView, "web", connURL),
|
2016-06-30 10:53:34 +00:00
|
|
|
testAccStepDeleteRole(t, "web-readonly"),
|
|
|
|
testAccStepDeleteRole(t, "web"),
|
|
|
|
testAccStepReadRole(t, "web-readonly", ""),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-05 17:52:59 +00:00
|
|
|
func TestBackend_roleReadOnly_revocationSQL(t *testing.T) {
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2016-10-05 17:52:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-29 18:21:23 +00:00
|
|
|
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "")
|
2018-08-07 16:56:33 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2016-10-05 17:52:59 +00:00
|
|
|
connData := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2018-11-07 01:21:24 +00:00
|
|
|
LogicalBackend: b,
|
2016-10-05 17:52:59 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepConfig(t, connData, false),
|
|
|
|
testAccStepCreateRoleWithRevocationSQL(t, "web", testRole, defaultRevocationSQL, false),
|
|
|
|
testAccStepCreateRoleWithRevocationSQL(t, "web-readonly", testReadOnlyRole, defaultRevocationSQL, false),
|
|
|
|
testAccStepReadRole(t, "web-readonly", testReadOnlyRole),
|
|
|
|
testAccStepCreateTable(t, b, config.StorageView, "web", connURL),
|
|
|
|
testAccStepReadCreds(t, b, config.StorageView, "web-readonly", connURL),
|
|
|
|
testAccStepDropTable(t, b, config.StorageView, "web", connURL),
|
|
|
|
testAccStepDeleteRole(t, "web-readonly"),
|
|
|
|
testAccStepDeleteRole(t, "web"),
|
|
|
|
testAccStepReadRole(t, "web-readonly", ""),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-19 02:03:47 +00:00
|
|
|
func testAccStepConfig(t *testing.T, d map[string]interface{}, expectError bool) logicaltest.TestStep {
|
2015-04-19 00:34:36 +00:00
|
|
|
return logicaltest.TestStep{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-04-19 00:34:36 +00:00
|
|
|
Path: "config/connection",
|
2016-02-19 02:03:47 +00:00
|
|
|
Data: d,
|
|
|
|
ErrorOk: true,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if expectError {
|
|
|
|
if resp.Data == nil {
|
|
|
|
return fmt.Errorf("data is nil")
|
|
|
|
}
|
|
|
|
var e struct {
|
|
|
|
Error string `mapstructure:"error"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(e.Error) == 0 {
|
2018-04-09 18:35:21 +00:00
|
|
|
return fmt.Errorf("expected error, but write succeeded")
|
2016-02-19 02:03:47 +00:00
|
|
|
}
|
|
|
|
return nil
|
2016-06-30 17:46:39 +00:00
|
|
|
} else if resp != nil && resp.IsError() {
|
|
|
|
return fmt.Errorf("got an error response: %v", resp.Error())
|
2016-02-19 02:03:47 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-04-19 00:34:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2015-04-19 01:09:33 +00:00
|
|
|
|
2016-08-03 19:34:50 +00:00
|
|
|
func testAccStepCreateRole(t *testing.T, name string, sql string, expectFail bool) logicaltest.TestStep {
|
2015-04-19 01:09:33 +00:00
|
|
|
return logicaltest.TestStep{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2016-07-11 11:35:35 +00:00
|
|
|
Path: path.Join("roles", name),
|
2015-04-19 01:09:33 +00:00
|
|
|
Data: map[string]interface{}{
|
2016-06-30 10:53:34 +00:00
|
|
|
"sql": sql,
|
2015-04-19 01:09:33 +00:00
|
|
|
},
|
2016-08-03 19:34:50 +00:00
|
|
|
ErrorOk: expectFail,
|
2015-04-19 01:09:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 17:52:59 +00:00
|
|
|
func testAccStepCreateRoleWithRevocationSQL(t *testing.T, name, sql, revocationSQL string, expectFail bool) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: path.Join("roles", name),
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"sql": sql,
|
|
|
|
"revocation_sql": revocationSQL,
|
|
|
|
},
|
|
|
|
ErrorOk: expectFail,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 11:35:35 +00:00
|
|
|
func testAccStepDeleteRole(t *testing.T, name string) logicaltest.TestStep {
|
2015-04-19 04:59:59 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.DeleteOperation,
|
2016-07-11 11:35:35 +00:00
|
|
|
Path: path.Join("roles", name),
|
2015-04-19 04:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 16:00:21 +00:00
|
|
|
func testAccStepReadCreds(t *testing.T, b logical.Backend, s logical.Storage, name string, connURL string) logicaltest.TestStep {
|
2015-04-19 01:37:27 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
2016-07-11 11:35:35 +00:00
|
|
|
Path: path.Join("creds", name),
|
2015-04-19 01:37:27 +00:00
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
var d struct {
|
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-11 11:35:35 +00:00
|
|
|
log.Printf("[TRACE] Generated credentials: %v", d)
|
2015-10-30 16:41:45 +00:00
|
|
|
|
2022-05-23 19:49:18 +00:00
|
|
|
db, err := sql.Open("pgx", connURL+"&timezone=utc")
|
2015-10-30 16:41:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-29 20:59:08 +00:00
|
|
|
returnedRows := func() int {
|
2016-07-03 23:20:27 +00:00
|
|
|
stmt, err := db.Prepare("SELECT DISTINCT schemaname FROM pg_tables WHERE has_table_privilege($1, 'information_schema.role_column_grants', 'select');")
|
2015-10-29 20:59:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
2016-07-03 23:20:27 +00:00
|
|
|
rows, err := stmt.Query(d.Username)
|
2015-10-29 20:59:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for rows.Next() {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2016-07-11 11:35:35 +00:00
|
|
|
// minNumPermissions is the minimum number of permissions that will always be present.
|
|
|
|
const minNumPermissions = 2
|
|
|
|
|
2015-10-29 20:59:08 +00:00
|
|
|
userRows := returnedRows()
|
2016-07-11 11:35:35 +00:00
|
|
|
if userRows < minNumPermissions {
|
2015-10-29 20:59:08 +00:00
|
|
|
t.Fatalf("did not get expected number of rows, got %d", userRows)
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
2015-10-29 20:59:08 +00:00
|
|
|
Operation: logical.RevokeOperation,
|
2016-07-01 16:00:21 +00:00
|
|
|
Storage: s,
|
2015-10-29 20:59:08 +00:00
|
|
|
Secret: &logical.Secret{
|
|
|
|
InternalData: map[string]interface{}{
|
|
|
|
"secret_type": "creds",
|
2015-10-30 16:41:45 +00:00
|
|
|
"username": d.Username,
|
2016-10-05 17:52:59 +00:00
|
|
|
"role": name,
|
2015-10-29 20:59:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
if resp.IsError() {
|
2018-04-09 18:35:21 +00:00
|
|
|
return fmt.Errorf("error on resp: %#v", *resp)
|
2015-10-29 20:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
userRows = returnedRows()
|
2016-01-28 19:41:13 +00:00
|
|
|
// User shouldn't exist so returnedRows() should encounter an error and exit with -1
|
|
|
|
if userRows != -1 {
|
2015-10-29 20:59:08 +00:00
|
|
|
t.Fatalf("did not get expected number of rows, got %d", userRows)
|
|
|
|
}
|
|
|
|
|
2015-04-19 01:37:27 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 11:35:35 +00:00
|
|
|
func testAccStepCreateTable(t *testing.T, b logical.Backend, s logical.Storage, name string, connURL string) logicaltest.TestStep {
|
2016-06-30 10:53:34 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
2016-07-11 11:35:35 +00:00
|
|
|
Path: path.Join("creds", name),
|
2016-06-30 10:53:34 +00:00
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
var d struct {
|
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-11 11:35:35 +00:00
|
|
|
log.Printf("[TRACE] Generated credentials: %v", d)
|
2016-06-30 10:53:34 +00:00
|
|
|
|
2022-05-23 19:49:18 +00:00
|
|
|
db, err := sql.Open("pgx", connURL+"&timezone=utc")
|
2016-06-30 10:53:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec("CREATE TABLE test (id SERIAL PRIMARY KEY);")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
2016-06-30 10:53:34 +00:00
|
|
|
Operation: logical.RevokeOperation,
|
2016-07-11 11:35:35 +00:00
|
|
|
Storage: s,
|
2016-06-30 10:53:34 +00:00
|
|
|
Secret: &logical.Secret{
|
|
|
|
InternalData: map[string]interface{}{
|
|
|
|
"secret_type": "creds",
|
|
|
|
"username": d.Username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
if resp.IsError() {
|
2018-04-09 18:35:21 +00:00
|
|
|
return fmt.Errorf("error on resp: %#v", *resp)
|
2016-06-30 10:53:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 11:35:35 +00:00
|
|
|
func testAccStepDropTable(t *testing.T, b logical.Backend, s logical.Storage, name string, connURL string) logicaltest.TestStep {
|
2016-06-30 10:53:34 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
2016-07-11 11:35:35 +00:00
|
|
|
Path: path.Join("creds", name),
|
2016-06-30 10:53:34 +00:00
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
var d struct {
|
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-11 11:35:35 +00:00
|
|
|
log.Printf("[TRACE] Generated credentials: %v", d)
|
2016-06-30 10:53:34 +00:00
|
|
|
|
2022-05-23 19:49:18 +00:00
|
|
|
db, err := sql.Open("pgx", connURL+"&timezone=utc")
|
2016-06-30 10:53:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec("DROP TABLE test;")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
2016-06-30 10:53:34 +00:00
|
|
|
Operation: logical.RevokeOperation,
|
2016-07-11 11:35:35 +00:00
|
|
|
Storage: s,
|
2016-06-30 10:53:34 +00:00
|
|
|
Secret: &logical.Secret{
|
|
|
|
InternalData: map[string]interface{}{
|
|
|
|
"secret_type": "creds",
|
|
|
|
"username": d.Username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
if resp.IsError() {
|
2018-04-09 18:35:21 +00:00
|
|
|
return fmt.Errorf("error on resp: %#v", *resp)
|
2016-06-30 10:53:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 04:59:59 +00:00
|
|
|
func testAccStepReadRole(t *testing.T, name string, sql string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "roles/" + name,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if resp == nil {
|
|
|
|
if sql == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var d struct {
|
|
|
|
SQL string `mapstructure:"sql"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.SQL != sql {
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 01:09:33 +00:00
|
|
|
const testRole = `
|
2015-04-19 01:37:27 +00:00
|
|
|
CREATE ROLE "{{name}}" WITH
|
2015-04-19 01:09:33 +00:00
|
|
|
LOGIN
|
2015-04-19 04:45:05 +00:00
|
|
|
PASSWORD '{{password}}'
|
|
|
|
VALID UNTIL '{{expiration}}';
|
2015-04-27 19:00:07 +00:00
|
|
|
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "{{name}}";
|
2015-04-19 01:09:33 +00:00
|
|
|
`
|
2016-06-30 10:53:34 +00:00
|
|
|
|
|
|
|
const testReadOnlyRole = `
|
|
|
|
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}}";
|
|
|
|
`
|
2016-08-03 19:34:50 +00:00
|
|
|
|
|
|
|
const testBlockStatementRole = `
|
|
|
|
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 testBlockStatementRoleSlice = []string{
|
2016-10-05 17:52:59 +00:00
|
|
|
`
|
|
|
|
DO $$
|
2016-08-03 19:34:50 +00:00
|
|
|
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
|
2016-10-05 17:52:59 +00:00
|
|
|
$$
|
|
|
|
`,
|
2016-08-03 19:34:50 +00:00
|
|
|
`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}}";`,
|
|
|
|
}
|
2016-10-05 17:52:59 +00:00
|
|
|
|
|
|
|
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}};
|
|
|
|
`
|