2.7 KiB
2.7 KiB
Plan: On-Demand Certificate Retrieval in NetworkProxy
When a TLS connection arrives with an SNI for a domain that has no certificate yet, we want to automatically kick off certificate issuance (ACME HTTP-01 or DNS-01) so the domain is provisioned on the fly without prior manual configuration.
Goals
- Automatically initiate certificate issuance upon first TLS handshake for an unprovisioned domain.
- Use Port80Handler (HTTP-01) or custom
certProvisionFunction(e.g., DNS-01) to retrieve the certificate. - Continue the TLS handshake immediately using the default certificate, then swap to the new certificate on subsequent connections.
- For HTTP traffic on port 80, register the domain for ACME and return a 503 until the challenge is complete.
Plan
-
Detect missing certificate in SNI callback:
- In
ts/networkproxy/classes.np.networkproxy.ts(or withinCertificateManager.handleSNI), after looking upcertificateCache, if no cert is found:- Call
port80Handler.addDomain({ domainName, sslRedirect: false, acmeMaintenance: true })to trigger dynamic provisioning. - Emit a
certificateRequestedevent for observability. - Immediately call
cb(null, defaultSecureContext)so the handshake uses the default cert.
- Call
- In
-
HTTP-01 fallback on port 80:
- In
ts/port80handler/classes.port80handler.ts``, inhandleRequest(), when a request arrives for a new domain not indomainCertificates`:- Call
addDomain({ domainName, sslRedirect: false, acmeMaintenance: true }). - Return HTTP 503 with a message like “Certificate issuance in progress.”
- Call
- In
-
CertProvisioner & events:
- Ensure
CertProvisioneris subscribed toPort80Handlerfor newly added domains. - After certificate issuance completes,
Port80HandleremitsCERTIFICATE_ISSUED,CertificateManagercaches and writes disk, and future SNI callbacks will serve the new cert.
- Ensure
-
Metrics and cleanup:
- Track dynamic requests count via a
certificateRequestedevent or metric. - Handle error paths: if ACME/DNS fails, emit
CERTIFICATE_FAILEDand continue serving default cert.
- Track dynamic requests count via a
-
Tests:
- Simulate a TLS ClientHello for an unconfigured domain:
• Verify
port80Handler.addDomainis called andcertificateRequestedevent emitted. • Confirm handshake completes with default cert context. - Simulate HTTP-01 challenge flow for a new domain:
• Verify on first HTTP request,
addDomainis invoked and 503 returned. • After manually injecting a challenge inHttp01MemoryHandler, verify 200 with key authorization. - Simulate successful ACME response and ensure SNI now returns the real cert.
- Simulate a TLS ClientHello for an unconfigured domain:
• Verify
-
Final validation:
- Run
pnpm testto ensure all existing tests pass. - Add new unit/integration tests for the dynamic provisioning flow.
- Run