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');