35 lines
1.4 KiB
Python
35 lines
1.4 KiB
Python
"""Operator-only rotation of one S3 service secret in .env; never prints secret material.
|
|
|
|
Apply with SECURITY_RUNBOOK.md sequence. No credentials are injected into a Docker image.
|
|
"""
|
|
import os
|
|
from pathlib import Path
|
|
import secrets
|
|
import sys
|
|
|
|
root=Path(__file__).resolve().parents[1]
|
|
role=(sys.argv[1] if len(sys.argv)>1 else 'worker').lower()
|
|
if role not in ('worker','api','dispatcher'):
|
|
raise SystemExit('Usage: python scripts/rotate_s3_secret.py [worker|api|dispatcher]')
|
|
key='S3_'+role.upper()+'_SECRET_KEY'
|
|
env=root/'.env'
|
|
lines=env.read_text(encoding='utf8').splitlines(keepends=True)
|
|
found=[i for i,line in enumerate(lines) if line.startswith(key+'=')]
|
|
if len(found)!=1: raise SystemExit('Expected exactly one '+key+' in .env')
|
|
idx=found[0]
|
|
old=lines[idx].split('=',1)[1].strip()
|
|
if not old: raise SystemExit('Old credential missing')
|
|
secret=secrets.token_urlsafe(40)
|
|
operator=root/'.local/operator'
|
|
operator.mkdir(parents=True,exist_ok=True)
|
|
old_file=operator/('old-'+role+'-s3-secret.txt')
|
|
old_file.write_text(old,encoding='utf8')
|
|
os.chmod(old_file,0o600)
|
|
lines[idx]=key+'='+secret+'\n'
|
|
replacement=env.with_suffix('.new')
|
|
replacement.write_text(''.join(lines),encoding='utf8')
|
|
os.chmod(replacement,0o600)
|
|
os.replace(replacement,env)
|
|
print('Rotated '+key+' in operator .env; previous credential held under .local/operator (never commit).')
|
|
print('Next: re-bootstrap OpenBao, regenerate S3 policy, restart S3 and matching app role, then run s3 rotation audit.')
|