Files
faunosteo/supabase/tests/004_audit.sql
T
bchanot 2d5ff0c33e feat(supabase): foundation migrations (audit, profiles, platform roles) + pgTAP suite
- schemas private/audit, set_updated_at, append-only audit.audit_logs, private.log_event
- profiles created by auth.users trigger; own-row RLS; column-level UPDATE grant
- platform_role enum (owner/admin/moderator/support), profile_roles history table,
  is_platform_* helpers, grant/revoke RPCs audited; writes only via RPC
- config.toml (public schema only, strong passwords, no edge runtime), local seed
- 79 pgTAP checks: allowed and denied paths for every policy
2026-10-08 22:20:54 +02:00

100 lines
3.6 KiB
PL/PgSQL

-- audit.audit_logs: sensitive operations leave a row, rows are immutable,
-- only owners/admins read them.
begin;
select plan(12);
insert into auth.users (id, email) values
('00000000-0000-0000-0000-000000000001', 'root@test.local'),
('00000000-0000-0000-0000-00000000000a', 'alice@test.local'),
('00000000-0000-0000-0000-00000000000b', 'bob@test.local');
select private.assign_platform_role(null, '00000000-0000-0000-0000-000000000001', 'owner');
-- 1. Bootstrap is audited as a system action
select results_eq(
$$ select actor_user_id, action, resource_type, target_user_id, metadata ->> 'role'
from audit.audit_logs $$,
$$ values (null::uuid, 'platform_role.granted'::text, 'profile_role'::text,
'00000000-0000-0000-0000-000000000001'::uuid, 'owner'::text) $$,
'bootstrap grant logged with null actor'
);
-- 2. RPC grant and revoke are audited with the actor
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
select public.grant_platform_role('00000000-0000-0000-0000-00000000000a', 'admin');
select public.revoke_platform_role('00000000-0000-0000-0000-00000000000a', 'admin', 'rotation');
reset role;
select results_eq(
$$ select action, actor_user_id, target_user_id, metadata ->> 'reason'
from audit.audit_logs where actor_user_id is not null order by occurred_at, action $$,
$$ values ('platform_role.granted'::text, '00000000-0000-0000-0000-000000000001'::uuid,
'00000000-0000-0000-0000-00000000000a'::uuid, null::text),
('platform_role.revoked'::text, '00000000-0000-0000-0000-000000000001'::uuid,
'00000000-0000-0000-0000-00000000000a'::uuid, 'rotation'::text) $$,
'grant and revoke logged with actor, target and metadata'
);
select is(
(select count(*) from audit.audit_logs a
join public.profile_roles r on r.id = a.resource_id),
3::bigint,
'every audit row points at its profile_roles row'
);
-- 3. Append-only, even for the privileged owner session
select throws_ok(
$$ update audit.audit_logs set action = 'platform_role.tampered' $$,
'42501',
null,
'audit rows cannot be updated'
);
select throws_ok(
$$ delete from audit.audit_logs $$,
'42501',
null,
'audit rows cannot be deleted'
);
-- 4. log_event validates its inputs
select throws_ok(
$$ select private.log_event(null, 'Bad Action', 'profile') $$,
'23514',
null,
'action must match <domain>.<verb>'
);
select throws_ok(
$$ select private.log_event(null, 'export.requested', 'profile', null, null, null, '[]'::jsonb) $$,
'23514',
null,
'metadata must be a JSON object'
);
-- 5. Read access: owner yes, plain user no, anon no, nobody writes through the API
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
select is((select count(*) from audit.audit_logs), 3::bigint, 'owner reads the audit log');
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select is((select count(*) from audit.audit_logs), 0::bigint, 'plain user reads nothing');
select throws_ok(
$$ insert into audit.audit_logs (action, resource_type) values ('x.y', 'z') $$,
'42501',
null,
'authenticated cannot insert audit rows'
);
select throws_ok(
$$ select private.log_event(null, 'x.y', 'z') $$,
'42501',
null,
'authenticated cannot call log_event'
);
set local role anon;
select throws_ok(
$$ select * from audit.audit_logs $$,
'42501',
null,
'anon cannot read the audit log'
);
select * from finish();
rollback;