Add initial SQL for managed groups (#1253)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-05-26 20:43:36 -04:00
1 parent ed5e34082c
commit 15bd1a5245
11 files changed
+1258 -464

No files matched your search

@@ -0,0 +1,60 @@
begin;
-- The base abstract table
create table auth_managed_group (
public_id wt_public_id
primary key,
auth_method_id wt_public_id
not null,
-- Ensure that if the auth method is deleted (which will also happen if the
-- scope is deleted) this is deleted too
constraint auth_method_fkey
foreign key (auth_method_id) -- fk1
references auth_method(public_id)
on delete cascade
on update cascade,
constraint auth_managed_group_auth_method_id_public_id_uq
unique(auth_method_id, public_id)
);
comment on table auth_managed_group is
'auth_managed_group is the abstract base table for managed groups.';
-- Define the immutable fields of auth_managed_group
create trigger
immutable_columns
before
update on auth_managed_group
for each row execute procedure immutable_columns('public_id', 'auth_method_id');
-- Function to insert into the base table when values are inserted into a
-- concrete type table. This happens before inserts so the foreign keys in the
-- concrete type will be valid.
create or replace function
insert_managed_group_subtype()
returns trigger
as $$
begin
insert into auth_managed_group
(public_id, auth_method_id)
values
(new.public_id, new.auth_method_id);
return new;
end;
$$ language plpgsql;
-- delete_managed_group_subtype() is an after delete trigger
-- function for subtypes of managed_group
create or replace function delete_managed_group_subtype()
returns trigger
as $$
begin
delete from auth_managed_group
where public_id = old.public_id;
return null; -- result is ignored since this is an after trigger
end;
$$ language plpgsql;
commit;
@@ -0,0 +1,83 @@
begin;
create table auth_oidc_managed_group (
public_id wt_public_id
primary key,
auth_method_id wt_public_id
not null,
name wt_name,
description wt_description,
create_time wt_timestamp,
update_time wt_timestamp,
version wt_version,
filter wt_bexprfilter
not null,
-- Ensure that this managed group relates to an oidc auth method, as opposed
-- to other types
constraint auth_oidc_method_fkey
foreign key (auth_method_id) -- fk1
references auth_oidc_method (public_id)
on delete cascade
on update cascade,
-- Ensure it relates to an abstract managed group
constraint auth_managed_group_fkey
foreign key (auth_method_id, public_id) -- fk2
references auth_managed_group (auth_method_id, public_id)
on delete cascade
on update cascade,
constraint auth_oidc_managed_group_auth_method_id_name_uq
unique(auth_method_id, name)
);
comment on table auth_oidc_managed_group is
'auth_oidc_managed_group entries are subtypes of auth_managed_group and represent an oidc managed group.';
-- Define the immutable fields of auth_oidc_managed_group
create trigger
immutable_columns
before
update on auth_oidc_managed_group
for each row execute procedure immutable_columns('public_id', 'auth_method_id', 'create_time');
-- Populate create time on insert
create trigger
default_create_time_column
before
insert on auth_oidc_managed_group
for each row execute procedure default_create_time();
-- Generate update time on update
create trigger
update_time_column
before
update on auth_oidc_managed_group
for each row execute procedure update_time_column();
-- Update version when something changes
create trigger
update_version_column
after
update on auth_oidc_managed_group
for each row execute procedure update_version_column();
-- Add into the base table when inserting into the concrete table
create trigger
insert_managed_group_subtype
before insert on auth_oidc_managed_group
for each row execute procedure insert_managed_group_subtype();
-- Ensure that deletions in the oidc subtype result in deletions to the base
-- table.
create trigger
delete_managed_group_subtype
after
delete on auth_oidc_managed_group
for each row execute procedure delete_managed_group_subtype();
-- The tickets for oplog are the subtypes not the base types because no updates
-- are done to any values in the base types.
insert into oplog_ticket
(name, version)
values
('auth_oidc_managed_group', 1);
commit;
@@ -0,0 +1,52 @@
begin;
-- Mappings of account to oidc managed groups. This is a non-abstract table with
-- a view (below) so that it is a natural aggregate for the oplog (also below).
create table auth_oidc_managed_group_member_account (
create_time wt_timestamp,
managed_group_id wt_public_id
references auth_oidc_managed_group(public_id)
on delete cascade
on update cascade,
member_id wt_public_id
references auth_oidc_account(public_id)
on delete cascade
on update cascade,
primary key (managed_group_id, member_id)
);
comment on table auth_oidc_managed_group_member_account is
'auth_oidc_managed_group_member_account is the join table for managed oidc groups and accounts.';
-- auth_immutable_managed_oidc_group_member_account() ensures that group members are immutable.
create or replace function
auth_immutable_managed_oidc_group_member_account()
returns trigger
as $$
begin
raise exception 'managed oidc group members are immutable';
end;
$$ language plpgsql;
create trigger
default_create_time_column
before
insert on auth_oidc_managed_group_member_account
for each row execute procedure default_create_time();
create trigger
auth_immutable_managed_oidc_group_member_account
before
update on auth_oidc_managed_group_member_account
for each row execute procedure auth_immutable_managed_oidc_group_member_account();
-- Initially create the view with just oidc; eventually we can replace this view
-- to union with other subtype tables.
create view auth_managed_group_member_account as
select
oidc.create_time,
oidc.managed_group_id,
oidc.member_id
from
auth_oidc_managed_group_member_account oidc;
commit;
@@ -0,0 +1,376 @@
package migration
import (
"fmt"
"testing"
"time"
"github.com/hashicorp/boundary/internal/servers/controller"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_ManagedGroupTable(t *testing.T) {
t.Parallel()
tc := controller.NewTestController(t, nil)
defer tc.Shutdown()
db := tc.DbConn().DB()
var err error
managedGroupId := "a_bcdefghijk"
defaultPasswordAuthMethodId := "ampw_1234567890"
defaultOidcAuthMethodId := "amoidc_1234567890"
insertTests := []struct {
testName string
publicId string
authMethodId string
wantErr bool
}{
{
testName: "invalid auth method",
publicId: managedGroupId,
authMethodId: "amoid_1234567890",
wantErr: true,
},
{
testName: "valid",
publicId: managedGroupId,
authMethodId: defaultOidcAuthMethodId,
wantErr: false,
},
}
for _, tt := range insertTests {
t.Run("insert: "+tt.testName, func(t *testing.T) {
require := require.New(t)
_, err = db.Exec("insert into auth_managed_group values ($1, $2)",
tt.publicId,
tt.authMethodId)
require.True(tt.wantErr == (err != nil))
})
}
updateTests := []struct {
testName string
column string
value string
publicId string
wantErr bool
}{
{
testName: "immutable public id",
column: "public_id",
value: "z_yxwvutsrqp",
publicId: managedGroupId,
wantErr: true,
},
{
testName: "immutable auth method",
column: "auth_method_id",
value: defaultPasswordAuthMethodId,
publicId: managedGroupId,
wantErr: true,
},
}
for _, tt := range updateTests {
t.Run("update: "+tt.testName, func(t *testing.T) {
assert := assert.New(t)
_, err = db.Exec(fmt.Sprintf("update auth_managed_group set %s = $1 where public_id = $2", tt.column), tt.value, tt.publicId)
assert.True(tt.wantErr == (err != nil))
})
}
}
func Test_OidcManagedGroupTable(t *testing.T) {
t.Parallel()
tc := controller.NewTestController(t, nil)
defer tc.Shutdown()
db := tc.DbConn().DB()
var err error
managedGroupId := "a_bcdefghijk"
defaultPasswordAuthMethodId := "ampw_1234567890"
defaultOidcAuthMethodId := "amoidc_1234567890"
name := "this is the name"
filter := "this is a filter"
// The first set of tests is for initial insertion
{
insertTests := []struct {
testName string
publicId string
authMethodId string
name string
filter string
wantErr bool
}{
{
testName: "null filter",
publicId: managedGroupId,
authMethodId: "amoid_1234567890",
name: name,
filter: "",
wantErr: true,
},
{
testName: "invalid auth method",
publicId: managedGroupId,
authMethodId: defaultPasswordAuthMethodId,
name: name,
filter: filter,
wantErr: true,
},
{
testName: "valid",
publicId: managedGroupId,
authMethodId: defaultOidcAuthMethodId,
name: name,
filter: filter,
wantErr: false,
},
{
testName: "duplicate public id",
publicId: managedGroupId,
authMethodId: defaultOidcAuthMethodId,
name: name,
filter: filter,
wantErr: true,
},
{
testName: "duplicate name",
publicId: "z_yxwvutsrqp",
authMethodId: defaultOidcAuthMethodId,
name: name,
filter: filter,
wantErr: true,
},
}
for _, tt := range insertTests {
t.Run("insert: "+tt.testName, func(t *testing.T) {
require := require.New(t)
_, err = db.Exec("insert into auth_oidc_managed_group (public_id, auth_method_id, name, filter) values ($1, $2, $3, $4)",
tt.publicId,
tt.authMethodId,
tt.name,
tt.filter)
require.True(tt.wantErr == (err != nil))
})
}
}
// Read some values to validate that things were set automatically
rows, err := db.Query("select create_time, update_time, version from auth_oidc_managed_group")
require.NoError(t, err)
require.True(t, rows.Next())
var create_time, update_time time.Time
var version int
require.NoError(t, rows.Scan(&create_time, &update_time, &version))
assert.False(t, create_time.IsZero())
assert.Equal(t, update_time, create_time)
assert.Equal(t, 1, version)
// These update tests check immutability
{
updateTests := []struct {
testName string
column string
value interface{}
wantErr bool
}{
{
testName: "immutable public id",
column: "public_id",
value: "z_yxwvutsrqp",
wantErr: true,
},
{
testName: "immutable auth method",
column: "auth_method_id",
value: defaultPasswordAuthMethodId,
wantErr: true,
},
{
testName: "immutable creation time",
column: "create_time",
value: time.Now(),
wantErr: true,
},
{
testName: "valid",
column: "description",
value: "this is the description",
wantErr: false,
},
}
for _, tt := range updateTests {
t.Run("update: "+tt.testName, func(t *testing.T) {
require := require.New(t)
_, err = db.Exec(fmt.Sprintf("update auth_oidc_managed_group set %s = $1 where public_id = $2", tt.column), tt.value, managedGroupId)
require.True(tt.wantErr == (err != nil))
})
}
}
// Read values again to validate that things were updated automatically
rows, err = db.Query("select create_time, update_time, version from auth_oidc_managed_group")
require.NoError(t, err)
require.True(t, rows.Next())
var updated_create_time, updated_update_time time.Time
require.NoError(t, rows.Scan(&updated_create_time, &updated_update_time, &version))
assert.Equal(t, create_time, updated_create_time)
assert.NotEqual(t, update_time, updated_update_time)
assert.Equal(t, 2, version)
// Read values from auth_managed_group to ensure it was populated automatically
rows, err = db.Query("select public_id, auth_method_id from auth_managed_group")
require.NoError(t, err)
require.True(t, rows.Next())
var public_id, auth_method_id string
require.NoError(t, rows.Scan(&public_id, &auth_method_id))
assert.Equal(t, managedGroupId, public_id)
assert.Equal(t, defaultOidcAuthMethodId, auth_method_id)
// Delete the value from the subtype table
res, err := db.Exec("delete from auth_oidc_managed_group where public_id = $1", managedGroupId)
require.NoError(t, err)
affected, err := res.RowsAffected()
require.NoError(t, err)
require.EqualValues(t, 1, affected)
// It should no longer be in the base table
rows, err = db.Query("select public_id, auth_method_id from auth_managed_group")
require.NoError(t, err)
require.False(t, rows.Next())
}
func Test_AuthManagedOidcGroupMemberAccountTable(t *testing.T) {
t.Parallel()
tc := controller.NewTestController(t, nil)
defer tc.Shutdown()
db := tc.DbConn().DB()
var err error
managedGroupId := "a_bcdefghijk"
defaultOidcAuthMethodId := "amoidc_1234567890"
name := "this is the name"
filter := "this is a filter"
// Insert valid data in auth_oidc_managed_group to use for the following tests
_, err = db.Exec("insert into auth_oidc_managed_group (public_id, auth_method_id, name, filter) values ($1, $2, $3, $4)",
managedGroupId,
defaultOidcAuthMethodId,
name,
filter)
require.NoError(t, err)
// Fetch a valid (oidc) account ID to use in insertion
rows, err := db.Query("select public_id from auth_oidc_account limit 1")
require.NoError(t, err)
require.True(t, rows.Next())
var accountId string
require.NoError(t, rows.Scan(&accountId))
require.NotEmpty(t, accountId)
// The first set of tests is for initial insertion
{
insertTests := []struct {
testName string
managedGroupId string
memberId string
wantErr bool
}{
{
testName: "invalid managed group id",
managedGroupId: "z_yxwvutsrqp",
memberId: accountId,
wantErr: true,
},
{
testName: "invalid member id",
managedGroupId: managedGroupId,
memberId: "acct_1234567890",
wantErr: true,
},
{
testName: "valid",
managedGroupId: managedGroupId,
memberId: accountId,
wantErr: false,
},
{
testName: "duplicate values",
managedGroupId: managedGroupId,
memberId: accountId,
wantErr: true,
},
}
for _, tt := range insertTests {
t.Run("insert: "+tt.testName, func(t *testing.T) {
assert := assert.New(t)
_, err = db.Exec("insert into auth_oidc_managed_group_member_account (managed_group_id, member_id) values ($1, $2)",
tt.managedGroupId,
tt.memberId)
assert.True(tt.wantErr == (err != nil))
})
}
}
// Read some values to validate that things were set automatically
rows, err = db.Query("select create_time, managed_group_id, member_id from auth_oidc_managed_group_member_account")
require.NoError(t, err)
require.True(t, rows.Next())
var create_time time.Time
var managed_group_id, member_id string
require.NoError(t, rows.Scan(&create_time, &managed_group_id, &member_id))
assert.False(t, create_time.IsZero())
// These update tests check immutability
{
updateTests := []struct {
testName string
column string
value interface{}
wantErr bool
}{
{
testName: "immutable managed group id",
column: "managed_group_id",
value: "z_yxwvutsrqp",
wantErr: true,
},
{
testName: "immutable member_id",
column: "member_id",
value: "acct_1234567890",
wantErr: true,
},
{
testName: "immutable creation time",
column: "create_time",
value: time.Now(),
wantErr: true,
},
}
for _, tt := range updateTests {
t.Run("update: "+tt.testName, func(t *testing.T) {
assert := assert.New(t)
_, err = db.Exec(fmt.Sprintf("update auth_managed_group_member_account set %s = $1 where managed_group_id = $2 and member_id = $3", tt.column), managedGroupId, accountId)
assert.True(tt.wantErr == (err != nil))
})
}
}
// Read from the view to ensure we see it there
rows, err = db.Query("select create_time, managed_group_id, member_id from auth_managed_group_member_account")
require.NoError(t, err)
require.True(t, rows.Next())
var view_create_time time.Time
var view_managed_group_id, view_member_id string
require.NoError(t, rows.Scan(&view_create_time, &view_managed_group_id, &view_member_id))
assert.Equal(t, create_time, view_create_time)
assert.Equal(t, managed_group_id, view_managed_group_id)
assert.Equal(t, member_id, view_member_id)
}