Authenticate Handler API and SDK, and much more (#183)

This commit is contained in:
Todd Knight authored and GitHub committed 2020-07-14 17:32:47 -04:00
1 parent acce195dcb
commit 28dbcd84bd
89 files changed
+5296 -1943

No files matched your search

+67 -11
View File
@@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"
)
func TestDomain_PublicPrivateId(t *testing.T) {
func TestDomain_Ids(t *testing.T) {
const (
createTableBase = `
create table if not exists test_table_{{rep}}_id (
@@ -29,7 +29,7 @@ returning id;
conn, _ := TestSetup(t, "postgres")
db := conn.DB()
for _, typ := range []string{"public", "private"} {
for _, typ := range []string{"public", "private", "role", "scope"} {
createTable := strings.Replace(createTableBase, "{{rep}}", typ, -1)
insert := strings.Replace(insertBase, "{{rep}}", typ, -1)
@@ -37,17 +37,44 @@ returning id;
t.Fatalf("query: \n%s\n error: %s", createTable, err)
}
failTests := []string{
" ",
"bar",
"00000009",
failTests := []struct {
value string
exception string
}{
{
" ",
"",
},
{
"bar",
"",
},
{
"00000009",
"",
},
{
"r_default",
"role",
},
{
"global",
"scope",
},
}
for _, tt := range failTests {
value := tt
t.Run(tt, func(t *testing.T) {
t.Logf("insert value: %q", value)
if _, err := db.Query(insert, value); err == nil {
t.Errorf("want error, got no error for inserting public_id: %s", value)
t.Run(tt.value, func(t *testing.T) {
t.Logf("insert value: %q", tt.value)
_, err := db.Query(insert, tt.value)
switch {
case err == nil && tt.exception != typ:
t.Errorf("want error, got no error for inserting public_id: %s", tt.value)
case err == nil && tt.exception == typ:
// All good
case err != nil && tt.exception != typ:
// All good
default:
t.Fatal("unhandled")
}
})
}
@@ -345,3 +372,32 @@ returning public_id;
require.NoError(err)
assert.Equal(3, v)
}
func TestDomain_DisallowDefaultRoleModification(t *testing.T) {
conn, _ := TestSetup(t, "postgres")
db := conn.DB()
_, err := db.Exec(`delete from iam_role where public_id = $1`, "r_default")
require.Error(t, err)
assert.Equal(t, `pq: deletion of default role not allowed`, err.Error())
// This actually tests not just default, but all roles
_, err = db.Exec(`update iam_role set public_id = $1 where public_id = $2`, "r_something", "r_default")
require.Error(t, err)
assert.Equal(t, `pq: update of role primary key not allowed`, err.Error())
}
func TestDomain_DefaultUsersExist(t *testing.T) {
conn, _ := TestSetup(t, "postgres")
db := conn.DB()
for _, val := range []string{"u_anon", "u_auth"} {
rows, err := db.Query(`select from iam_user where public_id = $1`, val)
require.NoError(t, err)
var count int
for rows.Next() {
count++
}
assert.Equal(t, 1, count)
}
}
+72 -13
View File
@@ -59,6 +59,13 @@ check(
comment on domain wt_scope_id is
'"u_anon", "u_auth", or random ID generated with github.com/hashicorp/vault/sdk/helper/base62';
create domain wt_role_id as text
check(
length(trim(value)) > 10 or value = 'r_default'
);
comment on domain wt_scope_id is
'"r_default", or random ID generated with github.com/hashicorp/vault/sdk/helper/base62';
create domain wt_timestamp as
timestamp with time zone
default current_timestamp;
@@ -453,7 +460,7 @@ drop table iam_role_grant cascade;
drop function iam_sub_names cascade;
drop function iam_immutable_scope_type_func cascade;
drop function iam_sub_scopes_func cascade;
drop function iam_immutable_role cascade;
drop function iam_immutable_role_principal cascade;
drop function iam_user_role_scope_check cascade;
drop function iam_group_role_scope_check cascade;
drop function iam_group_member_scope_check cascade;
@@ -814,7 +821,7 @@ insert into iam_user (public_id, name, description, scope_id)
values ('u_auth', 'authenticated', 'The authenticated user matches any user that has a valid token', 'global');
create table iam_role (
public_id wt_public_id primary key,
public_id wt_role_id primary key,
create_time wt_timestamp,
update_time wt_timestamp,
name text,
@@ -837,7 +844,7 @@ create table iam_role (
-- Grants are immutable, which is enforced via the trigger below
create table iam_role_grant (
create_time wt_timestamp,
role_id wt_public_id -- pk
role_id wt_role_id -- pk
references iam_role(public_id)
on delete cascade
on update cascade,
@@ -907,6 +914,51 @@ before
insert or update on iam_role
for each row execute procedure grant_scope_id_valid();
-- disallow_default_role_deletion prevents the default role (r_default) from
-- being deleted
create or replace function
disallow_default_role_deletion()
returns trigger
as $$
begin
if old.public_id = 'r_default' then
raise exception 'deletion of default role not allowed';
end if;
return old;
end;
$$ language plpgsql;
create trigger
disallow_default_role_deletion
before
delete on iam_role
for each row execute procedure disallow_default_role_deletion();
-- immutable_default_role_id prevents the a role's id from being updated.
create or replace function
immutable_role_id()
returns trigger
as $$
begin
if new.public_id is distinct from old.public_id then
raise exception 'update of role primary key not allowed';
end if;
return new;
end;
$$ language plpgsql;
create trigger
immutable_role_id
before
update on iam_role
for each row execute procedure immutable_role_id();
insert into iam_role (public_id, name, description, scope_id)
values('r_default', 'default', 'default role', 'global');
insert into iam_role_grant (role_id, canonical_grant, raw_grant)
values('r_default', 'type=org;actions=list', 'type=org;actions=list');
create table iam_group (
public_id wt_public_id not null primary key,
create_time wt_timestamp,
@@ -953,10 +1005,11 @@ update on iam_group
-- iam_user_role contains roles that have been assigned to users. Users can be
-- from any scope. The rows in this table must be immutable after insert, which
-- will be ensured with a before update trigger using iam_immutable_role().
-- will be ensured with a before update trigger using
-- iam_immutable_role_principal().
create table iam_user_role (
create_time wt_timestamp,
role_id wt_public_id
role_id wt_role_id
references iam_role(public_id)
on delete cascade
on update cascade,
@@ -970,10 +1023,10 @@ create table iam_user_role (
-- iam_group_role contains roles that have been assigned to groups.
-- Groups can be from any scope. The rows in this table must be immutable after
-- insert, which will be ensured with a before update trigger using
-- iam_immutable_role().
-- iam_immutable_role_principal().
create table iam_group_role (
create_time wt_timestamp,
role_id wt_public_id
role_id wt_role_id
references iam_role(public_id)
on delete cascade
on update cascade,
@@ -997,6 +1050,12 @@ begin
end;
$$ language plpgsql;
insert into iam_user_role (role_id, principal_id)
values
('r_default', 'u_anon'),
('r_default', 'u_auth');
-- iam_principle_role provides a consolidated view all principal roles assigned
-- (user and group roles).
create view iam_principal_role as
@@ -1032,9 +1091,9 @@ where
gr.role_id = r.public_id and
g.public_id = gr.principal_id;
-- iam_immutable_role() ensures that roles assigned to principals are immutable.
-- iam_immutable_role_principal() ensures that roles assigned to principals are immutable.
create or replace function
iam_immutable_role()
iam_immutable_role_principal()
returns trigger
as $$
begin
@@ -1042,10 +1101,10 @@ begin
end;
$$ language plpgsql;
create trigger immutable_role
create trigger immutable_role_principal
before
update on iam_user_role
for each row execute procedure iam_immutable_role();
for each row execute procedure iam_immutable_role_principal();
create trigger
immutable_create_time
@@ -1059,10 +1118,10 @@ before
insert on iam_user_role
for each row execute procedure default_create_time();
create trigger immutable_role
create trigger immutable_role_principal
before
update on iam_group_role
for each row execute procedure iam_immutable_role();
for each row execute procedure iam_immutable_role_principal();
create trigger
immutable_create_time
@@ -28,6 +28,13 @@ check(
comment on domain wt_scope_id is
'"u_anon", "u_auth", or random ID generated with github.com/hashicorp/vault/sdk/helper/base62';
create domain wt_role_id as text
check(
length(trim(value)) > 10 or value = 'r_default'
);
comment on domain wt_scope_id is
'"r_default", or random ID generated with github.com/hashicorp/vault/sdk/helper/base62';
create domain wt_timestamp as
timestamp with time zone
default current_timestamp;
@@ -18,7 +18,7 @@ drop table iam_role_grant cascade;
drop function iam_sub_names cascade;
drop function iam_immutable_scope_type_func cascade;
drop function iam_sub_scopes_func cascade;
drop function iam_immutable_role cascade;
drop function iam_immutable_role_principal cascade;
drop function iam_user_role_scope_check cascade;
drop function iam_group_role_scope_check cascade;
drop function iam_group_member_scope_check cascade;
+64 -12
View File
@@ -339,7 +339,7 @@ insert into iam_user (public_id, name, description, scope_id)
values ('u_auth', 'authenticated', 'The authenticated user matches any user that has a valid token', 'global');
create table iam_role (
public_id wt_public_id primary key,
public_id wt_role_id primary key,
create_time wt_timestamp,
update_time wt_timestamp,
name text,
@@ -362,7 +362,7 @@ create table iam_role (
-- Grants are immutable, which is enforced via the trigger below
create table iam_role_grant (
create_time wt_timestamp,
role_id wt_public_id -- pk
role_id wt_role_id -- pk
references iam_role(public_id)
on delete cascade
on update cascade,
@@ -432,6 +432,51 @@ before
insert or update on iam_role
for each row execute procedure grant_scope_id_valid();
-- disallow_default_role_deletion prevents the default role (r_default) from
-- being deleted
create or replace function
disallow_default_role_deletion()
returns trigger
as $$
begin
if old.public_id = 'r_default' then
raise exception 'deletion of default role not allowed';
end if;
return old;
end;
$$ language plpgsql;
create trigger
disallow_default_role_deletion
before
delete on iam_role
for each row execute procedure disallow_default_role_deletion();
-- immutable_default_role_id prevents the a role's id from being updated.
create or replace function
immutable_role_id()
returns trigger
as $$
begin
if new.public_id is distinct from old.public_id then
raise exception 'update of role primary key not allowed';
end if;
return new;
end;
$$ language plpgsql;
create trigger
immutable_role_id
before
update on iam_role
for each row execute procedure immutable_role_id();
insert into iam_role (public_id, name, description, scope_id)
values('r_default', 'default', 'default role', 'global');
insert into iam_role_grant (role_id, canonical_grant, raw_grant)
values('r_default', 'type=org;actions=list', 'type=org;actions=list');
create table iam_group (
public_id wt_public_id not null primary key,
create_time wt_timestamp,
@@ -478,10 +523,11 @@ update on iam_group
-- iam_user_role contains roles that have been assigned to users. Users can be
-- from any scope. The rows in this table must be immutable after insert, which
-- will be ensured with a before update trigger using iam_immutable_role().
-- will be ensured with a before update trigger using
-- iam_immutable_role_principal().
create table iam_user_role (
create_time wt_timestamp,
role_id wt_public_id
role_id wt_role_id
references iam_role(public_id)
on delete cascade
on update cascade,
@@ -495,10 +541,10 @@ create table iam_user_role (
-- iam_group_role contains roles that have been assigned to groups.
-- Groups can be from any scope. The rows in this table must be immutable after
-- insert, which will be ensured with a before update trigger using
-- iam_immutable_role().
-- iam_immutable_role_principal().
create table iam_group_role (
create_time wt_timestamp,
role_id wt_public_id
role_id wt_role_id
references iam_role(public_id)
on delete cascade
on update cascade,
@@ -522,6 +568,12 @@ begin
end;
$$ language plpgsql;
insert into iam_user_role (role_id, principal_id)
values
('r_default', 'u_anon'),
('r_default', 'u_auth');
-- iam_principle_role provides a consolidated view all principal roles assigned
-- (user and group roles).
create view iam_principal_role as
@@ -557,9 +609,9 @@ where
gr.role_id = r.public_id and
g.public_id = gr.principal_id;
-- iam_immutable_role() ensures that roles assigned to principals are immutable.
-- iam_immutable_role_principal() ensures that roles assigned to principals are immutable.
create or replace function
iam_immutable_role()
iam_immutable_role_principal()
returns trigger
as $$
begin
@@ -567,10 +619,10 @@ begin
end;
$$ language plpgsql;
create trigger immutable_role
create trigger immutable_role_principal
before
update on iam_user_role
for each row execute procedure iam_immutable_role();
for each row execute procedure iam_immutable_role_principal();
create trigger
immutable_create_time
@@ -584,10 +636,10 @@ before
insert on iam_user_role
for each row execute procedure default_create_time();
create trigger immutable_role
create trigger immutable_role_principal
before
update on iam_group_role
for each row execute procedure iam_immutable_role();
for each row execute procedure iam_immutable_role_principal();
create trigger
immutable_create_time