Skip to content

test(identitybroker): test session open against the real AS-1 - #2743

Merged
ChrisJBurns merged 3 commits into
mainfrom
test/identitybroker-real-as1-session-open
Nov 6, 2026
Merged

ChrisJBurns merged 3 commits into
mainfrom
test/identitybroker-real-as1-session-open

Conversation

@ChrisJBurns

@ChrisJBurns ChrisJBurns commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2677

Summary

The identity broker's session-open tests now run against the real AS-1 (ToolHive's embedded authorization server, in process) instead of the offline harness's AS-1, so refusal reasons and behaviour come from the server production uses.

  • New test-support package internal/identitybroker/adapter/as1server/as1servertest: starts a real AS-1 with the harness IdP upstream and a broker delegate client, signs users in, signs expired tokens with a test-owned key, and a FaultTransport in front of AS-1 that injects 429/5xx, dropped requests, lost responses, altered or missing echoed grants, and any OAuth error/reason body, and records requests and client assertions.
  • internal/identitybroker/sessionopen_test.go, authserverclient and internal/app session-open tests move to it. Broker clock is wall time plus a test offset; assertion clock is wall time. Hard expiry: advance the offset, observe the close without reaching AS-1, reset, reopen.
  • New TestIdentityBrokerAgainstRealAS1 (mecak8s): session close and hard-expiry reopen through real startup wiring against a real AS-1.
  • The another-audience case runs in as1server's in-package tests (TestSessionOpenWithATokenForAnotherAudience), so no audience option is exported.
  • Harness: one-shot IdP.SignInNextAs, RegisterAS1Upstream, ResignAccessToken; its AS-1 fault injection, emulated subject-token refusal tests, and session-open introspection (ExchangeRefusals, ReceivedClientAssertions, ExpireGrant) are deleted. Grant and GrantsIssued stay for the tests still on the harness.
  • internal/app: the test-only IdentityBrokerAS1.Now seam is split into Now (broker clock) and AssertionNow (client assertions), so the real-AS-1 test keeps assertions on wall time. Production leaves both nil.
  • Rebased on feat(identitybroker): verify an agent credential for a grant's owner #2733: its VerifyCredentialForOwner row is kept in TestSessionHardExpiryClosesTheSession.
  • .golangci.yml: a narrow depguard rule for as1servertest, and a deny so production internal/app code cannot import it.

Still on the harness's AS-1, and what retires it

Test Why Retired by
TestIdentityBrokerAgainstOfflineHarness (cmd/mecak8s) #2536/#2537 extend it with gateway calls that need harness ID-JAGs stacklok/toolhive#6898, stacklok/toolhive#6899
Harness session-open conversion (grant fixture for slice 4 ID-JAG tests) Harness ID-JAGs can only redeem harness-issued refresh tokens stacklok/toolhive#6898, stacklok/toolhive#6899
TestRevocationAuthenticates, TestFormMustNotCarryClientCredentials (authserverclient) Real AS-1 serves no revocation yet stacklok/toolhive#6966, stacklok/toolhive#6972 (#2568)

Dropped: assertion replay inside the clock skew (needs an AS-1 clock no ToolHive issue tracks); plain replay moved to the real AS-1.

Real AS-1 behaviour that differs from the old harness

  • Accepts its token endpoint as a client-assertion audience (RFC 7523).
  • Answers a missing client_id with 400 invalid_request, not 401 invalid_client.
  • A token for another audience has no sub_id, so the broker refuses it at check 1 before AS-1 is called; the subject_token_audience mapping is proven with a re-signed token.

Test plan

  • task lint: root and engine modules 0 issues (the existing gosec G703 in environment/microvm/launch_ownership_darwin.go is unrelated).
  • go test -race on identityharness, internal/identitybroker/..., cmd/mecak8s, internal/adapter/clientauth, and the internal/app identity broker tests.
  • Root go test -race ./...: remaining failures match origin/main in this environment (shell/git setup), none in touched packages.
  • go run ./cmd/mecademo: tool call, permission ask/approval, result.

API compatibility

None: tests, test support and lint configuration only; no remote-observable change.

@ChrisJBurns
ChrisJBurns requested a review from jhrozek November 6, 2026 17:11

