connect.external.databricks
connect.external.databricks
Databricks SDK integration.
Databricks SDK credentials implementations which support interacting with Posit OAuth integrations on Connect.
Notes
These APIs are provided as a convenience and are subject to breaking changes: https://github.com/databricks/databricks-sdk-py#interface-stability
Attributes
Name | Description |
---|---|
CredentialsProvider | |
POSIT_LOCAL_CLIENT_CREDENTIALS_AUTH_TYPE | |
POSIT_OAUTH_INTEGRATION_AUTH_TYPE |
Classes
Name | Description |
---|---|
CredentialsStrategy | Maintain compatibility with the Databricks SQL/SDK client libraries. |
PositContentCredentialsProvider | CredentialsProvider implementation which initiates a credential exchange using a content-session-token. |
PositContentCredentialsStrategy | CredentialsStrategy implementation which supports interacting with Service Account OAuth integrations on Connect. |
PositCredentialsProvider | CredentialsProvider implementation which initiates a credential exchange using a user-session-token. |
PositCredentialsStrategy | CredentialsStrategy implementation which supports interacting with Viewer OAuth integrations on Connect. |
PositLocalContentCredentialsProvider | CredentialsProvider implementation which provides a fallback for local development using a client credentials flow. |
PositLocalContentCredentialsStrategy | CredentialsStrategy implementation which supports local development using OAuth M2M authentication against Databricks. |
CredentialsStrategy
connect.external.databricks.CredentialsStrategy()
Maintain compatibility with the Databricks SQL/SDK client libraries.
See Also
- https://github.com/databricks/databricks-sql-python/blob/v3.3.0/src/databricks/sql/auth/authenticators.py#L19-L33
- https://github.com/databricks/databricks-sdk-py/blob/v0.29.0/databricks/sdk/credentials_provider.py#L44-L54
Methods
Name | Description |
---|---|
auth_type |
auth_type
connect.external.databricks.CredentialsStrategy.auth_type()
PositContentCredentialsProvider
connect.external.databricks.PositContentCredentialsProvider(self, client)
CredentialsProvider
implementation which initiates a credential exchange using a content-session-token.
The content-session-token is provided by Connect through the environment variable CONNECT_CONTENT_SESSION_TOKEN
.
See Also
- https://github.com/posit-dev/posit-sdk-py/blob/main/src/posit/connect/oauth/oauth.py
PositContentCredentialsStrategy
connect.external.databricks.PositContentCredentialsStrategy(
self
local_strategy=None
client )
CredentialsStrategy
implementation which supports interacting with Service Account OAuth integrations on Connect.
This strategy callable class returns a PositContentCredentialsProvider
when hosted on Connect, and its local_strategy
strategy otherwise.
Examples
NOTE: in the example below, the PositContentCredentialsStrategy
can be initialized anywhere that the Python process can read environment variables.
from posit.connect.external.databricks import PositContentCredentialsStrategy
import pandas as pd
from databricks import sql
from databricks.sdk.core import ApiClient, Config, databricks_cli
from databricks.sdk.service.iam import CurrentUserAPI
= "<REDACTED>"
DATABRICKS_HOST = f"https://{DATABRICKS_HOST}"
DATABRICKS_HOST_URL = "<REDACTED>"
SQL_HTTP_PATH
# NOTE: currently the databricks_cli local strategy only supports auth code OAuth flows.
# https://github.com/databricks/cli/issues/1939
#
# This means that the databricks_cli supports local development using the developer's
# databricks credentials, but not the credentials for a service principal.
# To fallback to service principal credentials in local development, use
# `PositLocalContentCredentialsStrategy` as a drop-in replacement.
= PositContentCredentialsStrategy(local_strategy=databricks_cli)
posit_strategy
= Config(host=DATABRICKS_HOST_URL, credentials_strategy=posit_strategy)
cfg
= CurrentUserAPI(ApiClient(cfg)).me()
databricks_user_info print(f"Hello, {databricks_user_info.display_name}!")
= "SELECT * FROM samples.nyctaxi.trips LIMIT 10;"
query with sql.connect(
=DATABRICKS_HOST,
server_hostname=SQL_HTTP_PATH,
http_path=posit_strategy.sql_credentials_provider(cfg),
credentials_provideras connection:
) with connection.cursor() as cursor:
cursor.execute(query)= cursor.fetchall()
rows print(pd.DataFrame([row.asDict() for row in rows]))
Methods
Name | Description |
---|---|
auth_type | |
sql_credentials_provider | The sql connector attempts to call the credentials provider w/o any args. |
auth_type
connect.external.databricks.PositContentCredentialsStrategy.auth_type()
sql_credentials_provider
connect.external.databricks.PositContentCredentialsStrategy.sql_credentials_provider(
*args
**kwargs
)
The sql connector attempts to call the credentials provider w/o any args.
The SQL client’s ExternalAuthProvider
is not compatible w/ the SDK’s implementation of CredentialsProvider
, so create a no-arg lambda that wraps the args defined by the real caller. This way we can pass in a databricks Config
object required by most of the SDK’s CredentialsProvider
implementations from where sql.connect
is called.
https://github.com/databricks/databricks-sql-python/issues/148#issuecomment-2271561365
PositCredentialsProvider
connect.external.databricks.PositCredentialsProvider(
self
client
user_session_token )
CredentialsProvider
implementation which initiates a credential exchange using a user-session-token.
The user-session-token is provided by Connect through the HTTP session header Posit-Connect-User-Session-Token
.
See Also
- https://github.com/posit-dev/posit-sdk-py/blob/main/src/posit/connect/oauth/oauth.py
PositCredentialsStrategy
connect.external.databricks.PositCredentialsStrategy(
self
local_strategy=None
client=None
user_session_token )
CredentialsStrategy
implementation which supports interacting with Viewer OAuth integrations on Connect.
This strategy callable class returns a PositCredentialsProvider
when hosted on Connect, and its local_strategy
strategy otherwise.
Examples
NOTE: In the example below, the PositCredentialsProvider must be initialized within the context of the shiny server
function, which provides access to the HTTP session headers.
import os
import pandas as pd
from databricks import sql
from databricks.sdk.core import ApiClient, Config, databricks_cli
from databricks.sdk.service.iam import CurrentUserAPI
from posit.connect.external.databricks import PositCredentialsStrategy
from shiny import App, Inputs, Outputs, Session, render, ui
= "<REDACTED>"
DATABRICKS_HOST = f"https://{DATABRICKS_HOST}"
DATABRICKS_HOST_URL = "<REDACTED>"
SQL_HTTP_PATH
= ui.page_fluid(ui.output_text("text"), ui.output_data_frame("result"))
app_ui
def server(i: Inputs, o: Outputs, session: Session):
# HTTP session headers are available in this context.
= session.http_conn.headers.get("Posit-Connect-User-Session-Token")
session_token = PositCredentialsStrategy(
posit_strategy =databricks_cli, user_session_token=session_token
local_strategy
)= Config(host=DATABRICKS_HOST_URL, credentials_strategy=posit_strategy)
cfg
@render.data_frame
def result():
= "SELECT * FROM samples.nyctaxi.trips LIMIT 10;"
query
with sql.connect(
=DATABRICKS_HOST,
server_hostname=SQL_HTTP_PATH,
http_path=posit_strategy.sql_credentials_provider(cfg),
credentials_provideras connection:
) with connection.cursor() as cursor:
cursor.execute(query)= cursor.fetchall()
rows = pd.DataFrame(rows, columns=[col[0] for col in cursor.description])
df return df
@render.text
def text():
= CurrentUserAPI(ApiClient(cfg)).me()
databricks_user_info return f"Hello, {databricks_user_info.display_name}!"
= App(app_ui, server) app
Methods
Name | Description |
---|---|
auth_type | |
sql_credentials_provider | The sql connector attempts to call the credentials provider w/o any args. |
auth_type
connect.external.databricks.PositCredentialsStrategy.auth_type()
sql_credentials_provider
connect.external.databricks.PositCredentialsStrategy.sql_credentials_provider(
*args
**kwargs
)
The sql connector attempts to call the credentials provider w/o any args.
The SQL client’s ExternalAuthProvider
is not compatible w/ the SDK’s implementation of CredentialsProvider
, so create a no-arg lambda that wraps the args defined by the real caller. This way we can pass in a databricks Config
object required by most of the SDK’s CredentialsProvider
implementations from where sql.connect
is called.
See Also
- https://github.com/databricks/databricks-sql-python/issues/148#issuecomment-2271561365
PositLocalContentCredentialsProvider
connect.external.databricks.PositLocalContentCredentialsProvider(
self
token_endpoint_url
client_id
client_secret )
CredentialsProvider
implementation which provides a fallback for local development using a client credentials flow.
There is an open issue against the Databricks CLI which prevents it from returning service principal access tokens. https://github.com/databricks/cli/issues/1939
Until the CLI issue is resolved, this CredentialsProvider implements the approach described in the Databricks documentation for manually generating a workspace-level access token using OAuth M2M authentication. Once it has acquired an access token, it returns it as a Bearer authorization header like other CredentialsProvider
implementations.
See Also
- https://docs.databricks.com/en/dev-tools/auth/oauth-m2m.html#manually-generate-a-workspace-level-access-token
PositLocalContentCredentialsStrategy
connect.external.databricks.PositLocalContentCredentialsStrategy(
self
token_endpoint_url
client_id
client_secret )
CredentialsStrategy
implementation which supports local development using OAuth M2M authentication against Databricks.
There is an open issue against the Databricks CLI which prevents it from returning service principal access tokens. https://github.com/databricks/cli/issues/1939
Until the CLI issue is resolved, this CredentialsStrategy provides a drop-in replacement as a local_strategy that can be used to develop applications which target Service Account OAuth integrations on Connect.
Examples
In the example below, the PositContentCredentialsStrategy
can be initialized anywhere that the Python process can read environment variables.
CLIENT_ID and CLIENT_SECRET are credentials associated with the Databricks service principal.
from posit.connect.external.databricks import (
PositContentCredentialsStrategy,
PositLocalContentCredentialsStrategy,
)
import pandas as pd
from databricks import sql
from databricks.sdk.core import ApiClient, Config
from databricks.sdk.service.iam import CurrentUserAPI
= "<REDACTED>"
DATABRICKS_HOST = f"https://{DATABRICKS_HOST}"
DATABRICKS_HOST_URL = "<REDACTED>"
SQL_HTTP_PATH = f"https://{DATABRICKS_HOST}/oidc/v1/token"
TOKEN_ENDPOINT_URL
= "<REDACTED>"
CLIENT_ID = "<REDACTED>"
CLIENT_SECRET
# Rather than relying on the Databricks CLI as a local strategy, we use
# PositLocalContentCredentialsStrategy as a drop-in replacement.
# Can be replaced with the Databricks CLI implementation when
# https://github.com/databricks/cli/issues/1939 is resolved.
= PositLocalContentCredentialsStrategy(
local_strategy =TOKEN_ENDPOINT_URL,
token_endpoint_url=CLIENT_ID,
client_id=CLIENT_SECRET,
client_secret
)
= PositContentCredentialsStrategy(local_strategy=local_strategy)
posit_strategy
= Config(host=DATABRICKS_HOST_URL, credentials_strategy=posit_strategy)
cfg
= CurrentUserAPI(ApiClient(cfg)).me()
databricks_user_info print(f"Hello, {databricks_user_info.display_name}!")
= "SELECT * FROM samples.nyctaxi.trips LIMIT 10;"
query with sql.connect(
=DATABRICKS_HOST,
server_hostname=SQL_HTTP_PATH,
http_path=posit_strategy.sql_credentials_provider(cfg),
credentials_provideras connection:
) with connection.cursor() as cursor:
cursor.execute(query)= cursor.fetchall()
rows print(pd.DataFrame([row.asDict() for row in rows]))
See Also
- https://docs.databricks.com/en/dev-tools/auth/oauth-m2m.html#manually-generate-a-workspace-level-access-token
Methods
Name | Description |
---|---|
auth_type | |
sql_credentials_provider |
auth_type
connect.external.databricks.PositLocalContentCredentialsStrategy.auth_type()
sql_credentials_provider
connect.external.databricks.PositLocalContentCredentialsStrategy.sql_credentials_provider(
*args
**kwargs
)