Add preferred_username support for Trino impersonation - #7773
Open
ottergottaott wants to merge 1 commit into
Open
Add preferred_username support for Trino impersonation#7773ottergottaott wants to merge 1 commit into
ottergottaott wants to merge 1 commit into
Conversation
ottergottaott
force-pushed
the
trino-preferred-username
branch
from
July 23, 2026 15:23
128ca57 to
a1780f0
Compare
Contributor
There was a problem hiding this comment.
1 issue found and verified against the latest diff
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="redash/authentication/__init__.py">
<violation number="1" location="redash/authentication/__init__.py:200">
P2: Trino continues impersonating a stale `preferred_username` when a later JWT omits (or clears) the claim, rather than using the configured datasource username. Remove the stored key when the claim is absent/empty so the documented fallback applies to the current identity.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
Comment on lines
+200
to
+209
| preferred_username = payload.get("preferred_username") | ||
| if preferred_username: | ||
| user_details = user.details or {} | ||
|
|
||
| if user_details.get("preferred_username") != preferred_username: | ||
| user.details = { | ||
| **user_details, | ||
| "preferred_username": preferred_username, | ||
| } | ||
| models.db.session.commit() |
Contributor
There was a problem hiding this comment.
P2: Trino continues impersonating a stale preferred_username when a later JWT omits (or clears) the claim, rather than using the configured datasource username. Remove the stored key when the claim is absent/empty so the documented fallback applies to the current identity.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At redash/authentication/__init__.py, line 200:
<comment>Trino continues impersonating a stale `preferred_username` when a later JWT omits (or clears) the claim, rather than using the configured datasource username. Remove the stored key when the claim is absent/empty so the documented fallback applies to the current identity.</comment>
<file context>
@@ -197,6 +197,17 @@ def jwt_token_load_user_from_request(request):
except models.NoResultFound:
user = create_and_login_user(current_org, payload["email"], payload["email"])
+ preferred_username = payload.get("preferred_username")
+ if preferred_username:
+ user_details = user.details or {}
</file context>
Suggested change
| preferred_username = payload.get("preferred_username") | |
| if preferred_username: | |
| user_details = user.details or {} | |
| if user_details.get("preferred_username") != preferred_username: | |
| user.details = { | |
| **user_details, | |
| "preferred_username": preferred_username, | |
| } | |
| models.db.session.commit() | |
| preferred_username = payload.get("preferred_username") | |
| user_details = user.details or {} | |
| updated_details = {**user_details} | |
| if preferred_username: | |
| updated_details["preferred_username"] = preferred_username | |
| else: | |
| updated_details.pop("preferred_username", None) | |
| if updated_details != user_details: | |
| user.details = updated_details | |
| models.db.session.commit() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
Description
Add support for using the JWT/OIDC
preferred_usernameclaim as the Trino impersonation user.Redash already supports Trino impersonation using the Redash user's
emailorname. In SSO setups, those values are not always the identity expected by Trino access-control policies:emailcan beotter@gotta.ottnamecan be a display name likeOtter Gottaottnameto the emailpreferred_usernamecan be the actual login/short username, for exampleottergottaottThis change stores
preferred_usernamefrom a valid JWT payload inUser.detailsand addsPreferred Usernameas a Trino impersonation attribute option. When selected, Trino receivesuser.details["preferred_username"], with fallback to the configured datasource username if the claim is missing.Expected compatible Trino versions: no Trino server-version dependency. This only changes the
uservalue passed to the existing Python Trino client connection.How is this tested?
Manual testing:
Verified locally with Keycloak + oauth2-proxy + Redash JWT auth that:
Keycloak emits preferred_username
Redash stores it in users.details
Trino datasource UI exposes Preferred Username in Impersonation User Attribute
Related Tickets & Documents
N/A
Mobile & Desktop Screenshots/Recordings (if there are UI changes)
N/A