Managoat.Broker

An egress credential proxy for sandboxed agents. The sandbox holds a placeholder where a credential used to be, plus a proxy address with a session token in it. Every outbound HTTP request goes through this proxy, which looks the token up in a session store the host implements, gets back the rules the host prepared for that session, and attaches the real credential to each request that matches one. The agent process never holds the credential; the proxy is the only host it may reach, so a placeholder is worthless off the box.

# The host's supervision tree:
children = [
{Managoat.Broker,
port: 14322,
store: MyApp.BrokerSessions, # a Managoat.Broker.Store
ca_seed: MyApp.broker_ca_seed(), # 32 bytes, the same on every replica
allow_private_upstreams: false}
]
# The sandbox's environment, prepared by the host:
# HTTPS_PROXY=http://<token>:<label>@broker.example.com:14322
# GITHUB_TOKEN=__github_token__
# and the root from Managoat.Broker.ca_pem/0 in its trust store.

What the proxy does

One plaintext HTTP listener (TLS toward the sandbox, if any, is the ingress's job) speaking the two things a forward proxy speaks:

Either form may name an IPv6 literal, bracketed: CONNECT [::1]:8443 and GET http://[::1]:8080/x. Names are resolved over A and AAAA, and the vetted addresses are dialed IPv4 first — every host that worked before takes the address it took before, and IPv6 is a path for hosts that previously had none. A leaf for a literal carries an iPAddress SAN rather than a dNSName one, so it validates as a client verifying an address expects.

The client authenticates with Proxy-Authorization: Basic base64(token:label), which is what an HTTP client sends for a proxy URL with userinfo. The token is looked up once per client connection (the unit a sandbox's HTTP client pools on); proxy-authorization never reaches the origin. A missing, unknown or expired token is 407.

Two guards protect the operator's network and the tenant's intent:

The Store behaviour

@callback lookup(token :: binary()) :: {:ok, Managoat.Broker.Session.t()} | :error

That is all the proxy needs at request time: the raw token in, a session with its rules (credentials already resolved) out. Creating, releasing and sweeping sessions are the host's business; they touch its tables and its key hierarchy, and the proxy never needs any of it. Hashing the token before storing it is the host's choice inside lookup/1; the library passes the raw token from the header.

A store with several instances (one per listener in a test) implements lookup/2 instead and is configured as store: {Module, instance}. Managoat.Broker.Store.Memory is the reference store, an Agent holding a map, for the library's tests and for a consumer without a database.

A Managoat.Broker.Session has rules, unmatched_host_policy (:passthrough or :deny), expires_at and an opaque meta map the host fills for its own logging. A Managoat.Broker.Rule has a pattern (host[:port][/path], wildcards allowed; an IPv6 literal is bracketed, [::1] or [::1]:8443, since otherwise there is no telling which colon is the port separator), a scheme and the fields the scheme needs:

schemefieldseffect on a matched request
:bearercredentialAuthorization: Bearer <credential> replaces any Authorization
:basiccredential as {username, password}Authorization: Basic base64(username:password)
:api_keyheader (default Authorization), prefix, credential<header>: <prefix><credential>
:customtemplate (%{header => "text {{ KEY }}"}), credential (%{"KEY" => value})each header rendered from its template
:substituteplaceholder, credentialevery header value and the request target have the placeholder replaced by the credential
:passthroughnoneforwarded untouched; under deny, how a host is allowed

When several rules match, the most specific one that sets a header does: an exact host beats a *. wildcard (whatever their paths), then a pinned port beats any port, then the longest literal path prefix wins, and declaration order breaks what is left — so defaults written first with overrides appended work the way they read, and a list of equally-specific rules resolves exactly as declaration order alone would. These are Agent Vault's tiers. A :passthrough rule never displaces a rule that injects, however specific it is: it is how a host is allowed under deny, not a way to suppress injection. Every matched :substitute rule applies, in declaration order, to the header values and to the request target. A credential goes into the target byte for byte — nothing is percent-encoded on the way in, because the proxy cannot know which URI component a placeholder sits in nor what the origin expects, and the canonical case says so: a bot token is <digits>:<rest> in a path segment, where : is legal unencoded and %3A is a different URL. A credential holding a control character or a space would split the request line, so it is refused with 403 rather than written into a target.

A rule the host could not put a credential in — credential left nil, or holding something other than the shape its scheme needs — has no header to build. Every request it matches is refused with 502, carrying error: :credential_missing on the request event, rather than being sent without the header: the broker failed to obtain a credential, which is not the agent doing anything wrong, and 502 is what tells it to retry once the credential is provisioned. 403 would say it is not allowed, which is a different and misleading thing. Inside a tunnel the request is refused without ending the tunnel, unless the refused request left a body behind it in the stream. :substitute and an unfilled {{ KEY }} are the exceptions described above: a placeholder the origin can see is the clearer failure there.

A placeholder must be distinctive enough to be one: four characters or more, holding a letter or digit, and carrying a boundary — __ at either end, or a character outside [A-Za-z0-9_]. Substitution is a literal find-and-replace, so id would rewrite every id in a path and account_sid is a real field name that appears in URLs. Managoat.Broker.Injector.valid_placeholder?/1 is the check; call it when building a session, so a bad rule fails where it is written rather than on every request it would have matched.

Rules match against the target the client sent, and telemetry is derived from that same original, so a placeholder in a path is logged as the placeholder and one in a query is not logged at all.

The child spec

{Managoat.Broker, port: 14322, store: Module, ca_seed: <32 bytes>, allow_private_upstreams: false}. Every option but the last is required, and a missing one raises at start naming the option. Optional: upstream_ssl_options (merged over the :ssl options the proxy dials origins with; a test origin's cacerts), max_cached_leaves (default 1024; see The CA) and name (default Managoat.Broker, for several listeners in one VM). There is no configuration module reading an otp_app: the listener is started by the host with values the host computed at boot, and a library that is not started serves nothing.

Once up: Managoat.Broker.port/1, running?/1 and ca_pem/1.

The CA

A brokered sandbox trusts one root, and the proxy presents a leaf for each host the sandbox CONNECTs to, signed by that root. Every replica of the host must present leaves the sandbox trusts, whichever one the ingress hands a connection to, and a sandbox that survived a restart must still trust what a fresh replica signs. So the root is not generated and stored: it is derived from the 32-byte ca_seed with HKDF-SHA256, reduced into P-256's scalar field, and the certificate's subject, serial and validity are fixed, so every replica computes the same key, subject and serial from the same seed. Rotating the seed rotates the CA; nothing else does.

The root's self-signature bytes vary per derivation, because ECDSA signing is randomised, so two replicas' PEMs differ byte for byte. That is harmless: a client matches a trust anchor by subject and public key and never verifies a root's own signature. ca_test.exs proves a leaf signed after a re-derivation chains to the first root.

The seed is the host's to derive, and it must not be a key the host uses for anything else: derive it from a master key with a fixed info string, so the CA key is never the storage key. Leaves live thirty days, are cached per host in an ETS table the listener owns, and are re-signed after twenty-nine.

That cache is bounded, because its key is the host from a sandbox's own CONNECT line: under :passthrough an agent browses wherever it likes, and a wildcard DNS record aimed at one address makes every *.attacker.example a distinct name that resolves, connects and would otherwise be cached for the life of the listener. It holds at most max_cached_leaves of them — 1024 by default — and the least recently used goes first, so a host still being visited keeps its leaf and a busy listener does not become a re-signing treadmill. Going over the cap costs one ECDSA signature the next time a fallen-off host is seen; it refuses nothing, which is why it is an option with a default rather than a decision the host has to make.

A host is validated before any of that. The name off the request line reaches :inet.getaddrs, the TLS server_name_indication, that cache's key and the subject and SAN of a certificate this proxy signs, so a host longer than 253 bytes, or holding a control character, whitespace or any of @ / \ ? # %, or beginning or ending with a dot, is 400 before it is resolved, dialed, cached or signed. It is not sanitised: forwarding a name the client did not ask for is worse than refusing the one it did. This is Agent Vault's brokercore.IsValidHost less its DNS-name blocklist (localhost, metadata.google.internal and two more), which is not worth porting — the SSRF guard above works on the addresses a name resolves to, so it catches every name that reaches a blocked range rather than the four anyone thought to write down.

Telemetry

Every request the proxy decides about emits [:managoat, :broker, :request] with the measurements %{count: 1, duration: <native units>} and the metadata method, host, path, outcome (:injected, :passthrough or :denied), rule (the matched rule's name, or nil), status, error and meta (the session's, unchanged). Never a header, never a body. The host attaches a handler and writes its log line with whatever meta carries; the library logs only refusals, which have no session to attribute.

The event is terminal: one per request, emitted when the request is over rather than when it starts.

A consequence worth planning for: a long-lived request is not recorded until it ends, so a streaming reply appears in a host's audit log when the stream finishes. That matches Agent Vault's total-duration semantics and avoids a second event and a row-update protocol. If immediate visibility for long-lived requests is ever needed, that is correlated start/stop events, not more meaning packed into this one.

Framing never touches the relay. Every byte from the origin is written to the sandbox the instant it arrives, and only then shown to the framer, so a streaming reply streams exactly as it did before responses were parsed and a framing failure costs telemetry rather than the response.

path is the URL path and nothing else. A query string never appears in it, on either request path, because a query can already hold a credential this proxy never brokered — a signed URL is one in itself, and ?key= is a shape clients use. The origin receives the request target unchanged; only the event is narrowed. This is Agent Vault's contract too: its request log recorded r.URL.Path. A CONNECT names an authority rather than a path, and is reported as it was sent.

Deviations from Agent Vault

This proxy replaced Infisical's Agent Vault behind the same interface. The parity suite (test/managoat/broker/agent_vault_parity_test.exs) replays the upstream tests it stands in for and lists what was not ported.

Each of these is a decision rather than a backlog item. Agent Vault is deleted from the cluster and from Fountain's codebase, so the A/B that settled the last round — the same request against both proxies, compared on the wire — no longer exists. A row reopened here has to be argued from the upstream tests in the parity suite, from Agent Vault v0.39.1's source, or from the protocol; it cannot be measured.

Deliberate, and expected to stay that way

Deliberate for now, with a condition attached

Two operational traps a host must handle

Both were found in production with the previous broker and are about the sandbox and the ingress, not the proxy, so this library cannot fix them.

Licence

Apache-2.0. See LICENSE.