|
| 1 | +"""Bring Your Own Identity Provider with fined grained OAuth scopes is currently public preview on |
| 2 | +Databricks in AWS. databricks-sql-connector supports user to machine OAuth login which means the |
| 3 | +end user has to be present to login in a browser which will be popped up by the Python process. You |
| 4 | +must enable OAuth in your Databricks account to run this example. More information on how to enable |
| 5 | +OAuth in your Databricks Account in AWS can be found here: |
| 6 | +
|
| 7 | +https://docs.databricks.com/administration-guide/account-settings-e2/single-sign-on.html |
| 8 | +
|
| 9 | +Pre-requisites: |
| 10 | +- You have a Databricks account in AWS. |
| 11 | +- You have configured OAuth in Databricks account in AWS using the link above. |
| 12 | +- You have installed a browser (Chrome, Firefox, Safari, Internet Explorer, etc) that will be |
| 13 | + accessible on the machine for performing OAuth login. |
| 14 | +
|
| 15 | +For security, databricks-sql-connector does not persist OAuth tokens automatically. Hence, after |
| 16 | +the Python process terminates the end user will have to log-in again. We provide APIs to be |
| 17 | +implemented by the end user for persisting the OAuth token. The SampleOAuthPersistence reference |
| 18 | +shows which methods you may implement. |
| 19 | +
|
| 20 | +For this example, the DevOnlyFilePersistence class is provided. Do not use this in production. |
| 21 | +
|
| 22 | +Bring Your Own Identity Provider is in public preview. The API may change prior to becoming GA. |
| 23 | +You can monitor these two links to find out when it will become generally available: |
| 24 | +
|
| 25 | + 1. https://docs.databricks.com/administration-guide/account-settings-e2/single-sign-on.html |
| 26 | + 2. https://docs.databricks.com/dev-tools/python-sql-connector.html |
| 27 | +""" |
| 28 | + |
| 29 | +import os |
| 30 | +from typing import Optional |
| 31 | + |
| 32 | +from databricks import sql |
| 33 | +from databricks.sql.experimental.oauth_persistence import OAuthPersistence, OAuthToken, DevOnlyFilePersistence |
| 34 | + |
| 35 | + |
| 36 | +class SampleOAuthPersistence(OAuthPersistence): |
| 37 | + def persist(self, hostname: str, oauth_token: OAuthToken): |
| 38 | + """To be implemented by the end user to persist in the preferred storage medium. |
| 39 | + |
| 40 | + OAuthToken has two properties: |
| 41 | + 1. OAuthToken.access_token |
| 42 | + 2. OAuthToken.refresh_token |
| 43 | +
|
| 44 | + Both should be persisted. |
| 45 | + """ |
| 46 | + pass |
| 47 | + |
| 48 | + def read(self, hostname: str) -> Optional[OAuthToken]: |
| 49 | + """To be implemented by the end user to fetch token from the preferred storage |
| 50 | +
|
| 51 | + Fetch the access_token and refresh_token for the given hostname. |
| 52 | + Return OAuthToken(access_token, refresh_token) |
| 53 | + """ |
| 54 | + pass |
| 55 | + |
| 56 | +with sql.connect(server_hostname = os.getenv("DATABRICKS_SERVER_HOSTNAME"), |
| 57 | + http_path = os.getenv("DATABRICKS_HTTP_PATH"), |
| 58 | + auth_type="databricks-oauth", |
| 59 | + experimental_oauth_persistence=DevOnlyFilePersistence("./sample.json")) as connection: |
| 60 | + |
| 61 | + for x in range(1, 100): |
| 62 | + cursor = connection.cursor() |
| 63 | + cursor.execute('SELECT 1+1') |
| 64 | + result = cursor.fetchall() |
| 65 | + for row in result: |
| 66 | + print(row) |
| 67 | + cursor.close() |
| 68 | + |
| 69 | + connection.close() |
0 commit comments