forked from mtusi2026/mtusi2026lab7
123 lines
3.8 KiB
Python
123 lines
3.8 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import sys
|
|
|
|
import psycopg
|
|
|
|
ROLE_TESTS = {
|
|
"messenger_api": {
|
|
"allowed": [
|
|
("read users", "SELECT id FROM public.users LIMIT 0"),
|
|
("read sessions", "SELECT * FROM public.sessions LIMIT 0"),
|
|
("read jobs", "SELECT id FROM public.attachment_jobs LIMIT 0"),
|
|
],
|
|
"denied": [
|
|
("read migration history",
|
|
"SELECT * FROM public.schema_migrations LIMIT 0"),
|
|
("create tables",
|
|
"CREATE TABLE public.lab7_acl_test (id integer)"),
|
|
],
|
|
},
|
|
|
|
"messenger_worker": {
|
|
"allowed": [
|
|
("read jobs", "SELECT id FROM public.attachment_jobs LIMIT 0"),
|
|
("read attachments", "SELECT id FROM public.attachments LIMIT 0"),
|
|
("read variants", "SELECT * FROM public.attachment_variants LIMIT 0"),
|
|
],
|
|
"denied": [
|
|
("read users", "SELECT * FROM public.users LIMIT 0"),
|
|
("read sessions", "SELECT * FROM public.sessions LIMIT 0"),
|
|
("read messages", "SELECT * FROM public.messages LIMIT 0"),
|
|
],
|
|
},
|
|
|
|
"messenger_dispatcher": {
|
|
"allowed": [
|
|
("read jobs", "SELECT id FROM public.attachment_jobs LIMIT 0"),
|
|
],
|
|
"denied": [
|
|
("read users", "SELECT * FROM public.users LIMIT 0"),
|
|
("read sessions", "SELECT * FROM public.sessions LIMIT 0"),
|
|
("read messages", "SELECT * FROM public.messages LIMIT 0"),
|
|
("read attachments", "SELECT * FROM public.attachments LIMIT 0"),
|
|
],
|
|
},
|
|
}
|
|
|
|
|
|
def run_query(conn, label, sql, should_succeed):
|
|
# Every test runs in its own transaction.
|
|
# All changes are rolled back, even if an unexpected
|
|
# permission grants the operation.
|
|
try:
|
|
with conn.transaction():
|
|
with conn.cursor() as cursor:
|
|
cursor.execute(sql)
|
|
|
|
if should_succeed:
|
|
print(f"PASS: allowed - {label}")
|
|
else:
|
|
print(f"FAIL: forbidden operation succeeded - {label}")
|
|
return False
|
|
|
|
except psycopg.errors.InsufficientPrivilege:
|
|
if should_succeed:
|
|
print(f"FAIL: allowed operation denied - {label}")
|
|
return False
|
|
|
|
print(f"PASS: denied - {label}")
|
|
return True
|
|
|
|
except psycopg.Error as exc:
|
|
print(f"FAIL: unexpected SQL error - {label}")
|
|
print(f"SQLSTATE: {exc.sqlstate}")
|
|
return False
|
|
|
|
return should_succeed
|
|
|
|
|
|
def main():
|
|
url = os.environ["DATABASE_URL"]
|
|
|
|
if "sslmode=verify-full" not in url:
|
|
raise SystemExit("FAIL: sslmode=verify-full not configured")
|
|
|
|
passed = True
|
|
|
|
# Autocommit allows each conn.transaction() to be independent.
|
|
with psycopg.connect(url, autocommit=True) as conn:
|
|
role, encrypted, version = conn.execute("""
|
|
SELECT current_user, ssl, version
|
|
FROM pg_stat_ssl
|
|
WHERE pid = pg_backend_pid()
|
|
""").fetchone()
|
|
|
|
print(f"PostgreSQL role: {role}")
|
|
print(f"TLS encrypted: {encrypted}")
|
|
print(f"TLS version: {version}")
|
|
|
|
if not encrypted or role not in ROLE_TESTS:
|
|
raise SystemExit("FAIL: unexpected role or TLS state")
|
|
|
|
tests = ROLE_TESTS[role]
|
|
|
|
for label, sql in tests["allowed"]:
|
|
if not run_query(conn, label, sql, True):
|
|
passed = False
|
|
|
|
for label, sql in tests["denied"]:
|
|
if not run_query(conn, label, sql, False):
|
|
passed = False
|
|
|
|
if passed:
|
|
print(f"PASS: all ACL tests for {role}")
|
|
else:
|
|
print(f"FAIL: ACL tests for {role}")
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|