diff --git a/internal/auth/db_test.go b/internal/auth/db_test.go new file mode 100644 index 0000000000..1f23d5ab43 --- /dev/null +++ b/internal/auth/db_test.go @@ -0,0 +1,135 @@ +package auth + +import ( + "testing" + + "github.com/hashicorp/watchtower/internal/db" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestDB_AuthMethodIDTrigger(t *testing.T) { + const ( + createTable = ` +create table if not exists test_auth_method ( + auth_method_id wt_public_id primary key +); +` + insert = ` +insert into test_auth_method (auth_method_id) +values ($1); +` + addTriggers = ` +create trigger + insert_auth_method_subtype +before +insert on test_auth_method + for each row execute procedure insert_auth_method_subtype(); +` + baseTableQuery = ` +select count(*) from auth_method where auth_method_id = $1; +` + testTableQuery = ` +select count(*) from test_auth_method where auth_method_id = $1; +` + ) + + assert, require := assert.New(t), require.New(t) + + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + if err := cleanup(); err != nil { + t.Error(err) + } + }() + defer func() { + if err := conn.Close(); err != nil { + t.Error(err) + } + }() + + db := conn.DB() + _, err := db.Exec(createTable) + require.NoError(err) + + _, err = db.Exec(addTriggers) + require.NoError(err) + + id := "l1Ocw0TpHn800CekIxIXlmQqRDgFDfYl" + _, err = db.Query(insert, id) + require.NoError(err) + + var count int + err = db.QueryRow(baseTableQuery, id).Scan(&count) + require.NoError(err) + assert.Equal(1, count) + + count = 0 + + err = db.QueryRow(testTableQuery, id).Scan(&count) + require.NoError(err) + assert.Equal(1, count) +} + +func TestDB_AuthAccountIDTrigger(t *testing.T) { + const ( + createTable = ` +create table if not exists test_auth_account ( + auth_account_id wt_public_id primary key +); +` + insert = ` +insert into test_auth_account (auth_account_id) +values ($1); +` + addTriggers = ` +create trigger + insert_auth_account_subtype +before +insert on test_auth_account + for each row execute procedure insert_auth_account_subtype(); +` + baseTableQuery = ` +select count(*) from auth_account where auth_account_id = $1; +` + testTableQuery = ` +select count(*) from test_auth_account where auth_account_id = $1; +` + ) + + assert, require := assert.New(t), require.New(t) + + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + if err := cleanup(); err != nil { + t.Error(err) + } + }() + defer func() { + if err := conn.Close(); err != nil { + t.Error(err) + } + }() + + db := conn.DB() + _, err := db.Exec(createTable) + require.NoError(err) + + _, err = db.Exec(addTriggers) + require.NoError(err) + + id := "l1Ocw0TpHn800CekIxIXlmQqRDgFDfYl" + _, err = db.Query(insert, id) + require.NoError(err) + + var count int + err = db.QueryRow(baseTableQuery, id).Scan(&count) + require.NoError(err) + assert.Equal(1, count) + + count = 0 + + err = db.QueryRow(testTableQuery, id).Scan(&count) + require.NoError(err) + assert.Equal(1, count) +} diff --git a/internal/db/migrations/postgres.gen.go b/internal/db/migrations/postgres.gen.go index 04d8f39227..7918451a10 100644 --- a/internal/db/migrations/postgres.gen.go +++ b/internal/db/migrations/postgres.gen.go @@ -332,8 +332,68 @@ commit; `), }, - "migrations/04_iam.down.sql": { - name: "04_iam.down.sql", + "migrations/04_auth.down.sql": { + name: "04_auth.down.sql", + bytes: []byte(` +begin; + + drop function insert_auth_account_subtype; + drop function insert_auth_method_subtype; + + drop table auth_account cascade; + drop table auth_method cascade; + +commit; + +`), + }, + "migrations/04_auth.up.sql": { + name: "04_auth.up.sql", + bytes: []byte(` +begin; + + -- base table for auth methods + create table auth_method ( + auth_method_id wt_public_id primary key + ); + + + -- base table for auth accounts + create table auth_account ( + auth_account_id wt_public_id primary key + ); + + + create or replace function + insert_auth_method_subtype() + returns trigger + as $$ + begin + insert into auth_method (auth_method_id) + values + (new.auth_method_id); + return new; + end; + $$ language plpgsql; + + create or replace function + insert_auth_account_subtype() + returns trigger + as $$ + begin + insert into auth_account (auth_account_id) + values + (new.auth_account_id); + return new; + end; + $$ language plpgsql; + +commit; + +`), + }, + "migrations/06_iam.down.sql": { + name: "06_iam.down.sql", bytes: []byte(` BEGIN; @@ -354,8 +414,8 @@ COMMIT; `), }, - "migrations/04_iam.up.sql": { - name: "04_iam.up.sql", + "migrations/06_iam.up.sql": { + name: "06_iam.up.sql", bytes: []byte(` begin; diff --git a/internal/db/migrations/postgres/04_auth.down.sql b/internal/db/migrations/postgres/04_auth.down.sql new file mode 100644 index 0000000000..f841266499 --- /dev/null +++ b/internal/db/migrations/postgres/04_auth.down.sql @@ -0,0 +1,9 @@ +begin; + + drop function insert_auth_account_subtype; + drop function insert_auth_method_subtype; + + drop table auth_account cascade; + drop table auth_method cascade; + +commit; diff --git a/internal/db/migrations/postgres/04_auth.up.sql b/internal/db/migrations/postgres/04_auth.up.sql new file mode 100644 index 0000000000..0fc955a8a4 --- /dev/null +++ b/internal/db/migrations/postgres/04_auth.up.sql @@ -0,0 +1,39 @@ +begin; + + -- base table for auth methods + create table auth_method ( + auth_method_id wt_public_id primary key + ); + + + -- base table for auth accounts + create table auth_account ( + auth_account_id wt_public_id primary key + ); + + + create or replace function + insert_auth_method_subtype() + returns trigger + as $$ + begin + insert into auth_method (auth_method_id) + values + (new.auth_method_id); + return new; + end; + $$ language plpgsql; + + create or replace function + insert_auth_account_subtype() + returns trigger + as $$ + begin + insert into auth_account (auth_account_id) + values + (new.auth_account_id); + return new; + end; + $$ language plpgsql; + +commit; diff --git a/internal/db/migrations/postgres/04_iam.down.sql b/internal/db/migrations/postgres/06_iam.down.sql similarity index 100% rename from internal/db/migrations/postgres/04_iam.down.sql rename to internal/db/migrations/postgres/06_iam.down.sql diff --git a/internal/db/migrations/postgres/04_iam.up.sql b/internal/db/migrations/postgres/06_iam.up.sql similarity index 100% rename from internal/db/migrations/postgres/04_iam.up.sql rename to internal/db/migrations/postgres/06_iam.up.sql