@ChrisJBurns ChrisJBurns left a comment

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.

  • medium · correctness — Unconsumed queued faults are silently ignored and can leak into later subtests (internal/identitybroker/adapter/as1server/as1servertest/faults.go:1280)

    FailNextExchange, DropNextExchange, LoseNextExchangeResponse and ReplaceNextExchangeEcho push onto a queue that nothing checks at the end of a test. If the code under test stops sending the exchange (for example, the broker refuses earlier), the fault is never applied and the test may still pass for the wrong reason. Several tests share one AS1 across subtests, for example TestFailNextExchangeAnswersWithoutAS1 and TestSignInSelectsUserScopeAndClient. In those, a leftover fault from a failed subtest is applied to the next subtest's exchange and causes misleading cascading failures.

    Recommendation: In Start, register a t.Cleanup that fails the test (t.Errorf) when len(f.queue) != 0, naming the pending fault kinds. Optionally add a Pending() int accessor so shared-fixture subtests can check that the queue is empty.

  • low · security — SignIn does not check that the selected user actually signed in, and the one-shot selector is a shared FIFO not bound to a request (internal/identitybroker/adapter/as1server/as1servertest/as1servertest.go:745)

    as1servertest.SignIn queues IdP.SignInNextAs(req.User) and then returns the access token without checking the token's sub_id. IdP.signInUser (internal/testutil/identityharness/idp.go) uses the first queued name for whichever authorization reaches the IdP next. A selection that is never used stays queued. That happens when authorization fails before reaching the IdP, or when a subtest on a shared fixture fails. Two concurrent sign-ins on one harness can also swap users. In each case a later sign-in silently gets the wrong identity. The ownership tests (owner mismatch, `ReasonSessionOwne

    Recommendation: In SignIn, parse the returned access token's sub_id.sub (as signedInUser already does in as1servertest_test.go) and t.Fatalf if it differs from req.User when one was given. Also document that SignInNextAs is unsafe for concurrent sign-ins on one IdP, or clear the queue when authorization fails.

  • low · security — Client-assertion expiry no longer checked at the skew boundary (internal/identitybroker/adapter/authserverclient/client_test.go:1827)

    The "expired" case used to send an assertion one second past exp plus the allowed clock skew. It now sends one dated 10 minutes in the past (exp is 9 minutes old), and "replay inside the skew" is gone. An AS-1 that accepted assertions expired by up to several minutes would still pass these tests. The issue accepts dropping the in-skew replay case, but the expired case could have kept a tighter boundary without needing a server clock.

    Recommendation: Date the expired assertion just past the real AS-1's documented leeway, e.g. time.Now().Add(-MaxClientAssertionLifetime - <AS-1 leeway> - few seconds), so the test still catches a too-lenient expiry check. If ToolHive's leeway is unknown, add a comment saying the boundary is deliberately not checked.

@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.

One thing for the rebase onto #2733: main's TestSessionHardExpiryClosesTheSession has a VerifyCredentialForOwner row in the old two-field shape (internal/identitybroker/sessionopen_test.go:514 on main). This PR changes the rows to {name, converts, lookup}, so that row will conflict. Please carry it over with converts: false. If it is dropped while fixing the conflict, nothing fails, and #2733's path silently loses its expiry test.

ChrisJBurns and others added 3 commits November 6, 2026 18:12
Move the identity broker's session-open tests from the offline
harness's AS-1 to ToolHive's AS-1 in process, behind a fault-injecting
transport. The harness keeps only its conversion, as the grant fixture
for ID-JAG tests, and revocation, each naming the ToolHive issue that
retires it.

Closes #2677

Co-authored-by: Mecatl <[email protected]>
- Record a request whose AS-1 response body fails to read.
- Close the fixture listener when setup fails before the server starts.
- Name the dropped-request case for what it injects, and assert the
  remembered-token refusal never reaches AS-1.
- Say that the first-call hard-expiry case uses a made-up echo.
- Do not print the error when it discloses an access token.

Co-authored-by: Mecatl <[email protected]>
- Fail a test that leaves injected faults queued.
- Check that a sign-in gets the user it selected, and clear an unused
  selection when authorization fails.
- Test an expired client assertion just past its expiry: AS-1 allows no
  leeway.
- Split the test-only IdentityBrokerAS1 clock into the broker clock and
  the client-assertion clock, so the real-AS-1 test signs on wall time.
- Bound the shared AS-1 client to ten seconds per request.
- Keep #2733's VerifyCredentialForOwner row in the hard-expiry table.

Co-authored-by: Mecatl <[email protected]>
@ChrisJBurns
ChrisJBurns force-pushed the test/identitybroker-real-as1-session-open branch from d9f088b to 688aa0f Compare November 6, 2026 18:19
@ChrisJBurns
ChrisJBurns merged commit b5161c3 into main Nov 6, 2026
50 checks passed
@ChrisJBurns
ChrisJBurns deleted the test/identitybroker-real-as1-session-open branch November 6, 2026 18:38
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Nov 18, 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.

Test session open against the real AS-1, not the harness's

2 participants