Files

247 lines
11 KiB
Python
Executable File

#!/usr/bin/env python3
"""Operator-local environment and verified OpenBao transport for lab7."""
from __future__ import annotations
import hashlib
import json
import os
from pathlib import Path
import re
import ssl
import subprocess
import tempfile
import time
import urllib.error
import urllib.request
ROOT = Path(__file__).resolve().parents[1]
ROLES = {
'api': ['api', 'api-replica'],
'worker': ['worker'],
'relay': ['relay'],
'lb': ['lb', 'localhost'],
'postgres': ['postgres', 'localhost'],
'redis': ['redis', 'localhost'],
's3': ['s3', 'seaweedfs', 'localhost'],
'broker': ['broker', 'localhost'],
'collector': ['collector', 'otel-collector', 'localhost'],
'prometheus': ['prometheus', 'localhost'],
'loki': ['loki', 'localhost'],
'tempo': ['tempo', 'localhost'],
'grafana': ['grafana', 'localhost'],
'openbao': ['openbao', 'localhost'],
'operator': ['operator'],
'rogue': ['rogue'],
}
ALPINE_IMAGE = 'alpine:3.22@sha256:49340b346798875b5592e5d420241f2f9cfba5f47e62ebb1446a1b34734e898f'
class OperationError(RuntimeError):
"""A sanitized failure; secret-bearing API bodies are never included."""
def read_env(path: Path) -> dict[str, str]:
values = {}
if not path.exists():
return values
for line in path.read_text().splitlines():
line = line.strip()
if line and not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
values[key.strip()] = value.strip().strip('"').strip("'")
return values
def atomic_write(path: Path, content: str, mode: int = 0o600) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
fd, temporary = tempfile.mkstemp(prefix='.' + path.name, dir=path.parent)
try:
with os.fdopen(fd, 'w') as stream:
os.fchmod(stream.fileno(), mode)
stream.write(content)
stream.flush()
os.fsync(stream.fileno())
os.replace(temporary, path)
finally:
if os.path.exists(temporary):
os.unlink(temporary)
class Environment:
def __init__(self) -> None:
self.config = read_env(ROOT / '.env')
self.project = self.value('COMPOSE_PROJECT_NAME', 'mtusi-messenger-lab6')
if not re.fullmatch(r'[a-z0-9][a-z0-9_.-]*', self.project):
raise OperationError('invalid local Compose project name')
self.secrets = Path(self.value('LAB7_SECRETS_DIR', str(ROOT / 'secrets/lab7')))
if not self.secrets.is_absolute():
self.secrets = ROOT / self.secrets
self.secrets = self.secrets.resolve()
self.state = Path(self.value(
'LAB7_OPERATOR_STATE',
str(Path.home() / '.local/share/mtusi-lab7' / self.project),
)).expanduser().resolve()
self.secrets.mkdir(parents=True, exist_ok=True, mode=0o700)
self.state.mkdir(parents=True, exist_ok=True, mode=0o700)
os.chmod(self.secrets, 0o700)
os.chmod(self.state, 0o700)
self.redactions: set[str] = set()
self.bao_url = 'https://localhost:' + self.value('BAO_PORT', '8200')
self.base_url = 'https://localhost:' + self.value('PUBLIC_PORT', '8443')
self.process_env = {
**os.environ,
'COMPOSE_PROJECT_NAME': self.project,
'LAB7_SECRETS_DIR': str(self.secrets),
'NO_PROXY': 'localhost,127.0.0.1,::1',
'no_proxy': 'localhost,127.0.0.1,::1',
}
for key in ('HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'http_proxy', 'https_proxy', 'all_proxy'):
self.process_env.pop(key, None)
def value(self, key: str, fallback: str) -> str:
return os.environ.get(key, self.config.get(key, fallback))
def redact(self, value: str) -> str:
for secret in self.redactions:
if len(secret) >= 8:
value = value.replace(secret, '[REDACTED]')
return value
def run(self, command: list[str], *, data: str | None = None,
check: bool = True, timeout: float = 180) -> subprocess.CompletedProcess:
result = subprocess.run(command, cwd=ROOT, input=data, text=True,
capture_output=True, timeout=timeout, env=self.process_env)
if check and result.returncode:
# SQL/bootstrap commands can echo a failed statement containing a password.
detail = '' if data is not None else self.redact(result.stderr[-1800:])
raise OperationError(f'{Path(command[0]).name} failed ({result.returncode}): {detail}')
return result
def compose(self, *args: str, data: str | None = None,
check: bool = True, timeout: float = 180) -> subprocess.CompletedProcess:
env_file = ROOT / ('.env' if (ROOT / '.env').exists() else '.env.example')
configured = self.process_env.get('COMPOSE_FILE', self.value('COMPOSE_FILE', str(ROOT / 'compose.yaml')))
files = configured.split(os.pathsep)
override = self.value('LAB7_COMPOSE_OVERRIDE', '')
if override and override not in files:
files.append(override)
arguments = ['docker', 'compose', '--env-file', str(env_file)]
for filename in files:
path = Path(filename)
if not path.is_absolute():
path = ROOT / path
arguments.extend(['-f', str(path)])
return self.run([*arguments, *args], data=data, check=check, timeout=timeout)
def private_json(self, path: Path, value: dict) -> None:
atomic_write(path, json.dumps(value, indent=2) + '\n')
def read_json(self, path: Path, fallback: dict | None = None) -> dict:
if not path.exists():
return {} if fallback is None else fallback
return json.loads(path.read_text())
def identity(self, role: str) -> Path:
return self.secrets / 'pki' / role / 'current'
def group_access(self, path: Path) -> None:
# Operator-only helper: no network, no daemon socket, narrowly scoped mount.
# DAC_OVERRIDE is needed to traverse operator-owned private directories;
# CHOWN assigns the runtime group without making private keys world-readable.
self.run(['docker', 'run', '--rm', '--network', 'none', '--user', '0:0',
'--cap-drop', 'ALL', '--cap-add', 'CHOWN', '--cap-add', 'DAC_OVERRIDE',
'--security-opt', 'no-new-privileges:true',
'--mount', f'type=bind,source={path},target=/generated',
ALPINE_IMAGE, 'chown', '-R', f'{os.getuid()}:10001', '/generated'])
def snapshot(self, role: str) -> dict[str, str]:
directory = self.identity(role).resolve(strict=True)
return {name: (directory / name).read_text()
for name in ('tls.crt', 'tls.key', 'ca.crt', 'ca.crl')}
def publish(self, role: str, certificate: str, private_key: str,
ca_bundle: str, crls: str) -> dict:
if role not in ROLES and role not in ('wrong-identity', 'untrusted', 'next-lb'):
raise OperationError('unknown service identity')
certificate = certificate.rstrip() + '\n'
private_key = private_key.rstrip() + '\n'
public = self.run(['openssl', 'x509', '-pubkey', '-noout'], data=certificate).stdout
key_public = self.run(['openssl', 'pkey', '-pubout'], data=private_key).stdout
if public.strip() != key_public.strip():
raise OperationError('certificate and private key do not match')
directory = self.secrets / 'pki' / role
version_name = str(time.time_ns()) + '-' + hashlib.sha256(certificate.encode()).hexdigest()[:12]
version = directory / 'versions' / version_name
version.mkdir(parents=True, mode=0o750)
for parent in (directory, directory / 'versions'):
os.chmod(parent, 0o750)
for name, content in {
'tls.crt': certificate, 'tls.key': private_key,
'tls.pem': certificate + private_key, 'ca.crt': ca_bundle, 'ca.crl': crls,
}.items():
atomic_write(version / name, content, 0o640)
self.group_access(directory if not (directory / 'current').exists() else version)
temporary = directory / ('.current-' + version_name)
temporary.symlink_to(Path('versions') / version_name)
os.replace(temporary, directory / 'current')
selector = directory / ('.current-version-' + version_name)
atomic_write(selector, version_name + '\n', 0o640)
self.group_access(selector)
os.replace(selector, directory / 'current-version')
description = self.run(['openssl', 'x509', '-noout', '-serial', '-enddate'],
data=certificate).stdout.splitlines()
serial = next(line.split('=', 1)[1].lower() for line in description if line.startswith('serial='))
expiry = next(line.split('=', 1)[1] for line in description if line.startswith('notAfter='))
return {'role': role, 'serial_number': serial, 'not_after': expiry}
class BaoClient:
def __init__(self, environment: Environment, role: str | None = 'operator') -> None:
self.environment = environment
self.role = role
identity = environment.identity(role or 'operator')
context = ssl.create_default_context(cafile=str(identity / 'ca.crt'))
context.minimum_version = ssl.TLSVersion.TLSv1_2
if role is not None:
context.load_cert_chain(str(identity / 'tls.crt'), str(identity / 'tls.key'))
self.opener = urllib.request.build_opener(
urllib.request.ProxyHandler({}), urllib.request.HTTPSHandler(context=context))
def request(self, method: str, path: str, data: dict | None = None,
token: str | None = None, raw: bool = False,
allowed: tuple[int, ...] = (200, 204), timeout: float = 5):
headers = {'Accept': 'application/json', 'Connection': 'close'}
payload = None
if data is not None:
headers['Content-Type'] = 'application/json'
payload = json.dumps(data).encode()
if token:
headers['X-Vault-Token'] = token
self.environment.redactions.add(token)
request = urllib.request.Request(self.environment.bao_url + '/v1/' + path.lstrip('/'),
data=payload, method=method, headers=headers)
try:
with self.opener.open(request, timeout=timeout) as response:
body = response.read(1024 * 1024 + 1)
status = response.status
except urllib.error.HTTPError as error:
status = error.code
body = error.read(1024 * 1024 + 1)
except (urllib.error.URLError, TimeoutError) as error:
raise OperationError('OpenBao verified TLS transport failed: ' + str(error)) from error
if status not in allowed:
raise OperationError(f'OpenBao {method} {path} rejected with HTTP {status}')
if len(body) > 1024 * 1024:
raise OperationError('OpenBao response exceeds one MiB')
if raw:
return body.decode()
return json.loads(body) if body else {}
def login(self, role: str | None = None) -> str:
response = self.request('POST', 'auth/cert/login', {'name': role or self.role})
token = response.get('auth', {}).get('client_token')
if not token:
raise OperationError('OpenBao did not issue a scoped service token')
self.environment.redactions.add(token)
return token