#!/usr/bin/env bash # Runs on the probe VM inside the VPC. Env: DBHOST, DBPORT, ROOTPW. CA at ./ca.crt set -u export PGSSLMODE=verify-full PGSSLROOTCERT=$PWD/ca.crt PGHOST=$DBHOST PGPORT=$DBPORT PGCONNECT_TIMEOUT=10 MIGPW=$(openssl rand -hex 16); APPPW=$(openssl rand -hex 16) A=00000000-0000-0000-0000-00000000000a; B=00000000-0000-0000-0000-00000000000b q() { local user=$1 pw=$2 db=$3; shift 3; PGPASSWORD=$pw psql -X -v ON_ERROR_STOP=0 -U "$user" -d "$db" "$@" 2>&1; } echo "== 0. DNS + TLS" getent hosts "$DBHOST" q akmadmin "$ROOTPW" defaultdb -Atc "select 'tls='||ssl||' '||version from pg_stat_ssl where pid=pg_backend_pid()" PGSSLMODE=verify-full PGPASSWORD=$ROOTPW psql -X -h "$(getent hosts "$DBHOST" | awk '{print $1}')" -U akmadmin -d defaultdb -Atc 'select 1' 2>&1 | sed 's/^/by-IP (expect cert mismatch): /' echo "-- server cert names" openssl s_client -starttls postgres -connect "$DBHOST:$DBPORT" /dev/null | openssl x509 -noout -subject -ext subjectAltName 2>&1 echo "== 1. platform settings / akmadmin attributes" q akmadmin "$ROOTPW" defaultdb -Atc "select version()" -c "show password_encryption" -c "show max_connections" -c "show superuser_reserved_connections" -c "show reserved_connections" \ -c "select rolname, rolsuper, rolcreaterole, rolcreatedb, rolbypassrls from pg_roles where rolname = current_user" \ -c "show createrole_self_grant" echo "== 2. bootstrap as akmadmin" q akmadmin "$ROOTPW" defaultdb -c "create role hapi_migrator login password '$MIGPW'" -c "create role hapi_app login password '$APPPW'" \ -c "select rolname, rolpassword like 'SCRAM%' as scram from pg_authid where rolname like 'hapi_%'" \ -c "create database hapi owner hapi_migrator" echo "-- retry create database after granting SET on hapi_migrator to akmadmin" q akmadmin "$ROOTPW" defaultdb -c "grant hapi_migrator to akmadmin with set true, inherit false" -c "create database hapi owner hapi_migrator" echo "-- db-level grants as owner hapi_migrator" q hapi_migrator "$MIGPW" hapi -c "revoke all on database hapi from public" -c "grant connect on database hapi to hapi_app" -c "revoke create on schema public from public" echo "-- audit: per-role log_statement" q akmadmin "$ROOTPW" defaultdb -c "alter role hapi_migrator set log_statement = 'all'" -c "alter role hapi_migrator set log_min_duration_statement = 0" echo "-- can akmadmin grant BYPASSRLS?" q akmadmin "$ROOTPW" defaultdb -c "create role probe_bypass bypassrls" -c "drop role if exists probe_bypass" echo "== 3. schema + RLS as hapi_migrator" q hapi_migrator "$MIGPW" hapi -c "create schema finance" -c "grant usage on schema finance to hapi_app" \ -c "alter default privileges in schema finance grant select, insert, update, delete on tables to hapi_app" \ -c "create table finance.note (id uuid primary key default uuidv7(), owner_id uuid not null, body text)" \ -c "alter table finance.note enable row level security" -c "alter table finance.note force row level security" \ -c "create policy own on finance.note using (owner_id = nullif(current_setting('hapi.principal_id', true), '')::uuid) with check (owner_id = nullif(current_setting('hapi.principal_id', true), '')::uuid)" echo "-- owner under FORCE, no principal set: insert (expect RLS violation)" q hapi_migrator "$MIGPW" hapi -c "insert into finance.note(owner_id, body) values ('$A','a1')" echo "-- owner seeds via per-row principal" q hapi_migrator "$MIGPW" hapi -c "begin" -c "select set_config('hapi.principal_id','$A',true)" -c "insert into finance.note(owner_id, body) values ('$A','a1'),('$A','a2')" -c "commit" \ -c "begin" -c "select set_config('hapi.principal_id','$B',true)" -c "insert into finance.note(owner_id, body) values ('$B','b1')" -c "commit" echo "-- owner, no principal: count (FORCE => 0)" q hapi_migrator "$MIGPW" hapi -Atc "select count(*) from finance.note" echo "-- owner temporarily NO FORCE (data-repair path)" q hapi_migrator "$MIGPW" hapi -Atc "begin" -c "alter table finance.note no force row level security" -c "select count(*) from finance.note" -c "rollback" echo "== 4. hapi_app" echo "-- DDL attempts (expect permission denied x2)" q hapi_app "$APPPW" hapi -c "create table finance.evil(x int)" -c "create table public.evil(x int)" echo "-- no principal set: count (expect 0)" q hapi_app "$APPPW" hapi -Atc "select count(*) from finance.note" echo "-- SET LOCAL A in txn, then after commit on same connection (expect 2 then 0)" q hapi_app "$APPPW" hapi -Atc "begin" -c "set local hapi.principal_id = '$A'" -c "select count(*) from finance.note" -c "commit" -c "select count(*) from finance.note" -c "select '['||current_setting('hapi.principal_id', true)||']'" echo "-- set_config(..., true) as A (expect 2)" q hapi_app "$APPPW" hapi -Atc "begin" -c "select set_config('hapi.principal_id','$A',true)" -c "select count(*) from finance.note" -c "commit" echo "-- WITH CHECK: as A insert B-owned row (expect RLS violation)" q hapi_app "$APPPW" hapi -c "begin" -c "set local hapi.principal_id = '$A'" -c "insert into finance.note(owner_id, body) values ('$B','x')" -c "rollback" echo "-- as A update B rows (expect UPDATE 0)" q hapi_app "$APPPW" hapi -c "begin" -c "set local hapi.principal_id = '$A'" -c "update finance.note set body='pwn' where owner_id='$B'" -c "rollback" echo "-- escalation attempts (expect all denied)" q hapi_app "$APPPW" hapi -c "set role hapi_migrator" -c "alter table finance.note no force row level security" -c "alter table finance.note disable row level security" -c "drop policy own on finance.note" echo "-- spoof: leaked hapi_app creds can set any principal (expect 1 = B's row visible)" q hapi_app "$APPPW" hapi -Atc "set hapi.principal_id = '$B'" -c "select count(*) from finance.note" echo "-- connections in use" q akmadmin "$ROOTPW" defaultdb -Atc "select backend_type, usename, count(*) from pg_stat_activity group by 1,2 order by 1,2" echo DONE