Skip to content

feat(identitybroker): start the identity broker in mecak8s behind an authentication boundary - #2450

Merged
ChrisJBurns merged 3 commits into
mainfrom
feat/identitybroker-mecak8s-startup
Oct 21, 2026
Merged

ChrisJBurns merged 3 commits into
mainfrom
feat/identitybroker-mecak8s-startup

Conversation

@ChrisJBurns

@ChrisJBurns ChrisJBurns commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

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)

  • Core is the broker side of the contract. Every call takes the calling Workload as an argument, so nothing reaches the core without one.
  • NewInProcess(authn, core) returns the Service Mecatl calls. On each call it asks an Authenticator which 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}. Verified is false unless an authenticator verifies a credential, so records cannot mistake a placeholder for a real identity.
  • UnverifiedAuthenticator stands in until workload credentials are verified.
  • WithCaller and CallerFrom are removed. Mecatl never names its own identity; Service is otherwise unchanged.
  • The stand-in now implements Core. It refuses an unnamed workload with UNAUTHENTICATED, and binds each broker session to the whole workload that opened it, Verified included (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), and FindSession reports none open.

Startup (cmd/mecak8s, internal/app)

  • --enable-identity-broker (experimental, off by default) sets app.Config.IdentityBroker. mecated never sets it.
  • app.Build builds the stand-in behind the boundary with the unverified workload mecak8s-unverified, and exposes it as app.Built.IdentityBroker.
  • It is released after the MCP broker, on startup failure and on shutdown, when the core implements 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

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

Baseline checks

  • Linting: golangci-lint clean on the changed packages and the root module; go vet clean. (task lint stops at a pre-existing darwin-only gosec finding in environment/microvm, untouched here.)
  • Fast offline test suite: covered below.
  • Pre-review race suite: go test -race passes for internal/identitybroker/..., internal/app (identity broker tests), and cmd/mecak8s. In internal/app and internal/adapter/server, 8 tests fail on this macOS machine; all 8 fail identically on unmodified main (9c7c01346).
  • Offline demo (go run ./cmd/mecademo): tool call, permission ask and approval, and result shown.
  • Markdown changed: scripts/server-cli-reference.py --check reports the reference current. (task docs needs buf, not installed locally.)
  • User docs/user-facing behavior changed: site build (task site:build)
  • Guarded engine API affected: compatibility check (task api:check)
  • Intentional engine API change: task api:update + engine/CHANGELOG.md

New tests:

  • Boundary: every call reaches the core with the authenticator's workload; a failed authentication reaches no core call, and a non-broker error becomes UNAUTHENTICATED without exposing its text; the stub never verifies.
  • Contract (conformance/workloads.go): calls the core directly. A session refuses another workload, including the same name with a different Verified, on every call, before a closed session's reason. Another workload's session gives the same error as an unknown ID, and FindSession reports none. Workloads open their own sessions. An unnamed or malformed workload is refused before the request is checked.
  • Composition (internal/app/identitybroker_test.go):
    • flag off constructs nothing;
    • flag on with the default wiring (no test override) opens and finds a session;
    • a call reaches the core with the unverified workload;
    • the broker is closed once on shutdown, and once when a later startup step fails.
  • Flag: --enable-identity-broker maps to app.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

File Change
internal/identitybroker/boundary.go Workload, Core, Authenticator, UnverifiedAuthenticator, NewInProcess
internal/identitybroker/service.go, broker.go Remove WithCaller/CallerFrom; docs point to the boundary
internal/identitybroker/standin/ Implements Core: workload check and session binding
internal/identitybroker/conformance/ Cases run through the boundary; new workloads.go calls the core directly
internal/app/identitybroker.go, build.go Construction, Built.IdentityBroker, release
cmd/mecak8s/flags.go --enable-identity-broker
user-docs/reference/server-cli.md Regenerated. Also picks up --permission-mode help text already changed on main

User-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

  • Interface change to merged code. WithCaller/CallerFrom are gone, and implementations now implement Core rather than Service. Nothing outside internal/identitybroker used them.
  • Service and Core cannot share an implementation (their AuthorizeCall signatures conflict), so Mecatl cannot hold the core instead of the boundary.
  • Named Workload, not Caller, because mcpbroker.Caller already means something else in the same composition code.
  • Workload binding always passes in production today: there is one stub workload. The core-level contract tests exercise it with different workloads.
  • Not included: handing the broker to the session code and the MCP broker (added by the changes that first call it), a signing key (the stand-in signs nothing), a workload identity flag or Helm chart change (the stub supplies the workload), the record sink (nothing emits records yet), and a slot for mecak8s to present a credential (added with real workload authentication).

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.

…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 jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread internal/app/build.go
MCPBrokerCallbackPath string
// IdentityBroker is the agent identity broker, as Mecatl calls it; nil when
// it is not enabled. Close releases it.
IdentityBroker identitybroker.Service

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread internal/app/build.go
// and on shutdown.
identityBroker, releaseIdentityBroker := buildIdentityBroker(cfg)
defer func() {
if !profilesTransferred {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All five points from my review are addressed in 9dcf743, and the rescoped issue now matches the PR.

@ChrisJBurns
ChrisJBurns merged commit 477a6e3 into main Oct 21, 2026
46 of 50 checks passed
@ChrisJBurns
ChrisJBurns deleted the feat/identitybroker-mecak8s-startup branch October 21, 2026 19:39
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Oct 23, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Start the identity broker in mecak8s, behind a flag

2 participants