A Streamlit app that allows users to upload files to Unity Catalog volumes using on-behalf-of-user authorization.
- Upload files to Unity Catalog volumes
- Browse volume contents
- Uses on-behalf-of-user authorization to access volumes with user permissions
git clone https://github.com/Mmodarre/Databricks-Apps-Unity-Catalog-File-Uploader.git
cd src
databricks apps createstreamlit run app.pyDeploy the app to your Databricks workspace:
databricks apps deployThis document explains how to properly set up on-behalf-of-user authorization for Databricks Apps.
On-behalf-of-user authorization allows a Databricks App to access resources using the permissions of the logged-in user. This provides several benefits:
- Users can only access resources they have permission to
- Permissions are managed through standard Databricks/Unity Catalog mechanisms
- No need to grant broad permissions to the app's service principal
OAuth scopes for on-behalf-of-user authorization must be configured using the Databricks CLI, not in the app.yaml file or the UI.
- Login to CLI as accont admin
auth login --account-id ccb842e7-2376-4152-b0b0-29fa952379b8 --profile test2
Host: https://accounts.azuredatabricks.net/- Get all custom app integrations
databricks account custom-app-integration list- Find your app integration and get the current scopes
databricks account custom-app-integration get 'e08f4a41-eea6-402c-926c-d28f3cdf0868'- Add required scopes for OBO for your app.
databricks account custom-app-integration update 'e08f4a41-eea6-402c-926c-d28f3cdf0868' --json '{"scopes": [ "offline_access","email","iam.current-user:read","openid","iam.access-control:read","profile","all-apis"]}'Ensure users have:
CAN_USEpermission on the app- Appropriate permissions on the Unity Catalog volumes they need to access
When users access the app for the first time (or after scope changes):
- They will be prompted to grant consent to the requested scopes
- They must accept all requested permissions for the app to function properly
If users previously accessed the app, they may need to log out and log back in to see the updated consent page.
If users encounter permission errors:
- Verify that all required OAuth scopes are configured using the Databricks CLI
- Check that users have appropriate permissions on the volumes they're trying to access
- Have users log out and log back in to trigger the consent flow again
- Use the "Test Authentication" button in the app's troubleshooting section to diagnose issues
For apps that need to access Unity Catalog volumes, the following scopes are recommended:
volumes:read: Read volumes in Unity Catalogvolumes:write: Write to volumes in Unity Catalogcatalog:read: Read catalog metadataunity-catalog:access: Access Unity Catalogworkspace:access: Access workspaceiam:access-control:read: Read access control information (default)iam:current-user:read: Read current user information (default)
In your app code, access the user's downscoped access token from the request headers:
# For Streamlit
user_access_token = st.context.headers.get('X-Forwarded-Access-Token')
# For Flask/Dash
from flask import request
user_access_token = request.headers.get('X-Forwarded-Access-Token')
# For Gradio
from fastapi import Request
import gradio as gr
request: Request = gr.Request().get("request")
user_access_token = dict(request.headers).get('X-Forwarded-Access-Token')
# For Shiny
user_access_token = session.http_conn.headers.get('X-Forwarded-Access-Token', None)from databricks.sdk.core import Config
from databricks.sdk import WorkspaceClient
# Create a config with the user's token
config = Config(
host=os.environ.get("DATABRICKS_HOST"),
token=user_access_token,
auth_type="pat",
insecure=False
)
# Create client with the user's token
client = WorkspaceClient(config=config)
# Now operations will use the user's permissions
volumes = client.volumes.list(catalog_name="main", schema_name="default")When a user accesses an app with on-behalf-of-user authorization for the first time:
- They will be redirected to a consent page
- The page will list all requested scopes with descriptions
- The user must grant consent to continue using the app
If the scopes change, users will need to log out and log back in to see the updated consent page.
-
"Provided OAuth token does not have required scopes"
- Ensure all required scopes are configured using the Databricks CLI
- Have users log out and log back in to trigger the consent flow again
-
"More than one authorization method configured"
- Ensure your code is explicitly setting
auth_type="pat"when using the user token - Don't mix authentication methods in the same Config object
- Ensure your code is explicitly setting
-
Permission errors when accessing resources
- Verify the user has appropriate permissions on the resources
- Check that all required scopes are configured
- Test with a user who has admin permissions to verify the app works correctly
