Skip to content

feat(identitybroker): prove reopen after hard expiry; reject a token whose session closed mid-conversion - #2648

Merged
ChrisJBurns merged 2 commits into
mainfrom
feat/identitybroker-reopen-after-expiry
Nov 2, 2026
Merged

ChrisJBurns merged 2 commits into
mainfrom
feat/identitybroker-reopen-after-expiry

Conversation

@ChrisJBurns

Copy link
Copy Markdown
Contributor

Closes #2493.

Summary

The reopen after a broker session ends already exists (#2593, #2601): the broker closes a session whose refresh token has expired when it is looked up, and the next open pairs a new one. This PR proves it end to end and fixes the one path that reported the wrong outcome.

  • Close during conversion → bearer_rejected. When CloseSession lands while an open converts the request's AS-1 token, the request now fails as for a refused token (401 bearer_rejected, so the client refreshes and retries) instead of 500 internal, as credentials-and-lifecycle.md step (3) requires. Both server OpenSession call sites are covered: openIdentityBrokerSessionOnce (run entry) and preOpenIdentityBrokerSession (create, fork, clear). The registration-time variant shares the same error. A tokenless session_closed keeps its current mapping.
  • Test clock. identitybroker.WithClock and app.IdentityBrokerAS1.Now (test-only, like ClientKey) let a test move the broker, its AS-1 client assertions, and the harness AS-1 past a session's hard expiry.
  • TestIdentityBrokerAgainstOfflineHarness gains a hard-expiry → reopen step: after expiry the old broker session refuses registration and authorization with session_closed; a prompt with a fresh token reopens the session under a new ID holding a refresh token, with the main agent's same SPIFFE ID; a tokenless open pairs a new session holding no refresh token.

Not reopening after signed_out, upstream_revoked, or owner_action_required moved to #2568; scheduled-run access is #2569.

Testing

  • New: TestIdentityBrokerCloseDuringConversionRejectsTheToken (run path) and a "session_closed during exchange" case in TestIdentityBrokerOpenRefusalMapping (create path), both checking state unchanged, no model call, and no broker session left open.
  • Mutation checks: disabling the fix fails both new server tests; disabling the clock wiring fails the harness step.
  • go test -race for internal/identitybroker/..., internal/app, internal/adapter/server -run IdentityBroker, and cmd/mecak8s (harness test -count=3) pass.
  • task lint: clean except the pre-existing darwin-only gosec finding in environment/microvm/launch_ownership_darwin.go:754.
  • Full race suite run locally on macOS: changed packages pass; the remaining failures (path canonicalization /var vs /private/var, BSD stat, git worktree probes, env: bash) reproduce on a clean main checkout and are environmental.

API compatibility

Assessment: non-breaking, no major or feature change. Rationale: one rare race (the broker session closing while the open converts the request's token) changes from 500 internal to the existing 401 bearer_rejected, which conforming clients already handle by refreshing and retrying, and which the registration-time variant of the same race already returns. No new code, field, or feature identifier. Behind --enable-identity-broker. Upgrade implications: none.

…whose session closed mid-conversion

When the broker session closes while an open converts the request's AS-1
token, fail the request as for a refused token (bearer_rejected, 401)
instead of 500, at both server OpenSession call sites.

Add a test clock (identitybroker.WithClock, app.IdentityBrokerAS1.Now)
and extend TestIdentityBrokerAgainstOfflineHarness with hard expiry:
the old broker session refuses with session_closed, a fresh token
reopens it under a new ID with the same agent, and a tokenless open
pairs a session holding no refresh token.

Closes #2493

Co-authored-by: Mecatl <[email protected]>
@ChrisJBurns
ChrisJBurns requested a review from jhrozek November 2, 2026 19:18

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

Looks good. The close-during-conversion case now returns bearer_rejected on both open paths, and the harness proves the reopen after hard expiry end to end. One optional suggestion inline.

// session holds is refused before saving (preOpenIdentityBrokerSession).
opened, err := broker.OpenSession(ctx, identitybroker.OpenSessionRequest{HostSessionID: host, Incarnation: incarnation, Owner: owner, AccessToken: token})
if err != nil {
closed := errors.Is(err, identitybroker.ReasonError(identitybroker.ReasonSessionClosed, ""))

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.

Suggestion, not blocking: identityBrokerFailure already maps an open's broker reason to the client error. The session_closed → errBrokerSessionClosedAfterToken case could go there as one more call == "open" case:

case reason == identitybroker.ReasonSessionClosed:
    return errBrokerSessionClosedAfterToken

Then the closed := … capture and the override after mapping can go away here and in preOpenIdentityBrokerSession (line 264). The existing token.IsZero() && errors.Is(err, ErrBearerRejected) guard below would still turn a tokenless open into ErrInternal. Behaviour stays the same, and the next path that opens a session won't need to remember to repeat it.

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.

Reopen the broker session after access ends

2 participants