Trino User Impersonation & Query Attribution
Overview
mcp-trino provides two complementary features for user identity tracking:
User Impersonation
X-Trino-User
Execute queries as the actual user (affects permissions)
TRINO_ENABLE_IMPERSONATION=true
Query Attribution
X-Trino-Client-Tags, X-Trino-Client-Info
Track who initiated queries (for auditing/monitoring)
OAuth enabled only
Key Difference:
Impersonation changes who Trino thinks is running the query (affects access control)
Attribution tags queries with user metadata (for monitoring/auditing without changing permissions)
User Impersonation
Trino user impersonation allows the MCP server to execute queries on behalf of authenticated users while maintaining a single set of static credentials for the Trino connection. When enabled, MCP executes queries as the actual OAuth user (via the X-Trino-User header) rather than the service account.
Benefits:
✅ Audit trails - Queries show actual user names in Trino logs
✅ Access control - Trino enforces user-specific permissions
✅ Static credentials - MCP uses one service account for all connections
✅ Security - OAuth user identity propagated securely via validated JWT tokens
Query Attribution
Query attribution automatically tags each query with the OAuth user's identity via Trino client metadata headers. This works independently of impersonation and is automatically enabled when OAuth is configured.
Benefits:
✅ Zero configuration - Works automatically with OAuth
✅ Non-intrusive - Doesn't affect Trino permissions or access control
✅ Monitoring - Track query patterns by user in Trino metrics
✅ Debugging - Identify which user initiated problematic queries
Headers set:
X-Trino-Client-Tags- OAuth username for query taggingX-Trino-Client-Info- OAuth username for client identificationX-Trino-Source- OAuth username (only ifTRINO_SOURCEnot configured globally)
Quick Start
Query Attribution (Automatic)
Query attribution requires no configuration beyond enabling OAuth:
With this setup, all queries from authenticated users will automatically include:
X-Trino-Client-Tags: [email protected]X-Trino-Client-Info: [email protected]
User Impersonation (Opt-in)
For full impersonation (Trino treats queries as coming from the actual user):
2. Configure Trino Access Control
Create /etc/trino/access-control.json:
Create /etc/trino/access-control.properties:
3. Restart Services
How It Works
Combined Flow (Attribution + Impersonation)
Feature Comparison
OAuth only
✅ Client-Tags/Info
❌ None
Runs as service account, tagged with user
OAuth + Impersonation
✅ Client-Tags/Info
✅ X-Trino-User
Runs as actual user, tagged with user
No OAuth
❌ None
❌ None
Runs as service account, no user info
Configuration
Query Source Attribution
By default, mcp-trino always identifies itself to Trino via the X-Trino-Source header as mcp-trino/<version>. This enables proper query attribution and monitoring in Trino.
Why this matters:
Query attribution in Trino logs and metrics
Identify which application generated queries
Debug and monitor query patterns by source
Similar to how DB clients like DBeaver identify themselves
Principal Field Selection
By default, the preferred_username JWT claim is used. You can configure which field to use:
username
preferred_username
Username from identity provider (default)
alice
subject
sub
Unique subject identifier
user-123-abc
Example: If your JWT contains:
Then:
TRINO_IMPERSONATION_FIELD=username→ Trino sees user asaliceTRINO_IMPERSONATION_FIELD=email→ Trino sees user as[email protected]TRINO_IMPERSONATION_FIELD=subject→ Trino sees user asauth0|5f8b3c4d2e1a6c0071234567
Choosing the Right Field
Use email (recommended):
Human-readable in audit logs
Matches directory services (LDAP/AD)
Works with most OAuth providers
Unique and stable
Use username:
Short names in logs
Match Unix/Linux usernames
Compatibility with existing Trino users
Note: Some OAuth providers don't include this claim
Use subject:
Maximum stability (never changes)
Works with all providers
Highest security (unique per user)
Note: Usually not human-readable
Provider-Specific Notes
Okta:
Recommendation: Use email field.
Google:
Note: No preferred_username claim. Use email field.
Azure AD:
Recommendation: Use email field.
Trino Access Control
Basic Configuration
Allow the MCP service account to impersonate any user:
Restrictive Configuration
Deny impersonation of admin users:
User Mapping
Transform usernames before Trino uses them:
Strip domain from email:
This maps [email protected] → alice in Trino.
Extract username from Auth0 subject:
This maps auth0|alice-123 → alice-123 in Trino.
Complete Setup Example
Trino access control (/etc/trino/access-control.json):
When to Use Which Feature
Use Query Attribution Only (OAuth without Impersonation)
Best when:
You want audit trails without changing Trino permissions
Service account should handle all access control
You need to track who initiated queries for monitoring
Trino access control rules are based on service account, not individual users
Use Full Impersonation
Best when:
Trino has user-specific access control rules
Different users need different data access permissions
Audit logs must show actual user as query executor
Row-level or column-level security depends on user identity
Verification
Check MCP Logs
For Query Attribution (OAuth enabled):
For Impersonation (if enabled):
Check Trino Logs
With Query Attribution only:
With Impersonation enabled:
Query Trino System Tables
Check active queries with attribution:
Troubleshooting
Query Attribution Not Working
Symptom: client_tags and client_info are empty in Trino logs
Solution: Verify:
OAuth is enabled (
OAUTH_ENABLED=true)User is authenticated (JWT token is valid)
OAuth user has at least one of:
username,email, orsubjectfields
Note: Attribution only works when there's an actual OAuth user. If no user is found in context, no attribution headers are added (by design).
Impersonation Fails
Error: "Access Denied: Cannot impersonate user"
Solution: Verify:
TRINO_ENABLE_IMPERSONATION=trueis setTrino access control allows service account to impersonate users
Target username exists or matches allowed patterns
User Not Found in Context
Symptom: No impersonation or attribution occurs, queries run as service account without user tags
Solution: Verify:
OAuth is enabled (
OAUTH_ENABLED=true)JWT tokens are being properly validated
The username field is present in JWT claims
Missing JWT Claim
Error: "Missing preferred_username in token"
Solution: Your OAuth provider doesn't include this claim. Use email or subject:
Access Denied in Trino
Error: "Access Denied: User not found"
Solution: The username format doesn't match Trino's expected format. Either:
Configure user mapping in Trino (recommended)
Change the impersonation field
Update Trino user definitions
Empty Principal
Error: No username is extracted
Solution: The configured field is not present in the JWT. Check your JWT claims and switch to an available field.
Security Considerations
Impersonation Rules: Trino access control must explicitly allow the service account to impersonate users
OAuth Validation: Only validated OAuth tokens can trigger impersonation
Username Extraction: Usernames extracted from validated JWT claims only
Header Security:
X-Trino-Userheader only added when impersonation is enabled and user is authenticatedAudit Trail: All impersonation attempts logged at INFO level
Implementation Details
Architecture
The user identity features consist of four main components:
Configuration - Reads
TRINO_ENABLE_IMPERSONATION,TRINO_IMPERSONATION_FIELD, andTRINO_SOURCEHTTP Round Tripper - Intercepts HTTP requests to Trino and adds
X-Trino-UserandX-Trino-SourceheadersMCP Handlers - Extracts OAuth user and adds to query context for impersonation
Query Attribution - Adds
X-Trino-Client-Tags/Infovia sql.Named parameters per query
Request Flow
Context Management
User information flows through Go contexts for both features:
Header Priority
X-Trino-User
headerRoundTripper
TRINO_ENABLE_IMPERSONATION=true
TRINO_IMPERSONATION_FIELD
X-Trino-Source
headerRoundTripper
TRINO_SOURCE configured
N/A (static)
X-Trino-Source
sql.Named
OAuth enabled, TRINO_SOURCE empty
Uses OAuth username
X-Trino-Client-Tags
sql.Named
OAuth enabled
Uses OAuth username
X-Trino-Client-Info
sql.Named
OAuth enabled
Uses OAuth username
Related Documentation
OAuth Configuration - OAuth provider setup
Deployment Guide - Production deployment
Trino Access Control - Trino documentation
Trino Impersonation - Impersonation rules
Last updated
Was this helpful?