Skip to content

Commit ee0e932

Browse files
committed
Add OBO sample
1 parent cf12ce5 commit ee0e932

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

docs/blog/2025-03-17-dabs/2025-03-17-dabs.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Databricks Apps is supported by the Databricks [REST API](https://docs.databrick
1313

1414
This blog post demonstrates how you can quickly set up a CI/CD pipeline using DABs and [GitHub Actions](https://github.com/features/actions). The associated GitHub repository provides a starting point and reference for your own deployments.
1515

16+
{/* truncate */}
17+
1618
## Overview
1719

1820
In the following sections of this blog post, you will set up a deployment pipeline that allows for local testing and development of your application, on-demand deployments to a live Databricks development environment, and fully automated deployments to a production environment.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# On-behalf-of-user authentication
6+
7+
This recipe demonstrates how to use Databricks Apps [on-behalf-of-user authentication](https://docs.databricks.com/aws/en/dev-tools/databricks-apps/app-development#-using-the-databricks-apps-authorization-model) to run a SQL query using the user's credentials instead of the app's service principal.
8+
9+
## Code snippet
10+
11+
```python title="app.py"
12+
import streamlit as st
13+
from databricks import sql
14+
from databricks.sdk.core import Config
15+
16+
cfg = Config()
17+
18+
def get_user_token():
19+
headers = st.context.headers
20+
user_token = headers["X-Forwarded-Access-Token"]
21+
return user_token
22+
23+
@st.cache_resource
24+
def connect_with_obo(http_path, user_token):
25+
return sql.connect(
26+
server_hostname=cfg.host,
27+
http_path=http_path,
28+
access_token=user_token
29+
)
30+
31+
def execute_query(table_name, conn):
32+
with conn.cursor() as cursor:
33+
query = f"SELECT * FROM {table_name} LIMIT 10"
34+
cursor.execute(query)
35+
return cursor.fetchall_arrow().to_pandas()
36+
37+
user_token = get_user_token()
38+
39+
http_path = "/sql/1.0/warehouses/abcd1234" # Replace with your SQL warehouse HTTP path
40+
table_name = "samples.nyctaxi.trips" # Replace with your catalog.schema.table
41+
42+
if st.button("Run Query"):
43+
conn = connect_with_obo(http_path, user_token)
44+
df = execute_query(table_name, conn)
45+
st.dataframe(df)
46+
```
47+
48+
:::info
49+
50+
This sample uses Streamlit's [st.cache_resource](https://docs.streamlit.io/develop/concepts/architecture/caching#stcache_resource) to cache the database connection across users, sessions, and reruns. The app will only work when deployed to Databricks Apps with on-behalf-of-user authentication enabled.
51+
52+
:::
53+
54+
:::warning
55+
56+
You need to enable [on-behalf-of-user authentication](https://docs.databricks.com/aws/en/dev-tools/databricks-apps/app-development#-using-the-databricks-apps-authorization-model) for your application for this sample to work. When running this code locally, the `X-Forwarded-Access-Token` will not be present and the sample will not work as intended.
57+
58+
:::
59+
60+
## Resources
61+
62+
- [SQL warehouse](https://docs.databricks.com/aws/en/compute/sql-warehouse/)
63+
- [Unity Catalog table](https://docs.databricks.com/aws/en/tables/)
64+
65+
## Permissions
66+
67+
For the on-behalf-of-user authentication model, permissions work as follows:
68+
69+
- **User's permissions**: When using OBO authentication, the query runs with the end user's permissions
70+
- User needs `SELECT` permissions on the tables being queried
71+
- User needs `CAN USE` on the SQL warehouse
72+
73+
- **App service principal**: When falling back to service principal authentication
74+
- Needs `CAN USE` on the SQL warehouse
75+
- Needs `SELECT` on the Unity Catalog tables for fallback access
76+
77+
See [Databricks Apps authorization model](https://docs.databricks.com/aws/en/dev-tools/databricks-apps/#how-does-databricks-apps-manage-authorization) for more information.
78+
79+
## Dependencies
80+
81+
- [Databricks SDK](https://pypi.org/project/databricks-sdk/) - `databricks-sdk`
82+
- [Databricks SQL Connector](https://pypi.org/project/databricks-sql-connector/) - `databricks-sql-connector`
83+
- [Streamlit](https://pypi.org/project/streamlit/) - `streamlit`
84+
85+
```python title="requirements.txt"
86+
databricks-sdk
87+
databricks-sql-connector
88+
streamlit
89+
```

0 commit comments

Comments
 (0)