Files
mtusi2026lab7/scripts/init_secrets.py
T
HellsTomatoandCopilot 2ef0299e34
Skeleton contracts / contracts (pull_request) Canceled after 0s
Complete lab7 grade 4 implementation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-06 15:35:53 +03:00

82 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""Create local synthetic secrets under secrets/ without printing values."""
import json
import os
import secrets
import string
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
DIR = ROOT / 'secrets'
BUCKET = os.environ.get('S3_BUCKET', 'messenger-attachments')
def value(name, generate):
path = DIR / name
if not path.exists():
path.write_text(generate(), encoding='utf-8')
os.chmod(path, 0o644)
return path.read_text(encoding='utf-8')
def token():
return secrets.token_urlsafe(30)
def access_key():
return ''.join(secrets.choice(string.ascii_uppercase + string.digits) for _ in range(20))
DIR.mkdir(exist_ok=True)
os.chmod(DIR, 0o700)
value('db_password', token)
value('bao-api-role-id', lambda: '')
value('bao-api-secret-id', lambda: '')
value('bao-worker-role-id', lambda: '')
value('bao-worker-secret-id', lambda: '')
redis_password = value('redis_password', token)
value('redis.conf', lambda: '''requirepass %s
appendonly yes
dir /data
port 0
tls-port 6379
tls-cert-file /run/secrets/redis.crt
tls-key-file /run/secrets/redis.key
tls-ca-cert-file /run/secrets/ca.crt
tls-auth-clients no
''' % redis_password)
api_key, api_secret = value('s3_api_access_key', access_key), value('s3_api_secret_key', token)
admin_key, admin_secret = value('s3_admin_access_key', access_key), value('s3_admin_secret_key', token)
worker_key, worker_secret = value('s3_worker_access_key', access_key), value('s3_worker_secret_key', token)
value('s3_config.json', lambda: json.dumps({'identities': [
{'name': 'admin', 'credentials': [{'accessKey': admin_key, 'secretKey': admin_secret}],
'actions': ['Admin', 'Read', 'Write', 'List']},
{'name': 'api', 'credentials': [{'accessKey': api_key, 'secretKey': api_secret}],
'actions': ['Read:' + BUCKET, 'Write:' + BUCKET, 'List:' + BUCKET]},
{'name': 'worker', 'credentials': [{'accessKey': worker_key, 'secretKey': worker_secret}],
'actions': ['Read:' + BUCKET, 'Write:' + BUCKET, 'List:' + BUCKET]},
]}, indent=2))
# RabbitMQ uses EXTERNAL SASL: the verified client certificate CN becomes the
# broker username, so reusable passwords cannot bypass service identity.
value('broker_api_url', lambda: 'amqps://rabbitmq:5671/%2F')
value('broker_worker_url', lambda: 'amqps://rabbitmq:5671/%2F')
user = lambda name: {'name': name, 'tags': []}
exchange = lambda name, kind: {'name': name, 'vhost': '/', 'type': kind, 'durable': True, 'auto_delete': False, 'internal': False, 'arguments': {}}
queue = lambda name, args: {'name': name, 'vhost': '/', 'durable': True, 'auto_delete': False, 'arguments': args}
binding = lambda source, queue, key: {'source': source, 'vhost': '/', 'destination': queue, 'destination_type': 'queue', 'routing_key': key, 'arguments': {}}
value('rabbitmq_definitions.json', lambda: json.dumps({
'users': [user('api-broker'), user('worker-broker')],
'vhosts': [{'name': '/'}],
'permissions': [
{'user': 'api-broker', 'vhost': '/', 'configure': '^$', 'write': '^attachments$', 'read': '^$'},
{'user': 'worker-broker', 'vhost': '/', 'configure': '^$', 'write': '^attachments$', 'read': '^attachments\\.process$'},
],
'exchanges': [exchange('attachments', 'direct'), exchange('attachments.dlx', 'fanout')],
'queues': [queue('attachments.process', {'x-queue-type': 'quorum', 'x-dead-letter-exchange': 'attachments.dlx', 'x-delivery-limit': 50}),
queue('attachments.dlq', {'x-queue-type': 'quorum'})],
'bindings': [binding('attachments', 'attachments.process', 'process'), binding('attachments.dlx', 'attachments.dlq', '')],
}, indent=2))
value('grafana_admin_password', token)
print('secrets ready in %s (values are not printed)' % DIR)