Skip to content

feat: Write one complete, asynchronous audit event per request through Binding.Record #2089

Backlog

Description

@huang195

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

  1. The core calls Record once for each admitted request, when it ends, with an Outcome:
    • Tunnels: in tunnelLog.close. Plain requests: at the end of serveOutbound. 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 == nil returns (transparent.go:370, tunnel.go:139).
    • A request that Admit refused gets no Record, because the driver already wrote its line.
  2. The Outcome fields:
  3. vmBinding.Record builds moca's event from the outcome, and emits it:
    • Drop the outcome when the binding is aborted: the wrapper writes the one aborted line.
    • Drop the outcome of an intercepted CONNECT unless it has a Code. Its requests get their own events.
    • decision: Code when it is set, else credential_revoked when Revoked, else credential_injected when Credential is set, else tunnel when Tunnel, else forward.
    • sub: Subject, only on credential_injected, and on upstream_unreachable when Credential is set and not Revoked.
    • credential: the name, never a value. It is also set on credential_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.
    • via stays "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.
  4. vmBinding.ServeHTTP no longer writes the tunnel and forward lines. It keeps the aborted line and the "binding closed" 503 line, which happen outside the core. Update the vmcontrol tests that expect the old lines (server_test.go, bridge_binding_test.go).
  5. conn_cap events by time: combine them to at most one event per second per VM, with a coalesced count, as moca does. Keep at most 8 refusals per VM in flight.
  6. An asynchronous sink, vmcontrol.NewAsyncSink(inner AuditSink, size int), as moca's: a queue of 4096 by default, audit_dropped with a dropped count every 10 s, and time stamped at Emit. A request and an Unregister never wait for it. startVMControl uses it. vmControlServer.Close closes it after the forward proxy and the registry close, and the close flushes the queue.
  7. The demo driver (feat: Add a tenant-listener identity source to demo multi-tenancy without moca #2091) gets an empty Record.

Not in scope

Tests

  • The core with a test binding: exactly one Record for a plain request, a CONNECT, and each decrypted request, with the right Outcome fields. None for a request that Admit refused.
  • vmBinding.Record: one case for each decision name above, and the sub rule on every one. An aborted request writes only aborted. An intercepted CONNECT writes no line of its own. host has no port.
  • The core with shared mode on (no session store): Record still runs for a tunnel and for a misdirected request.
  • conn_cap: a burst of refusals in one second gives one event with the right coalesced count.
  • A credential_injected event has sub, credential, dial_addr and byte counts, and no credential value. The JSON has exactly moca's keys.
  • Unregister with live connections: one aborted event for each, and nothing else on them.
  • A sink whose writer blocks: requests and Unregister complete, and audit_dropped reports the count.
  • The moca-conformance job (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 from known-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.txt lists no audit case and no credential case.

Activity

  1. changed the title [-]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[/+] on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions