[+] lab7
Grade 3 secure application acceptance / secure (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
Xabenskie
2026-11-30 09:09:57 +03:00
parent 20afc2c790
commit 4eca49fe3d
149 changed files with 68317 additions and 58 deletions
+28
View File
@@ -0,0 +1,28 @@
"""Operator migration phase only; application roles cannot create/alter database objects."""
import json
from pathlib import Path
import psycopg
from psycopg import sql
from messenger.db import setting
def main():
values=json.loads(Path('/operator/db-roles.json').read_text())
with psycopg.connect(setting('DATABASE_URL')) as conn:
for role,password in values.items():
if not conn.execute('SELECT 1 FROM pg_roles WHERE rolname=%s',(role,)).fetchone():
conn.execute(sql.SQL('CREATE ROLE {} LOGIN').format(sql.Identifier(role)))
conn.execute(sql.SQL('ALTER ROLE {} PASSWORD {} NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION').format(sql.Identifier(role),sql.Literal(password)))
conn.execute(sql.SQL('GRANT CONNECT ON DATABASE messenger TO {}').format(sql.Identifier(role)))
conn.execute(sql.SQL('GRANT USAGE ON SCHEMA public TO {}').format(sql.Identifier(role)))
api=['users','chats','chat_members','messages','sessions','idempotency_records','attachments','message_attachments','attachment_jobs','job_outbox','auth_rate_limits']
conn.execute(sql.SQL('GRANT SELECT,INSERT,UPDATE,DELETE ON {} TO db_api').format(sql.SQL(',').join(map(sql.Identifier,api))))
for role in ['db_worker','db_relay']:
conn.execute(sql.SQL('GRANT SELECT,UPDATE ON attachments,attachment_jobs,job_outbox TO {}').format(sql.Identifier(role)))
conn.execute('GRANT SELECT,INSERT ON job_replays TO db_worker')
conn.execute('GRANT USAGE,SELECT ON SEQUENCE job_replays_id_seq TO db_worker')
conn.execute('REVOKE CREATE ON SCHEMA public FROM PUBLIC')
print('Least privilege DB roles ready')
if __name__=='__main__': main()