Repository navigation
feat(identitybroker): start the identity broker in mecak8s behind an authentication boundary - #2450
Conversation
…authentication boundary Add the broker side of the identity broker contract. identitybroker.Core takes the caller as an explicit argument on every call, so nothing reaches the core without one, and NewInProcess joins Core to the Service Mecatl calls through an Authenticator, so the in-process path takes the same authentication step as a remote one. Caller carries Verified, false unless an authenticator verifies a credential. UnverifiedAuthenticator stands in until workload credentials are verified. WithCaller and CallerFrom are removed: no request or context names its own caller. The stand-in now implements Core: it refuses an invalid caller with UNAUTHENTICATED and binds each broker session to the workload that opened it, refusing another workload with PERMISSION_DENIED before revealing a closing reason. The contract tests run through the boundary and, for caller cases, against the core directly. mecak8s gains an off-by-default --enable-identity-broker flag. app.Build constructs the stand-in behind the boundary with the unverified caller "mecak8s-unverified" and gives only the boundary to the session code and to the MCP broker on both construction paths, as its own option so the bundled ToolHive path stays selected. The broker is released after the MCP broker on startup failure and on shutdown. mecated never sets the flag. Regenerate the server CLI reference, which also picks up --permission-mode help text already changed on main. Co-authored-by: Mecatl <[email protected]>
…andoffs
Rename identitybroker.Caller to Workload{Name, Verified}: mcpbroker.Caller
already names something else in the same composition code, and the value
identifies the calling workload, not a user.
Stop passing the identity broker to the session code and the MCP broker.
Nothing reads either yet, and each only had an accessor so tests could
see it. The callers that first use the broker will add the handoff with
a real test. app.Built.IdentityBroker exposes the constructed broker
instead, and it is still released after the MCP broker.
Co-authored-by: Mecatl <[email protected]>
jhrozek
left a comment
There was a problem hiding this comment.
Review from a pass over spec, standards, tests, security and architecture. Five points below. The first decides whether this PR closes its issue. The other four are smaller. Everything else I found was minor, so I left it out.
| MCPBrokerCallbackPath string | ||
| // IdentityBroker is the agent identity broker, as Mecatl calls it; nil when | ||
| // it is not enabled. Close releases it. | ||
| IdentityBroker identitybroker.Service |
There was a problem hiding this comment.
The issue "Start the identity broker in mecak8s, behind a flag" (#2447) asks app.Build to hand only the boundary to the server's session code (a new server.Config field) and to the MCP broker on both construction paths (mcpbroker.NewToolHiveProcess and mcpbroker.New), in its own field and not through MCPBrokerOptions. Its "Done when" says mecak8s starts connected to both.
This PR builds the boundary and returns it here, but nothing reads this field, and the PR body leaves the handoffs to "the changes that first call it". The issue says the reverse: "later work does not change startup code". The composition test "flag on: the boundary reaches the session code and both MCP broker construction paths" doesn't exist either.
Either add the handoffs and that test (cheap, since only the Service is passed), or change "Closes #2447" to a partial reference and re-scope the issue, so the follow-up knows it owns the startup edits.
There was a problem hiding this comment.
Agreed that the PR and #2447 disagreed. We kept the trim on purpose: the handoffs had no reader, and each needed an accessor only so a test could see it. So I've rescoped #2447 instead of putting them back. The session-code field now belongs to the session-hooks work, and the MCP broker option, on both construction paths and kept out of MCPBrokerOptions, to the gateway-token work. Each arrives with a real caller and a real test. The broker is exposed as app.Built.IdentityBroker so tests can see it was constructed, and "Closes #2447" now matches the issue.
| // and on shutdown. | ||
| identityBroker, releaseIdentityBroker := buildIdentityBroker(cfg) | ||
| defer func() { | ||
| if !profilesTransferred { |
There was a problem hiding this comment.
No test covers this failure path. If Build fails after the broker is built, this defer is the only thing that releases it. The shutdown mutation check in the PR body doesn't touch it, so deleting the defer, or inverting !profilesTransferred, fails nothing.
A test with the flag on, a closable core, and a later startup failure that asserts the core was closed once would cover it.
There was a problem hiding this comment.
Good catch. Added TestIdentityBrokerIsClosedWhenLaterStartupFails (9dcf743): flag on, a closable core, and a protected MCP route without a callback URL, which fails bundled MCP broker construction after the identity broker is built. It asserts the core was closed exactly once. Disabling the defer's release now fails it.
| } | ||
| } | ||
|
|
||
| func TestIdentityBrokerCallsCarryTheUnverifiedWorkload(t *testing.T) { |
There was a problem hiding this comment.
Both wiring tests set identityBrokerCore, so the production path never runs here: standin.New() and UnverifiedAuthenticator{Name: identityBrokerWorkload} are never built through Build.
One flag-on test with no override, making a real OpenSession or FindSession call through built.IdentityBroker, would show the default wiring works. It could also check that the session is bound to mecak8s-unverified.
There was a problem hiding this comment.
Added TestIdentityBrokerDefaultWiringOpensSessions (9dcf743). With no core override, it builds the production stand-in and UnverifiedAuthenticator, then opens and finds a session through built.IdentityBroker. The binding to mecak8s-unverified isn't visible through the boundary, so it's covered where the core is reachable: the override test checks the core receives that unverified workload.
| s := b.sessions[id].Session | ||
| s := b.sessions[id] | ||
| switch { | ||
| case s.workload != workload.Name: |
There was a problem hiding this comment.
Binding compares workload.Name only and ignores Verified. That's harmless now, because the only authenticator never sets Verified. Once a verifying authenticator exists, a verified and an unverified caller with the same name would share sessions: each could use a session the other opened.
Cheap to fix now: store Verified in the binding and compare both. It would also help if the Core doc comment said what Verified=false means to implementations. Today it says only that records can tell the difference, and the stand-in records nothing.
There was a problem hiding this comment.
Agreed. The stand-in now stores and compares the whole Workload, Verified included (9dcf743), and the contract has a "same name, now verified" case. The Workload.Verified and Core docs now say what false means: the name is a claim nobody proved, implementations never treat it as the verified workload of the same name, and records never present it as verified.
| return identitybroker.NewError(identitybroker.CodePermissionDenied, "", "unknown broker session") | ||
| } | ||
|
|
||
| func otherWorkload() error { |
There was a problem hiding this comment.
This message differs from unknownSession() ("unknown broker session"), so a caller can tell a session ID that exists under another workload from one that doesn't. The same applies to the host session ID in OpenSession and FindSession, which is less random than the broker ID.
The impact is small today (one workload, unguessable broker IDs). Returning the same text for both is a one-line change, and it's the safer default for a multi-workload broker.
There was a problem hiding this comment.
Done (9dcf743). Another workload's session now gives exactly the same error as an unknown session ID for close, register, and authorize, and FindSession reports none open instead of PERMISSION_DENIED. A contract case compares the error text. OpenSession still refuses a host session another workload holds, so that one case necessarily reveals the session exists; the Core doc says so.
…er workloads' sessions Address review of #2450: - Bind a broker session to the whole Workload, Verified included, so a verified and an unverified workload with the same name never share a session. Document what Verified=false means to implementations. - Make another workload's session indistinguishable from none: closing, registering into, or calling through it fails exactly as for an unknown session ID, and FindSession reports none open. - Test that the identity broker is released when startup fails after it is built, and that the default wiring, with no test override, opens and finds a session. Co-authored-by: Mecatl <[email protected]>
Summary
Starts the agent identity broker in mecak8s behind an off-by-default flag, and gives the broker the place where it decides which workload is calling.
Authentication boundary (
internal/identitybroker)Coreis the broker side of the contract. Every call takes the callingWorkloadas an argument, so nothing reaches the core without one.NewInProcess(authn, core)returns theServiceMecatl calls. On each call it asks anAuthenticatorwhich workload is calling, then calls the core. In-process calls therefore take the same authentication step as remote ones, as the modulith constraints require.Workload{Name, Verified}.Verifiedisfalseunless an authenticator verifies a credential, so records cannot mistake a placeholder for a real identity.UnverifiedAuthenticatorstands in until workload credentials are verified.WithCallerandCallerFromare removed. Mecatl never names its own identity;Serviceis otherwise unchanged.Core. It refuses an unnamed workload withUNAUTHENTICATED, and binds each broker session to the whole workload that opened it,Verifiedincluded (design check 2 of steps (4) and (6)). Another workload cannot tell such a session from none: closing, registering into, or calling through it fails exactly as for an unknown ID (PERMISSION_DENIED, before any closing reason), andFindSessionreports none open.Startup (
cmd/mecak8s,internal/app)--enable-identity-broker(experimental, off by default) setsapp.Config.IdentityBroker. mecated never sets it.app.Buildbuilds the stand-in behind the boundary with the unverified workloadmecak8s-unverified, and exposes it asapp.Built.IdentityBroker.io.Closer.Nothing calls the broker yet. Handing it to the session code and to the MCP broker is left to the changes that first call it, so each handoff arrives with a real user and a real test. With the flag off, nothing is constructed.
Issue relationship
Closes #2447
Type of change
Test plan
Baseline checks
golangci-lintclean on the changed packages and the root module;go vetclean. (task lintstops at a pre-existing darwin-only gosec finding inenvironment/microvm, untouched here.)go test -racepasses forinternal/identitybroker/...,internal/app(identity broker tests), andcmd/mecak8s. Ininternal/appandinternal/adapter/server, 8 tests fail on this macOS machine; all 8 fail identically on unmodifiedmain(9c7c01346).go run ./cmd/mecademo): tool call, permission ask and approval, and result shown.scripts/server-cli-reference.py --checkreports the reference current. (task docsneedsbuf, not installed locally.)task site:build)task api:check)task api:update+engine/CHANGELOG.mdNew tests:
UNAUTHENTICATEDwithout exposing its text; the stub never verifies.conformance/workloads.go): calls the core directly. A session refuses another workload, including the same name with a differentVerified, on every call, before a closed session's reason. Another workload's session gives the same error as an unknown ID, andFindSessionreports none. Workloads open their own sessions. An unnamed or malformed workload is refused before the request is checked.internal/app/identitybroker_test.go):--enable-identity-brokermaps toapp.Config.IdentityBroker.Mutation checks: each of these fails a test: removing the stand-in's workload check; comparing only the workload name; giving another workload's session its own error; skipping the release on shutdown; skipping it on failed startup.
Changes
internal/identitybroker/boundary.goWorkload,Core,Authenticator,UnverifiedAuthenticator,NewInProcessinternal/identitybroker/service.go,broker.goWithCaller/CallerFrom; docs point to the boundaryinternal/identitybroker/standin/Core: workload check and session bindinginternal/identitybroker/conformance/workloads.gocalls the core directlyinternal/app/identitybroker.go,build.goBuilt.IdentityBroker, releasecmd/mecak8s/flags.go--enable-identity-brokeruser-docs/reference/server-cli.md--permission-modehelp text already changed onmainUser-facing change
A new experimental, off-by-default mecak8s flag,
--enable-identity-broker. It changes no behavior a user can observe yet.Special notes for reviewers
WithCaller/CallerFromare gone, and implementations now implementCorerather thanService. Nothing outsideinternal/identitybrokerused them.ServiceandCorecannot share an implementation (theirAuthorizeCallsignatures conflict), so Mecatl cannot hold the core instead of the boundary.Workload, notCaller, becausemcpbroker.Calleralready means something else in the same composition code.API compatibility: no remote-observable change. The packages are internal, nothing calls the broker, and no protobuf, gRPC, HTTP, or engine API surface changes. No major or feature-identifier change.