Skeleton contracts / contracts (pull_request) Canceled after 0s
Закрывает обязательный минимум и трек 1: ротация leaf и секрета, sealed OpenBao и сохранность queued-задачи.
326 lines
12 KiB
Python
326 lines
12 KiB
Python
"""Local grade-4 proofs: rehash, session expiry, LB leaf rotation, sealed OpenBao, telemetry.
|
|
|
|
Prints statuses only. Restores certificates, the allowlist and the vault before exiting.
|
|
"""
|
|
import json
|
|
import ssl
|
|
import subprocess
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
from cryptography import x509
|
|
from cryptography.hazmat.primitives import hashes, serialization
|
|
from cryptography.hazmat.primitives.asymmetric import rsa
|
|
from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PKI = ROOT / "secrets" / "pki"
|
|
OPERATOR = ROOT / "secrets" / "operator"
|
|
EVIDENCE = ROOT / "evidence"
|
|
BASE = "https://127.0.0.1:8443"
|
|
CA = PKI / "ca.crt"
|
|
|
|
|
|
def run(command, check=True, input_text=None):
|
|
result = subprocess.run(
|
|
command,
|
|
cwd=ROOT,
|
|
input=input_text,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
if check and result.returncode != 0:
|
|
detail = ((result.stderr or result.stdout or "")[:240]).replace("\n", " ")
|
|
raise SystemExit("command failed: " + detail)
|
|
return result
|
|
|
|
|
|
def bao(*args, token=None):
|
|
command = [
|
|
"docker",
|
|
"compose",
|
|
"--env-file",
|
|
".env",
|
|
"exec",
|
|
"-T",
|
|
"-e",
|
|
"BAO_ADDR=https://127.0.0.1:8200",
|
|
"-e",
|
|
"BAO_CACERT=/openbao/tls/ca.crt",
|
|
"-e",
|
|
"BAO_CLIENT_CERT=/openbao/tls/operator.crt",
|
|
"-e",
|
|
"BAO_CLIENT_KEY=/openbao/tls/operator.key",
|
|
]
|
|
if token:
|
|
command += ["-e", "BAO_TOKEN=" + token]
|
|
command += ["openbao", "bao", *args]
|
|
return run(command, check=False)
|
|
|
|
|
|
def bao_status():
|
|
result = bao("status", "-format=json")
|
|
text = result.stdout or ""
|
|
start = text.find("{")
|
|
end = text.rfind("}")
|
|
if start < 0 or end < start:
|
|
return None
|
|
return json.loads(text[start : end + 1])
|
|
|
|
|
|
def root_token():
|
|
return json.loads((OPERATOR / "init.json").read_text(encoding="utf-8"))["root_token"]
|
|
|
|
|
|
def https(path, cert=None, key=None, data=None, method=None, headers=None):
|
|
ctx = ssl.create_default_context(cafile=str(CA))
|
|
if cert and key:
|
|
ctx.load_cert_chain(str(cert), str(key))
|
|
request = urllib.request.Request(BASE + path, data=data, headers=headers or {}, method=method)
|
|
try:
|
|
with urllib.request.urlopen(request, context=ctx, timeout=8) as response:
|
|
return response.status, response.read()
|
|
except urllib.error.HTTPError as exc:
|
|
return exc.code, exc.read()
|
|
|
|
|
|
def psql(sql):
|
|
result = run(
|
|
["docker", "exec", "-i", "mtusi-lab7-postgres-1", "psql", "-U", "messenger", "-d", "messenger", "-t", "-A"],
|
|
input_text=sql,
|
|
)
|
|
return (result.stdout or "").strip()
|
|
|
|
|
|
def mtls_ready(cert, key):
|
|
code = (
|
|
"import ssl,urllib.request,urllib.error\n"
|
|
"ctx=ssl.create_default_context(cafile='/c/ca.crt')\n"
|
|
"ctx.load_cert_chain('/c/client.crt','/c/client.key')\n"
|
|
"try:\n"
|
|
" r=urllib.request.urlopen('https://api-1:8443/health/ready', context=ctx, timeout=8)\n"
|
|
" print(r.status)\n"
|
|
"except urllib.error.HTTPError as exc:\n"
|
|
" print('http', exc.code)\n"
|
|
"except Exception as exc:\n"
|
|
" print(type(exc).__name__)\n"
|
|
)
|
|
result = run(
|
|
[
|
|
"docker",
|
|
"run",
|
|
"--rm",
|
|
"--network",
|
|
"mtusi-lab7_app",
|
|
"-v",
|
|
str(CA) + ":/c/ca.crt:ro",
|
|
"-v",
|
|
str(cert) + ":/c/client.crt:ro",
|
|
"-v",
|
|
str(key) + ":/c/client.key:ro",
|
|
"--entrypoint",
|
|
"python",
|
|
"mtusi-lab7-messenger:1.7.0",
|
|
"-c",
|
|
code,
|
|
]
|
|
)
|
|
return (result.stdout or "").strip()
|
|
|
|
|
|
def issue_lb_leaf():
|
|
ca_key = serialization.load_pem_private_key((PKI / "ca.key").read_bytes(), password=None)
|
|
ca_cert = x509.load_pem_x509_certificate((PKI / "ca.crt").read_bytes())
|
|
key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
|
|
now = __import__("datetime").datetime.now(__import__("datetime").timezone.utc)
|
|
cert = (
|
|
x509.CertificateBuilder()
|
|
.subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "lb.messenger.internal")]))
|
|
.issuer_name(ca_cert.subject)
|
|
.public_key(key.public_key())
|
|
.serial_number(x509.random_serial_number())
|
|
.not_valid_before(now - __import__("datetime").timedelta(minutes=5))
|
|
.not_valid_after(now + __import__("datetime").timedelta(days=7))
|
|
.add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True)
|
|
.add_extension(x509.SubjectAlternativeName([x509.DNSName("lb.messenger.internal")]), critical=False)
|
|
.add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.CLIENT_AUTH]), critical=False)
|
|
.sign(ca_key, hashes.SHA256())
|
|
)
|
|
leaf_key = PKI / "lb-rotated.key"
|
|
leaf_crt = PKI / "lb-rotated.crt"
|
|
leaf_key.write_bytes(
|
|
key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, serialization.NoEncryption())
|
|
)
|
|
leaf_crt.write_bytes(cert.public_bytes(serialization.Encoding.PEM))
|
|
return leaf_crt, leaf_key, format(cert.serial_number, "X")
|
|
|
|
|
|
def reload_nginx():
|
|
run(["docker", "exec", "mtusi-lab7-nginx-1", "nginx", "-s", "reload"])
|
|
|
|
|
|
def count_leaks():
|
|
patterns = ("Bearer ", "password=", "postgresql://", "aws_secret_access_key", "BEGIN PRIVATE")
|
|
counts = {pattern: 0 for pattern in patterns}
|
|
for name in ("mtusi-lab7-api-1-1", "mtusi-lab7-api-2-1", "mtusi-lab7-worker-1", "mtusi-lab7-loki-1"):
|
|
result = run(["docker", "logs", name, "--since", "20m"], check=False)
|
|
text = (result.stdout or "") + (result.stderr or "")
|
|
for pattern in patterns:
|
|
counts[pattern] += text.count(pattern)
|
|
return counts
|
|
|
|
|
|
def rehash_and_expiry():
|
|
from argon2 import PasswordHasher
|
|
|
|
username = "rehash" + uuid.uuid4().hex[:8]
|
|
password = "rotate-horse-2"
|
|
body = json.dumps({"username": username, "display_name": "Rehash", "password": password}).encode("utf-8")
|
|
status, _ = https("/api/v1/users", data=body, method="POST", headers={"Content-Type": "application/json"})
|
|
if status != 201:
|
|
raise SystemExit("rehash register failed")
|
|
old = PasswordHasher(time_cost=1, memory_cost=8192, parallelism=1).hash(password)
|
|
psql("UPDATE users SET password_hash = %s WHERE username = %s;\n" % (_quote(old), _quote(username)))
|
|
before = psql("SELECT substring(password_hash from 'm=[0-9]+,t=[0-9]+,p=[0-9]+') FROM users WHERE username = %s;\n" % _quote(username))
|
|
import base64
|
|
|
|
basic = base64.b64encode((username + ":" + password).encode("utf-8")).decode("ascii")
|
|
login_status, raw = https("/api/v1/auth/sessions", method="POST", headers={"Authorization": "Basic " + basic})
|
|
after = psql("SELECT substring(password_hash from 'm=[0-9]+,t=[0-9]+,p=[0-9]+') FROM users WHERE username = %s;\n" % _quote(username))
|
|
token = json.loads(raw.decode("utf-8")).get("token") if login_status == 201 else ""
|
|
expired = None
|
|
if token:
|
|
psql(
|
|
"UPDATE sessions SET expires_at = NOW() - INTERVAL '1 minute' "
|
|
"WHERE user_id = (SELECT id FROM users WHERE username = %s);\n" % _quote(username)
|
|
)
|
|
expired, _ = https("/api/v1/users/me", headers={"Authorization": "Bearer " + token})
|
|
psql("DELETE FROM sessions WHERE user_id IN (SELECT id FROM users WHERE username = %s);\n" % _quote(username))
|
|
psql("DELETE FROM users WHERE username = %s;\n" % _quote(username))
|
|
return {
|
|
"login": login_status,
|
|
"params_before": before,
|
|
"params_after": after,
|
|
"expired_session": expired,
|
|
}
|
|
|
|
|
|
def _quote(value):
|
|
return "'" + value.replace("'", "''") + "'"
|
|
|
|
|
|
def _until(action, expected, attempts=8):
|
|
last = ""
|
|
for _ in range(attempts):
|
|
last = action()
|
|
if last == expected or last.startswith(expected):
|
|
return last
|
|
time.sleep(1)
|
|
return last
|
|
|
|
|
|
def rotate_leaf():
|
|
allow = PKI / "lb-allowed-serials.txt"
|
|
backup_allow = allow.read_bytes()
|
|
backup_crt = (PKI / "lb.crt").read_bytes()
|
|
backup_key = (PKI / "lb.key").read_bytes()
|
|
leaf_crt, leaf_key, serial = issue_lb_leaf()
|
|
try:
|
|
allow.write_text(serial + "\n", encoding="ascii")
|
|
rotated = _until(lambda: mtls_ready(leaf_crt, leaf_key), "200")
|
|
retired = _until(lambda: mtls_ready(PKI / "lb.crt", PKI / "lb.key"), "http 403")
|
|
(PKI / "lb.crt").write_bytes(leaf_crt.read_bytes())
|
|
(PKI / "lb.key").write_bytes(leaf_key.read_bytes())
|
|
reload_nginx()
|
|
time.sleep(1)
|
|
public, _ = https("/health/ready")
|
|
return {"new_serial_ready": rotated, "old_serial": retired, "public_after_reload": public}
|
|
finally:
|
|
allow.write_bytes(backup_allow)
|
|
(PKI / "lb.crt").write_bytes(backup_crt)
|
|
(PKI / "lb.key").write_bytes(backup_key)
|
|
leaf_crt.unlink(missing_ok=True)
|
|
leaf_key.unlink(missing_ok=True)
|
|
reload_nginx()
|
|
|
|
|
|
def seal_cycle():
|
|
before, _ = https("/health/ready")
|
|
open_login, _ = https(
|
|
"/api/v1/auth/sessions",
|
|
method="POST",
|
|
headers={"Authorization": "Basic YmFkOmJhZA=="},
|
|
)
|
|
sealed = bao("operator", "seal", token=root_token())
|
|
if sealed.returncode != 0:
|
|
raise SystemExit("OpenBao seal failed")
|
|
report = {"ready_before": before, "login_before_seal": open_login, "login_while_sealed": None, "secret_env": []}
|
|
try:
|
|
time.sleep(20)
|
|
deadline = time.monotonic() + 30
|
|
denied = None
|
|
while time.monotonic() < deadline:
|
|
denied, _ = https(
|
|
"/api/v1/auth/sessions",
|
|
method="POST",
|
|
headers={"Authorization": "Basic YmFkOmJhZA=="},
|
|
)
|
|
if denied == 503:
|
|
break
|
|
time.sleep(2)
|
|
env = run(["docker", "exec", "mtusi-lab7-api-1-1", "printenv"], check=False)
|
|
leaked = [
|
|
line.split("=", 1)[0]
|
|
for line in (env.stdout or "").splitlines()
|
|
if line.startswith(("DATABASE_URL=", "S3_SECRET_ACCESS_KEY=", "VALKEY_PASSWORD=", "BAO_TOKEN="))
|
|
]
|
|
report["login_while_sealed"] = denied
|
|
report["secret_env"] = leaked
|
|
finally:
|
|
run(["python", "scripts/unseal.py"])
|
|
deadline = time.monotonic() + 90
|
|
while time.monotonic() < deadline:
|
|
status, _ = https("/health/ready")
|
|
if status == 200:
|
|
break
|
|
time.sleep(2)
|
|
return report
|
|
|
|
|
|
def main():
|
|
EVIDENCE.mkdir(exist_ok=True)
|
|
report = {
|
|
"rehash": rehash_and_expiry(),
|
|
"leaf": rotate_leaf(),
|
|
"telemetry_leak_counts": count_leaks(),
|
|
}
|
|
sealed = seal_cycle()
|
|
report["sealed"] = sealed if isinstance(sealed, dict) else {"restored": True}
|
|
ready, _ = https("/health/ready")
|
|
report["ready_after_unseal"] = ready
|
|
(EVIDENCE / "verify-local.json").write_text(json.dumps(report, indent=2), encoding="utf-8")
|
|
print(json.dumps(report))
|
|
ok = (
|
|
report["rehash"]["login"] == 201
|
|
and report["rehash"]["params_before"].startswith("m=8192")
|
|
and report["rehash"]["params_after"] == "m=19456,t=2,p=1"
|
|
and report["rehash"]["expired_session"] == 401
|
|
and report["leaf"]["new_serial_ready"] == "200"
|
|
and report["leaf"]["old_serial"].startswith("http 403")
|
|
and report["leaf"]["public_after_reload"] == 200
|
|
and report["sealed"]["login_before_seal"] == 401
|
|
and report["sealed"]["login_while_sealed"] == 503
|
|
and report["sealed"]["secret_env"] == []
|
|
and ready == 200
|
|
and all(value == 0 for value in report["telemetry_leak_counts"].values())
|
|
)
|
|
if not ok:
|
|
raise SystemExit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|