forked from mtusi2026/mtusi2026lab7
256 lines
12 KiB
Python
256 lines
12 KiB
Python
"""Generate local credentials, certificates and runtime configurations once."""
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import secrets
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
from urllib.parse import quote
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
BUCKET = 'messenger-attachments'
|
|
PASSWORDS = {key: secrets.token_hex(24) for key in (
|
|
'pg-admin', 'pg-api', 'pg-worker', 's3-admin', 's3-api', 's3-worker',
|
|
'producer', 'consumer', 'otel-api', 'otel-worker', 'loki-write', 'tempo-write',
|
|
'grafana', 'loki-read', 'tempo-read', 'prom-read', 'scrape')}
|
|
S3_IDS = {'admin': 'messenger-bootstrap', 'api': 'messenger-api', 'worker': 'messenger-worker'}
|
|
|
|
|
|
def write(root, path, content):
|
|
file = root / path
|
|
file.parent.mkdir(parents=True, exist_ok=True)
|
|
file.write_text(content)
|
|
# The enclosing secrets directory is 0700 on the host. Exact read-only
|
|
# mounts also work with the image-specific UIDs inside Docker Desktop.
|
|
file.chmod(0o444)
|
|
|
|
|
|
def openssl(*args):
|
|
subprocess.run(['openssl', *map(str, args)], check=True, stdout=subprocess.DEVNULL)
|
|
|
|
|
|
def issue(root, ca, target, name, usage, names=()):
|
|
folder = root / target
|
|
folder.mkdir(parents=True, exist_ok=True)
|
|
key, crt = folder / f'{usage}.key', folder / f'{usage}.crt'
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
csr, config = Path(temporary) / 'leaf.csr', Path(temporary) / 'ext'
|
|
config.write_text(
|
|
'basicConstraints=critical,CA:FALSE\nkeyUsage=critical,digitalSignature\n'
|
|
'subjectKeyIdentifier=hash\nauthorityKeyIdentifier=keyid:always\n'
|
|
f'extendedKeyUsage={usage}Auth\n'
|
|
+ ('subjectAltName=' + ','.join('DNS:' + dns for dns in names) + '\n' if names else ''))
|
|
openssl('req', '-new', '-newkey', 'ec', '-pkeyopt', 'ec_paramgen_curve:P-256',
|
|
'-nodes', '-subj', '/CN=' + name, '-keyout', key, '-out', csr)
|
|
openssl('x509', '-req', '-in', csr, '-CA', root / f'operator/{ca}.crt',
|
|
'-CAkey', root / f'operator/{ca}.key', '-set_serial', '0x' + secrets.token_hex(16),
|
|
'-days', '30', '-sha256', '-extfile', config, '-out', crt)
|
|
key.chmod(0o444)
|
|
write(root, f'{target}/{usage}.pem', crt.read_text() + key.read_text())
|
|
|
|
|
|
def pki(root):
|
|
(root / 'operator').mkdir(mode=0o700)
|
|
for name in ('ca', 'foreign'):
|
|
openssl('req', '-x509', '-newkey', 'ec', '-pkeyopt', 'ec_paramgen_curve:P-256',
|
|
'-nodes', '-subj', '/CN=MTUSI lab7 ' + name, '-days', '365',
|
|
'-addext', 'basicConstraints=critical,CA:TRUE',
|
|
'-addext', 'keyUsage=critical,keyCertSign,cRLSign',
|
|
'-addext', 'subjectKeyIdentifier=hash',
|
|
'-keyout', root / f'operator/{name}.key', '-out', root / f'operator/{name}.crt')
|
|
write(root, 'ca/ca.crt', (root / 'operator/ca.crt').read_text())
|
|
for name in ('lb', 'api1', 'api2', 'postgres', 's3', 'broker', 'otel-collector',
|
|
'loki', 'tempo', 'prometheus', 'grafana', 'openbao'):
|
|
names = (name, 'localhost') if name in ('lb', 'grafana', 'openbao') else (name,)
|
|
issue(root, 'ca', 'tls/' + name, name, 'server', names)
|
|
for name in ('lb', 'api', 'worker'):
|
|
issue(root, 'ca', 'identity/' + name, name, 'client')
|
|
issue(root, 'foreign', 'identity/foreign', 'lb', 'client')
|
|
|
|
|
|
COMMON = '''global
|
|
maxconn 256
|
|
ssl-default-bind-options ssl-min-ver TLSv1.2
|
|
ssl-default-server-options ssl-min-ver TLSv1.2
|
|
defaults
|
|
mode http
|
|
timeout connect 2s
|
|
timeout client 35s
|
|
timeout server 35s
|
|
'''
|
|
|
|
|
|
def frontend(port, params=''):
|
|
return f'frontend tls\n bind :{port} ssl crt /run/tls/server.pem {params}\n'
|
|
|
|
|
|
def proxy(root, name, config):
|
|
write(root, f'proxy/{name}.cfg', COMMON + config)
|
|
|
|
|
|
def local_backend(port):
|
|
return f' default_backend local\nbackend local\n server local 127.0.0.1:{port}\n'
|
|
|
|
|
|
def telemetry_proxy(root, name, port, grants, rules):
|
|
users = ''.join(f'userlist {identity}\n user {identity} insecure-password {PASSWORDS[key]}\n'
|
|
for identity, key in grants.items())
|
|
proxy(root, name, users + 'frontend tls\n' + ''.join(
|
|
f' bind :{public} ssl crt /run/tls/server.pem\n' for public in rules)
|
|
+ ''.join(f' acl port{public} dst_port {public}\n' + statements
|
|
for public, statements in rules.items())
|
|
+ ' http-request deny deny_status 403\n'
|
|
+ ''.join(f'backend port{public}\n server local 127.0.0.1:{target}\n'
|
|
for public, target in port.items()))
|
|
|
|
|
|
def grant(public, identity, method, path, exact=False):
|
|
match = 'path' if exact else 'path_beg'
|
|
return (f' http-request allow if port{public} {{ http_auth({identity}) }} '
|
|
f'{{ method {method} }} {{ {match} {path} }}\n'
|
|
f' use_backend port{public} if port{public}\n')
|
|
|
|
|
|
def proxies(root):
|
|
proxy(root, 'lb', '''resolvers docker
|
|
nameserver embedded 127.0.0.11:53
|
|
timeout resolve 1s
|
|
timeout retry 1s
|
|
hold valid 5s
|
|
''' + frontend(8443) + ''' http-request set-header X-Lab-Client-IP %[src]
|
|
default_backend api
|
|
backend api
|
|
balance roundrobin
|
|
option httpchk GET /health/ready
|
|
http-check expect status 200
|
|
default-server inter 1s fall 2 rise 1 resolvers docker init-addr libc,none ssl verify required ca-file /run/ca/ca.crt crt /run/identity/client.pem
|
|
server api1 api1:9443 check verifyhost api1 sni str(api1) check-sni api1
|
|
server api2 api2:9443 check verifyhost api2 sni str(api2) check-sni api2
|
|
''')
|
|
for name in ('api1', 'api2'):
|
|
proxy(root, name, frontend(9443, 'verify required ca-file /run/ca/ca.crt') +
|
|
' http-request deny deny_status 403 unless { ssl_c_s_dn(CN) -m str lb }\n' +
|
|
local_backend(8080))
|
|
proxy(root, 'broker', frontend(5671) + ''' mode tcp
|
|
timeout client 150s
|
|
default_backend amqp
|
|
backend amqp
|
|
mode tcp
|
|
timeout server 150s
|
|
server local 127.0.0.1:5672
|
|
''')
|
|
proxy(root, 's3', frontend(9443) + f''' acl one_auth req.fhdr_cnt(authorization) eq 1
|
|
acl admin req.fhdr(authorization) -m beg "AWS4-HMAC-SHA256 Credential={S3_IDS['admin']}/"
|
|
acl api req.fhdr(authorization) -m beg "AWS4-HMAC-SHA256 Credential={S3_IDS['api']}/"
|
|
acl worker req.fhdr(authorization) -m beg "AWS4-HMAC-SHA256 Credential={S3_IDS['worker']}/"
|
|
acl bucket path /{BUCKET} /{BUCKET}/
|
|
acl staging path_reg ^/{BUCKET}/staging/[0-9a-f-]+$
|
|
acl final path_reg ^/{BUCKET}/final/[0-9a-f-]+/(original|thumbnail)$
|
|
acl query_present query -m found
|
|
http-request deny unless one_auth !query_present
|
|
http-request allow if admin bucket {{ method HEAD PUT }}
|
|
http-request allow if api bucket {{ method HEAD }}
|
|
http-request allow if api staging {{ method PUT }}
|
|
http-request allow if api final {{ method GET }}
|
|
http-request allow if worker staging {{ method GET }}
|
|
http-request allow if worker final {{ method PUT }}
|
|
http-request deny
|
|
''' + local_backend(9000))
|
|
telemetry_proxy(root, 'otel-collector', {9443: 4318, 9444: 8889},
|
|
{'api': 'otel-api', 'worker': 'otel-worker', 'scraper': 'scrape'},
|
|
{9443: ''' http-request allow if port9443 { http_auth(api) } { method POST } { path /v1/logs /v1/traces /v1/metrics }
|
|
http-request allow if port9443 { http_auth(worker) } { method POST } { path /v1/logs /v1/traces /v1/metrics }
|
|
use_backend port9443 if port9443
|
|
''', 9444: grant(9444, 'scraper', 'GET', '/metrics', exact=True)})
|
|
for name, internal, write_path, read_prefix in (
|
|
('loki', 3100, '/otlp/v1/logs', '/loki/api/v1/'),
|
|
('tempo', 3200, '/v1/traces', '/api/')):
|
|
ingest_port = 4318 if name == 'tempo' else internal
|
|
telemetry_proxy(root, name, {9443: internal, 9444: ingest_port},
|
|
{'grafana': name + '-read', 'collector': name + '-write'},
|
|
{9443: grant(9443, 'grafana', 'GET POST', read_prefix),
|
|
9444: grant(9444, 'collector', 'POST', write_path, exact=True)})
|
|
telemetry_proxy(root, 'prometheus', {9443: 9090}, {'grafana': 'prom-read'},
|
|
{9443: grant(9443, 'grafana', 'GET POST', '/api/v1/')})
|
|
|
|
|
|
def settings(root):
|
|
for name in ('pg-admin', 'pg-api', 'pg-worker', 'grafana', 'scrape'):
|
|
write(root, 'values/' + name, PASSWORDS[name] + '\n')
|
|
identities = []
|
|
for role, access in S3_IDS.items():
|
|
identities.append({'name': role, 'credentials': [
|
|
{'accessKey': access, 'secretKey': PASSWORDS['s3-' + role]}],
|
|
'actions': ['Admin'] if role == 'admin' else ['Read:' + BUCKET, 'Write:' + BUCKET, 'List:' + BUCKET]})
|
|
write(root, 's3/identities.json', json.dumps({'identities': identities}))
|
|
write(root, 's3/admin.env', f'S3_ACCESS_KEY_ID={S3_IDS["admin"]}\nS3_SECRET_ACCESS_KEY={PASSWORDS["s3-admin"]}\n')
|
|
for role in ('api', 'worker'):
|
|
database = f'postgresql://{role}:{PASSWORDS["pg-" + role]}@postgres:5432/messenger'
|
|
data = {'DATABASE_URL': database + '?sslmode=verify-full&sslrootcert=/run/ca/ca.crt',
|
|
'S3_ACCESS_KEY_ID': S3_IDS[role], 'S3_SECRET_ACCESS_KEY': PASSWORDS['s3-' + role],
|
|
'OTEL_PASSWORD': PASSWORDS['otel-' + role]}
|
|
if role == 'worker':
|
|
for identity in ('producer', 'consumer'):
|
|
data[f'BROKER_{identity.upper()}_URL'] = (
|
|
f'amqps://{identity}:{quote(PASSWORDS[identity])}@broker:5671/messenger')
|
|
write(root, f'operator/{role}.json', json.dumps(data))
|
|
|
|
|
|
def broker(root):
|
|
users = []
|
|
for name in ('producer', 'consumer'):
|
|
salt = secrets.token_bytes(4)
|
|
digest = hashlib.sha256(salt + PASSWORDS[name].encode()).digest()
|
|
users.append({'name': name, 'password_hash': base64.b64encode(salt + digest).decode(),
|
|
'hashing_algorithm': 'rabbit_password_hashing_sha256', 'tags': []})
|
|
manifest = {'users': users, 'vhosts': [{'name': 'messenger'}], 'permissions': [
|
|
{'user': 'producer', 'vhost': 'messenger', 'configure': '^$', 'write': '^amq\\.default$', 'read': '^$'},
|
|
{'user': 'consumer', 'vhost': 'messenger', 'configure': '^$', 'write': '^$', 'read': '^attachments$'}],
|
|
'queues': [{'name': 'attachments', 'vhost': 'messenger', 'durable': True,
|
|
'auto_delete': False, 'arguments': {}}]}
|
|
write(root, 'broker/definitions.json', json.dumps(manifest))
|
|
|
|
|
|
def telemetry(root):
|
|
config = (ROOT / 'infra/otel/collector.yaml').read_text()
|
|
for name in ('loki', 'tempo'):
|
|
header = base64.b64encode(f'collector:{PASSWORDS[name + "-write"]}'.encode()).decode()
|
|
config = config.replace(f'@{name.upper()}_AUTH@', header)
|
|
write(root, 'telemetry/collector.yaml', config)
|
|
config = (ROOT / 'infra/grafana/datasources.yaml').read_text()
|
|
for name, key in (('loki', 'loki-read'), ('tempo', 'tempo-read'), ('prom', 'prom-read')):
|
|
config = config.replace(f'@{name.upper()}_PASSWORD@', PASSWORDS[key])
|
|
config = config.replace('@TLS_CA@', '\n'.join(
|
|
' ' + line for line in (root / 'ca/ca.crt').read_text().splitlines()))
|
|
write(root, 'telemetry/datasources.yaml', config)
|
|
|
|
|
|
def main():
|
|
os.umask(0o077)
|
|
destination = ROOT / 'secrets'
|
|
if destination.exists():
|
|
if not (destination / '.ready').exists():
|
|
raise SystemExit('Existing secrets/ has no bootstrap marker. Inspect it before retrying.')
|
|
print('Using existing local runtime material')
|
|
return
|
|
temporary = Path(tempfile.mkdtemp(prefix='lab7-secrets-', dir=ROOT))
|
|
try:
|
|
pki(temporary)
|
|
settings(temporary)
|
|
broker(temporary)
|
|
telemetry(temporary)
|
|
proxies(temporary)
|
|
write(temporary, '.ready', 'lab7\n')
|
|
temporary.rename(destination)
|
|
finally:
|
|
if temporary.exists():
|
|
shutil.rmtree(temporary)
|
|
print('Created local runtime material in secrets/')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|