From c8bf1917f5f40e4e28cff2149c01d9cd55966f2e Mon Sep 17 00:00:00 2001 From: Timothy Messier Date: Wed, 28 Jul 2021 11:05:11 -0400 Subject: [PATCH 1/3] db: Refactor test setup for pre-populated data Now when initializing the test database for sql tests, it will load the colors persona data into the database. This will provide a data set that tests can use without needing to call `wtt_load`. The main motivation is to allow for a separation of setup and test execution that requires crossing a transaction boundary. The pgTap tests are required to exectue as a single transaction, so that the rollback can reset the state, and each test can be isolated. However, can cause issues with some test senarios, particuarly with the use of some times releated functions like `current_timestamp` and `now` which return the time that the current transaction started. This creates an issue if a test relies on some setup that sets a time via `now`, then an update that expects a new time via `now`. See: https://www.postgresql.org/docs/13/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT --- .../db/sqltest/initdb.d/01_colors_persona.sql | 172 ++++++++++++++ .../db/sqltest/initdb.d/02_colors_persona.sql | 216 ------------------ .../{01_wtt_load.sql => 02_wtt_load.sql} | 0 .../sqltest/initdb.d/03_widgets_persona.sql | 39 ++-- internal/db/sqltest/tests/setup/wtt_load.sql | 8 +- .../sqltest/tests/setup/wtt_load_colors.sql | 7 - 6 files changed, 196 insertions(+), 246 deletions(-) create mode 100644 internal/db/sqltest/initdb.d/01_colors_persona.sql delete mode 100644 internal/db/sqltest/initdb.d/02_colors_persona.sql rename internal/db/sqltest/initdb.d/{01_wtt_load.sql => 02_wtt_load.sql} (100%) delete mode 100644 internal/db/sqltest/tests/setup/wtt_load_colors.sql diff --git a/internal/db/sqltest/initdb.d/01_colors_persona.sql b/internal/db/sqltest/initdb.d/01_colors_persona.sql new file mode 100644 index 000000000..1abed6164 --- /dev/null +++ b/internal/db/sqltest/initdb.d/01_colors_persona.sql @@ -0,0 +1,172 @@ +begin; + -- Add organizations + insert into iam_scope + (parent_id, type, public_id, name) + values + ('global', 'org', 'o_____colors', 'Colors R Us'); + + -- Add projects to the organizations + insert into iam_scope + (parent_id, type, public_id, name) + values + ('o_____colors', 'project', 'p____bcolors', 'Blue Color Mill'), + ('o_____colors', 'project', 'p____rcolors', 'Red Color Mill'); + + -- Add global users + insert into iam_user + (scope_id, public_id, name) + values + ('global', 'u_______gary', 'Gary'), + ('global', 'u_______gina', 'Gina'), + ('global', 'u______nancy', 'Nancy'); + + -- Add organization users + insert into iam_user + (scope_id, public_id, name) + values + ('o_____colors', 'u______clare', 'Clare'), + ('o_____colors', 'u______cindy', 'Cindy'), + ('o_____colors', 'u______carly', 'Carly'), + ('o_____colors', 'u______ciara', 'Ciara'); + + insert into iam_group + (scope_id, public_id, name) + values + ('global', 'g___gg-group', 'Global Group'), + ('o_____colors', 'g___oc-group', 'Colors R Us Group'), + ('p____bcolors', 'g___cb-group', 'Blue Color Group'), + ('p____rcolors', 'g___cr-group', 'Red Color Group'); + + insert into iam_group_member_user + (group_id, member_id) + values + ('g___gg-group', 'u_______gary'), + ('g___oc-group', 'u______clare'), + ('g___cb-group', 'u______cindy'), + ('g___cr-group', 'u______carly'); + + insert into iam_role + (scope_id, grant_scope_id, public_id, name) + values + ('p____bcolors', 'p____bcolors', 'r_pp_bc__mix', 'Color Mixer'), + ('p____rcolors', 'p____rcolors', 'r_pp_rc__mix', 'Color Mixer'), + ('o_____colors', 'p____bcolors', 'r_op_bc__art', 'Blue Color Artist'), + ('o_____colors', 'p____rcolors', 'r_op_rc__art', 'Red Color Artist'), + ('o_____colors', 'o_____colors', 'r_oo_____art', 'Color Artist'), + ('global', 'o_____colors', 'r_go____name', 'Color Namer'), + ('global', 'p____bcolors', 'r_gp____spec', 'Blue Color Inspector'), + ('global', 'global', 'r_gg_____buy', 'Purchaser'), + ('global', 'global', 'r_gg____shop', 'Shopper'); + + insert into iam_role_grant + (role_id, canonical_grant, raw_grant) + values + ('r_gg_____buy', 'type=*;action=purchase', 'purchase anything'), + ('r_gg____shop', 'type=*;action=view', 'view anything'), + ('r_go____name', 'type=color;action=name', 'name colors'), + ('r_gp____spec', 'type=color;action=inspect', 'inspect colors'), + ('r_oo_____art', 'type=color;action=create', 'create color'), + ('r_op_bc__art', 'type=color;action=create', 'create color'), + ('r_op_rc__art', 'type=color;action=create', 'create color'), + ('r_pp_bc__mix', 'type=color;action=mix', 'mix color'), + ('r_pp_rc__mix', 'type=color;action=mix', 'mix color'); + + insert into iam_group_role + (role_id, principal_id) + values + ('r_op_rc__art', 'g___oc-group'), -- color + ('r_pp_bc__mix', 'g___cb-group'), -- color + ('r_pp_rc__mix', 'g___cr-group'); -- color + + insert into iam_user_role + (role_id, principal_id) + values + ('r_go____name', 'u_______gary'), + ('r_gp____spec', 'u_______gina'), + ('r_gg_____buy', 'u_auth'), + ('r_gg____shop', 'u_anon'); + + insert into auth_password_conf + (password_method_id, private_id) + values + ('apm___colors', 'apmc__colors'); + + + -- Add password auth method to organizations + insert into auth_password_method + (scope_id, public_id, password_conf_id, name) + values + ('o_____colors', 'apm___colors', 'apmc__colors', 'Colors Auth Password'); + + insert into auth_password_account + (auth_method_id, public_id, login_name) + values + ('apm___colors', 'apa____clare', 'clare'), + ('apm___colors', 'apa____cindy', 'cindy'), + ('apm___colors', 'apa____carly', 'carly'), + ('apm___colors', 'apa____ciara', 'ciara'); + + update auth_account set iam_user_id = 'u______clare' where public_id = 'apa____clare'; + update auth_account set iam_user_id = 'u______cindy' where public_id = 'apa____cindy'; + update auth_account set iam_user_id = 'u______carly' where public_id = 'apa____carly'; + update auth_account set iam_user_id = 'u______ciara' where public_id = 'apa____ciara'; + + insert into static_host_catalog + (scope_id, public_id, name) + values + ('p____bcolors', 'c___cb-sthcl', 'Blue Color Static Catalog'), + ('p____rcolors', 'c___cr-sthcl', 'Red Color Static Catalog'); + + insert into static_host + (catalog_id, public_id, address) + values + ('c___cb-sthcl', 'h_____cb__01', '1.blue.color'), + ('c___cb-sthcl', 'h_____cb__02', '2.blue.color'), + ('c___cb-sthcl', 'h_____cb__03', '3.blue.color'), + ('c___cb-sthcl', 'h_____cb__04', '4.blue.color'), + ('c___cb-sthcl', 'h_____cb__05', '5.blue.color'), + ('c___cb-sthcl', 'h_____cb__06', '6.blue.color'), + ('c___cb-sthcl', 'h_____cb__07', '7.blue.color'), + ('c___cb-sthcl', 'h_____cb__08', '8.blue.color'), + ('c___cb-sthcl', 'h_____cb__09', '9.blue.color'), + + ('c___cr-sthcl', 'h_____cr__01', '1.red.color'), + ('c___cr-sthcl', 'h_____cr__02', '2.red.color'), + ('c___cr-sthcl', 'h_____cr__03', '3.red.color'), + ('c___cr-sthcl', 'h_____cr__04', '4.red.color'), + ('c___cr-sthcl', 'h_____cr__05', '5.red.color'), + ('c___cr-sthcl', 'h_____cr__06', '6.red.color'), + ('c___cr-sthcl', 'h_____cr__07', '7.red.color'), + ('c___cr-sthcl', 'h_____cr__08', '8.red.color'), + ('c___cr-sthcl', 'h_____cr__09', '9.red.color'); + + insert into static_host_set + (catalog_id, public_id, name) + values + ('c___cb-sthcl', 's___1cb-sths', 'Blue Color Static Set 1'), + ('c___cb-sthcl', 's___2cb-sths', 'Blue Color Static Set 2'), + ('c___cr-sthcl', 's___1cr-sths', 'Red Color Static Set 1'), + ('c___cr-sthcl', 's___2cr-sths', 'Red Color Static Set 2'); + + insert + into static_host_set_member + ( host_id, set_id, catalog_id) + select h.public_id, s.public_id, s.catalog_id + from static_host as h, + static_host_set as s + where h.catalog_id = s.catalog_id; + + insert into target_tcp + (scope_id, public_id, name) + values + ('p____bcolors', 't_________cb', 'Blue Color Target'), + ('p____rcolors', 't_________cr', 'Red Color Target'); + + insert into target_host_set + (target_id, host_set_id) + values + ('t_________cb', 's___1cb-sths'), + ('t_________cb', 's___2cb-sths'), + ('t_________cr', 's___1cr-sths'), + ('t_________cr', 's___2cr-sths'); +commit; diff --git a/internal/db/sqltest/initdb.d/02_colors_persona.sql b/internal/db/sqltest/initdb.d/02_colors_persona.sql deleted file mode 100644 index 16046005d..000000000 --- a/internal/db/sqltest/initdb.d/02_colors_persona.sql +++ /dev/null @@ -1,216 +0,0 @@ -begin; - -- _wtt_load_colors_iam populates all iam_ tables for the colors persona. - -- iam does not depend on any other aggregates, but others depend on it, - -- as such is it should be first in the list. - create function _wtt_load_colors_iam() - returns void - as $$ - begin - -- Add organizations - insert into iam_scope - (parent_id, type, public_id, name) - values - ('global', 'org', 'o_____colors', 'Colors R Us'); - - -- Add projects to the organizations - insert into iam_scope - (parent_id, type, public_id, name) - values - ('o_____colors', 'project', 'p____bcolors', 'Blue Color Mill'), - ('o_____colors', 'project', 'p____rcolors', 'Red Color Mill'); - - -- Add global users - insert into iam_user - (scope_id, public_id, name) - values - ('global', 'u_______gary', 'Gary'), - ('global', 'u_______gina', 'Gina'), - ('global', 'u______nancy', 'Nancy'); - - -- Add organization users - insert into iam_user - (scope_id, public_id, name) - values - ('o_____colors', 'u______clare', 'Clare'), - ('o_____colors', 'u______cindy', 'Cindy'), - ('o_____colors', 'u______carly', 'Carly'), - ('o_____colors', 'u______ciara', 'Ciara'); - - insert into iam_group - (scope_id, public_id, name) - values - ('global', 'g___gg-group', 'Global Group'), - ('o_____colors', 'g___oc-group', 'Colors R Us Group'), - ('p____bcolors', 'g___cb-group', 'Blue Color Group'), - ('p____rcolors', 'g___cr-group', 'Red Color Group'); - - insert into iam_group_member_user - (group_id, member_id) - values - ('g___gg-group', 'u_______gary'), - ('g___oc-group', 'u______clare'), - ('g___cb-group', 'u______cindy'), - ('g___cr-group', 'u______carly'); - - insert into iam_role - (scope_id, grant_scope_id, public_id, name) - values - ('p____bcolors', 'p____bcolors', 'r_pp_bc__mix', 'Color Mixer'), - ('p____rcolors', 'p____rcolors', 'r_pp_rc__mix', 'Color Mixer'), - ('o_____colors', 'p____bcolors', 'r_op_bc__art', 'Blue Color Artist'), - ('o_____colors', 'p____rcolors', 'r_op_rc__art', 'Red Color Artist'), - ('o_____colors', 'o_____colors', 'r_oo_____art', 'Color Artist'), - ('global', 'o_____colors', 'r_go____name', 'Color Namer'), - ('global', 'p____bcolors', 'r_gp____spec', 'Blue Color Inspector'), - ('global', 'global', 'r_gg_____buy', 'Purchaser'), - ('global', 'global', 'r_gg____shop', 'Shopper'); - - insert into iam_role_grant - (role_id, canonical_grant, raw_grant) - values - ('r_gg_____buy', 'type=*;action=purchase', 'purchase anything'), - ('r_gg____shop', 'type=*;action=view', 'view anything'), - ('r_go____name', 'type=color;action=name', 'name colors'), - ('r_gp____spec', 'type=color;action=inspect', 'inspect colors'), - ('r_oo_____art', 'type=color;action=create', 'create color'), - ('r_op_bc__art', 'type=color;action=create', 'create color'), - ('r_op_rc__art', 'type=color;action=create', 'create color'), - ('r_pp_bc__mix', 'type=color;action=mix', 'mix color'), - ('r_pp_rc__mix', 'type=color;action=mix', 'mix color'); - - insert into iam_group_role - (role_id, principal_id) - values - ('r_op_rc__art', 'g___oc-group'), -- color - ('r_pp_bc__mix', 'g___cb-group'), -- color - ('r_pp_rc__mix', 'g___cr-group'); -- color - - insert into iam_user_role - (role_id, principal_id) - values - ('r_go____name', 'u_______gary'), - ('r_gp____spec', 'u_______gina'), - ('r_gg_____buy', 'u_auth'), - ('r_gg____shop', 'u_anon'); - end; - $$ language plpgsql; - - -- _wtt_load_kms populates all kms_ tables for the colors persona. - -- kms depends on iam. - create function _wtt_load_colors_kms() - returns void - as $$ - begin - end; - $$ language plpgsql; - - -- _wtt_load_colors_auth populates all auth_ tables for the colors persona. - -- auth depends on iam, and kms. - create function _wtt_load_colors_auth() - returns void - as $$ - begin - insert into auth_password_conf - (password_method_id, private_id) - values - ('apm___colors', 'apmc__colors'); - - - -- Add password auth method to organizations - insert into auth_password_method - (scope_id, public_id, password_conf_id, name) - values - ('o_____colors', 'apm___colors', 'apmc__colors', 'Colors Auth Password'); - - insert into auth_password_account - (auth_method_id, public_id, login_name) - values - ('apm___colors', 'apa____clare', 'clare'), - ('apm___colors', 'apa____cindy', 'cindy'), - ('apm___colors', 'apa____carly', 'carly'), - ('apm___colors', 'apa____ciara', 'ciara'); - - update auth_account set iam_user_id = 'u______clare' where public_id = 'apa____clare'; - update auth_account set iam_user_id = 'u______cindy' where public_id = 'apa____cindy'; - update auth_account set iam_user_id = 'u______carly' where public_id = 'apa____carly'; - update auth_account set iam_user_id = 'u______ciara' where public_id = 'apa____ciara'; - - end; - $$ language plpgsql; - - -- _wtt_load_colors_hosts populates all host_ tables for the colors persona. - -- hosts depend on iam. - create function _wtt_load_colors_hosts() - returns void - as $$ - begin - insert into static_host_catalog - (scope_id, public_id, name) - values - ('p____bcolors', 'c___cb-sthcl', 'Blue Color Static Catalog'), - ('p____rcolors', 'c___cr-sthcl', 'Red Color Static Catalog'); - - insert into static_host - (catalog_id, public_id, address) - values - ('c___cb-sthcl', 'h_____cb__01', '1.blue.color'), - ('c___cb-sthcl', 'h_____cb__02', '2.blue.color'), - ('c___cb-sthcl', 'h_____cb__03', '3.blue.color'), - ('c___cb-sthcl', 'h_____cb__04', '4.blue.color'), - ('c___cb-sthcl', 'h_____cb__05', '5.blue.color'), - ('c___cb-sthcl', 'h_____cb__06', '6.blue.color'), - ('c___cb-sthcl', 'h_____cb__07', '7.blue.color'), - ('c___cb-sthcl', 'h_____cb__08', '8.blue.color'), - ('c___cb-sthcl', 'h_____cb__09', '9.blue.color'), - - ('c___cr-sthcl', 'h_____cr__01', '1.red.color'), - ('c___cr-sthcl', 'h_____cr__02', '2.red.color'), - ('c___cr-sthcl', 'h_____cr__03', '3.red.color'), - ('c___cr-sthcl', 'h_____cr__04', '4.red.color'), - ('c___cr-sthcl', 'h_____cr__05', '5.red.color'), - ('c___cr-sthcl', 'h_____cr__06', '6.red.color'), - ('c___cr-sthcl', 'h_____cr__07', '7.red.color'), - ('c___cr-sthcl', 'h_____cr__08', '8.red.color'), - ('c___cr-sthcl', 'h_____cr__09', '9.red.color'); - - insert into static_host_set - (catalog_id, public_id, name) - values - ('c___cb-sthcl', 's___1cb-sths', 'Blue Color Static Set 1'), - ('c___cb-sthcl', 's___2cb-sths', 'Blue Color Static Set 2'), - ('c___cr-sthcl', 's___1cr-sths', 'Red Color Static Set 1'), - ('c___cr-sthcl', 's___2cr-sths', 'Red Color Static Set 2'); - - insert - into static_host_set_member - ( host_id, set_id, catalog_id) - select h.public_id, s.public_id, s.catalog_id - from static_host as h, - static_host_set as s - where h.catalog_id = s.catalog_id; - end; - $$ language plpgsql; - - -- _wtt_load_colors_targets populates all target_ tables for the colors persona. - -- targets depend on iam, auth, hosts. - create function _wtt_load_colors_targets() - returns void - as $$ - begin - insert into target_tcp - (scope_id, public_id, name) - values - ('p____bcolors', 't_________cb', 'Blue Color Target'), - ('p____rcolors', 't_________cr', 'Red Color Target'); - - insert into target_host_set - (target_id, host_set_id) - values - ('t_________cb', 's___1cb-sths'), - ('t_________cb', 's___2cb-sths'), - ('t_________cr', 's___1cr-sths'), - ('t_________cr', 's___2cr-sths'); - - end; - $$ language plpgsql; -commit; diff --git a/internal/db/sqltest/initdb.d/01_wtt_load.sql b/internal/db/sqltest/initdb.d/02_wtt_load.sql similarity index 100% rename from internal/db/sqltest/initdb.d/01_wtt_load.sql rename to internal/db/sqltest/initdb.d/02_wtt_load.sql diff --git a/internal/db/sqltest/initdb.d/03_widgets_persona.sql b/internal/db/sqltest/initdb.d/03_widgets_persona.sql index 7f2c95f88..9630ee94e 100644 --- a/internal/db/sqltest/initdb.d/03_widgets_persona.sql +++ b/internal/db/sqltest/initdb.d/03_widgets_persona.sql @@ -20,12 +20,12 @@ begin; ('o_____widget', 'project', 'p____swidget', 'Small Widget Factory'); -- Add global users - insert into iam_user - (scope_id, public_id, name) - values - ('global', 'u_______gary', 'Gary'), - ('global', 'u_______gina', 'Gina'), - ('global', 'u______nancy', 'Nancy'); + -- insert into iam_user + -- (scope_id, public_id, name) + -- values + -- ('global', 'u_______gary', 'Gary'), + -- ('global', 'u_______gina', 'Gina'), + -- ('global', 'u______nancy', 'Nancy'); -- Add organization users insert into iam_user @@ -39,7 +39,7 @@ begin; insert into iam_group (scope_id, public_id, name) values - ('global', 'g___gg-group', 'Global Group'), + -- ('global', 'g___gg-group', 'Global Group'), ('o_____widget', 'g___ow-group', 'Widget Inc Group'), ('p____bwidget', 'g___wb-group', 'Big Widget Group'), ('p____swidget', 'g___ws-group', 'Small Widget Group'); @@ -47,7 +47,7 @@ begin; insert into iam_group_member_user (group_id, member_id) values - ('g___gg-group', 'u_______gary'), + -- ('g___gg-group', 'u_______gary'), ('g___ow-group', 'u_____walter'), ('g___wb-group', 'u_____warren'), ('g___ws-group', 'u_____waylon'); @@ -55,18 +55,18 @@ begin; insert into iam_role (scope_id, grant_scope_id, public_id, name) values + -- ('global', 'global', 'r_gg_____buy', 'Purchaser'), + -- ('global', 'global', 'r_gg____shop', 'Shopper'), ('p____bwidget', 'p____bwidget', 'r_pp_bw__bld', 'Widget Builder'), ('p____swidget', 'p____swidget', 'r_pp_sw__bld', 'Widget Builder'), ('o_____widget', 'p____swidget', 'r_op_sw__eng', 'Small Widget Engineer'), - ('o_____widget', 'o_____widget', 'r_oo_____eng', 'Widget Engineer'), - ('global', 'global', 'r_gg_____buy', 'Purchaser'), - ('global', 'global', 'r_gg____shop', 'Shopper'); + ('o_____widget', 'o_____widget', 'r_oo_____eng', 'Widget Engineer'); insert into iam_role_grant (role_id, canonical_grant, raw_grant) values - ('r_gg_____buy', 'type=*;action=purchase', 'purchase anything'), - ('r_gg____shop', 'type=*;action=view', 'view anything'), + -- ('r_gg_____buy', 'type=*;action=purchase', 'purchase anything'), + -- ('r_gg____shop', 'type=*;action=view', 'view anything'), ('r_oo_____eng', 'type=widget;action=design', 'design widget'), ('r_op_sw__eng', 'type=widget;action=design', 'design widget'), ('r_op_sw__eng', 'type=widget;action=tune', 'tune widget'), @@ -81,11 +81,11 @@ begin; ('r_pp_bw__bld', 'g___wb-group'), -- widget ('r_pp_sw__bld', 'g___ws-group'); -- widget - insert into iam_user_role - (role_id, principal_id) - values - ('r_gg_____buy', 'u_auth'), - ('r_gg____shop', 'u_anon'); + -- insert into iam_user_role + -- (role_id, principal_id) + -- values + -- ('r_gg_____buy', 'u_auth'), + -- ('r_gg____shop', 'u_anon'); end; $$ language plpgsql; @@ -234,7 +234,8 @@ begin; select h.public_id, s.public_id, s.catalog_id from static_host as h, static_host_set as s - where h.catalog_id = s.catalog_id; + where h.catalog_id = s.catalog_id + and h.address like '%.widget'; end; $$ language plpgsql; diff --git a/internal/db/sqltest/tests/setup/wtt_load.sql b/internal/db/sqltest/tests/setup/wtt_load.sql index f5fa580ed..351ab0722 100644 --- a/internal/db/sqltest/tests/setup/wtt_load.sql +++ b/internal/db/sqltest/tests/setup/wtt_load.sql @@ -4,15 +4,15 @@ begin; -- invalid or missing args select throws_ok($$select wtt_load('unknown', 'iam')$$); - select throws_ok($$select wtt_load('colors', 'unknown')$$); - select throws_ok($$select wtt_load('colors')$$); + select throws_ok($$select wtt_load('widgets', 'unknown')$$); + select throws_ok($$select wtt_load('widgets')$$); select throws_ok($$select wtt_load()$$); -- incorrect order since auth depends on iam - select throws_ok($$select wtt_load('colors', 'auth', 'iam')$$); + select throws_ok($$select wtt_load('widgets', 'auth', 'iam')$$); -- missing a dependancy, auth depends on iam - select throws_ok($$select wtt_load('colors', 'auth')$$); + select throws_ok($$select wtt_load('widgets', 'auth')$$); select * from finish(); rollback; diff --git a/internal/db/sqltest/tests/setup/wtt_load_colors.sql b/internal/db/sqltest/tests/setup/wtt_load_colors.sql deleted file mode 100644 index 318f0adfa..000000000 --- a/internal/db/sqltest/tests/setup/wtt_load_colors.sql +++ /dev/null @@ -1,7 +0,0 @@ --- wtt_load_colors tests the wtt_load test helper function for the colors persona. -begin; - select plan(1); - select lives_ok($$select wtt_load('colors', 'iam', 'kms', 'auth', 'hosts', 'targets')$$); - - select * from finish(); -rollback; From 026f09a9a89ddbce6db756c5a336fe4c23d27b6e Mon Sep 17 00:00:00 2001 From: Timothy Messier Date: Fri, 30 Jul 2021 13:58:57 -0400 Subject: [PATCH 2/3] refactor(wh): Rename primary and foreign key columns The tables in the operational part of the Boundary database (meaning non-warehouse tables) suffix primary key columns and foreign key columns with `_id`. Many of these columns appear in the warehouse dimension and fact tables as normal attributes, not primary keys or foreign keys. To disambiguate ID columns from operational tables, the primary key column for dimension tables will be renamed from `id` to `key` and the foreign key columns in the fact tables will be renamed to change the suffix from `_id` to `_key`. --- .../15/01_wh_rename_key_columns.up.sql | 449 ++++++++++++++++++ .../db/sqltest/initdb.d/01_colors_persona.sql | 15 + .../db/sqltest/tests/wh/session/insert.sql | 19 + .../sqltest/tests/wh/session/update_state.sql | 24 + .../tests/wh/session_connection/insert.sql | 23 + .../tests/wh/session_connection/update.sql | 21 + .../sqltest/tests/wh/upsert_user/insert.sql | 4 +- .../sqltest/tests/wh/upsert_user/upsert.sql | 8 +- .../user_dimension/oidc_auth_new_session.sql | 42 +- .../password_auth_description_change.sql | 80 ++-- .../password_auth_new_session.sql | 42 +- .../session_multiple_sessions.sql | 8 +- ...ssion_multiple_sessions_different_auth.sql | 38 +- .../wh/user_dimension/session_update.sql | 6 +- .../tests/wh/user_dimension_views/target.sql | 2 +- 15 files changed, 666 insertions(+), 115 deletions(-) create mode 100644 internal/db/schema/migrations/postgres/15/01_wh_rename_key_columns.up.sql create mode 100644 internal/db/sqltest/tests/wh/session/insert.sql create mode 100644 internal/db/sqltest/tests/wh/session/update_state.sql create mode 100644 internal/db/sqltest/tests/wh/session_connection/insert.sql create mode 100644 internal/db/sqltest/tests/wh/session_connection/update.sql diff --git a/internal/db/schema/migrations/postgres/15/01_wh_rename_key_columns.up.sql b/internal/db/schema/migrations/postgres/15/01_wh_rename_key_columns.up.sql new file mode 100644 index 000000000..52a330a5f --- /dev/null +++ b/internal/db/schema/migrations/postgres/15/01_wh_rename_key_columns.up.sql @@ -0,0 +1,449 @@ +begin; + alter table wh_date_dimension + rename column id to key; + + alter table wh_time_of_day_dimension + rename column id to key; + + alter table wh_host_dimension + rename column id to key; + + alter table wh_user_dimension + rename column id to key; + + alter table wh_session_accumulating_fact + rename column host_id to host_key; + alter table wh_session_accumulating_fact + rename column user_id to user_key; + alter table wh_session_accumulating_fact + rename column session_pending_date_id to session_pending_date_key; + alter table wh_session_accumulating_fact + rename column session_pending_time_id to session_pending_time_key; + alter table wh_session_accumulating_fact + rename column session_active_date_id to session_active_date_key; + alter table wh_session_accumulating_fact + rename column session_active_time_id to session_active_time_key; + alter table wh_session_accumulating_fact + rename column session_canceling_date_id to session_canceling_date_key; + alter table wh_session_accumulating_fact + rename column session_canceling_time_id to session_canceling_time_key; + alter table wh_session_accumulating_fact + rename column session_terminated_date_id to session_terminated_date_key; + alter table wh_session_accumulating_fact + rename column session_terminated_time_id to session_terminated_time_key; + + alter table wh_session_connection_accumulating_fact + rename column host_id to host_key; + alter table wh_session_connection_accumulating_fact + rename column user_id to user_key; + alter table wh_session_connection_accumulating_fact + rename column connection_authorized_date_id to connection_authorized_date_key; + alter table wh_session_connection_accumulating_fact + rename column connection_authorized_time_id to connection_authorized_time_key; + alter table wh_session_connection_accumulating_fact + rename column connection_connected_date_id to connection_connected_date_key; + alter table wh_session_connection_accumulating_fact + rename column connection_connected_time_id to connection_connected_time_key; + alter table wh_session_connection_accumulating_fact + rename column connection_closed_date_id to connection_closed_date_key; + alter table wh_session_connection_accumulating_fact + rename column connection_closed_time_id to connection_closed_time_key; + + -- rename function from internal/db/schema/migrations/postgres/0/60_wh_domain_types.up.sql + alter function wh_dim_id rename to wh_dim_key; + + -- rename domain from internal/db/schema/migrations/postgres/0/60_wh_domain_types.up.sql + alter domain wh_dim_id rename to wh_dim_key; + + -- removes unused function from internal/db/schema/migrations/postgres/0/60_wh_domain_types.up.sql + drop function wh_current_date_id; + + -- removes unused function from internal/db/schema/migrations/postgres/0/60_wh_domain_types.up.sql + drop function wh_current_time_id; + + -- rename function from internal/db/schema/migrations/postgres/0/60_wh_domain_types.up.sql + alter function wh_date_id rename to wh_date_key; + + -- rename function from internal/db/schema/migrations/postgres/0/60_wh_domain_types.up.sql + alter function wh_time_id rename to wh_time_key; + + -- replaces view from 14/01_wh_user_dimension_oidc.up.sql + drop view whx_user_dimension_target; + create view whx_user_dimension_target as + select key, + user_id, + user_name, + user_description, + auth_account_id, + auth_account_type, + auth_account_name, + auth_account_description, + auth_account_external_id, + auth_account_full_name, + auth_account_email, + auth_method_id, + auth_method_type, + auth_method_name, + auth_method_description, + auth_method_external_id, + user_organization_id, + user_organization_name, + user_organization_description + from wh_user_dimension + where current_row_indicator = 'Current' + ; + + -- replaces function from 14/01_wh_user_dimension_oidc.up.sql + drop function wh_upsert_user; + create function wh_upsert_user(p_user_id wt_user_id, p_auth_token_id wt_public_id) + returns wh_dim_key + as $$ + declare + src whx_user_dimension_target%rowtype; + target whx_user_dimension_target%rowtype; + new_row wh_user_dimension%rowtype; + acct_id wt_public_id; + begin + select auth_account_id into strict acct_id + from auth_token + where public_id = p_auth_token_id; + + select * into target + from whx_user_dimension_target as t + where t.user_id = p_user_id + and t.auth_account_id = acct_id; + + select target.key, t.* into src + from whx_user_dimension_source as t + where t.user_id = p_user_id + and t.auth_account_id = acct_id; + + if src is distinct from target then + + -- expire the current row + update wh_user_dimension + set current_row_indicator = 'Expired', + row_expiration_time = current_timestamp + where user_id = p_user_id + and auth_account_id = acct_id + and current_row_indicator = 'Current'; + + -- insert a new row + insert into wh_user_dimension ( + user_id, user_name, user_description, + auth_account_id, auth_account_type, auth_account_name, auth_account_description, + auth_account_external_id, auth_account_full_name, auth_account_email, + auth_method_id, auth_method_type, auth_method_name, auth_method_description, + auth_method_external_id, + user_organization_id, user_organization_name, user_organization_description, + current_row_indicator, row_effective_time, row_expiration_time + ) + select user_id, user_name, user_description, + auth_account_id, auth_account_type, auth_account_name, auth_account_description, + auth_account_external_id, auth_account_full_name, auth_account_email, + auth_method_id, auth_method_type, auth_method_name, auth_method_description, + auth_method_external_id, + user_organization_id, user_organization_name, user_organization_description, + 'Current', current_timestamp, 'infinity'::timestamptz + from whx_user_dimension_source + where user_id = p_user_id + and auth_account_id = acct_id + returning * into new_row; + + return new_row.key; + end if; + return target.key; + + end; + $$ language plpgsql; + + -- replaces view from 0/65_wh_session_dimensions.up.sql + drop view whx_host_dimension_target; + create view whx_host_dimension_target as + select key, + host_id, + host_type, + host_name, + host_description, + host_address, + host_set_id, + host_set_type, + host_set_name, + host_set_description, + host_catalog_id, + host_catalog_type, + host_catalog_name, + host_catalog_description, + target_id, + target_type, + target_name, + target_description, + target_default_port_number, + target_session_max_seconds, + target_session_connection_limit, + project_id, + project_name, + project_description, + host_organization_id, + host_organization_name, + host_organization_description + from wh_host_dimension + where current_row_indicator = 'Current' + ; + + -- replaces function from 0/66_wh_session_dimensions.up.sql + drop function wh_upsert_host; + create function wh_upsert_host(p_host_id wt_public_id, p_host_set_id wt_public_id, p_target_id wt_public_id) + returns wh_dim_key + as $$ + declare + src whx_host_dimension_target%rowtype; + target whx_host_dimension_target%rowtype; + new_row wh_host_dimension%rowtype; + begin + select * into target + from whx_host_dimension_target as t + where t.host_id = p_host_id + and t.host_set_id = p_host_set_id + and t.target_id = p_target_id; + + select target.key, t.* into src + from whx_host_dimension_source as t + where t.host_id = p_host_id + and t.host_set_id = p_host_set_id + and t.target_id = p_target_id; + + if src is distinct from target then + + -- expire the current row + update wh_host_dimension + set current_row_indicator = 'Expired', + row_expiration_time = current_timestamp + where host_id = p_host_id + and host_set_id = p_host_set_id + and target_id = p_target_id + and current_row_indicator = 'Current'; + + -- insert a new row + insert into wh_host_dimension ( + host_id, host_type, host_name, host_description, host_address, + host_set_id, host_set_type, host_set_name, host_set_description, + host_catalog_id, host_catalog_type, host_catalog_name, host_catalog_description, + target_id, target_type, target_name, target_description, + target_default_port_number, target_session_max_seconds, target_session_connection_limit, + project_id, project_name, project_description, + host_organization_id, host_organization_name, host_organization_description, + current_row_indicator, row_effective_time, row_expiration_time + ) + select host_id, host_type, host_name, host_description, host_address, + host_set_id, host_set_type, host_set_name, host_set_description, + host_catalog_id, host_catalog_type, host_catalog_name, host_catalog_description, + target_id, target_type, target_name, target_description, + target_default_port_number, target_session_max_seconds, target_session_connection_limit, + project_id, project_name, project_description, + host_organization_id, host_organization_name, host_organization_description, + 'Current', current_timestamp, 'infinity'::timestamptz + from whx_host_dimension_source + where host_id = p_host_id + and host_set_id = p_host_set_id + and target_id = p_target_id + returning * into new_row; + + return new_row.key; + end if; + return target.key; + + end; + $$ language plpgsql; + + -- replaces function from 0/69_wh_session_facts.up.sql + drop trigger wh_insert_session on session; + drop function wh_insert_session; + create function wh_insert_session() + returns trigger + as $$ + declare + new_row wh_session_accumulating_fact%rowtype; + begin + with + pending_timestamp (date_dim_key, time_dim_key, ts) as ( + select wh_date_key(start_time), wh_time_key(start_time), start_time + from session_state + where session_id = new.public_id + and state = 'pending' + ) + insert into wh_session_accumulating_fact ( + session_id, + auth_token_id, + host_key, + user_key, + session_pending_date_key, + session_pending_time_key, + session_pending_time + ) + select new.public_id, + new.auth_token_id, + wh_upsert_host(new.host_id, new.host_set_id, new.target_id), + wh_upsert_user(new.user_id, new.auth_token_id), + pending_timestamp.date_dim_key, + pending_timestamp.time_dim_key, + pending_timestamp.ts + from pending_timestamp + returning * into strict new_row; + return null; + end; + $$ language plpgsql; + create trigger wh_insert_session + after insert on session + for each row + execute function wh_insert_session(); + + -- replaces function from 0/69_wh_session_facts.up.sql + drop trigger wh_insert_session_connection on session_connection; + drop function wh_insert_session_connection; + create function wh_insert_session_connection() + returns trigger + as $$ + declare + new_row wh_session_connection_accumulating_fact%rowtype; + begin + with + authorized_timestamp (date_dim_key, time_dim_key, ts) as ( + select wh_date_key(start_time), wh_time_key(start_time), start_time + from session_connection_state + where connection_id = new.public_id + and state = 'authorized' + ), + session_dimension (host_dim_key, user_dim_key) as ( + select host_key, user_key + from wh_session_accumulating_fact + where session_id = new.session_id + ) + insert into wh_session_connection_accumulating_fact ( + connection_id, + session_id, + host_key, + user_key, + connection_authorized_date_key, + connection_authorized_time_key, + connection_authorized_time, + client_tcp_address, + client_tcp_port_number, + endpoint_tcp_address, + endpoint_tcp_port_number, + bytes_up, + bytes_down + ) + select new.public_id, + new.session_id, + session_dimension.host_dim_key, + session_dimension.user_dim_key, + authorized_timestamp.date_dim_key, + authorized_timestamp.time_dim_key, + authorized_timestamp.ts, + new.client_tcp_address, + new.client_tcp_port, + new.endpoint_tcp_address, + new.endpoint_tcp_port, + new.bytes_up, + new.bytes_down + from authorized_timestamp, + session_dimension + returning * into strict new_row; + perform wh_rollup_connections(new.session_id); + return null; + end; + $$ language plpgsql; + + create trigger wh_insert_session_connection + after insert on session_connection + for each row + execute function wh_insert_session_connection(); + + -- replaces function from 0/69_wh_session_facts.up.sql + drop trigger wh_insert_session_state on session_state; + drop function wh_insert_session_state; + create function wh_insert_session_state() + returns trigger + as $$ + declare + date_col text; + time_col text; + ts_col text; + q text; + session_row wh_session_accumulating_fact%rowtype; + begin + if new.state = 'pending' then + -- The pending state is the first state which is handled by the + -- wh_insert_session trigger. The update statement in this trigger will + -- fail for the pending state because the row for the session has not yet + -- been inserted into the wh_session_accumulating_fact table. + return null; + end if; + + date_col = 'session_' || new.state || '_date_key'; + time_col = 'session_' || new.state || '_time_key'; + ts_col = 'session_' || new.state || '_time'; + + q = format('update wh_session_accumulating_fact + set (%I, %I, %I) = (select wh_date_key(%L), wh_time_key(%L), %L::timestamptz) + where session_id = %L + returning *', + date_col, time_col, ts_col, + new.start_time, new.start_time, new.start_time, + new.session_id); + execute q into strict session_row; + + return null; + end; + $$ language plpgsql; + + create trigger wh_insert_session_state + after insert on session_state + for each row + execute function wh_insert_session_state(); + + -- replaces function from 0/69_wh_session_facts.up.sql + drop trigger wh_insert_session_connection_state on session_connection_state; + drop function wh_insert_session_connection_state; + create function wh_insert_session_connection_state() + returns trigger + as $$ + declare + date_col text; + time_col text; + ts_col text; + q text; + connection_row wh_session_connection_accumulating_fact%rowtype; + begin + if new.state = 'authorized' then + -- The authorized state is the first state which is handled by the + -- wh_insert_session_connection trigger. The update statement in this + -- trigger will fail for the authorized state because the row for the + -- session connection has not yet been inserted into the + -- wh_session_connection_accumulating_fact table. + return null; + end if; + + date_col = 'connection_' || new.state || '_date_key'; + time_col = 'connection_' || new.state || '_time_key'; + ts_col = 'connection_' || new.state || '_time'; + + q = format('update wh_session_connection_accumulating_fact + set (%I, %I, %I) = (select wh_date_key(%L), wh_time_key(%L), %L::timestamptz) + where connection_id = %L + returning *', + date_col, time_col, ts_col, + new.start_time, new.start_time, new.start_time, + new.connection_id); + execute q into strict connection_row; + + return null; + end; + $$ language plpgsql; + + create trigger wh_insert_session_connection_state + after insert on session_connection_state + for each row + execute function wh_insert_session_connection_state(); +commit; diff --git a/internal/db/sqltest/initdb.d/01_colors_persona.sql b/internal/db/sqltest/initdb.d/01_colors_persona.sql index 1abed6164..54c4121f6 100644 --- a/internal/db/sqltest/initdb.d/01_colors_persona.sql +++ b/internal/db/sqltest/initdb.d/01_colors_persona.sql @@ -117,6 +117,11 @@ begin; ('p____bcolors', 'c___cb-sthcl', 'Blue Color Static Catalog'), ('p____rcolors', 'c___cr-sthcl', 'Red Color Static Catalog'); + insert into auth_token + (key_id, auth_account_id, public_id, token) + values + ('key', 'apa____clare', 'tok____clare', 'tok____clare'::bytea); + insert into static_host (catalog_id, public_id, address) values @@ -169,4 +174,14 @@ begin; ('t_________cb', 's___2cb-sths'), ('t_________cr', 's___1cr-sths'), ('t_________cr', 's___2cr-sths'); + + insert into session + ( scope_id , target_id , host_set_id , host_id , user_id , auth_token_id , certificate , endpoint , public_id) + values + ('p____bcolors' , 't_________cb' , 's___1cb-sths' , 'h_____cb__01' , 'u______clare' , 'tok____clare' , 'abc'::bytea , 'ep1' , 's1_____clare'); + + insert into session_connection + (session_id, public_id) + values + ('s1_____clare', 'sc1_____clare'); commit; diff --git a/internal/db/sqltest/tests/wh/session/insert.sql b/internal/db/sqltest/tests/wh/session/insert.sql new file mode 100644 index 000000000..87257edc6 --- /dev/null +++ b/internal/db/sqltest/tests/wh/session/insert.sql @@ -0,0 +1,19 @@ +-- insert tests that a wh_session_accumulating_fact is created when +-- a session is created. +begin; + select plan(2); + + select wtt_load('widgets', 'iam', 'kms', 'auth', 'hosts', 'targets'); + + select is(count(*), 0::bigint) from wh_session_accumulating_fact where session_id = 's1____walter'; + + -- insert first session, should result in a new wh_session_accumulating_fact + insert into session + ( scope_id , target_id , host_set_id , host_id , user_id , auth_token_id , certificate , endpoint , public_id) + values + ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____walter' , 'tok___walter' , 'abc'::bytea , 'ep1' , 's1____walter'); + + select is(count(*), 1::bigint) from wh_session_accumulating_fact where session_id = 's1____walter'; + + select * from finish(); +rollback; diff --git a/internal/db/sqltest/tests/wh/session/update_state.sql b/internal/db/sqltest/tests/wh/session/update_state.sql new file mode 100644 index 000000000..1f52440c8 --- /dev/null +++ b/internal/db/sqltest/tests/wh/session/update_state.sql @@ -0,0 +1,24 @@ +begin; + select plan(8); + + select is(count(*), 1::bigint) from wh_session_accumulating_fact where session_id = 's1_____clare'; + select is(total_bytes_up, null) from wh_session_accumulating_fact where session_id = 's1_____clare'; + select is(total_bytes_down, null) from wh_session_accumulating_fact where session_id = 's1_____clare'; + select is(session_terminated_time, 'infinity'::wh_timestamp) from wh_session_accumulating_fact where session_id = 's1_____clare'; + + update session_connection set + bytes_up = 10, + bytes_down = 5, + closed_reason = 'closed by end-user' + where public_id = 'sc1_____clare'; + update session set + termination_reason = 'closed by end-user' + where public_id = 's1_____clare'; + + select is(count(*), 1::bigint) from wh_session_accumulating_fact; + select is(total_bytes_up, 10::wh_bytes_transmitted) from wh_session_accumulating_fact where session_id = 's1_____clare'; + select is(total_bytes_down, 5::wh_bytes_transmitted) from wh_session_accumulating_fact where session_id = 's1_____clare'; + select is(session_terminated_time, now()::wh_timestamp) from wh_session_accumulating_fact where session_id = 's1_____clare'; + + select * from finish(); +rollback; diff --git a/internal/db/sqltest/tests/wh/session_connection/insert.sql b/internal/db/sqltest/tests/wh/session_connection/insert.sql new file mode 100644 index 000000000..248ae53b0 --- /dev/null +++ b/internal/db/sqltest/tests/wh/session_connection/insert.sql @@ -0,0 +1,23 @@ +-- insert tests that a wh_session_connection_accumulating_fact is created when +-- a connection is established. +begin; + select plan(2); + + select wtt_load('widgets', 'iam', 'kms', 'auth', 'hosts', 'targets'); + + select is(count(*), 0::bigint) from wh_session_connection_accumulating_fact where connection_id = 'sc1____walter'; + + insert into session + ( scope_id , target_id , host_set_id , host_id , user_id , auth_token_id , certificate , endpoint , public_id) + values + ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____walter' , 'tok___walter' , 'abc'::bytea , 'ep1' , 's1____walter'); + + insert into session_connection + (session_id, public_id) + values + ('s1____walter', 'sc1____walter'); + + select is(count(*), 1::bigint) from wh_session_connection_accumulating_fact where connection_id = 'sc1____walter'; + + select * from finish(); +rollback; diff --git a/internal/db/sqltest/tests/wh/session_connection/update.sql b/internal/db/sqltest/tests/wh/session_connection/update.sql new file mode 100644 index 000000000..49f337e13 --- /dev/null +++ b/internal/db/sqltest/tests/wh/session_connection/update.sql @@ -0,0 +1,21 @@ +begin; + select plan(8); + + select is(count(*), 1::bigint) from wh_session_connection_accumulating_fact where connection_id = 'sc1_____clare'; + select is(bytes_up, null) from wh_session_connection_accumulating_fact where connection_id = 'sc1_____clare'; + select is(bytes_down, null) from wh_session_connection_accumulating_fact where connection_id = 'sc1_____clare'; + select is(connection_closed_time, 'infinity'::wh_timestamp) from wh_session_connection_accumulating_fact where connection_id = 'sc1_____clare'; + + update session_connection set + bytes_up = 10, + bytes_down = 5, + closed_reason = 'closed by end-user' + where public_id = 'sc1_____clare'; + + select is(count(*), 1::bigint) from wh_session_connection_accumulating_fact; + select is(bytes_up, 10::wh_bytes_transmitted) from wh_session_connection_accumulating_fact where connection_id = 'sc1_____clare'; + select is(bytes_down, 5::wh_bytes_transmitted) from wh_session_connection_accumulating_fact where connection_id = 'sc1_____clare'; + select is(connection_closed_time, now()::wh_timestamp) from wh_session_connection_accumulating_fact where connection_id = 'sc1_____clare'; + + select * from finish(); +rollback; diff --git a/internal/db/sqltest/tests/wh/upsert_user/insert.sql b/internal/db/sqltest/tests/wh/upsert_user/insert.sql index 28289e2e3..ccefee657 100644 --- a/internal/db/sqltest/tests/wh/upsert_user/insert.sql +++ b/internal/db/sqltest/tests/wh/upsert_user/insert.sql @@ -6,12 +6,12 @@ begin; select wtt_load('widgets', 'iam', 'kms', 'auth', 'hosts', 'targets'); -- ensure no existing dimensions - select is(count(*), 0::bigint) from wh_user_dimension; + select is(count(*), 0::bigint) from wh_user_dimension where user_id = 'u_____walter'; select lives_ok($$select wh_upsert_user('u_____walter', 'tok___walter')$$); -- upsert should insert a user_dimension - select is(count(*), 1::bigint) from wh_user_dimension; + select is(count(*), 1::bigint) from wh_user_dimension where user_id = 'u_____walter'; select * from finish(); rollback; diff --git a/internal/db/sqltest/tests/wh/upsert_user/upsert.sql b/internal/db/sqltest/tests/wh/upsert_user/upsert.sql index aad62d112..845675c21 100644 --- a/internal/db/sqltest/tests/wh/upsert_user/upsert.sql +++ b/internal/db/sqltest/tests/wh/upsert_user/upsert.sql @@ -6,11 +6,11 @@ begin; select wtt_load('widgets', 'iam', 'kms', 'auth', 'hosts', 'targets'); -- ensure no existing dimensions - select is(count(*), 0::bigint) from wh_user_dimension; + select is(count(*), 0::bigint) from wh_user_dimension where user_id = 'u_____walter'; insert into wh_user_dimension ( - id, + key, user_id, user_name, user_description, auth_account_id, auth_account_type, auth_account_name, auth_account_description, auth_account_external_id, auth_account_full_name, auth_account_email, @@ -34,8 +34,8 @@ begin; select lives_ok($$select wh_upsert_user('u_____walter', 'tok___walter')$$); -- upsert should insert a user_dimension - select is(count(*), 2::bigint) from wh_user_dimension; - select is(count(*), 1::bigint) from wh_user_dimension where current_row_indicator = 'Current'; + select is(count(*), 2::bigint) from wh_user_dimension where user_id = 'u_____walter'; + select is(count(*), 1::bigint) from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; select * from finish(); rollback; diff --git a/internal/db/sqltest/tests/wh/user_dimension/oidc_auth_new_session.sql b/internal/db/sqltest/tests/wh/user_dimension/oidc_auth_new_session.sql index 92865ee50..0bbeab1b2 100644 --- a/internal/db/sqltest/tests/wh/user_dimension/oidc_auth_new_session.sql +++ b/internal/db/sqltest/tests/wh/user_dimension/oidc_auth_new_session.sql @@ -6,7 +6,7 @@ begin; select wtt_load('widgets', 'iam', 'kms', 'auth', 'hosts', 'targets'); -- ensure no existing dimensions - select is(count(*), 0::bigint) from wh_user_dimension; + select is(count(*), 0::bigint) from wh_user_dimension where user_organization_id = 'o_____widget'; -- insert first session, should result in a new user dimension insert into session @@ -14,30 +14,30 @@ begin; values ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____walter' , 'oidc__walter' , 'abc'::bytea , 'ep1' , 's1____walter'); - select is(count(*), 1::bigint) from wh_user_dimension; - select is(user_id, 'u_____walter') from wh_user_dimension; - select is(user_name, 'Walter') from wh_user_dimension; - select is(user_description, 'None') from wh_user_dimension; + select is(count(*), 1::bigint) from wh_user_dimension where user_id = 'u_____walter'; + select is(user_id, 'u_____walter') from wh_user_dimension where user_id = 'u_____walter'; + select is(user_name, 'Walter') from wh_user_dimension where user_id = 'u_____walter'; + select is(user_description, 'None') from wh_user_dimension where user_id = 'u_____walter'; - select is(auth_account_id, 'aoa___walter') from wh_user_dimension; - select is(auth_account_type, 'oidc auth account') from wh_user_dimension; - select is(auth_account_name, 'walter account') from wh_user_dimension; - select is(auth_account_description, 'Walter OIDC Account') from wh_user_dimension; - select is(auth_account_external_id, 'sub___walter') from wh_user_dimension; - select is(auth_account_full_name, 'Walter') from wh_user_dimension; - select is(auth_account_email, 'walter@widget.test') from wh_user_dimension; + select is(auth_account_id, 'aoa___walter') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_type, 'oidc auth account') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_name, 'walter account') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_description, 'Walter OIDC Account') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_external_id, 'sub___walter') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_full_name, 'Walter') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_email, 'walter@widget.test') from wh_user_dimension where user_id = 'u_____walter'; - select is(auth_method_id, 'aom___widget') from wh_user_dimension; - select is(auth_method_type, 'oidc auth method') from wh_user_dimension; - select is(auth_method_name, 'Widget OIDC') from wh_user_dimension; - select is(auth_method_description, 'None') from wh_user_dimension; - select is(auth_method_external_id, 'https://oidc.widget.test') from wh_user_dimension; + select is(auth_method_id, 'aom___widget') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_method_type, 'oidc auth method') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_method_name, 'Widget OIDC') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_method_description, 'None') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_method_external_id, 'https://oidc.widget.test') from wh_user_dimension where user_id = 'u_____walter'; - select is(user_organization_id, 'o_____widget') from wh_user_dimension; - select is(user_organization_name, 'Widget Inc') from wh_user_dimension; - select is(user_organization_description, 'None') from wh_user_dimension; + select is(user_organization_id, 'o_____widget') from wh_user_dimension where user_id = 'u_____walter'; + select is(user_organization_name, 'Widget Inc') from wh_user_dimension where user_id = 'u_____walter'; + select is(user_organization_description, 'None') from wh_user_dimension where user_id = 'u_____walter'; - select is(current_row_indicator, 'Current') from wh_user_dimension; + select is(current_row_indicator, 'Current') from wh_user_dimension where user_id = 'u_____walter'; -- insert session without full name or email insert into session diff --git a/internal/db/sqltest/tests/wh/user_dimension/password_auth_description_change.sql b/internal/db/sqltest/tests/wh/user_dimension/password_auth_description_change.sql index dd20a5e75..ce69f9a5e 100644 --- a/internal/db/sqltest/tests/wh/user_dimension/password_auth_description_change.sql +++ b/internal/db/sqltest/tests/wh/user_dimension/password_auth_description_change.sql @@ -7,7 +7,7 @@ begin; select wtt_load('widgets', 'iam', 'kms', 'auth', 'hosts', 'targets'); -- ensure no existing dimensions - select is(count(*), 0::bigint) from wh_user_dimension; + select is(count(*), 0::bigint) from wh_user_dimension where user_organization_id = 'o_____widget'; -- insert first session, should result in a new user dimension insert into session @@ -15,30 +15,30 @@ begin; values ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____walter' , 'tok___walter' , 'abc'::bytea , 'ep1' , 's1____walter'); - select is(count(*), 1::bigint) from wh_user_dimension; - select is(user_id, 'u_____walter') from wh_user_dimension; - select is(user_name, 'Walter') from wh_user_dimension; - select is(user_description, 'None') from wh_user_dimension; + select is(count(*), 1::bigint) from wh_user_dimension where user_id = 'u_____walter'; + select is(user_id, 'u_____walter') from wh_user_dimension where user_id = 'u_____walter'; + select is(user_name, 'Walter') from wh_user_dimension where user_id = 'u_____walter'; + select is(user_description, 'None') from wh_user_dimension where user_id = 'u_____walter'; - select is(auth_account_id, 'apa___walter') from wh_user_dimension; - select is(auth_account_type, 'password auth account') from wh_user_dimension; - select is(auth_account_name, 'None') from wh_user_dimension; - select is(auth_account_description, 'None') from wh_user_dimension; - select is(auth_account_external_id, 'Not Applicable') from wh_user_dimension; - select is(auth_account_full_name, 'Not Applicable') from wh_user_dimension; - select is(auth_account_email, 'Not Applicable') from wh_user_dimension; + select is(auth_account_id, 'apa___walter') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_type, 'password auth account') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_name, 'None') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_description, 'None') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_external_id, 'Not Applicable') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_full_name, 'Not Applicable') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_email, 'Not Applicable') from wh_user_dimension where user_id = 'u_____walter'; - select is(auth_method_id, 'apm___widget') from wh_user_dimension; - select is(auth_method_type, 'password auth method') from wh_user_dimension; - select is(auth_method_name, 'Widget Auth Password') from wh_user_dimension; - select is(auth_method_description, 'None') from wh_user_dimension; - select is(auth_method_external_id, 'Not Applicable') from wh_user_dimension; + select is(auth_method_id, 'apm___widget') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_method_type, 'password auth method') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_method_name, 'Widget Auth Password') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_method_description, 'None') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_method_external_id, 'Not Applicable') from wh_user_dimension where user_id = 'u_____walter'; - select is(user_organization_id, 'o_____widget') from wh_user_dimension; - select is(user_organization_name, 'Widget Inc') from wh_user_dimension; - select is(user_organization_description, 'None') from wh_user_dimension; + select is(user_organization_id, 'o_____widget') from wh_user_dimension where user_id = 'u_____walter'; + select is(user_organization_name, 'Widget Inc') from wh_user_dimension where user_id = 'u_____walter'; + select is(user_organization_description, 'None') from wh_user_dimension where user_id = 'u_____walter'; - select is(current_row_indicator, 'Current') from wh_user_dimension; + select is(current_row_indicator, 'Current') from wh_user_dimension where user_id = 'u_____walter'; -- change auth description update auth_password_account set @@ -56,29 +56,29 @@ begin; values ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____walter' , 'tok___walter' , 'abc'::bytea , 'ep1' , 's2____walter'); - select is(count(*), 2::bigint) from wh_user_dimension; + select is(count(*), 2::bigint) from wh_user_dimension where user_organization_id = 'o_____widget'; - select is(user_id, 'u_____walter') from wh_user_dimension where current_row_indicator = 'Current'; - select is(user_name, 'Walter') from wh_user_dimension where current_row_indicator = 'Current'; - select is(user_description, 'None') from wh_user_dimension where current_row_indicator = 'Current'; + select is(user_id, 'u_____walter') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; + select is(user_name, 'Walter') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; + select is(user_description, 'None') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; - select is(auth_account_id, 'apa___walter') from wh_user_dimension where current_row_indicator = 'Current'; - select is(auth_account_type, 'password auth account') from wh_user_dimension where current_row_indicator = 'Current'; - select is(auth_account_name, 'None') from wh_user_dimension where current_row_indicator = 'Current'; - select is(auth_account_description, 'Walter Password Account') from wh_user_dimension where current_row_indicator = 'Current'; - select is(auth_account_external_id, 'Not Applicable') from wh_user_dimension where current_row_indicator = 'Current'; - select is(auth_account_full_name, 'Not Applicable') from wh_user_dimension where current_row_indicator = 'Current'; - select is(auth_account_email, 'Not Applicable') from wh_user_dimension where current_row_indicator = 'Current'; + select is(auth_account_id, 'apa___walter') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; + select is(auth_account_type, 'password auth account') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; + select is(auth_account_name, 'None') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; + select is(auth_account_description, 'Walter Password Account') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; + select is(auth_account_external_id, 'Not Applicable') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; + select is(auth_account_full_name, 'Not Applicable') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; + select is(auth_account_email, 'Not Applicable') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; - select is(auth_method_id, 'apm___widget') from wh_user_dimension where current_row_indicator = 'Current'; - select is(auth_method_type, 'password auth method') from wh_user_dimension where current_row_indicator = 'Current'; - select is(auth_method_name, 'Widget Auth Password') from wh_user_dimension where current_row_indicator = 'Current'; - select is(auth_method_description, 'None') from wh_user_dimension where current_row_indicator = 'Current'; - select is(auth_method_external_id, 'Not Applicable') from wh_user_dimension where current_row_indicator = 'Current'; + select is(auth_method_id, 'apm___widget') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; + select is(auth_method_type, 'password auth method') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; + select is(auth_method_name, 'Widget Auth Password') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; + select is(auth_method_description, 'None') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; + select is(auth_method_external_id, 'Not Applicable') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; - select is(user_organization_id, 'o_____widget') from wh_user_dimension where current_row_indicator = 'Current'; - select is(user_organization_name, 'Widget Inc') from wh_user_dimension where current_row_indicator = 'Current'; - select is(user_organization_description, 'None') from wh_user_dimension where current_row_indicator = 'Current'; + select is(user_organization_id, 'o_____widget') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; + select is(user_organization_name, 'Widget Inc') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; + select is(user_organization_description, 'None') from wh_user_dimension where user_id = 'u_____walter' and current_row_indicator = 'Current'; select * from finish(); rollback; diff --git a/internal/db/sqltest/tests/wh/user_dimension/password_auth_new_session.sql b/internal/db/sqltest/tests/wh/user_dimension/password_auth_new_session.sql index 531d45a51..95fef385e 100644 --- a/internal/db/sqltest/tests/wh/user_dimension/password_auth_new_session.sql +++ b/internal/db/sqltest/tests/wh/user_dimension/password_auth_new_session.sql @@ -6,7 +6,7 @@ begin; select wtt_load('widgets', 'iam', 'kms', 'auth', 'hosts', 'targets'); -- ensure no existing dimensions - select is(count(*), 0::bigint) from wh_user_dimension; + select is(count(*), 0::bigint) from wh_user_dimension where user_organization_id = 'o_____widget'; -- insert first session, should result in a new user dimension insert into session @@ -14,30 +14,30 @@ begin; values ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____walter' , 'tok___walter' , 'abc'::bytea , 'ep1' , 's1____walter'); - select is(count(*), 1::bigint) from wh_user_dimension; - select is(user_id, 'u_____walter') from wh_user_dimension; - select is(user_name, 'Walter') from wh_user_dimension; - select is(user_description, 'None') from wh_user_dimension; + select is(count(*), 1::bigint) from wh_user_dimension where user_id = 'u_____walter'; + select is(user_id, 'u_____walter') from wh_user_dimension where user_id = 'u_____walter'; + select is(user_name, 'Walter') from wh_user_dimension where user_id = 'u_____walter'; + select is(user_description, 'None') from wh_user_dimension where user_id = 'u_____walter'; - select is(auth_account_id, 'apa___walter') from wh_user_dimension; - select is(auth_account_type, 'password auth account') from wh_user_dimension; - select is(auth_account_name, 'None') from wh_user_dimension; - select is(auth_account_description, 'None') from wh_user_dimension; - select is(auth_account_external_id, 'Not Applicable') from wh_user_dimension; - select is(auth_account_full_name, 'Not Applicable') from wh_user_dimension; - select is(auth_account_email, 'Not Applicable') from wh_user_dimension; + select is(auth_account_id, 'apa___walter') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_type, 'password auth account') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_name, 'None') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_description, 'None') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_external_id, 'Not Applicable') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_full_name, 'Not Applicable') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_account_email, 'Not Applicable') from wh_user_dimension where user_id = 'u_____walter'; - select is(auth_method_id, 'apm___widget') from wh_user_dimension; - select is(auth_method_type, 'password auth method') from wh_user_dimension; - select is(auth_method_name, 'Widget Auth Password') from wh_user_dimension; - select is(auth_method_description, 'None') from wh_user_dimension; - select is(auth_method_external_id, 'Not Applicable') from wh_user_dimension; + select is(auth_method_id, 'apm___widget') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_method_type, 'password auth method') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_method_name, 'Widget Auth Password') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_method_description, 'None') from wh_user_dimension where user_id = 'u_____walter'; + select is(auth_method_external_id, 'Not Applicable') from wh_user_dimension where user_id = 'u_____walter'; - select is(user_organization_id, 'o_____widget') from wh_user_dimension; - select is(user_organization_name, 'Widget Inc') from wh_user_dimension; - select is(user_organization_description, 'None') from wh_user_dimension; + select is(user_organization_id, 'o_____widget') from wh_user_dimension where user_id = 'u_____walter'; + select is(user_organization_name, 'Widget Inc') from wh_user_dimension where user_id = 'u_____walter'; + select is(user_organization_description, 'None') from wh_user_dimension where user_id = 'u_____walter'; - select is(current_row_indicator, 'Current') from wh_user_dimension; + select is(current_row_indicator, 'Current') from wh_user_dimension where user_id = 'u_____walter'; select * from finish(); rollback; diff --git a/internal/db/sqltest/tests/wh/user_dimension/session_multiple_sessions.sql b/internal/db/sqltest/tests/wh/user_dimension/session_multiple_sessions.sql index dd7c6713a..cae2bbc2b 100644 --- a/internal/db/sqltest/tests/wh/user_dimension/session_multiple_sessions.sql +++ b/internal/db/sqltest/tests/wh/user_dimension/session_multiple_sessions.sql @@ -6,7 +6,7 @@ begin; select wtt_load('widgets', 'iam', 'kms', 'auth', 'hosts', 'targets'); -- ensure no existing dimensions - select is(count(*), 0::bigint) from wh_user_dimension; + select is(count(*), 0::bigint) from wh_user_dimension where user_organization_id = 'o_____widget'; -- insert first session, should result in a new user dimension insert into session @@ -14,7 +14,7 @@ begin; values ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____walter' , 'tok___walter' , 'abc'::bytea , 'ep1' , 's1____walter'); - select is(count(*), 1::bigint) from wh_user_dimension; + select is(count(*), 1::bigint) from wh_user_dimension where user_organization_id = 'o_____widget'; -- another session with: -- * same user @@ -26,7 +26,7 @@ begin; values ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____walter' , 'tok___walter' , 'abc'::bytea , 'ep1' , 's2____walter'); - select is(count(*), 1::bigint) from wh_user_dimension; + select is(count(*), 1::bigint) from wh_user_dimension where user_organization_id = 'o_____widget'; -- another session with: -- * same user @@ -38,7 +38,7 @@ begin; values ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__02' , 'u_____walter' , 'tok___walter' , 'abc'::bytea , 'ep1' , 's3____walter'); - select is(count(*), 1::bigint) from wh_user_dimension; + select is(count(*), 1::bigint) from wh_user_dimension where user_organization_id = 'o_____widget'; select * from finish(); rollback; diff --git a/internal/db/sqltest/tests/wh/user_dimension/session_multiple_sessions_different_auth.sql b/internal/db/sqltest/tests/wh/user_dimension/session_multiple_sessions_different_auth.sql index 824a73771..659417d6b 100644 --- a/internal/db/sqltest/tests/wh/user_dimension/session_multiple_sessions_different_auth.sql +++ b/internal/db/sqltest/tests/wh/user_dimension/session_multiple_sessions_different_auth.sql @@ -7,7 +7,7 @@ begin; select wtt_load('widgets', 'iam', 'kms', 'auth', 'hosts', 'targets'); -- ensure no existing dimensions - select is(count(*), 0::bigint) from wh_user_dimension; + select is(count(*), 0::bigint) from wh_user_dimension where user_organization_id = 'o_____widget'; -- insert first session, should result in a new user dimension insert into session @@ -15,7 +15,7 @@ begin; values ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____walter' , 'tok___walter' , 'abc'::bytea , 'ep1' , 's1____walter'); - select is(count(*), 1::bigint) from wh_user_dimension; + select is(count(*), 1::bigint) from wh_user_dimension where user_organization_id = 'o_____widget'; -- another session with: -- * same user @@ -27,28 +27,28 @@ begin; values ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____walter' , 'tok1__walter' , 'abc'::bytea , 'ep1' , 's4____walter'); - select is(count(*), 2::bigint) from wh_user_dimension; - select is(count(*), 1::bigint) from wh_user_dimension where auth_account_id = 'apa1__walter'; + select is(count(*), 2::bigint) from wh_user_dimension where user_organization_id = 'o_____widget'; + select is(count(*), 1::bigint) from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; - select is(user_id, 'u_____walter') from wh_user_dimension where auth_account_id = 'apa1__walter'; - select is(user_name, 'Walter') from wh_user_dimension where auth_account_id = 'apa1__walter'; - select is(user_description, 'None') from wh_user_dimension where auth_account_id = 'apa1__walter'; + select is(user_id, 'u_____walter') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; + select is(user_name, 'Walter') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; + select is(user_description, 'None') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; - select is(auth_account_id, 'apa1__walter') from wh_user_dimension where auth_account_id = 'apa1__walter'; - select is(auth_account_type, 'password auth account') from wh_user_dimension where auth_account_id = 'apa1__walter'; - select is(auth_account_name, 'None') from wh_user_dimension where auth_account_id = 'apa1__walter'; - select is(auth_account_description, 'None') from wh_user_dimension where auth_account_id = 'apa1__walter'; + select is(auth_account_id, 'apa1__walter') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; + select is(auth_account_type, 'password auth account') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; + select is(auth_account_name, 'None') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; + select is(auth_account_description, 'None') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; - select is(auth_method_id, 'apm1__widget') from wh_user_dimension where auth_account_id = 'apa1__walter'; - select is(auth_method_type, 'password auth method') from wh_user_dimension where auth_account_id = 'apa1__walter'; - select is(auth_method_name, 'Widget Auth Password 1') from wh_user_dimension where auth_account_id = 'apa1__walter'; - select is(auth_method_description, 'None') from wh_user_dimension where auth_account_id = 'apa1__walter'; + select is(auth_method_id, 'apm1__widget') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; + select is(auth_method_type, 'password auth method') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; + select is(auth_method_name, 'Widget Auth Password 1') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; + select is(auth_method_description, 'None') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; - select is(user_organization_id, 'o_____widget') from wh_user_dimension where auth_account_id = 'apa1__walter'; - select is(user_organization_name, 'Widget Inc') from wh_user_dimension where auth_account_id = 'apa1__walter'; - select is(user_organization_description, 'None') from wh_user_dimension where auth_account_id = 'apa1__walter'; + select is(user_organization_id, 'o_____widget') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; + select is(user_organization_name, 'Widget Inc') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; + select is(user_organization_description, 'None') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; - select is(current_row_indicator, 'Current') from wh_user_dimension where auth_account_id = 'apa1__walter'; + select is(current_row_indicator, 'Current') from wh_user_dimension where user_id = 'u_____walter' and auth_account_id = 'apa1__walter'; select * from finish(); rollback; diff --git a/internal/db/sqltest/tests/wh/user_dimension/session_update.sql b/internal/db/sqltest/tests/wh/user_dimension/session_update.sql index 201502ce0..962a659ed 100644 --- a/internal/db/sqltest/tests/wh/user_dimension/session_update.sql +++ b/internal/db/sqltest/tests/wh/user_dimension/session_update.sql @@ -6,7 +6,7 @@ begin; select wtt_load('widgets', 'iam', 'kms', 'auth', 'hosts', 'targets'); -- ensure no existing dimensions - select is(count(*), 0::bigint) from wh_user_dimension; + select is(count(*), 0::bigint) from wh_user_dimension where user_organization_id = 'o_____widget'; -- insert first session, should result in a new user dimension insert into session @@ -14,7 +14,7 @@ begin; values ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____walter' , 'tok___walter' , 'abc'::bytea , 'ep1' , 's1____walter'); - select is(count(*), 1::bigint) from wh_user_dimension; + select is(count(*), 1::bigint) from wh_user_dimension where user_organization_id = 'o_____widget'; -- update session, should not impact wh_user_dimension update session set @@ -22,7 +22,7 @@ begin; where public_id = 's1____walter'; - select is(count(*), 1::bigint) from wh_user_dimension; + select is(count(*), 1::bigint) from wh_user_dimension where user_organization_id = 'o_____widget'; select * from finish(); rollback; diff --git a/internal/db/sqltest/tests/wh/user_dimension_views/target.sql b/internal/db/sqltest/tests/wh/user_dimension_views/target.sql index f79ede236..a44acd562 100644 --- a/internal/db/sqltest/tests/wh/user_dimension_views/target.sql +++ b/internal/db/sqltest/tests/wh/user_dimension_views/target.sql @@ -6,7 +6,7 @@ begin; insert into wh_user_dimension ( - id, + key, user_id, user_name, user_description, auth_account_id, auth_account_type, auth_account_name, auth_account_description, auth_account_external_id, auth_account_full_name, auth_account_email, From 90e2c871f0339cb308f9d65fceb9b123904a3baf Mon Sep 17 00:00:00 2001 From: Timothy Messier Date: Mon, 23 Aug 2021 15:46:47 -0400 Subject: [PATCH 3/3] refactor(wh): Remove host prefix from wh_user_dimension org fields These fields are being refactored so they conform with the new wh_credential_dimension. This will inform Business Intelligence tools that these attributes conform across dimensions. --- .../15/02_wh_rename_host_dimension_org.up.sql | 156 +++++ internal/db/schema/postgres_migration.gen.go | 607 +++++++++++++++++- .../session_multiple_sessions.sql | 45 ++ .../wh/host_dimension/session_update.sql | 28 + .../tests/wh/host_dimension_views/source.sql | 22 + .../tests/wh/host_dimension_views/target.sql | 61 ++ .../tests/wh/user_dimension_views/source.sql | 2 +- 7 files changed, 919 insertions(+), 2 deletions(-) create mode 100644 internal/db/schema/migrations/postgres/15/02_wh_rename_host_dimension_org.up.sql create mode 100644 internal/db/sqltest/tests/wh/host_dimension/session_multiple_sessions.sql create mode 100644 internal/db/sqltest/tests/wh/host_dimension/session_update.sql create mode 100644 internal/db/sqltest/tests/wh/host_dimension_views/source.sql create mode 100644 internal/db/sqltest/tests/wh/host_dimension_views/target.sql diff --git a/internal/db/schema/migrations/postgres/15/02_wh_rename_host_dimension_org.up.sql b/internal/db/schema/migrations/postgres/15/02_wh_rename_host_dimension_org.up.sql new file mode 100644 index 000000000..d40a297b4 --- /dev/null +++ b/internal/db/schema/migrations/postgres/15/02_wh_rename_host_dimension_org.up.sql @@ -0,0 +1,156 @@ +begin; + alter table wh_host_dimension + rename column host_organization_id to organization_id; + alter table wh_host_dimension + rename column host_organization_name to organization_name; + alter table wh_host_dimension + rename column host_organization_description to organization_description; + + -- replaces view from 0/65_wh_session_dimensions.up.sql + drop view whx_host_dimension_source; + create view whx_host_dimension_source as + select -- id is the first column in the target view + h.public_id as host_id, + 'static host' as host_type, + coalesce(h.name, 'None') as host_name, + coalesce(h.description, 'None') as host_description, + coalesce(h.address, 'Unknown') as host_address, + s.public_id as host_set_id, + 'static host set' as host_set_type, + coalesce(s.name, 'None') as host_set_name, + coalesce(s.description, 'None') as host_set_description, + c.public_id as host_catalog_id, + 'static host catalog' as host_catalog_type, + coalesce(c.name, 'None') as host_catalog_name, + coalesce(c.description, 'None') as host_catalog_description, + t.public_id as target_id, + 'tcp target' as target_type, + coalesce(t.name, 'None') as target_name, + coalesce(t.description, 'None') as target_description, + coalesce(t.default_port, 0) as target_default_port_number, + t.session_max_seconds as target_session_max_seconds, + t.session_connection_limit as target_session_connection_limit, + p.public_id as project_id, + coalesce(p.name, 'None') as project_name, + coalesce(p.description, 'None') as project_description, + o.public_id as organization_id, + coalesce(o.name, 'None') as organization_name, + coalesce(o.description, 'None') as organization_description + from static_host as h, + static_host_catalog as c, + static_host_set_member as m, + static_host_set as s, + target_host_set as ts, + target_tcp as t, + iam_scope as p, + iam_scope as o + where h.catalog_id = c.public_id + and h.public_id = m.host_id + and s.public_id = m.set_id + and t.public_id = ts.target_id + and s.public_id = ts.host_set_id + and p.public_id = t.scope_id + and p.type = 'project' + and o.public_id = p.parent_id + and o.type = 'org' + ; + + -- replaces view from 15/01_wh_rename_key_columns.up.sql + drop view whx_host_dimension_target; + create view whx_host_dimension_target as + select key, + host_id, + host_type, + host_name, + host_description, + host_address, + host_set_id, + host_set_type, + host_set_name, + host_set_description, + host_catalog_id, + host_catalog_type, + host_catalog_name, + host_catalog_description, + target_id, + target_type, + target_name, + target_description, + target_default_port_number, + target_session_max_seconds, + target_session_connection_limit, + project_id, + project_name, + project_description, + organization_id, + organization_name, + organization_description + from wh_host_dimension + where current_row_indicator = 'Current' + ; + + -- replaces function from 15/01_wh_rename_key_columns.up.sql + drop function wh_upsert_host; + create function wh_upsert_host(p_host_id wt_public_id, p_host_set_id wt_public_id, p_target_id wt_public_id) + returns wh_dim_key + as $$ + declare + src whx_host_dimension_target%rowtype; + target whx_host_dimension_target%rowtype; + new_row wh_host_dimension%rowtype; + begin + select * into target + from whx_host_dimension_target as t + where t.host_id = p_host_id + and t.host_set_id = p_host_set_id + and t.target_id = p_target_id; + + select target.key, t.* into src + from whx_host_dimension_source as t + where t.host_id = p_host_id + and t.host_set_id = p_host_set_id + and t.target_id = p_target_id; + + if src is distinct from target then + + -- expire the current row + update wh_host_dimension + set current_row_indicator = 'Expired', + row_expiration_time = current_timestamp + where host_id = p_host_id + and host_set_id = p_host_set_id + and target_id = p_target_id + and current_row_indicator = 'Current'; + + -- insert a new row + insert into wh_host_dimension ( + host_id, host_type, host_name, host_description, host_address, + host_set_id, host_set_type, host_set_name, host_set_description, + host_catalog_id, host_catalog_type, host_catalog_name, host_catalog_description, + target_id, target_type, target_name, target_description, + target_default_port_number, target_session_max_seconds, target_session_connection_limit, + project_id, project_name, project_description, + organization_id, organization_name, organization_description, + current_row_indicator, row_effective_time, row_expiration_time + ) + select host_id, host_type, host_name, host_description, host_address, + host_set_id, host_set_type, host_set_name, host_set_description, + host_catalog_id, host_catalog_type, host_catalog_name, host_catalog_description, + target_id, target_type, target_name, target_description, + target_default_port_number, target_session_max_seconds, target_session_connection_limit, + project_id, project_name, project_description, + organization_id, organization_name, organization_description, + 'Current', current_timestamp, 'infinity'::timestamptz + from whx_host_dimension_source + where host_id = p_host_id + and host_set_id = p_host_set_id + and target_id = p_target_id + returning * into new_row; + + return new_row.key; + end if; + return target.key; + + end; + $$ language plpgsql; +commit; diff --git a/internal/db/schema/postgres_migration.gen.go b/internal/db/schema/postgres_migration.gen.go index 4c8a30c99..852676e63 100644 --- a/internal/db/schema/postgres_migration.gen.go +++ b/internal/db/schema/postgres_migration.gen.go @@ -4,7 +4,7 @@ package schema func init() { migrationStates["postgres"] = migrationState{ - binarySchemaVersion: 14001, + binarySchemaVersion: 15002, upMigrations: map[int][]byte{ 1: []byte(` create domain wt_public_id as text @@ -6331,6 +6331,611 @@ alter table wh_user_dimension end if; return target.id; + end; + $$ language plpgsql; +`), + 15001: []byte(` +alter table wh_date_dimension + rename column id to key; + + alter table wh_time_of_day_dimension + rename column id to key; + + alter table wh_host_dimension + rename column id to key; + + alter table wh_user_dimension + rename column id to key; + + alter table wh_session_accumulating_fact + rename column host_id to host_key; + alter table wh_session_accumulating_fact + rename column user_id to user_key; + alter table wh_session_accumulating_fact + rename column session_pending_date_id to session_pending_date_key; + alter table wh_session_accumulating_fact + rename column session_pending_time_id to session_pending_time_key; + alter table wh_session_accumulating_fact + rename column session_active_date_id to session_active_date_key; + alter table wh_session_accumulating_fact + rename column session_active_time_id to session_active_time_key; + alter table wh_session_accumulating_fact + rename column session_canceling_date_id to session_canceling_date_key; + alter table wh_session_accumulating_fact + rename column session_canceling_time_id to session_canceling_time_key; + alter table wh_session_accumulating_fact + rename column session_terminated_date_id to session_terminated_date_key; + alter table wh_session_accumulating_fact + rename column session_terminated_time_id to session_terminated_time_key; + + alter table wh_session_connection_accumulating_fact + rename column host_id to host_key; + alter table wh_session_connection_accumulating_fact + rename column user_id to user_key; + alter table wh_session_connection_accumulating_fact + rename column connection_authorized_date_id to connection_authorized_date_key; + alter table wh_session_connection_accumulating_fact + rename column connection_authorized_time_id to connection_authorized_time_key; + alter table wh_session_connection_accumulating_fact + rename column connection_connected_date_id to connection_connected_date_key; + alter table wh_session_connection_accumulating_fact + rename column connection_connected_time_id to connection_connected_time_key; + alter table wh_session_connection_accumulating_fact + rename column connection_closed_date_id to connection_closed_date_key; + alter table wh_session_connection_accumulating_fact + rename column connection_closed_time_id to connection_closed_time_key; + + -- rename function from internal/db/schema/migrations/postgres/0/60_wh_domain_types.up.sql + alter function wh_dim_id rename to wh_dim_key; + + -- rename domain from internal/db/schema/migrations/postgres/0/60_wh_domain_types.up.sql + alter domain wh_dim_id rename to wh_dim_key; + + -- removes unused function from internal/db/schema/migrations/postgres/0/60_wh_domain_types.up.sql + drop function wh_current_date_id; + + -- removes unused function from internal/db/schema/migrations/postgres/0/60_wh_domain_types.up.sql + drop function wh_current_time_id; + + -- rename function from internal/db/schema/migrations/postgres/0/60_wh_domain_types.up.sql + alter function wh_date_id rename to wh_date_key; + + -- rename function from internal/db/schema/migrations/postgres/0/60_wh_domain_types.up.sql + alter function wh_time_id rename to wh_time_key; + + -- replaces view from 14/01_wh_user_dimension_oidc.up.sql + drop view whx_user_dimension_target; + create view whx_user_dimension_target as + select key, + user_id, + user_name, + user_description, + auth_account_id, + auth_account_type, + auth_account_name, + auth_account_description, + auth_account_external_id, + auth_account_full_name, + auth_account_email, + auth_method_id, + auth_method_type, + auth_method_name, + auth_method_description, + auth_method_external_id, + user_organization_id, + user_organization_name, + user_organization_description + from wh_user_dimension + where current_row_indicator = 'Current' + ; + + -- replaces function from 14/01_wh_user_dimension_oidc.up.sql + drop function wh_upsert_user; + create function wh_upsert_user(p_user_id wt_user_id, p_auth_token_id wt_public_id) + returns wh_dim_key + as $$ + declare + src whx_user_dimension_target%rowtype; + target whx_user_dimension_target%rowtype; + new_row wh_user_dimension%rowtype; + acct_id wt_public_id; + begin + select auth_account_id into strict acct_id + from auth_token + where public_id = p_auth_token_id; + + select * into target + from whx_user_dimension_target as t + where t.user_id = p_user_id + and t.auth_account_id = acct_id; + + select target.key, t.* into src + from whx_user_dimension_source as t + where t.user_id = p_user_id + and t.auth_account_id = acct_id; + + if src is distinct from target then + + -- expire the current row + update wh_user_dimension + set current_row_indicator = 'Expired', + row_expiration_time = current_timestamp + where user_id = p_user_id + and auth_account_id = acct_id + and current_row_indicator = 'Current'; + + -- insert a new row + insert into wh_user_dimension ( + user_id, user_name, user_description, + auth_account_id, auth_account_type, auth_account_name, auth_account_description, + auth_account_external_id, auth_account_full_name, auth_account_email, + auth_method_id, auth_method_type, auth_method_name, auth_method_description, + auth_method_external_id, + user_organization_id, user_organization_name, user_organization_description, + current_row_indicator, row_effective_time, row_expiration_time + ) + select user_id, user_name, user_description, + auth_account_id, auth_account_type, auth_account_name, auth_account_description, + auth_account_external_id, auth_account_full_name, auth_account_email, + auth_method_id, auth_method_type, auth_method_name, auth_method_description, + auth_method_external_id, + user_organization_id, user_organization_name, user_organization_description, + 'Current', current_timestamp, 'infinity'::timestamptz + from whx_user_dimension_source + where user_id = p_user_id + and auth_account_id = acct_id + returning * into new_row; + + return new_row.key; + end if; + return target.key; + + end; + $$ language plpgsql; + + -- replaces view from 0/65_wh_session_dimensions.up.sql + drop view whx_host_dimension_target; + create view whx_host_dimension_target as + select key, + host_id, + host_type, + host_name, + host_description, + host_address, + host_set_id, + host_set_type, + host_set_name, + host_set_description, + host_catalog_id, + host_catalog_type, + host_catalog_name, + host_catalog_description, + target_id, + target_type, + target_name, + target_description, + target_default_port_number, + target_session_max_seconds, + target_session_connection_limit, + project_id, + project_name, + project_description, + host_organization_id, + host_organization_name, + host_organization_description + from wh_host_dimension + where current_row_indicator = 'Current' + ; + + -- replaces function from 0/66_wh_session_dimensions.up.sql + drop function wh_upsert_host; + create function wh_upsert_host(p_host_id wt_public_id, p_host_set_id wt_public_id, p_target_id wt_public_id) + returns wh_dim_key + as $$ + declare + src whx_host_dimension_target%rowtype; + target whx_host_dimension_target%rowtype; + new_row wh_host_dimension%rowtype; + begin + select * into target + from whx_host_dimension_target as t + where t.host_id = p_host_id + and t.host_set_id = p_host_set_id + and t.target_id = p_target_id; + + select target.key, t.* into src + from whx_host_dimension_source as t + where t.host_id = p_host_id + and t.host_set_id = p_host_set_id + and t.target_id = p_target_id; + + if src is distinct from target then + + -- expire the current row + update wh_host_dimension + set current_row_indicator = 'Expired', + row_expiration_time = current_timestamp + where host_id = p_host_id + and host_set_id = p_host_set_id + and target_id = p_target_id + and current_row_indicator = 'Current'; + + -- insert a new row + insert into wh_host_dimension ( + host_id, host_type, host_name, host_description, host_address, + host_set_id, host_set_type, host_set_name, host_set_description, + host_catalog_id, host_catalog_type, host_catalog_name, host_catalog_description, + target_id, target_type, target_name, target_description, + target_default_port_number, target_session_max_seconds, target_session_connection_limit, + project_id, project_name, project_description, + host_organization_id, host_organization_name, host_organization_description, + current_row_indicator, row_effective_time, row_expiration_time + ) + select host_id, host_type, host_name, host_description, host_address, + host_set_id, host_set_type, host_set_name, host_set_description, + host_catalog_id, host_catalog_type, host_catalog_name, host_catalog_description, + target_id, target_type, target_name, target_description, + target_default_port_number, target_session_max_seconds, target_session_connection_limit, + project_id, project_name, project_description, + host_organization_id, host_organization_name, host_organization_description, + 'Current', current_timestamp, 'infinity'::timestamptz + from whx_host_dimension_source + where host_id = p_host_id + and host_set_id = p_host_set_id + and target_id = p_target_id + returning * into new_row; + + return new_row.key; + end if; + return target.key; + + end; + $$ language plpgsql; + + -- replaces function from 0/69_wh_session_facts.up.sql + drop trigger wh_insert_session on session; + drop function wh_insert_session; + create function wh_insert_session() + returns trigger + as $$ + declare + new_row wh_session_accumulating_fact%rowtype; + begin + with + pending_timestamp (date_dim_key, time_dim_key, ts) as ( + select wh_date_key(start_time), wh_time_key(start_time), start_time + from session_state + where session_id = new.public_id + and state = 'pending' + ) + insert into wh_session_accumulating_fact ( + session_id, + auth_token_id, + host_key, + user_key, + session_pending_date_key, + session_pending_time_key, + session_pending_time + ) + select new.public_id, + new.auth_token_id, + wh_upsert_host(new.host_id, new.host_set_id, new.target_id), + wh_upsert_user(new.user_id, new.auth_token_id), + pending_timestamp.date_dim_key, + pending_timestamp.time_dim_key, + pending_timestamp.ts + from pending_timestamp + returning * into strict new_row; + return null; + end; + $$ language plpgsql; + create trigger wh_insert_session + after insert on session + for each row + execute function wh_insert_session(); + + -- replaces function from 0/69_wh_session_facts.up.sql + drop trigger wh_insert_session_connection on session_connection; + drop function wh_insert_session_connection; + create function wh_insert_session_connection() + returns trigger + as $$ + declare + new_row wh_session_connection_accumulating_fact%rowtype; + begin + with + authorized_timestamp (date_dim_key, time_dim_key, ts) as ( + select wh_date_key(start_time), wh_time_key(start_time), start_time + from session_connection_state + where connection_id = new.public_id + and state = 'authorized' + ), + session_dimension (host_dim_key, user_dim_key) as ( + select host_key, user_key + from wh_session_accumulating_fact + where session_id = new.session_id + ) + insert into wh_session_connection_accumulating_fact ( + connection_id, + session_id, + host_key, + user_key, + connection_authorized_date_key, + connection_authorized_time_key, + connection_authorized_time, + client_tcp_address, + client_tcp_port_number, + endpoint_tcp_address, + endpoint_tcp_port_number, + bytes_up, + bytes_down + ) + select new.public_id, + new.session_id, + session_dimension.host_dim_key, + session_dimension.user_dim_key, + authorized_timestamp.date_dim_key, + authorized_timestamp.time_dim_key, + authorized_timestamp.ts, + new.client_tcp_address, + new.client_tcp_port, + new.endpoint_tcp_address, + new.endpoint_tcp_port, + new.bytes_up, + new.bytes_down + from authorized_timestamp, + session_dimension + returning * into strict new_row; + perform wh_rollup_connections(new.session_id); + return null; + end; + $$ language plpgsql; + + create trigger wh_insert_session_connection + after insert on session_connection + for each row + execute function wh_insert_session_connection(); + + -- replaces function from 0/69_wh_session_facts.up.sql + drop trigger wh_insert_session_state on session_state; + drop function wh_insert_session_state; + create function wh_insert_session_state() + returns trigger + as $$ + declare + date_col text; + time_col text; + ts_col text; + q text; + session_row wh_session_accumulating_fact%rowtype; + begin + if new.state = 'pending' then + -- The pending state is the first state which is handled by the + -- wh_insert_session trigger. The update statement in this trigger will + -- fail for the pending state because the row for the session has not yet + -- been inserted into the wh_session_accumulating_fact table. + return null; + end if; + + date_col = 'session_' || new.state || '_date_key'; + time_col = 'session_' || new.state || '_time_key'; + ts_col = 'session_' || new.state || '_time'; + + q = format('update wh_session_accumulating_fact + set (%I, %I, %I) = (select wh_date_key(%L), wh_time_key(%L), %L::timestamptz) + where session_id = %L + returning *', + date_col, time_col, ts_col, + new.start_time, new.start_time, new.start_time, + new.session_id); + execute q into strict session_row; + + return null; + end; + $$ language plpgsql; + + create trigger wh_insert_session_state + after insert on session_state + for each row + execute function wh_insert_session_state(); + + -- replaces function from 0/69_wh_session_facts.up.sql + drop trigger wh_insert_session_connection_state on session_connection_state; + drop function wh_insert_session_connection_state; + create function wh_insert_session_connection_state() + returns trigger + as $$ + declare + date_col text; + time_col text; + ts_col text; + q text; + connection_row wh_session_connection_accumulating_fact%rowtype; + begin + if new.state = 'authorized' then + -- The authorized state is the first state which is handled by the + -- wh_insert_session_connection trigger. The update statement in this + -- trigger will fail for the authorized state because the row for the + -- session connection has not yet been inserted into the + -- wh_session_connection_accumulating_fact table. + return null; + end if; + + date_col = 'connection_' || new.state || '_date_key'; + time_col = 'connection_' || new.state || '_time_key'; + ts_col = 'connection_' || new.state || '_time'; + + q = format('update wh_session_connection_accumulating_fact + set (%I, %I, %I) = (select wh_date_key(%L), wh_time_key(%L), %L::timestamptz) + where connection_id = %L + returning *', + date_col, time_col, ts_col, + new.start_time, new.start_time, new.start_time, + new.connection_id); + execute q into strict connection_row; + + return null; + end; + $$ language plpgsql; + + create trigger wh_insert_session_connection_state + after insert on session_connection_state + for each row + execute function wh_insert_session_connection_state(); +`), + 15002: []byte(` +alter table wh_host_dimension + rename column host_organization_id to organization_id; + alter table wh_host_dimension + rename column host_organization_name to organization_name; + alter table wh_host_dimension + rename column host_organization_description to organization_description; + + -- replaces view from 0/65_wh_session_dimensions.up.sql + drop view whx_host_dimension_source; + create view whx_host_dimension_source as + select -- id is the first column in the target view + h.public_id as host_id, + 'static host' as host_type, + coalesce(h.name, 'None') as host_name, + coalesce(h.description, 'None') as host_description, + coalesce(h.address, 'Unknown') as host_address, + s.public_id as host_set_id, + 'static host set' as host_set_type, + coalesce(s.name, 'None') as host_set_name, + coalesce(s.description, 'None') as host_set_description, + c.public_id as host_catalog_id, + 'static host catalog' as host_catalog_type, + coalesce(c.name, 'None') as host_catalog_name, + coalesce(c.description, 'None') as host_catalog_description, + t.public_id as target_id, + 'tcp target' as target_type, + coalesce(t.name, 'None') as target_name, + coalesce(t.description, 'None') as target_description, + coalesce(t.default_port, 0) as target_default_port_number, + t.session_max_seconds as target_session_max_seconds, + t.session_connection_limit as target_session_connection_limit, + p.public_id as project_id, + coalesce(p.name, 'None') as project_name, + coalesce(p.description, 'None') as project_description, + o.public_id as organization_id, + coalesce(o.name, 'None') as organization_name, + coalesce(o.description, 'None') as organization_description + from static_host as h, + static_host_catalog as c, + static_host_set_member as m, + static_host_set as s, + target_host_set as ts, + target_tcp as t, + iam_scope as p, + iam_scope as o + where h.catalog_id = c.public_id + and h.public_id = m.host_id + and s.public_id = m.set_id + and t.public_id = ts.target_id + and s.public_id = ts.host_set_id + and p.public_id = t.scope_id + and p.type = 'project' + and o.public_id = p.parent_id + and o.type = 'org' + ; + + -- replaces view from 15/01_wh_rename_key_columns.up.sql + drop view whx_host_dimension_target; + create view whx_host_dimension_target as + select key, + host_id, + host_type, + host_name, + host_description, + host_address, + host_set_id, + host_set_type, + host_set_name, + host_set_description, + host_catalog_id, + host_catalog_type, + host_catalog_name, + host_catalog_description, + target_id, + target_type, + target_name, + target_description, + target_default_port_number, + target_session_max_seconds, + target_session_connection_limit, + project_id, + project_name, + project_description, + organization_id, + organization_name, + organization_description + from wh_host_dimension + where current_row_indicator = 'Current' + ; + + -- replaces function from 15/01_wh_rename_key_columns.up.sql + drop function wh_upsert_host; + create function wh_upsert_host(p_host_id wt_public_id, p_host_set_id wt_public_id, p_target_id wt_public_id) + returns wh_dim_key + as $$ + declare + src whx_host_dimension_target%rowtype; + target whx_host_dimension_target%rowtype; + new_row wh_host_dimension%rowtype; + begin + select * into target + from whx_host_dimension_target as t + where t.host_id = p_host_id + and t.host_set_id = p_host_set_id + and t.target_id = p_target_id; + + select target.key, t.* into src + from whx_host_dimension_source as t + where t.host_id = p_host_id + and t.host_set_id = p_host_set_id + and t.target_id = p_target_id; + + if src is distinct from target then + + -- expire the current row + update wh_host_dimension + set current_row_indicator = 'Expired', + row_expiration_time = current_timestamp + where host_id = p_host_id + and host_set_id = p_host_set_id + and target_id = p_target_id + and current_row_indicator = 'Current'; + + -- insert a new row + insert into wh_host_dimension ( + host_id, host_type, host_name, host_description, host_address, + host_set_id, host_set_type, host_set_name, host_set_description, + host_catalog_id, host_catalog_type, host_catalog_name, host_catalog_description, + target_id, target_type, target_name, target_description, + target_default_port_number, target_session_max_seconds, target_session_connection_limit, + project_id, project_name, project_description, + organization_id, organization_name, organization_description, + current_row_indicator, row_effective_time, row_expiration_time + ) + select host_id, host_type, host_name, host_description, host_address, + host_set_id, host_set_type, host_set_name, host_set_description, + host_catalog_id, host_catalog_type, host_catalog_name, host_catalog_description, + target_id, target_type, target_name, target_description, + target_default_port_number, target_session_max_seconds, target_session_connection_limit, + project_id, project_name, project_description, + organization_id, organization_name, organization_description, + 'Current', current_timestamp, 'infinity'::timestamptz + from whx_host_dimension_source + where host_id = p_host_id + and host_set_id = p_host_set_id + and target_id = p_target_id + returning * into new_row; + + return new_row.key; + end if; + return target.key; + end; $$ language plpgsql; `), diff --git a/internal/db/sqltest/tests/wh/host_dimension/session_multiple_sessions.sql b/internal/db/sqltest/tests/wh/host_dimension/session_multiple_sessions.sql new file mode 100644 index 000000000..b80d4a3de --- /dev/null +++ b/internal/db/sqltest/tests/wh/host_dimension/session_multiple_sessions.sql @@ -0,0 +1,45 @@ +-- session_multiple_sessions tests the wh_host_dimesion when +-- multiple sessions are created using the same user and auth method. +begin; + select plan(4); + + select wtt_load('widgets', 'iam', 'kms', 'auth', 'hosts', 'targets'); + + -- ensure no existing dimensions + select is(count(*), 0::bigint) from wh_host_dimension where organization_id = 'o_____widget'; + + -- insert first session, should result in a new user dimension + insert into session + ( scope_id , target_id , host_set_id , host_id , user_id , auth_token_id , certificate , endpoint , public_id) + values + ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____walter' , 'tok___walter' , 'abc'::bytea , 'ep1' , 's1____walter'); + + select is(count(*), 1::bigint) from wh_host_dimension where organization_id = 'o_____widget'; + + -- another session with: + -- * same user + -- * same auth + -- * same host + -- should not result in a new user dimension + insert into session + ( scope_id , target_id , host_set_id , host_id , user_id , auth_token_id , certificate , endpoint , public_id) + values + ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____walter' , 'tok___walter' , 'abc'::bytea , 'ep1' , 's2____walter'); + + select is(count(*), 1::bigint) from wh_host_dimension where organization_id = 'o_____widget'; + + -- another session with: + -- * different user + -- * same auth + -- * same host + -- should not result in a new user dimension + insert into session + ( scope_id , target_id , host_set_id , host_id , user_id , auth_token_id , certificate , endpoint , public_id) + values + ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____warren' , 'tok___warren' , 'abc'::bytea , 'ep1' , 's3____walter'); + + select is(count(*), 1::bigint) from wh_host_dimension where organization_id = 'o_____widget'; + + select * from finish(); +rollback; + diff --git a/internal/db/sqltest/tests/wh/host_dimension/session_update.sql b/internal/db/sqltest/tests/wh/host_dimension/session_update.sql new file mode 100644 index 000000000..c99b9f7ed --- /dev/null +++ b/internal/db/sqltest/tests/wh/host_dimension/session_update.sql @@ -0,0 +1,28 @@ +-- session_update tests the wh_host_dimesion when +-- a session is inserted and then updated. +begin; + select plan(3); + + select wtt_load('widgets', 'iam', 'kms', 'auth', 'hosts', 'targets'); + + -- ensure no existing dimensions + select is(count(*), 0::bigint) from wh_host_dimension where organization_id = 'o_____widget'; + + -- insert first session, should result in a new user dimension + insert into session + ( scope_id , target_id , host_set_id , host_id , user_id , auth_token_id , certificate , endpoint , public_id) + values + ('p____bwidget' , 't_________wb' , 's___1wb-sths' , 'h_____wb__01' , 'u_____walter' , 'tok___walter' , 'abc'::bytea , 'ep1' , 's1____walter'); + + select is(count(*), 1::bigint) from wh_host_dimension where organization_id = 'o_____widget'; + + -- update session, should not impact wh_host_dimension + update session set + version = 2 + where + public_id = 's1____walter'; + + select is(count(*), 1::bigint) from wh_host_dimension where organization_id = 'o_____widget'; + + select * from finish(); +rollback; diff --git a/internal/db/sqltest/tests/wh/host_dimension_views/source.sql b/internal/db/sqltest/tests/wh/host_dimension_views/source.sql new file mode 100644 index 000000000..5c9bc77ae --- /dev/null +++ b/internal/db/sqltest/tests/wh/host_dimension_views/source.sql @@ -0,0 +1,22 @@ +-- source tests the whx_host_dimension_source view. +begin; + select plan(1); + select wtt_load('widgets', 'iam', 'kms', 'auth', 'hosts', 'targets'); + + -- auth_password_account + select is(s.*, row( + 'h_____wb__01', 'static host', 'None', 'None', '1.big.widget', + 's___2wb-sths', 'static host set', 'Big Widget Static Set 2', 'None', + 'c___wb-sthcl', 'static host catalog', 'Big Widget Static Catalog', 'None', + 't_________wb', 'tcp target', 'Big Widget Target', 'None', 0, 28800, 1, + 'p____bwidget', 'Big Widget Factory', 'None', + 'o_____widget', 'Widget Inc', 'None' + )::whx_host_dimension_source) + from whx_host_dimension_source as s + where s.host_id = 'h_____wb__01' + and s.host_set_id = 's___2wb-sths' + and s.target_id = 't_________wb'; + + select * from finish(); +rollback; + diff --git a/internal/db/sqltest/tests/wh/host_dimension_views/target.sql b/internal/db/sqltest/tests/wh/host_dimension_views/target.sql new file mode 100644 index 000000000..e70d3a8fa --- /dev/null +++ b/internal/db/sqltest/tests/wh/host_dimension_views/target.sql @@ -0,0 +1,61 @@ +-- target tests teh whx_host_dimension_target view. +begin; + select plan(2); + + select is_empty($$ + select * from whx_host_dimension_target + where host_id = 'h_____wb__01' + and host_set_id = 's___2wb-sths' + and target_id = 't_________wb'; + $$); + + insert into wh_host_dimension + ( + key, + host_id, host_type, host_name, host_description, host_address, + host_set_id, host_set_type, host_set_name, host_set_description, + host_catalog_id, host_catalog_type, host_catalog_name, host_catalog_description, + target_id, target_type, target_name, target_description, target_default_port_number, target_session_max_seconds, target_session_connection_limit, + project_id, project_name, project_description, + organization_id, organization_name, organization_description, + current_row_indicator, row_effective_time, row_expiration_time + ) + values + ( + 'whd_____1', + 'h_____wb__01', 'static host', 'None', 'None', '1.big.widget', + 's___2wb-sths', 'static host set', 'Big Widget Static Set 2', 'None', + 'c___wb-sthcl', 'static host catalog', 'Big Widget Static Catalog', 'None', + 't_________wb', 'tcp target', 'Big Widget Target', 'None', 0, 28800, 1, + 'p____bwidget', 'Big Widget Factory', 'None', + 'o_____widget', 'Widget Inc', 'None', + 'Expired', '2021-07-21T11:01'::timestamptz, '2021-07-21T12:01'::timestamptz + ), + ( + 'whd_____2', + 'h_____wb__01', 'static host', 'None', 'None', '1.big.widget', + 's___2wb-sths', 'static host set', 'Big Widget Static Set 2', 'None', + 'c___wb-sthcl', 'static host catalog', 'Big Widget Static Catalog', 'None', + 't_________wb', 'tcp target', 'Big Widget Target', 'None', 0, 28800, 1, + 'p____bwidget', 'Big Widget Factory', 'None', + 'o_____widget', 'Widget Inc', 'None', + 'Current', '2021-07-21T12:01'::timestamptz, 'infinity'::timestamptz + ); + + select is(t.*, row( + 'whd_____2', + 'h_____wb__01', 'static host', 'None', 'None', '1.big.widget', + 's___2wb-sths', 'static host set', 'Big Widget Static Set 2', 'None', + 'c___wb-sthcl', 'static host catalog', 'Big Widget Static Catalog', 'None', + 't_________wb', 'tcp target', 'Big Widget Target', 'None', 0, 28800, 1, + 'p____bwidget', 'Big Widget Factory', 'None', + 'o_____widget', 'Widget Inc', 'None' + )::whx_host_dimension_target) + from whx_host_dimension_target as t + where t.host_id = 'h_____wb__01' + and t.host_set_id = 's___2wb-sths' + and t.target_id = 't_________wb'; + + select * from finish(); +rollback; + diff --git a/internal/db/sqltest/tests/wh/user_dimension_views/source.sql b/internal/db/sqltest/tests/wh/user_dimension_views/source.sql index 9e2a1bfc5..e91078e8f 100644 --- a/internal/db/sqltest/tests/wh/user_dimension_views/source.sql +++ b/internal/db/sqltest/tests/wh/user_dimension_views/source.sql @@ -1,4 +1,4 @@ --- source tests teh whx_user_dimension_source view. +-- source tests the whx_user_dimension_source view. begin; select plan(5); select wtt_load('widgets', 'iam', 'kms', 'auth', 'hosts', 'targets');