diff --git a/internal/db/schema/migrations/oss/postgres/100/02_recording_session.up.sql b/internal/db/schema/migrations/oss/postgres/100/02_recording_session.up.sql index cbf994a74c..e1afccfcdc 100644 --- a/internal/db/schema/migrations/oss/postgres/100/02_recording_session.up.sql +++ b/internal/db/schema/migrations/oss/postgres/100/02_recording_session.up.sql @@ -25,11 +25,11 @@ begin; alter table recording_session add column delete_after rec_timestamp constraint delete_after_null_or_after_retain_until - check(delete_after >= retain_until); + check(delete_after is null or delete_after >= retain_until); alter table recording_session add column delete_time rec_timestamp constraint delete_time_null_or_after_retain_until - check(delete_time >= retain_until); + check(delete_time is null or delete_time >= retain_until); alter table recording_session add column target_org_id wt_public_id null references iam_scope_org(scope_id) @@ -176,8 +176,8 @@ begin; rs.host_hst_id = shh.history_id left join host_plugin_host_hst as hph on rs.host_hst_id = hph.history_id - where (rs.delete_after is null or rs.delete_after < now()) - and (rs.delete_time is null or rs.delete_time < now()); + where (rs.delete_after is null or rs.delete_after > now()) + and (rs.delete_time is null or rs.delete_time > now()); comment on view session_recording_aggregate is 'session_recording_aggregate contains the session recording resource with its storage bucket scope info and historical user info.'; diff --git a/internal/db/sqltest/tests/policy/policy_storage_policy.sql b/internal/db/sqltest/tests/policy/policy_storage_policy.sql new file mode 100644 index 0000000000..676bd15cda --- /dev/null +++ b/internal/db/sqltest/tests/policy/policy_storage_policy.sql @@ -0,0 +1,47 @@ +-- Copyright (c) HashiCorp, Inc. +-- SPDX-License-Identifier: BUSL-1.1 + +begin; + + select plan(8); + + -- helper statements for resetting test env + prepare delete_policy_storage_policy_resource as + delete from policy_storage_policy where public_id = 'pst__bcolors'; + + -- test constraints + + -- retain_for_days and delete_after_days both cannot be set to 0 + prepare insert_policy_delete_after_days_and_retain_for_days_zero as + insert into policy_storage_policy + (public_id, scope_id, retain_for_days, delete_after_days) + values + ('pst__bcolors', 'global', 0, 0); + select throws_ok('insert_policy_delete_after_days_and_retain_for_days_zero', 'P0001', null, 'delete_after_days and retain_for_days both cannot be zero'); + select lives_ok('delete_policy_storage_policy_resource', 'policy_storage_policy cleanup'); + + prepare insert_policy_delete_after_days_negative as + insert into policy_storage_policy + (public_id, scope_id, retain_for_days, delete_after_days) + values + ('pst__bcolors', 'global', 10, -1); + select throws_ok('insert_policy_delete_after_days_negative', 23514, null, 'delete_after_days cannot be negative'); + select lives_ok('delete_policy_storage_policy_resource', 'policy_storage_policy cleanup'); + + prepare insert_policy_delete_after_days_while_inf_retain as + insert into policy_storage_policy + (public_id, scope_id, retain_for_days, delete_after_days) + values + ('pst__bcolors', 'global', -1, 10); + select throws_ok('insert_policy_delete_after_days_while_inf_retain', 'P0001', null, 'delete_after_days must be 0 while retain_for_days is inf'); + select lives_ok('delete_policy_storage_policy_resource', 'policy_storage_policy cleanup'); + + prepare insert_policy_delete_after_less_than_retain as + insert into policy_storage_policy + (public_id, scope_id, retain_for_days, delete_after_days) + values + ('pst__bcolors', 'global', 6, 5); + select throws_ok('insert_policy_delete_after_less_than_retain', 23514, null, 'delete_after must be greater than or equal to retain_for'); + select lives_ok('delete_policy_storage_policy_resource', 'policy_storage_policy cleanup'); + +rollback; diff --git a/internal/db/sqltest/tests/recording/session_recording_aggregate.sql b/internal/db/sqltest/tests/recording/session_recording_aggregate.sql new file mode 100644 index 0000000000..4da6b072f8 --- /dev/null +++ b/internal/db/sqltest/tests/recording/session_recording_aggregate.sql @@ -0,0 +1,71 @@ +-- Copyright (c) HashiCorp, Inc. +-- SPDX-License-Identifier: BUSL-1.1 + +begin; + + select plan(9); + + -- check view + -- no endtime, view should be fine + insert into recording_session + (public_id, storage_bucket_id, session_id, target_org_id, retain_for_days, delete_after_days) + values + ('sr_________1', 'sb____global', 's2_____clare', 'o_____colors', 10, 10); + select results_eq('select public_id from session_recording_aggregate where public_id = ''sr_________1''', + ARRAY['sr_________1'::wt_public_id], 'null delete_after and delete_time allows session_recording_aggregate to return session_recording'); + + -- update endtime to something impossibly large + update recording_session set end_time = '3000-01-23 12:34:56.789+00' where public_id = 'sr_________1'; + -- check to make sure the view where clause works correctly + select results_eq('select public_id from session_recording_aggregate where public_id = ''sr_________1''', + ARRAY['sr_________1'::wt_public_id], 'future delete_after allows session_recording_aggregate to return session_recording'); + + -- update delete_time to something equally large + update recording_session set delete_time = retain_until where public_id = 'sr_________1'; + -- check to make sure the view where clause works correctly + select results_eq('select public_id from session_recording_aggregate where public_id = ''sr_________1''', + ARRAY['sr_________1'::wt_public_id], 'future delete_after and delete_time allows session_recording_aggregate to return session_recording'); + + -- no endtime, view should be fine + insert into recording_session + (public_id, storage_bucket_id, session_id, target_org_id, retain_for_days, delete_after_days) + values + ('sr_________2', 'sb____global', 's2______cora', 'o_____colors', 10, 10); + select results_eq('select public_id from session_recording_aggregate where public_id = ''sr_________2''', + ARRAY['sr_________2'::wt_public_id], 'null delete_after and delete_time allows session_recording_aggregate to return session_recording'); + + -- update endtime to something already past + update recording_session set end_time = '2000-01-23 12:34:56.789+00' where public_id = 'sr_________2'; + -- check to make sure the view where clause works correctly + select results_eq('select count(public_id) from session_recording_aggregate where public_id = ''sr_________2''', + ARRAY[0::bigint], 'past delete_after makes session_recording_aggregate omit session_recording'); + + -- update delete after to something in the future + update recording_session set delete_time = '3000-01-23 12:34:56.789+00' where public_id = 'sr_________2'; + -- check to make sure the view where clause works correctly + select results_eq('select count(public_id) from session_recording_aggregate where public_id = ''sr_________2''', + ARRAY[0::bigint], 'past delete_after with future delete_time, session_recording_aggregate still omits session_recording'); + + -- no endtime, view should be fine (notice the 0 retain_for) + insert into recording_session + (public_id, storage_bucket_id, session_id, target_org_id, retain_for_days, delete_after_days) + values + ('sr_________3', 'sb____global', 's2_____carly', 'o_____colors', 0, 10); + select results_eq('select public_id from session_recording_aggregate where public_id = ''sr_________3''', + ARRAY['sr_________3'::wt_public_id], 'null delete_after and delete_time allows session_recording_aggregate to return session_recording'); + + -- update endtime to now + update recording_session set end_time = now() where public_id = 'sr_________3'; + -- check to make sure the view where clause works correctly + select results_eq('select public_id from session_recording_aggregate where public_id = ''sr_________3''', + ARRAY['sr_________3'::wt_public_id], 'future delete_after allows session_recording_aggregate to return session_recording'); + + -- update delete time to now + update recording_session set delete_time = now() where public_id = 'sr_________3'; + -- check to make sure the view where clause works correctly + select results_eq('select count(public_id) from session_recording_aggregate where public_id = ''sr_________3''', + ARRAY[0::bigint], 'future delete_after with past delete_time, session_recording_aggregate omits session_recording correctly'); + + select * from finish(); + +rollback;