Skip to content

Resolve token exchange principals from subject token claims - #1691

Merged
yili-db merged 10 commits into
unitycatalog:mainfrom
s-maling-celonis:feat/subject-token-principal
Sep 4, 2026
Merged

yili-db merged 10 commits into
unitycatalog:mainfrom
s-maling-celonis:feat/subject-token-principal

Conversation

@s-maling-celonis

@s-maling-celonis s-maling-celonis commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR contains subject-token principal resolution for POST /auth/tokens.

UC resolves the principal from the subject token — no OAuth client credentials on the UC request:

  1. id_token (CLI/browser): email claim or sub → getUserByEmail, then externalId fallback via azp or client_id
  2. access_token (M2M): if no email claim, externalId via azp/client_id first, then sub; otherwise same as id_token

The issued UC access token always uses the resolved user's email as sub (for grants/authorization).

Additional behavior:

  • Audience bypass: signed subject tokens whose azp or client_id matches a registered UC user externalId are accepted even when the client UUID is not in server.audiences
  • Unique index on uc_users.external_id (multiple NULL values still allowed)
  • Prefer the externalId flow over server.audiences=* for per-tenant service principals

Bootstrap UC users with email for grants and externalId = OAuth client id (same value as the token's azp or client_id claim).

Test plan

  • build/sbt "server/testOnly io.unitycatalog.server.service.TokenExchangePrincipalResolverTest io.unitycatalog.server.service.AuthServiceClientPrincipalTest io.unitycatalog.server.persist.UserRepositoryTest io.unitycatalog.server.service.AuthServiceTest io.unitycatalog.server.service.AuthServiceAllowlistTest" (24/24 passed locally)
  • M2M/access_token flow resolves service principal via externalId (AuthServiceClientPrincipalTest)
  • Issued UC token sub claim is the resolved user email (AuthServiceClientPrincipalTest)
  • extractOAuthClientId reads azp then client_id (TokenExchangePrincipalResolverTest)
  • Browser/id_token flow resolves user by email (manual)
  • Full CI (Unit Tests workflow on fork PR)

Comment thread docs/server/auth.md Resolved
@s-maling-celonis
s-maling-celonis force-pushed the feat/subject-token-principal branch from eb8f9a1 to 149de6c Compare August 7, 2026 08:23

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This becomes dead code?

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.

Yes — after switching issuance to the resolved principal email, that overload was unused. Removed in 909af1d.

}

if (audiences.isEmpty()) {
if (tokenClientId.isPresent()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This means that for a matching azp/client_id tokens, audience allowlist is bypassed? May I ask why? It seems to defeat the purpose of audience allowlist.
If audience is truly unpredictable, the wildcard * audience can already disable 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 — that bypass was intentional for dynamic client UUIDs, but you're right that it weakens the audience allowlist.

Fixed in 909af1d: audience validation now uses server.audiences only (aud match or *). azp / client_id are used only for principal resolution via externalId, not to skip the allowlist. Docs and tests updated accordingly (including a case that rejects a mismatched aud even when externalId matches).

ErrorCode.UNAUTHENTICATED, "Token verification failed: " + e.getMessage(), e);
}

if (!serverProperties.isAudienceValidationDisabled()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The issuer validation remains in this function but audience validation moves to resolvePrincipalEmail?
Earlier in this PR you had audience validation conditional on if it's picking up email or id. Now that is no longer true. I think audience validation should be restored here to remain together with the other verification.

Then the resolvePrincipalEmail just do one thing: extract the email from a JWT token that is already validated and trusted.

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 — audience validation is back in grantToken with issuer and signature checks. resolvePrincipalEmail only extracts the UC email from the already-validated token (bb8ccd59).

@s-maling-celonis
s-maling-celonis force-pushed the feat/subject-token-principal branch from bb8ccd5 to bd5287b Compare August 25, 2026 18:09

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

couple of issues found.

}
}

return Optional.empty();

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.

[AI-assisted]

we should never eat this without logging. if the lookup fails, this turns a db/server issue into User not allowed, doesnt it? same below for email lookup.

future operational failures become indistinguishable from auth denial, and debugging token exchange gets much harder.

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 — swallowing the lookup turned DB/server failures into User not allowed.

dc54e569 only treats NOT_FOUND as a miss. Any other BaseException or runtime error is logged and rethrown (email and externalId lookups share that path).

* id_token} subjects, resolution order is email (or {@code sub}) then OAuth client id from {@code
* azp} or {@code client_id} mapped to {@code externalId}. For {@code access_token} subjects
* without an {@code email} claim, {@code externalId} is tried before {@code sub}.
*/

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.

[AI-assisted]

blocker: why can an id_token fall back to azp/client_id? for browser tokens azp is the app, not the human, so this can issue a service principal token for the wrong user, right?

future cli/browser tokens that dont resolve by email could inherit grants from whichever UC user has externalId equal to the oauth client id.

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.

Yes — for an id_token, azp/client_id is the app, not the human.

dc54e569 drops that fallback: id_token resolves only from email/sub. azp/client_id → externalId remains for M2M access_token subjects. Covered by idTokenDoesNotResolveViaOauthClientId and testIdTokenExchangeDoesNotFallBackToExternalId.

s-maling-celonis and others added 9 commits September 3, 2026 09:56
Drop OAuth client credentials on POST /auth/tokens and map service principals
via azp or non-URL aud to UC user externalId after email resolution fails.

Co-authored-by: Cursor <[email protected]>
Signed-off-by: s-maling-celonis <[email protected]>
Add a unique index on uc_users.external_id, prefer externalId over sub for
access_token subjects without an email claim, and assert issued UC token sub
in integration tests.

Co-authored-by: Cursor <[email protected]>
Signed-off-by: s-maling-celonis <[email protected]>
Signed-off-by: s-maling-celonis <[email protected]>
Co-authored-by: Cursor <[email protected]>
Stop falling back to aud when mapping subject tokens to UC externalId;
aud remains for audience validation only. Update docs and tests.

Signed-off-by: s-maling-celonis <[email protected]>
Co-authored-by: Cursor <[email protected]>
Rename TokenExchangePrincipalResolver to TokenExchangeSubjectTokenHandler,
deduplicate externalId user lookup, extract resolution order into a private
strategy, and align AuthService javadoc with the handler behavior.

Signed-off-by: s-maling-celonis <[email protected]>
Co-authored-by: Cursor <[email protected]>
Keep aud validation independent of principal resolution so registered
externalIds cannot skip server.audiences; remove unused DecodedJWT token helper.

Co-authored-by: Cursor <[email protected]>

Signed-off-by: s-maling-celonis <[email protected]>
…ecks.

resolvePrincipalEmail now only maps an already-trusted JWT to a UC principal.

Co-authored-by: Cursor <[email protected]>

Signed-off-by: s-maling-celonis <[email protected]>
id_token exchange now resolves only from email/sub so a browser or CLI
token cannot inherit a service principal via azp. User lookups log and
rethrow non-NOT_FOUND failures instead of mapping them to User not allowed.

Signed-off-by: s-maling-celonis <[email protected]>
Co-authored-by: Cursor <[email protected]>
@s-maling-celonis
s-maling-celonis force-pushed the feat/subject-token-principal branch from dc54e56 to 39c317f Compare September 3, 2026 07:58
Signed-off-by: s-maling-celonis <[email protected]>
Co-authored-by: Cursor <[email protected]>
@yili-db
yili-db merged commit 531857a into unitycatalog:main Sep 4, 2026
16 checks passed
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.

3 participants