|
| 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