Repository navigation
feat(identitybroker): prove reopen after hard expiry; reject a token whose session closed mid-conversion - #2648
Conversation
…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]>
…e Service contract Co-authored-by: Mecatl <[email protected]>
jhrozek
left a comment
There was a problem hiding this comment.
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, "")) |
There was a problem hiding this comment.
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 errBrokerSessionClosedAfterTokenThen 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.
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.
bearer_rejected. WhenCloseSessionlands while an open converts the request's AS-1 token, the request now fails as for a refused token (401bearer_rejected, so the client refreshes and retries) instead of 500internal, ascredentials-and-lifecycle.mdstep (3) requires. Both serverOpenSessioncall sites are covered:openIdentityBrokerSessionOnce(run entry) andpreOpenIdentityBrokerSession(create, fork, clear). The registration-time variant shares the same error. A tokenlesssession_closedkeeps its current mapping.identitybroker.WithClockandapp.IdentityBrokerAS1.Now(test-only, likeClientKey) let a test move the broker, its AS-1 client assertions, and the harness AS-1 past a session's hard expiry.TestIdentityBrokerAgainstOfflineHarnessgains a hard-expiry → reopen step: after expiry the old broker session refuses registration and authorization withsession_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, orowner_action_requiredmoved to #2568; scheduled-run access is #2569.Testing
TestIdentityBrokerCloseDuringConversionRejectsTheToken(run path) and a "session_closed during exchange" case inTestIdentityBrokerOpenRefusalMapping(create path), both checking state unchanged, no model call, and no broker session left open.go test -raceforinternal/identitybroker/...,internal/app,internal/adapter/server -run IdentityBroker, andcmd/mecak8s(harness test-count=3) pass.task lint: clean except the pre-existing darwin-only gosec finding inenvironment/microvm/launch_ownership_darwin.go:754./varvs/private/var, BSDstat, git worktree probes,env: bash) reproduce on a cleanmaincheckout 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
internalto the existing 401bearer_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.