Authentication & Permissions
This is the authoritative reference for how identity and authorization work in Genie Workbench. The app uses a dual-identity model: the signed-in user's token for interactive operations and the app's Service Principal (SP) for background jobs and API fallback.
Overview
OBO (On-Behalf-Of) Authentication
How it works
-
The Databricks Apps platform authenticates the user via SSO and forwards their OAuth access token in the
x-forwarded-access-tokenHTTP header on every request. -
OBOAuthMiddlewareinbackend/main.pyintercepts all/api/*requests and callsset_obo_user_token(token)frombackend/services/auth.py. -
set_obo_user_token()creates aWorkspaceClientconfigured withauth_type="pat"using the user's token. This is stored in a PythonContextVarso it is scoped to the current request.noteThe explicit
auth_type="pat"is required because the Databricks Apps environment hasDATABRICKS_CLIENT_ID/DATABRICKS_CLIENT_SECRETset for the SP. Without it, the SDK would default to OAuth M2M instead of the user's token. -
Most downstream service calls use
get_workspace_client(), which returns the OBO client if set, otherwise falls back to the SP singleton. -
Routes whose visibility must stay bound to the signed-in user instead call
require_obo_workspace_client(), which returns only the request's user client and never falls back to the SP. If no OBO client is set, the request fails rather than widening access. GenieWatch traffic-gap reads use this path — see OBO-only routes.
SSE streaming caveat
For the Create Agent's Server-Sent Events endpoint, the ContextVar is not cleared after call_next in the middleware. This is because the response body streams lazily — the generator runs after the middleware returns. Streaming handlers stash the raw token on request.state.user_token and re-set it inside the generator function.
OBO-only routes
Some reads expose data that the app's SP could see but the signed-in user must not. Those routes require user authorization and have no SP fallback.
| Route | Requirement | Reason |
|---|---|---|
GET /api/watch/spaces/{space_id}/traffic-gaps | OBO only, user needs CAN_MANAGE on the Agent | Reads production conversation traffic. The Genie list conversations API requires CAN_MANAGE, so SP fallback would let a non-manager read other users' questions. |
What OBO protects
Users can only interact with resources they have permission to access:
- Unity Catalog: catalogs, schemas, tables visible to the user
- Genie Agents: only agents the user can manage
- SQL Warehouse: queries execute under the user's identity
- Genie Agent creation/modification: agents are created and patched as the user
Service Principal (SP)
Singleton client
get_service_principal_client() in backend/services/auth.py always returns the SP singleton — a WorkspaceClient initialized from the platform environment variables (DATABRICKS_CLIENT_ID, DATABRICKS_CLIENT_SECRET, DATABRICKS_HOST). On Databricks Apps, this is the app's own SP.
When SP is used
The SP is used in three scenarios:
1. Genie API scope fallback
Some user OAuth tokens lack the dashboards.genie scope, even though it is listed in app.yaml user_api_scopes. When get_genie_space() or list_genie_spaces() in backend/services/genie_client.py catches a scope error, it transparently retries with the SP:
def _is_scope_error(e: Exception) -> bool:
msg = str(e).lower()
return "scope" in msg or "insufficient_scope" in msg
For this fallback to work, the SP must have CAN_MANAGE on each Genie Agent.
2. Optimization job execution
The Auto-Optimize pipeline runs as a Lakeflow Job — a long-running, multi-task DAG. Lakeflow Jobs execute in a separate environment with a fixed run_as identity. There is no mechanism to forward the user's short-lived OAuth token into a background job that may run for minutes.
The job is configured to run_as the app's SP. At startup, _ensure_gso_job_run_as() in backend/main.py verifies and updates the job's run_as to match the current app SP. The notebook installer also sets it when the installing user holds servicePrincipal.user on the app SP; neither installer requires that role. See ensure_job_run_as Self-Healing.
3. GSO Delta table operations
Reads and writes to the optimizer state tables under GSO_CATALOG.GSO_SCHEMA use the SP because these tables are owned by the SP and are not user-scoped. See Durable state for the table list.
Optimization Trigger Flow
When a user triggers Auto-Optimize, the app uses both identities in a carefully sequenced flow:
Source: packages/genie-space-optimizer/src/genie_space_optimizer/integration/trigger.py — trigger_optimization()
Requested OAuth Scopes
The user_api_scopes in app.yaml request these scopes for the user's OBO token:
| Scope | Purpose |
|---|---|
sql | Execute SQL queries via warehouses |
dashboards.genie | Access Genie Agent API |
serving.serving-endpoints | Call model serving endpoints (LLM on the classic route) |
ai-gateway | Call Unity AI Gateway (LLM on the gateway route, the default) |
catalog.catalogs:read | Browse Unity Catalog catalogs |
catalog.schemas:read | Browse Unity Catalog schemas |
catalog.tables:read | Browse Unity Catalog tables |
files.files | Access workspace files |
The same list is applied in three places that must stay identical: app.yaml, APP_SCOPES in scripts/deploy_lib/apps.py (notebook installer) and the scopes list in scripts/deploy.sh. Databricks asks each user to consent once per app and set of scopes, so adding a scope, as the ai-gateway scope was added, prompts every existing user to consent again on their next visit.
If the workspace or user's OAuth consent doesn't grant all scopes, the app degrades gracefully — the SP fallback handles the most common gap (dashboards.genie). LLM calls on the gateway route do the same: when Unity AI Gateway refuses the user's token for a missing scope (403 Invalid scope), Create Agent and the other backend LLM calls retry once as the app service principal and log a warning, so that usage is attributed to the SP instead of the user. OBO-only routes are the exception: they return 401 instead of falling back.
Model Access for LLM Calls
LLM calls go through Unity AI Gateway by default (see LLM calls). They run under two identities:
| Calls | Identity |
|---|---|
| Create Agent and the plan builder | The signed-in user, through the OBO token. If the token lacks the ai-gateway scope, the call is retried once as the app service principal |
The optimization job (gso-optimize) and metric-view suggestion embeddings (mv-suggest), including the IQ panel's suggestion scan | The app service principal |
The installers grant nothing on the gateway models. What a deployment needs:
- Unity AI Gateway in the workspace's region. Both installers send one chat request through the gateway and stop if it fails:
verify_ai_gatewayinscripts/deploy_lib/verify.pyand_preflight_check_ai_gatewayinscripts/preflight.sh. - User consent to the
ai-gatewayscope. Each user accepts it once, on the app's consent screen. - Model access for both identities, if your workspace restricts it. If an admin limits who can use the
system.aimodels, or you use your own model (GENIE_BYOK_ENABLED), grant access to the app's users and to the app's service principal. Grant whatever privilege your workspace's gateway governance requires.
A caller without access gets 404 from the gateway, because the gateway hides models a caller can't use. The app shows it as Model unavailable or access not granted.
The install-time check runs as the installing user, so it can pass while the app service principal, or another user, has no access. For the SP, that shows up at the first optimization run.
To avoid the gateway entirely, install with llm_route=classic (notebook) or GENIE_LLM_ROUTE=classic (terminal). The app then calls serving endpoints directly, under the serving.serving-endpoints scope and your existing serving-endpoint permissions.
SP Permissions Required
Per Genie Agent
| Permission | Purpose |
|---|---|
CAN_MANAGE | API fallback when user token lacks Genie scope; applying optimization patches during the GSO pipeline |
Grant via the Genie Agent sharing UI, or let the installer do it — both notebooks/install.py and scripts/install.sh grant the SP access to your visible Genie Agents.
Per referenced data schema
| Permission | Purpose |
|---|---|
USE_CATALOG | Access the catalog containing the schema |
USE_SCHEMA | Access the schema |
SELECT | Read table data during optimization benchmarks |
GRANT USE_CATALOG ON CATALOG <catalog> TO `<service-principal-name>`;
GRANT USE_SCHEMA ON SCHEMA <catalog>.<schema> TO `<service-principal-name>`;
GRANT SELECT ON SCHEMA <catalog>.<schema> TO `<service-principal-name>`;
GSO optimizer schema
The SP needs full access to the optimizer state schema (<GSO_CATALOG>.genie_space_optimizer):
| Permission | Purpose |
|---|---|
USE_CATALOG | Access the catalog |
USE_SCHEMA | Access the schema |
SELECT | Read optimizer state tables |
MODIFY | Write optimizer state tables |
CREATE_TABLE | Create state tables on first run |
CREATE_FUNCTION | Create UDFs if needed |
CREATE_MODEL | Granted for historical/forward compatibility; the pipeline no longer registers models |
CREATE_VOLUME | Artifact storage |
EXECUTE | Execute functions |
MANAGE | Schema management |
These are granted automatically by scripts/grant_permissions.py during deployment.
Complete Permission Boundary
| Operation | Identity | Code Reference | Rationale |
|---|---|---|---|
| Browse Genie Agents, UC catalogs/schemas/tables | OBO (user) | services/uc_client.py, routers/create.py | User sees only what they have access to |
| GenieWatch traffic-gap analysis | OBO only, no SP fallback | watch/routers/traffic_gaps.py require_obo_workspace_client() | Conversation traffic requires CAN_MANAGE; SP fallback would leak other users' questions |
| Genie API — fetch a gated agent's configuration | OBO (user) only | services/genie_client.py get_genie_space / get_serialized_space | Strict OBO; no SP retry on a gated path |
| Genie API — list agents for the space list | OBO (user) | routers/spaces.py list_genie_spaces(sp_fallback=False) | Genie filters the list to the caller |
| Admin dashboard, leaderboard and alerts | OBO (user) | routers/admin.py list_genie_spaces(sp_fallback=False) | Counts and alerts cover the agents the caller can see; an alert's finding is its viewer-safe form; a listing failure is an error |
| Create Agent — tools, SQL, agent creation | OBO (user) | services/create_agent.py, services/create_agent_tools.py | Agent created under user identity |
| Create Agent — new chat session on an existing space | OBO (user) | routers/create.py POST /api/create/agent/chat | Can Edit when body.space_id pre-seeds a new session |
| Trigger optimization — permission check | OBO (user) | backend/services/space_access.py require_space_access() | Genie answers under the user's token; no SP fallback |
| Auto-Optimize reads that expose configuration | OBO (user) | routers/auto_optimize.py require_space_access / _require_run_space_access | Can Edit (MV-D110): proposals, semantic graph, join advice, mv-ddl, mv-created, run detail |
| Run status, run list, and active-run badge | OBO (user) | routers/auto_optimize.py | Can View on the run's agent |
| Auto-Optimize writes and metric-view routes | OBO (user) | routers/auto_optimize.py | Can Edit; create-and-attach asks Can Edit again in the probe, which downgrades |
| Run apply / discard / revert / history removal | OBO (user) | routers/auto_optimize.py _require_run_space_access() | Can Edit on the run's agent |
| Space detail, history, and star | OBO (user) | routers/spaces.py | Can View; below Can Edit the stored scan's findings, warnings, and check details appear only in the scorer's count-only forms (viewer_safe_text() in iq_scan/scoring.py, applied by services/scanner.py redact_for_viewer()), and anything else is blanked; history carries score, maturity, accuracy, and time only |
| IQ Scan | OBO for the space; SP for GSO run data | routers/spaces.py scan; services/scanner.py | Can Edit (the scan reads the export); the GSO run lookup (Lakebase, then Delta) runs as the SP |
/api/space/fetch | OBO (user) | routers/analysis.py fetch | Can Edit (the fetch reads the export) |
| Version history list and tags | OBO (user) | services/version_control/space_authz.py authorize_space() | Can View on the agent |
| Version detail, diff, capture, restore, tag writes | OBO (user) | space_authz.py authorize() / authorize_space(); live read/write in routers/vc_spaces.py | Can Edit; the live read and write run under the user's token, with no SP fallback |
| The user's access level, for the UI | OBO (user) | routers/spaces.py GET /api/spaces/{space_id}/access | Genie answers once per agent page; below Can Edit the page shows the score, runs and history read-only, and sends no request that needs Can Edit. The highest level it reports is Can Edit, because the app's user token can't prove Can Manage, so a manager reads as Can Edit |
| Trigger optimization — SP entitlement check | SP | integration/trigger.py sp_can_manage_space() | Verify SP can manage the agent |
| Optimization job submission | SP | backend/job_launcher.py submit_optimization() | jobs.run_now() requires SP |
| Optimization job execution (4-task DAG) | SP (run_as) | genie_space_optimizer/backend/job_launcher.py ensure_job_run_as(), called at startup by backend/main.py _ensure_gso_job_run_as() | Lakeflow Jobs have no OBO mechanism |
| GSO Delta table reads/writes | SP | routers/auto_optimize.py _delta_query() | Optimizer state tables owned by SP |
| Lakebase persistence | SP | services/lakebase.py | App-level storage, not user-scoped |
| GenieWatch space reads and listings | OBO → SP fallback (flagged) | watch/services/genie_client.py get_genie_space_with_sp_fallback and list_genie_spaces() (default sp_fallback=True), called from watch/routers/*.py | Outside MV-D109; keeps the scope fallback |
Space configuration reads and writes on user paths run as the user, with no service-principal fallback. That includes the Create Agent's update_space tool: the model chooses the space, so Genie's Can Edit check on the user's token is the authority for the write. Flagged exceptions keep an SP fallback: GenieWatch space reads and listings (scope fallback) and new-agent creation in genie_creator.py (create_genie_space uses the OBO-first client, which falls back to the SP when no user token is set, and the post-create list lookup also retries as the SP on an OAuth scope error).
Security Considerations
-
Authorization before execution — the user must have
CAN_EDITorCAN_MANAGEon the Genie Agent before any optimization job is submitted. This check happens with the user's OBO token, not the SP. -
SP entitlement validated — even if the user is authorized, the SP must also have
CAN_MANAGEon the agent. If the SP lacks access, the trigger is rejected with a clear error. -
Minimum-privilege SP — the SP only needs read access to referenced data schemas (for benchmarking) and manage access to the GSO state schema. It does not need workspace-admin privileges.
-
No token forwarding to jobs — the design intentionally does not pass user tokens into background jobs. Short-lived OAuth tokens would expire during long-running DAGs, and storing user credentials in job parameters would be a security risk.
-
Audit trail — the triggering user's email is recorded in the optimization run metadata, providing traceability even though the job executes as the SP.
Related Documentation
- Architecture Overview — system components
- Auto-Optimize — the optimization pipeline that runs under SP
- Deployment Guide — how SP permissions are granted during deploy
- API Reference — per-endpoint auth identity