Repository navigation
Description
Parent: #2042, step 6 of the build order (#2085 → #2087 → #2086 → #2088 → #2091 → #2089 → #2090). Needs #2087 (Binding, shared mode) and #2088 (credential names). Replaces #1482 and #1486, which are closed.
Problem
On a shared egress host, the audit event is the record of what each tenant did. moca's host agent forwards it to moca's control plane. It must be complete for every request, decrypted ones included, it must be in moca's exact format, and it must never slow the proxy down.
Today vmcontrol writes one audit line per request from its own wrapper. That wrapper never sees a decrypted request, and it does not know the credential, the dialed address or the byte counts. Its sink writes synchronously.
Shared mode (#2087) already turns off the session store, the session API and the other listeners on this role, so this issue is only about the audit event.
Shared contract
This issue adds the last method of Binding (#2042, "Shared contract"), and the Outcome type:
// Record receives the one terminal outcome of each admitted request.
Record(r *http.Request, o Outcome)
// core/pipeline
type Outcome struct {
Status int
Code string // the refusal code, "" when the request was not refused
Tunnel bool // an opaque CONNECT
Intercepted bool // a CONNECT that the TLS bridge opened
Credential string // the injected or revoked credential's name, "" when none
Revoked bool // the credential was revoked during the connection
Subject string // the credential's subject from the lookup, "" when none
DialAddr string // the checked address that was dialed, "" when none
BytesUp int64
BytesDown int64
}Today
Read at Cortex main 7f3eec7 and moca main e464dd6 (2026-11-23).
| What | Where |
|---|---|
| The audit event, in moca's shape | vmcontrol.Event (core/listener/vmcontrol/audit.go:29-46): time, via, vm_id, sid, sub, host, dial_addr, method, path, status, bytes_up, bytes_down, credential, decision, dropped, coalesced |
| The sink | AuditSink interface{ Emit(Event) } (audit.go:49). cmd/cortex/vmcontrol_unix.go:28 uses vmcontrol.NewJSONSink(os.Stdout) (audit.go:63), which writes synchronously. There is no asynchronous sink. |
| Why synchronous is a problem | A stalled sink ties up a VM's 8 refusal slots, so over-cap connections close without a 429. Unregister waits for each in-flight request's line, so a stalled sink stalls Unregister (#1482's comment). moca uses NewAsyncSink(NewJSONSink(os.Stdout), 0). |
| Where lines are written today | vmBinding.ServeHTTP (vmcontrol/handler.go:189-223) → emit (:228-243): aborted, tunnel for a CONNECT, forward otherwise. chargeRequest (:82-95, request_cap) and refuseConn (:60-76, conn_cap). Nothing is written for a decrypted request. event() uses r.Host, which can carry a port (:247). moca's host has no port, is lower case and has no trailing dot (moca handler.go:115-141). |
| Byte counts | Counted for tunnels (sessionevent.TunnelClose, forwardproxy/transparent.go:373) and WebSockets (websocket.go:277, 482, 881). For forwarded requests only the response is counted (pctx.ResponseBytes through bodycount.Wrap, server.go:964-990, pipeline/context.go:326). |
| Paths that skip work when the store is off | recordTunnelClosed (transparent.go:370) and refuseMisdirected (tunnel.go:139) return early when s.Sessions == nil. Shared mode turns the store off. |
| Where a request ends | Tunnels: tunnelLog.close (tunnel.go:771-786). Plain requests: the end of serveOutbound. Decrypted requests: the end of the bridged handler. |
| The dial address | pctx.DialedAddr is set only for CONNECT tunnels and the transparent listener (tunnel.go:436, transparent.go:187). On a bound, intercepted connection, #2088 dials the checked address once and keeps it. |
conn_cap events in Cortex |
Combined by count: one event per 64 refusals (vmcontrol/registry.go:56, 271-280). moca combines them by time: at most one per second per VM. |
| moca's sink | A queue of 4096, a drop report every 10 s, a flush on close, and time stamped when the event is emitted, not when it is written (moca remote-worker/internal/egress/audit.go:99-202) |
| moca's decision names | tunnel, forward, destination_denied, destination_private, conn_cap, request_cap, no_binding, credential_unavailable, upstream_unreachable, bad_request, aborted, credential_injected, credential_revoked, plaintext_credential, upstream_unverified, misdirected_request, leaf_unavailable, audit_dropped (moca audit.go:13-34) |
The sub rule |
sub is present only on credential_injected, and on upstream_unreachable after an injection, where the token may have been sent. The suite checks it on every event it reads (moca remote-worker/egress/conformance/conformance.go:467, helpers at cases.go:1030-1042, doc.go). |
| Other rules the suite checks | Exactly one credential_injected event per request (cases.go:1152-1160). One aborted event for each live connection that Unregister ends, and nothing else on it (moca handler.go:191-202). No event for the CONNECT of an intercepted connection: only its requests get events, plus a handshake-time misdirected_request or leaf_unavailable (moca intercept.go:127-138). conn_cap events combined to at most one per second per VM, with at most 8 refusals per VM in flight (handler.go:27-36). Case audit_query_removed. |
| The reader | moca's host agent reads the unit's journal, keeps .via == "moca-egress", and posts the events unchanged (moca deploy/sbx/host-agent.sh:37-60, 806-807). moca's control plane does not store an event with a field it does not know, and the host agent does not send an event over 8 KiB (moca deploy/sbx/host-agent.sh:806-807, docs/specs/2026-11-08-sandbox-egress-records-design.md §3). moca closes its sink after the proxy closes (CloseAudit, moca audit.go:67-75). |
Change
- The core calls
Recordonce for each admitted request, when it ends, with anOutcome:- Tunnels: in
tunnelLog.close. Plain requests: at the end ofserveOutbound. Decrypted requests: at the end of the bridged handler. - These calls do not depend on the session store. They must not sit behind the
s.Sessions == nilreturns (transparent.go:370,tunnel.go:139). - A request that
Admitrefused gets noRecord, because the driver already wrote its line.
- Tunnels: in
- The
Outcomefields:Code: the refusal writer of feat: Answer egress refusals in Cortex's own shape and move moca's format and MOCA_EGRESS_ALLOW into the moca driver #2086 stores the code of the refusal it wrote in the request context, and the core copies it. That covers every refusal site, thehttp.Errorsites included.Credential,Subject,Revoked: frompctx.SetCredential(feat: Resolve credentials per caller through a resolver seam, with moca's resolver in the moca driver #2088).DialAddr: for a tunnel,pctx.DialedAddr. For a decrypted request, the address that feat: Resolve credentials per caller through a resolver seam, with moca's resolver in the moca driver #2088's bound connection dialed. For a plain forwarded request, the address its dial used (httptrace.GotConn).BytesUp: count the request body, as moca does.BytesDown:pctx.ResponseBytes, as today. For tunnels, the existing counts.
vmBinding.Recordbuilds moca's event from the outcome, and emits it:- Drop the outcome when the binding is aborted: the wrapper writes the one
abortedline. - Drop the outcome of an intercepted CONNECT unless it has a
Code. Its requests get their own events. decision:Codewhen it is set, elsecredential_revokedwhenRevoked, elsecredential_injectedwhenCredentialis set, elsetunnelwhenTunnel, elseforward.sub:Subject, only oncredential_injected, and onupstream_unreachablewhenCredentialis set and notRevoked.credential: the name, never a value. It is also set oncredential_revoked.path:RedactPath, never on a CONNECT.host: without a port, lower case, no trailing dot, as moca's. Use the same rule in the wrapper's own lines.viastays"moca-egress". A change is moca's decision (D9 in design: A general multi-tenant egress core in Cortex, with moca's code behind one build tag #2042).- Exactly moca's field set, nothing more. An extra field makes moca drop the event with no error. A worker field (D6) waits for moca's control plane.
- Drop the outcome when the binding is aborted: the wrapper writes the one
vmBinding.ServeHTTPno longer writes thetunnelandforwardlines. It keeps theabortedline and the "binding closed"503line, which happen outside the core. Update the vmcontrol tests that expect the old lines (server_test.go,bridge_binding_test.go).conn_capevents by time: combine them to at most one event per second per VM, with acoalescedcount, as moca does. Keep at most 8 refusals per VM in flight.- An asynchronous sink,
vmcontrol.NewAsyncSink(inner AuditSink, size int), as moca's: a queue of 4096 by default,audit_droppedwith adroppedcount every 10 s, andtimestamped atEmit. A request and anUnregisternever wait for it.startVMControluses it.vmControlServer.Closecloses it after the forward proxy and the registry close, and the close flushes the queue. - The demo driver (feat: Add a tenant-listener identity source to demo multi-tenancy without moca #2091) gets an empty
Record.
Not in scope
- The session store, the session API and the other listeners on this role. Shared mode (feat: Add a connection Binding and shared mode, and key VM bindings by control connection #2087) turns them off.
- Recording for each subject, with an operator-only API. Later, if someone needs it.
- The host agent reading Cortex's unit name (D9, moca's work).
Tests
- The core with a test binding: exactly one
Recordfor a plain request, a CONNECT, and each decrypted request, with the rightOutcomefields. None for a request thatAdmitrefused. vmBinding.Record: one case for each decision name above, and thesubrule on every one. An aborted request writes onlyaborted. An intercepted CONNECT writes no line of its own.hosthas no port.- The core with shared mode on (no session store):
Recordstill runs for a tunnel and for a misdirected request. conn_cap: a burst of refusals in one second gives one event with the rightcoalescedcount.- A
credential_injectedevent hassub,credential,dial_addrand byte counts, and no credential value. The JSON has exactly moca's keys. Unregisterwith live connections: oneabortedevent for each, and nothing else on them.- A sink whose writer blocks: requests and
Unregistercomplete, andaudit_droppedreports the count. - The
moca-conformancejob (feat: Add a host-egress build and keep moca's vmcontrol driver out of every other build #2085): remove each case this issue makes pass fromknown-failures.txt, among them the credential cases that feat: Resolve credentials per caller through a resolver seam, with moca's resolver in the moca driver #2088 left on it because they wait for an audit event.
Done when
- Every request on a handed-over connection leaves exactly one complete audit event in moca's format, decrypted requests included.
- A slow journal cannot slow the proxy.
known-failures.txtlists no audit case and no credential case.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBacklog
Activity
[-]feat: Keep callers' data apart on a shared egress host: recording off and one complete, asynchronous audit event per request[/-][+]feat: Write one complete, asynchronous audit event per request through Binding.Record[/+]