Лабораторная 7: TLS, mTLS и OpenBao
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
2026-11-28 01:28:30 +03:00
parent 20afc2c790
commit 2c2734df60
119 changed files with 8651 additions and 104 deletions
+33
View File
@@ -0,0 +1,33 @@
"""One-shot operator grants after migrations. No credentials printed."""
import json
from pathlib import Path
import psycopg2
from psycopg2 import sql
values=json.loads(Path('/run/bootstrap/roles.json').read_text())
conn=psycopg2.connect(Path('/run/bootstrap/database_url').read_text().strip())
conn.autocommit=False
with conn.cursor() as cur:
for role,keys in values.items():
password=keys['database_url'].split(':',2)[2].split('@')[0]
cur.execute('SELECT 1 FROM pg_roles WHERE rolname=%s',(role,))
if not cur.fetchone():cur.execute(sql.SQL('CREATE ROLE {} LOGIN').format(sql.Identifier(role)))
cur.execute(sql.SQL('ALTER ROLE {} PASSWORD %s NOSUPERUSER NOCREATEDB NOCREATEROLE').format(sql.Identifier(role)),(password,))
cur.execute(sql.SQL('GRANT CONNECT ON DATABASE messenger TO {}').format(sql.Identifier(role)))
cur.execute(sql.SQL('GRANT USAGE ON SCHEMA public TO {}').format(sql.Identifier(role)))
for role in values:
cur.execute(sql.SQL('REVOKE ALL ON ALL TABLES IN SCHEMA public FROM {}').format(sql.Identifier(role)))
api={'users':'SELECT,INSERT,UPDATE','sessions':'SELECT,INSERT,UPDATE,DELETE','chats':'SELECT,INSERT','chat_members':'SELECT,INSERT','messages':'SELECT,INSERT','attachments':'SELECT,INSERT,UPDATE','attachment_jobs':'SELECT,INSERT','attachment_outbox':'SELECT,INSERT','attachment_variants':'SELECT','message_idempotency':'SELECT,INSERT,UPDATE,DELETE','message_attachments':'SELECT,INSERT','login_budgets':'SELECT,INSERT,UPDATE'}
cur.execute("SELECT tablename FROM pg_tables WHERE schemaname='public'")
tables={row[0] for row in cur.fetchall()}
for table,privileges in api.items():
if table in tables:cur.execute(sql.SQL('GRANT '+privileges+' ON {} TO api').format(sql.Identifier(table)))
grants={
'worker':{'attachments':'SELECT,UPDATE','attachment_jobs':'SELECT,UPDATE','attachment_outbox':'SELECT,INSERT,UPDATE','attachment_variants':'SELECT,INSERT'},
'publisher':{'attachments':'SELECT,UPDATE','attachment_jobs':'SELECT,UPDATE','attachment_outbox':'SELECT,INSERT,UPDATE'},
'cleaner':{'attachments':'SELECT,DELETE','attachment_jobs':'SELECT,UPDATE','attachment_outbox':'SELECT,UPDATE'},
}
for role,tables in grants.items():
for table,privileges in tables.items():
cur.execute(sql.SQL('GRANT '+privileges+' ON {} TO {}').format(sql.Identifier(table),sql.Identifier(role)))
cur.execute('GRANT UPDATE (updated_at) ON attachments TO cleaner')
conn.commit();conn.close();print('PASS: separate database roles and grants applied')