Files
mtusi2026lab7/scripts/bootstrap.py
T

112 lines
6.4 KiB
Python

#!/usr/bin/env python3
"""Operator: generate a private local CA, leaf certs and synthetic credentials once."""
import json
import os
from pathlib import Path
import secrets
import subprocess
os.chdir(Path(__file__).resolve().parents[1])
os.umask(0o077)
root = Path('secrets')
root.mkdir(exist_ok=True)
operator = Path('operator')
operator.mkdir(exist_ok=True)
pki = operator / 'pki'
pki.mkdir(exist_ok=True)
def openssl(*args):
subprocess.run(['openssl', *map(str,args)], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
if not (pki/'ca.key').exists():
openssl('req','-x509','-newkey','rsa:3072','-nodes','-keyout',pki/'ca.key','-out',root/'ca.crt','-days','365','-subj','/CN=Lab7 local CA','-addext','basicConstraints=critical,CA:TRUE','-addext','keyUsage=critical,keyCertSign,cRLSign','-addext','subjectKeyIdentifier=hash')
roles = ['lb','api','worker','dispatcher','postgres','valkey','broker','s3','otel-collector','loki','tempo','prometheus','grafana','openbao','wrong-role']
for role in roles:
d = root/role
d.mkdir(exist_ok=True)
if (d/'service.crt').exists() and '--renew-leaves' not in __import__('sys').argv:
continue
ext = pki/(role+'.ext')
sans = f'DNS:{role},URI:spiffe://lab7/{role}'
if role in ('lb','openbao','grafana'):
sans += ',DNS:localhost,IP:127.0.0.1'
ext.write_text('subjectKeyIdentifier=hash\nauthorityKeyIdentifier=keyid,issuer\nbasicConstraints=critical,CA:FALSE\nkeyUsage=critical,digitalSignature,keyEncipherment\nextendedKeyUsage=serverAuth,clientAuth\nsubjectAltName='+sans+'\n')
openssl('req','-new','-newkey','rsa:2048','-nodes','-keyout',d/'service.key','-out',pki/(role+'.csr'),'-subj','/CN='+role)
openssl('x509','-req','-in',pki/(role+'.csr'),'-CA',root/'ca.crt','-CAkey',pki/'ca.key','-set_serial',str(secrets.randbits(128)),'-out',d/'service.crt','-days','30','-extfile',ext)
(d/'service.pem').write_bytes((d/'service.crt').read_bytes()+(d/'service.key').read_bytes())
# Separate untrusted CA for negative checks.
if not (operator/'foreign.key').exists():
openssl('req','-x509','-newkey','rsa:2048','-nodes','-keyout',operator/'foreign.key','-out',operator/'foreign.crt','-days','30','-subj','/CN=Foreign CA')
openssl('req','-new','-newkey','rsa:2048','-nodes','-keyout',operator/'foreign-client.key','-out',operator/'foreign.csr','-subj','/CN=lb')
openssl('x509','-req','-in',operator/'foreign.csr','-CA',operator/'foreign.crt','-CAkey',operator/'foreign.key','-set_serial','42','-out',operator/'foreign-client.crt','-days','30','-extfile',pki/'lb.ext')
credentials = operator/'credentials.json'
if not credentials.exists():
passwords = {k: secrets.token_hex(24) for k in ('postgres','api','worker','dispatcher','session','broker-worker','broker-dispatcher')}
keys = {r: (secrets.token_hex(10),secrets.token_hex(24)) for r in ('api','worker','admin')}
credentials.write_text(json.dumps({'passwords':passwords,'keys':keys}))
c = json.loads(credentials.read_text()); pw=c['passwords']; keys=c['keys']
(root/'postgres_password').write_text(pw['postgres'])
(root/'database_url').write_text(f"postgres://messenger:{pw['postgres']}@postgres:5432/messenger?sslmode=verify-full&sslrootcert=/tls/ca.crt")
# Executed only on first DB initialization. No app has a superuser credential.
(root/'roles.sql').write_text('\n'.join(f"CREATE ROLE {r} LOGIN PASSWORD '{pw[r]}';" for r in ('api','worker','dispatcher')))
(root/'valkey.conf').write_text(f'''bind 0.0.0.0
port 0
tls-port 6379
tls-cert-file /tls/service.crt
tls-key-file /tls/service.key
tls-ca-cert-file /tls/ca.crt
tls-auth-clients no
appendonly yes
appendfsync always
maxmemory 128mb
maxmemory-policy noeviction
user default off
user api on >{pw['session']} ~session:* +ping +get +set +del
''')
(root/'broker.conf').write_text(f'''bind 0.0.0.0
port 0
tls-port 6379
tls-cert-file /tls/service.crt
tls-key-file /tls/service.key
tls-ca-cert-file /tls/ca.crt
tls-auth-clients no
appendonly yes
appendfsync always
maxmemory-policy noeviction
user default off
user dispatcher on >{pw['broker-dispatcher']} ~attachment-jobs-v1 +ping +xadd
user worker on >{pw['broker-worker']} ~attachment-jobs-v1 +ping +xgroup +xreadgroup +xautoclaim +xack
''')
bucket='messenger-attachments'
identities=[]
for role in ('admin','api','worker'):
actions=['Admin','Read','Write','List'] if role=='admin' else ([f'Write:{bucket}/staging/*',f'Read:{bucket}/originals/*',f'Read:{bucket}/variants/*'] if role=='api' else [f'Read:{bucket}/staging/*',f'Write:{bucket}/originals/*',f'Write:{bucket}/variants/*'])
identities.append(dict(name=role,credentials=[dict(accessKey=keys[role][0],secretKey=keys[role][1])],actions=actions))
(root/'s3_config').write_text(json.dumps(dict(identities=identities)))
for suffix,value in zip(('access_key_id','secret_access_key'),keys['admin']):
(root/('s3_admin_'+suffix)).write_text(value)
bundles={}
for role in ('api','worker','dispatcher'):
data={'database_url':f"postgres://{role}:{pw[role]}@postgres:5432/messenger?sslmode=verify-full&sslrootcert=/tls/ca.crt"}
if role=='api':
data['cache_url']=f"rediss://api:{pw['session']}@valkey:6379/0"
else:
data['broker_url']=f"rediss://{role}:{pw['broker-'+role]}@broker:6379/0"
if role in keys:
data.update(s3_access_key_id=keys[role][0],s3_secret_access_key=keys[role][1])
bundles[role]=data
(operator/'bundles.json').write_text(json.dumps(bundles))
# Grafana datasource provisioning contains private client material and stays outside Git.
datasources=[]
for name,uid,kind,url in [('Prometheus','prometheus','prometheus','https://prometheus:9443'),('Loki','loki','loki','https://loki:3443'),('Tempo','tempo','tempo','https://tempo:3443')]:
datasources.append(dict(name=name,uid=uid,type=kind,access='proxy',url=url,isDefault=(uid=='prometheus'),jsonData=dict(tlsAuth=True,tlsAuthWithCACert=True),secureJsonData=dict(tlsCACert=(root/'ca.crt').read_text(),tlsClientCert=(root/'grafana/service.crt').read_text(),tlsClientKey=(root/'grafana/service.key').read_text())))
(root/'grafana-datasources.yaml').write_text(json.dumps(dict(apiVersion=1,datasources=datasources)))
# Host directory is 0700. Individual files readable by non-root containers;
# only explicitly selected files are mounted, never the whole secrets directory.
for f in root.rglob('*'):
if f.is_file(): f.chmod(0o644)
if not Path('.env').exists():
Path('.env').write_text(Path('.env.example').read_text())
print('Local CA, 30-day leaf certificates and separate credentials ready; no values printed.')