Implement lab7 mTLS, scoped OpenBao secrets and worker compromise experiment
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
"""Operator migrations/grants over the container-local PostgreSQL Unix socket.
|
||||
|
||||
SQL containing passwords travels on stdin, not argv/logs; no admin DB credential
|
||||
is made available to an application. Existing migration hashes must match.
|
||||
"""
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def sql(query):
|
||||
result=subprocess.run(['docker','compose','exec','-T','db','psql','-X','-qAt',
|
||||
'-v','ON_ERROR_STOP=1','-U','messenger','-d','messenger'],input=query,
|
||||
text=True,encoding='utf-8',capture_output=True,cwd=ROOT)
|
||||
if result.returncode:
|
||||
# Server error text may echo a credential-bearing statement.
|
||||
raise RuntimeError('Operator SQL failed; no query or credentials printed')
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def main():
|
||||
sql('CREATE TABLE IF NOT EXISTS schema_migrations (name text PRIMARY KEY, sha256 text NOT NULL, applied_at timestamptz NOT NULL DEFAULT clock_timestamp())')
|
||||
for file in sorted((ROOT/'migrations').glob('*.sql')):
|
||||
digest=hashlib.sha256(file.read_bytes()).hexdigest()
|
||||
old=sql(f"SELECT sha256 FROM schema_migrations WHERE name='{file.name}'")
|
||||
if old:
|
||||
assert old==digest, 'Applied migration changed: '+file.name
|
||||
else:
|
||||
sql('BEGIN;\n'+file.read_text()+f"\nINSERT INTO schema_migrations(name,sha256) VALUES ('{file.name}','{digest}'); COMMIT;")
|
||||
print('Migration verified:',file.name)
|
||||
passwords=json.loads((ROOT/'secrets/operator/config.json').read_text())['database_passwords']
|
||||
for role,password in passwords.items():
|
||||
assert role in ('api1','api2','worker','dispatcher','reconciler') and all(c in '0123456789abcdef' for c in password)
|
||||
if not sql(f"SELECT 1 FROM pg_roles WHERE rolname='{role}'"):sql(f'CREATE ROLE {role} LOGIN')
|
||||
sql(f"ALTER ROLE {role} PASSWORD '{password}' NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION; GRANT CONNECT ON DATABASE messenger TO {role}; GRANT USAGE ON SCHEMA public TO {role}; REVOKE ALL ON ALL TABLES IN SCHEMA public FROM {role}; REVOKE ALL ON ALL SEQUENCES IN SCHEMA public FROM {role};")
|
||||
sql('REVOKE ALL ON DATABASE messenger FROM PUBLIC; REVOKE CREATE ON SCHEMA public FROM PUBLIC;')
|
||||
sql('GRANT SELECT,INSERT,UPDATE,DELETE ON users,sessions,chats,members,messages,idempotency,attachments,message_attachments,jobs,outbox,login_budget TO api1,api2;')
|
||||
sql('GRANT SELECT,UPDATE ON jobs,attachments,outbox,job_faults TO worker; GRANT INSERT ON job_audit TO worker; GRANT USAGE ON SEQUENCE job_audit_id_seq TO worker;')
|
||||
sql('GRANT SELECT,UPDATE ON jobs,attachments,outbox TO dispatcher; GRANT INSERT ON job_audit TO dispatcher; GRANT USAGE ON SEQUENCE job_audit_id_seq TO dispatcher;')
|
||||
sql('GRANT SELECT,UPDATE ON attachments TO reconciler;')
|
||||
print('Scoped database roles applied')
|
||||
|
||||
|
||||
if __name__=='__main__':main()
|
||||
Reference in New Issue
Block a user