417 lines
14 KiB
Python
417 lines
14 KiB
Python
"""Operator-only offline trust/bootstrap generation; never prints key material."""
|
|
|
|
import argparse
|
|
import hashlib
|
|
import ipaddress
|
|
import json
|
|
import os
|
|
import secrets
|
|
from datetime import UTC, datetime, timedelta
|
|
from pathlib import Path
|
|
from urllib.parse import quote
|
|
|
|
from cryptography import x509
|
|
from cryptography.hazmat.primitives import hashes, serialization
|
|
from cryptography.hazmat.primitives.asymmetric import ec
|
|
from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
OPERATOR = ROOT / "operator"
|
|
SECRET = ROOT / "secrets"
|
|
TRUST = ROOT / "trust"
|
|
ROLES = {
|
|
"openbao": {
|
|
"dns": ["openbao", "localhost"],
|
|
"ip": ["127.0.0.1"],
|
|
"uid": 1000,
|
|
"kv": False,
|
|
},
|
|
"api": {"dns": ["api", "localhost"], "uid": 10001, "kv": True},
|
|
"worker": {"dns": ["worker"], "uid": 10001, "kv": True},
|
|
"relay": {"dns": ["relay"], "uid": 10001, "kv": True},
|
|
"observer": {"dns": ["observer"], "uid": 10001, "kv": True},
|
|
"reconciler": {"dns": ["reconciler"], "uid": 10001, "kv": True},
|
|
"migrate": {"dns": ["migrate"], "uid": 10001, "kv": True},
|
|
"lb": {
|
|
"dns": ["lb", "localhost"],
|
|
"ip": ["127.0.0.1"],
|
|
"uid": 99,
|
|
"kv": False,
|
|
},
|
|
"postgres": {"dns": ["postgres"], "uid": 10001, "kv": False},
|
|
"s3": {
|
|
"dns": ["s3", "localhost"],
|
|
"ip": ["127.0.0.1"],
|
|
"uid": 10001,
|
|
"kv": False,
|
|
},
|
|
"broker": {"dns": ["broker"], "uid": 10001, "kv": False},
|
|
"cache": {"dns": ["cache"], "uid": 10001, "kv": False},
|
|
"otel": {"dns": ["otel"], "uid": 10001, "kv": False},
|
|
"prometheus": {"dns": ["prometheus"], "uid": 65534, "kv": False},
|
|
"loki": {"dns": ["loki"], "uid": 10001, "kv": False},
|
|
"tempo": {"dns": ["tempo"], "uid": 10001, "kv": False},
|
|
"grafana": {
|
|
"dns": ["grafana", "localhost"],
|
|
"ip": ["127.0.0.1"],
|
|
"uid": 472,
|
|
"kv": False,
|
|
},
|
|
}
|
|
|
|
|
|
def write_private(path, content, *, mode=0o600):
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
raw = content.encode() if isinstance(content, str) else content
|
|
temporary = path.with_name(path.name + ".new")
|
|
fd = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, mode)
|
|
try:
|
|
os.fchmod(fd, mode)
|
|
with os.fdopen(fd, "wb") as stream:
|
|
stream.write(raw)
|
|
stream.flush()
|
|
os.fsync(stream.fileno())
|
|
except BaseException:
|
|
temporary.unlink(missing_ok=True)
|
|
raise
|
|
temporary.replace(path)
|
|
|
|
|
|
def key_bytes(key):
|
|
return key.private_bytes(
|
|
serialization.Encoding.PEM,
|
|
serialization.PrivateFormat.PKCS8,
|
|
serialization.NoEncryption(),
|
|
)
|
|
|
|
|
|
def public(cert):
|
|
return cert.public_bytes(serialization.Encoding.PEM)
|
|
|
|
|
|
def make_root():
|
|
key = ec.generate_private_key(ec.SECP256R1())
|
|
name = x509.Name(
|
|
[
|
|
x509.NameAttribute(
|
|
NameOID.COMMON_NAME,
|
|
"Messenger lab root " + secrets.token_hex(4),
|
|
)
|
|
]
|
|
)
|
|
now = datetime.now(UTC)
|
|
cert = (
|
|
x509.CertificateBuilder()
|
|
.subject_name(name)
|
|
.issuer_name(name)
|
|
.public_key(key.public_key())
|
|
.serial_number(x509.random_serial_number())
|
|
.not_valid_before(now - timedelta(minutes=1))
|
|
.not_valid_after(now + timedelta(days=365))
|
|
.add_extension(
|
|
x509.BasicConstraints(ca=True, path_length=1), critical=True
|
|
)
|
|
.add_extension(
|
|
x509.KeyUsage(
|
|
digital_signature=True,
|
|
content_commitment=False,
|
|
key_encipherment=False,
|
|
data_encipherment=False,
|
|
key_agreement=False,
|
|
key_cert_sign=True,
|
|
crl_sign=True,
|
|
encipher_only=False,
|
|
decipher_only=False,
|
|
),
|
|
critical=True,
|
|
)
|
|
.add_extension(
|
|
x509.SubjectKeyIdentifier.from_public_key(key.public_key()),
|
|
critical=False,
|
|
)
|
|
.sign(key, hashes.SHA256())
|
|
)
|
|
return key, cert
|
|
|
|
|
|
def sign_leaf(ca_key, ca_cert, role, dns, ips=(), *, seconds=1800):
|
|
key = ec.generate_private_key(ec.SECP256R1())
|
|
now = datetime.now(UTC)
|
|
names = [x509.DNSName(name) for name in dns]
|
|
names += [x509.IPAddress(ipaddress.ip_address(ip)) for ip in ips]
|
|
names += [x509.UniformResourceIdentifier("spiffe://messenger/" + role)]
|
|
cert = (
|
|
x509.CertificateBuilder()
|
|
.subject_name(
|
|
x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, role)])
|
|
)
|
|
.issuer_name(ca_cert.subject)
|
|
.public_key(key.public_key())
|
|
.serial_number(x509.random_serial_number())
|
|
.not_valid_before(now - timedelta(seconds=30))
|
|
.not_valid_after(now + timedelta(seconds=seconds))
|
|
.add_extension(
|
|
x509.BasicConstraints(ca=False, path_length=None), critical=True
|
|
)
|
|
.add_extension(x509.SubjectAlternativeName(names), critical=False)
|
|
.add_extension(
|
|
x509.ExtendedKeyUsage(
|
|
[
|
|
ExtendedKeyUsageOID.CLIENT_AUTH,
|
|
ExtendedKeyUsageOID.SERVER_AUTH,
|
|
]
|
|
),
|
|
critical=False,
|
|
)
|
|
.add_extension(
|
|
x509.KeyUsage(
|
|
digital_signature=True,
|
|
content_commitment=False,
|
|
key_encipherment=False,
|
|
data_encipherment=False,
|
|
key_agreement=False,
|
|
key_cert_sign=False,
|
|
crl_sign=False,
|
|
encipher_only=False,
|
|
decipher_only=False,
|
|
),
|
|
critical=True,
|
|
)
|
|
.add_extension(
|
|
x509.AuthorityKeyIdentifier.from_issuer_public_key(
|
|
ca_key.public_key()
|
|
),
|
|
critical=False,
|
|
)
|
|
.sign(ca_key, hashes.SHA256())
|
|
)
|
|
return key, cert
|
|
|
|
|
|
def sign_intermediate(ca_key, ca_cert, csr):
|
|
now = datetime.now(UTC)
|
|
assert csr.is_signature_valid
|
|
return (
|
|
x509.CertificateBuilder()
|
|
.subject_name(csr.subject)
|
|
.issuer_name(ca_cert.subject)
|
|
.public_key(csr.public_key())
|
|
.serial_number(x509.random_serial_number())
|
|
.not_valid_before(now - timedelta(seconds=30))
|
|
.not_valid_after(now + timedelta(days=30))
|
|
.add_extension(
|
|
x509.BasicConstraints(ca=True, path_length=0), critical=True
|
|
)
|
|
.add_extension(
|
|
x509.KeyUsage(
|
|
digital_signature=True,
|
|
content_commitment=False,
|
|
key_encipherment=False,
|
|
data_encipherment=False,
|
|
key_agreement=False,
|
|
key_cert_sign=True,
|
|
crl_sign=True,
|
|
encipher_only=False,
|
|
decipher_only=False,
|
|
),
|
|
critical=True,
|
|
)
|
|
.add_extension(
|
|
x509.SubjectKeyIdentifier.from_public_key(csr.public_key()),
|
|
critical=False,
|
|
)
|
|
.add_extension(
|
|
x509.AuthorityKeyIdentifier.from_issuer_public_key(
|
|
ca_key.public_key()
|
|
),
|
|
critical=False,
|
|
)
|
|
.sign(ca_key, hashes.SHA256())
|
|
)
|
|
|
|
|
|
def ca():
|
|
return (
|
|
serialization.load_pem_private_key(
|
|
(OPERATOR / "ca-key.pem").read_bytes(), password=None
|
|
),
|
|
x509.load_pem_x509_certificate((OPERATOR / "ca.pem").read_bytes()),
|
|
)
|
|
|
|
|
|
def generate():
|
|
for directory in [OPERATOR, SECRET]:
|
|
directory.mkdir(mode=0o700, exist_ok=True)
|
|
directory.chmod(0o700)
|
|
TRUST.mkdir(mode=0o755, exist_ok=True)
|
|
if not (OPERATOR / "ca-key.pem").exists():
|
|
key, cert = make_root()
|
|
write_private(OPERATOR / "ca-key.pem", key_bytes(key))
|
|
write_private(OPERATOR / "ca.pem", public(cert))
|
|
key, cert = ca()
|
|
if not (TRUST / "ca.pem").exists():
|
|
write_private(TRUST / "ca.pem", public(cert), mode=0o644)
|
|
write_private(TRUST / "revoked.json", "{}\n", mode=0o644)
|
|
for role, cfg in {
|
|
**ROLES,
|
|
"openbao": {"dns": ["openbao", "localhost"], "ip": ["127.0.0.1"]},
|
|
"operator": {"dns": ["operator"]},
|
|
}.items():
|
|
directory = (
|
|
(OPERATOR / "client")
|
|
if role == "operator"
|
|
else SECRET / "bootstrap" / role
|
|
)
|
|
renew_operator = (
|
|
role == "operator"
|
|
and (directory / "cert.pem").exists()
|
|
and x509.load_pem_x509_certificate(
|
|
(directory / "cert.pem").read_bytes()
|
|
).not_valid_after_utc
|
|
< datetime.now(UTC) + timedelta(hours=1)
|
|
)
|
|
if not (directory / "key.pem").exists() or renew_operator:
|
|
leaf_key, leaf = sign_leaf(
|
|
key,
|
|
cert,
|
|
role,
|
|
cfg["dns"],
|
|
cfg.get("ip", ()),
|
|
seconds=86400 if role in {"operator", "openbao"} else 1800,
|
|
)
|
|
directory.mkdir(parents=True, exist_ok=True)
|
|
directory.chmod(0o700 if role == "operator" else 0o755)
|
|
mode = 0o600 if role == "operator" else 0o644
|
|
write_private(
|
|
directory / "key.pem", key_bytes(leaf_key), mode=mode
|
|
)
|
|
write_private(
|
|
directory / "cert.pem", public(leaf) + public(cert), mode=mode
|
|
)
|
|
write_private(
|
|
directory / "bundle.pem",
|
|
public(leaf) + public(cert) + key_bytes(leaf_key),
|
|
mode=mode,
|
|
)
|
|
path = OPERATOR / "service-secrets.json"
|
|
if path.exists():
|
|
raw = json.loads(path.read_text())
|
|
else:
|
|
raw = {
|
|
name: secrets.token_urlsafe(32)
|
|
for name in [
|
|
"postgres_password",
|
|
"s3_root_password",
|
|
"broker_admin",
|
|
"cache_admin",
|
|
"cache_api",
|
|
"grafana_password",
|
|
]
|
|
}
|
|
raw["s3_root_user"] = "operator-" + secrets.token_hex(12)
|
|
raw["services"] = {}
|
|
for role in [
|
|
"api",
|
|
"worker",
|
|
"relay",
|
|
"observer",
|
|
"reconciler",
|
|
"migrate",
|
|
]:
|
|
pwd = secrets.token_urlsafe(32)
|
|
values = {
|
|
"DATABASE_URL": "postgresql://"
|
|
+ ("messenger" if role == "migrate" else "svc_" + role)
|
|
+ ":"
|
|
+ quote(
|
|
raw["postgres_password"] if role == "migrate" else pwd,
|
|
safe="",
|
|
)
|
|
+ "@postgres:5443/messenger?sslmode=verify-full&gssencmode=disable&sslrootcert=/trust/ca.pem&sslcert=/run/identity/current/cert.pem&sslkey=/run/identity/current/key.pem"
|
|
}
|
|
service = {"database_password": pwd, "values": values}
|
|
if role == "api":
|
|
values["CACHE_URL"] = (
|
|
"rediss://api:" + raw["cache_api"] + "@cache:6380/0"
|
|
)
|
|
if role in {"relay", "worker"}:
|
|
service["broker_password"] = secrets.token_urlsafe(32)
|
|
values["BROKER_URL"] = (
|
|
"rediss://"
|
|
+ role
|
|
+ ":"
|
|
+ service["broker_password"]
|
|
+ "@broker:6380/0"
|
|
)
|
|
if role in {"api", "worker", "reconciler"}:
|
|
values.update(
|
|
S3_ACCESS_KEY=role + "-" + secrets.token_hex(12),
|
|
S3_SECRET_KEY=secrets.token_urlsafe(32),
|
|
)
|
|
raw["services"][role] = service
|
|
write_private(path, json.dumps(raw, indent=2) + "\n")
|
|
for name in [
|
|
"postgres_password",
|
|
"s3_root_user",
|
|
"s3_root_password",
|
|
"grafana_password",
|
|
]:
|
|
write_private(SECRET / name, raw[name] + "\n", mode=0o644)
|
|
for name in ["broker", "cache"]:
|
|
admin = raw[name + "_admin"]
|
|
lines = [
|
|
"bind 127.0.0.1",
|
|
"port 6379",
|
|
"protected-mode yes",
|
|
'save ""',
|
|
"maxmemory 64mb" if name == "cache" else "maxmemory 192mb",
|
|
"maxmemory-policy allkeys-lru"
|
|
if name == "cache"
|
|
else "maxmemory-policy noeviction",
|
|
"user default off",
|
|
"user admin on #"
|
|
+ hashlib.sha256(admin.encode()).hexdigest()
|
|
+ " ~* &* +@all",
|
|
]
|
|
if name == "cache":
|
|
lines += [
|
|
"user api on #"
|
|
+ hashlib.sha256(raw["cache_api"].encode()).hexdigest()
|
|
+ " ~user:* +get +set +ping +select +client|setinfo +client|setname"
|
|
]
|
|
else:
|
|
lines += ["appendonly yes", "appendfsync always", "dir /data"]
|
|
for role, commands in [
|
|
("relay", "+xadd +waitaof"),
|
|
(
|
|
"worker",
|
|
"+xgroup|create +xreadgroup +xautoclaim +xack +xdel",
|
|
),
|
|
]:
|
|
lines += [
|
|
"user "
|
|
+ role
|
|
+ " on #"
|
|
+ hashlib.sha256(
|
|
raw["services"][role]["broker_password"].encode()
|
|
).hexdigest()
|
|
+ " ~attachments +ping +select +client|setinfo +client|setname "
|
|
+ commands
|
|
]
|
|
write_private(
|
|
SECRET / (name + ".conf"), "\n".join(lines) + "\n", mode=0o644
|
|
)
|
|
write_private(SECRET / (name + "_admin"), admin + "\n", mode=0o644)
|
|
|
|
|
|
def main():
|
|
argparse.ArgumentParser(description=__doc__).parse_args()
|
|
generate()
|
|
print(
|
|
"Local operator trust, scoped service credentials and bootstrap certificates prepared; existing secrets retained"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|