2015-04-19 00:34:36 +00:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
2015-10-29 20:59:08 +00:00
|
|
|
"database/sql"
|
2015-04-19 04:59:59 +00:00
|
|
|
"fmt"
|
2015-04-19 01:37:27 +00:00
|
|
|
"log"
|
2015-04-19 00:34:36 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
logicaltest "github.com/hashicorp/vault/logical/testing"
|
2015-10-29 20:59:08 +00:00
|
|
|
"github.com/lib/pq"
|
2015-04-19 01:37:27 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-04-19 00:34:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBackend_basic(t *testing.T) {
|
2015-10-29 20:59:08 +00:00
|
|
|
b, _ := Factory(logical.TestBackendConfig())
|
|
|
|
|
2016-02-19 02:03:47 +00:00
|
|
|
d1 := map[string]interface{}{
|
|
|
|
"connection_url": os.Getenv("PG_URL"),
|
|
|
|
}
|
|
|
|
d2 := map[string]interface{}{
|
|
|
|
"value": os.Getenv("PG_URL"),
|
|
|
|
}
|
|
|
|
|
2015-04-19 00:34:36 +00:00
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
2015-04-19 04:45:05 +00:00
|
|
|
Backend: b,
|
2015-04-19 00:34:36 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
2016-02-19 02:03:47 +00:00
|
|
|
testAccStepConfig(t, d1, false),
|
|
|
|
testAccStepRole(t),
|
|
|
|
testAccStepReadCreds(t, b, "web"),
|
|
|
|
testAccStepConfig(t, d2, false),
|
2015-04-19 01:09:33 +00:00
|
|
|
testAccStepRole(t),
|
2015-10-29 20:59:08 +00:00
|
|
|
testAccStepReadCreds(t, b, "web"),
|
2015-04-19 00:34:36 +00:00
|
|
|
},
|
|
|
|
})
|
2015-10-29 20:59:08 +00:00
|
|
|
|
2015-04-19 00:34:36 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 04:59:59 +00:00
|
|
|
func TestBackend_roleCrud(t *testing.T) {
|
2015-10-29 20:59:08 +00:00
|
|
|
b, _ := Factory(logical.TestBackendConfig())
|
2016-02-19 02:03:47 +00:00
|
|
|
d := map[string]interface{}{
|
|
|
|
"connection_url": os.Getenv("PG_URL"),
|
|
|
|
}
|
2015-04-19 04:59:59 +00:00
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Backend: b,
|
|
|
|
Steps: []logicaltest.TestStep{
|
2016-02-19 02:03:47 +00:00
|
|
|
testAccStepConfig(t, d, false),
|
2015-04-19 04:59:59 +00:00
|
|
|
testAccStepRole(t),
|
|
|
|
testAccStepReadRole(t, "web", testRole),
|
|
|
|
testAccStepDeleteRole(t, "web"),
|
|
|
|
testAccStepReadRole(t, "web", ""),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-19 02:03:47 +00:00
|
|
|
func TestBackend_configConnection(t *testing.T) {
|
|
|
|
b := Backend()
|
|
|
|
d1 := map[string]interface{}{
|
|
|
|
"value": os.Getenv("PG_URL"),
|
|
|
|
}
|
|
|
|
d2 := map[string]interface{}{
|
|
|
|
"connection_url": os.Getenv("PG_URL"),
|
|
|
|
}
|
|
|
|
d3 := map[string]interface{}{
|
|
|
|
"value": os.Getenv("PG_URL"),
|
|
|
|
"connection_url": os.Getenv("PG_URL"),
|
|
|
|
}
|
|
|
|
d4 := map[string]interface{}{}
|
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Backend: b,
|
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepConfig(t, d1, false),
|
|
|
|
testAccStepConfig(t, d2, false),
|
|
|
|
testAccStepConfig(t, d3, false),
|
|
|
|
testAccStepConfig(t, d4, true),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-19 00:34:36 +00:00
|
|
|
func testAccPreCheck(t *testing.T) {
|
|
|
|
if v := os.Getenv("PG_URL"); v == "" {
|
|
|
|
t.Fatal("PG_URL must be set for acceptance tests")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("expected error, but write succeeded.")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} else if resp != nil {
|
|
|
|
return fmt.Errorf("response should be nil")
|
|
|
|
}
|
|
|
|
return nil
|
2015-04-19 00:34:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2015-04-19 01:09:33 +00:00
|
|
|
|
|
|
|
func testAccStepRole(t *testing.T) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-04-19 01:09:33 +00:00
|
|
|
Path: "roles/web",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"sql": testRole,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 04:59:59 +00:00
|
|
|
func testAccStepDeleteRole(t *testing.T, n string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.DeleteOperation,
|
|
|
|
Path: "roles/" + n,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 20:59:08 +00:00
|
|
|
func testAccStepReadCreds(t *testing.T, b logical.Backend, name string) logicaltest.TestStep {
|
2015-04-19 01:37:27 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
2015-04-19 04:59:59 +00:00
|
|
|
Path: "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
|
|
|
|
}
|
|
|
|
log.Printf("[WARN] Generated credentials: %v", d)
|
|
|
|
|
2015-10-30 16:41:45 +00:00
|
|
|
conn, err := pq.ParseURL(os.Getenv("PG_URL"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn += " timezone=utc"
|
|
|
|
|
|
|
|
db, err := sql.Open("postgres", conn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-29 20:59:08 +00:00
|
|
|
returnedRows := func() int {
|
|
|
|
stmt, err := db.Prepare(fmt.Sprintf(
|
2016-01-28 19:41:13 +00:00
|
|
|
"SELECT DISTINCT schemaname FROM pg_tables WHERE has_table_privilege('%s', 'information_schema.role_column_grants', 'select');",
|
2015-10-30 16:41:45 +00:00
|
|
|
d.Username))
|
2015-10-29 20:59:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
rows, err := stmt.Query()
|
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for rows.Next() {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
userRows := returnedRows()
|
2016-01-28 19:41:13 +00:00
|
|
|
if userRows != 2 {
|
2015-10-29 20:59:08 +00:00
|
|
|
t.Fatalf("did not get expected number of rows, got %d", userRows)
|
|
|
|
}
|
|
|
|
|
2015-10-30 16:41:45 +00:00
|
|
|
resp, err = b.HandleRequest(&logical.Request{
|
2015-10-29 20:59:08 +00:00
|
|
|
Operation: logical.RevokeOperation,
|
|
|
|
Secret: &logical.Secret{
|
|
|
|
InternalData: map[string]interface{}{
|
|
|
|
"secret_type": "creds",
|
2015-10-30 16:41:45 +00:00
|
|
|
"username": d.Username,
|
2015-10-29 20:59:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
if resp.IsError() {
|
|
|
|
return fmt.Errorf("Error on resp: %#v", *resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
`
|