Files
mtusi2026lab6/scripts/bootstrap-storage.py
T
2026-11-17 13:08:13 +03:00

26 lines
1.1 KiB
Python

#!/usr/bin/env python3
"""Generate local synthetic storage credentials without rotating existing keys."""
import json
import os
from pathlib import Path
import secrets
root=Path(__file__).resolve().parents[1]/'secrets'/'lab6'
root.mkdir(parents=True,exist_ok=True)
for name in ('s3_admin_access','s3_admin_secret','s3_api_access','s3_api_secret'):
p=root/name
if not p.exists():
p.write_text(secrets.token_hex(24)+'\n')
p.chmod(0o444) # Non-root containers must read these local synthetic secrets.
bucket=os.environ.get('S3_BUCKET','messenger-attachments')
identities=[]
for role,actions in [('admin',['Admin','Read','Write','List','Tagging']),('api',['Read:'+bucket,'Write:'+bucket,'List:'+bucket])]:
identities.append({'name':role,'credentials':[{'accessKey':(root/('s3_'+role+'_access')).read_text().strip(),'secretKey':(root/('s3_'+role+'_secret')).read_text().strip()}],'actions':actions})
p=root/'s3.json'
content=json.dumps({'identities':identities})+'\n'
if not p.exists() or p.read_text()!=content:
if p.exists(): p.chmod(0o600)
p.write_text(content)
p.chmod(0o444)
print('Local storage credentials ready; existing keys preserved.')