Call the ledger over its internal endpoint
Node · Node · intermediate · modification
Moves the billing worker's ledger calls from the public endpoint to the internal one. A new `src/ledger/tls.ts` builds a dedicated `https.Agent` for the ledger: keep-alive with up to 10 sockets, our internal CA as the only trusted root (loaded from `LEDGER_CA_FILE`, which `src/config.ts` now requires), and an identity check for the certificates our internal CA issues. `postJournalEntry` in `src/ledger/client.ts` now sends its requests through that agent; the request, the response handling and the errors are unchanged.
Node 22 LTS, TypeScript 5 (strict), CommonJS; a background billing worker, no HTTP server of its own. `postJournalEntry` is called only by the invoice-finalisation job (unchanged), which marks the invoice as posted with the returned `entryId` when the call resolves, and on `LedgerError` retries the invoice later with the same `invoiceId` (the ledger dedupes on it). Infra sets `LEDGER_URL=https://ledger.billing.internal` in every environment (deploy config, outside this repo). `LEDGER_CA_FILE` points to the company's internal root CA in PEM, mounted read-only by the platform; a missing or unreadable file fails the worker at startup, which is intended, as for a missing variable. Internal DNS: `*.internal` names resolve inside the company network. The internal CA is the platform's self-service issuer: every internal service (the wiki, CI runners, team sandboxes) gets a certificate from it for its own `*.internal` hostname, and the ledger's certificate names `ledger.billing.internal` as a DNS SAN. The ledger answers with small JSON bodies (under 1 KB). `src/logger.ts` is unchanged and writes one JSON line per call.
Requirements
- Billing posts each finalised invoice to the ledger through `postJournalEntry` (existing). This PR moves those calls from the ledger's public endpoint to its internal one: `LEDGER_URL` becomes `https://ledger.billing.internal`, served with a certificate from the company's internal CA. For ledger calls the client trusts only that CA, read once at startup from the PEM file at `LEDGER_CA_FILE` (new, required like the other variables); public CAs are no longer trusted for this connection. Ledger calls go through their own connection pool: kept alive and reused, at most 10 sockets at a time.
- The bearer token (`LEDGER_TOKEN`) and the journal entries are sent only to the ledger: a TLS connection is used only when the server's certificate chains to the internal CA and is valid for the hostname in `LEDGER_URL`. A connection that fails either check is closed before any request is written, and the call rejects with `LedgerError`.
- Unchanged: `postJournalEntry` resolves `{ entryId }` on a 201 whose JSON body has a string `entryId`; any other status, an unreadable body, a connection or TLS failure, or a socket idle for 10 seconds rejects with `LedgerError` (the reason goes to the log, never the token).
Files touched
- src/config.ts
- src/ledger/tls.ts
- src/ledger/client.ts