Add self-contained relay deployment project and operational guidance

This commit is contained in:
ex_tianzy
2026-10-27 17:51:43 +08:00
parent 011a7797ee
commit b774941890
49 changed files with 4454 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
"""Internal automatic CA issuer. No published host port, no application credentials."""
import os
from pathlib import Path
from cryptography import x509
from cryptography.hazmat.primitives import serialization
from fastapi import FastAPI, Request
from .common import BodyLimitMiddleware, require_secret
from .crypto import load_private, issue_device, raw_public, verify_device
from .enrollment import EnrollmentRequest, validate_enrollment
def create_app(*, ca_key_path=None, ca_cert_path=None, secret=None):
key = load_private(Path(ca_key_path or os.environ.get("HN_ISSUER_CA_KEY", "/issuer/ca.key")))
ca_bytes = Path(ca_cert_path or os.environ.get("HN_ISSUER_CA_CERT", "/trust/ca.pem")).read_bytes()
ca = x509.load_pem_x509_certificate(ca_bytes)
if raw_public(key.public_key()) != raw_public(ca.public_key()):
raise ValueError("issuer CA key mismatch")
token = secret or os.environ.get("HN_INTERNAL_SECRET", "")
if len(token) < 32:
raise ValueError("issuer requires internal credential")
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
app.add_middleware(BodyLimitMiddleware, max_body=16384)
@app.get("/ready")
async def ready():
return {"status": "ready"}
@app.post("/issue")
async def issue(body: EnrollmentRequest, request: Request):
require_secret(request.headers.get("X-Internal-Secret"), token)
public = validate_enrollment(body)
cert = issue_device(key, ca, body.host_id, public).public_bytes(serialization.Encoding.PEM).decode()
verify_device(cert, ca_bytes, body.host_id)
return {"cert": cert}
return app
if __name__ == "__main__":
import uvicorn
uvicorn.run("relay.enrollment_signer:create_app", factory=True, host="0.0.0.0", port=8000,
workers=1, access_log=False, proxy_headers=False, log_level="warning")