|
| 1 | +from enum import Enum |
| 2 | +import logging |
| 3 | +from typing import Optional, List |
| 4 | +from urllib.parse import urlparse |
| 5 | +from databricks.sql.common.http import DatabricksHttpClient, HttpMethod |
| 6 | + |
| 7 | +logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +class AuthType(Enum): |
| 11 | + DATABRICKS_OAUTH = "databricks-oauth" |
| 12 | + AZURE_OAUTH = "azure-oauth" |
| 13 | + AZURE_SP_M2M = "azure-sp-m2m" |
| 14 | + |
| 15 | + |
| 16 | +class AzureAppId(Enum): |
| 17 | + DEV = (".dev.azuredatabricks.net", "62a912ac-b58e-4c1d-89ea-b2dbfc7358fc") |
| 18 | + STAGING = (".staging.azuredatabricks.net", "4a67d088-db5c-48f1-9ff2-0aace800ae68") |
| 19 | + PROD = (".azuredatabricks.net", "2ff814a6-3304-4ab8-85cb-cd0e6f879c1d") |
| 20 | + |
| 21 | + |
| 22 | +class ClientContext: |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + hostname: str, |
| 26 | + access_token: Optional[str] = None, |
| 27 | + auth_type: Optional[str] = None, |
| 28 | + oauth_scopes: Optional[List[str]] = None, |
| 29 | + oauth_client_id: Optional[str] = None, |
| 30 | + azure_client_id: Optional[str] = None, |
| 31 | + azure_client_secret: Optional[str] = None, |
| 32 | + azure_tenant_id: Optional[str] = None, |
| 33 | + azure_workspace_resource_id: Optional[str] = None, |
| 34 | + oauth_redirect_port_range: Optional[List[int]] = None, |
| 35 | + use_cert_as_auth: Optional[str] = None, |
| 36 | + tls_client_cert_file: Optional[str] = None, |
| 37 | + oauth_persistence=None, |
| 38 | + credentials_provider=None, |
| 39 | + ): |
| 40 | + self.hostname = hostname |
| 41 | + self.access_token = access_token |
| 42 | + self.auth_type = auth_type |
| 43 | + self.oauth_scopes = oauth_scopes |
| 44 | + self.oauth_client_id = oauth_client_id |
| 45 | + self.azure_client_id = azure_client_id |
| 46 | + self.azure_client_secret = azure_client_secret |
| 47 | + self.azure_tenant_id = azure_tenant_id |
| 48 | + self.azure_workspace_resource_id = azure_workspace_resource_id |
| 49 | + self.oauth_redirect_port_range = oauth_redirect_port_range |
| 50 | + self.use_cert_as_auth = use_cert_as_auth |
| 51 | + self.tls_client_cert_file = tls_client_cert_file |
| 52 | + self.oauth_persistence = oauth_persistence |
| 53 | + self.credentials_provider = credentials_provider |
| 54 | + |
| 55 | + |
| 56 | +def get_effective_azure_login_app_id(hostname) -> str: |
| 57 | + """ |
| 58 | + Get the effective Azure login app ID for a given hostname. |
| 59 | + This function determines the appropriate Azure login app ID based on the hostname. |
| 60 | + If the hostname does not match any of these domains, it returns the default Databricks resource ID. |
| 61 | +
|
| 62 | + """ |
| 63 | + for azure_app_id in AzureAppId: |
| 64 | + domain, app_id = azure_app_id.value |
| 65 | + if domain in hostname: |
| 66 | + return app_id |
| 67 | + |
| 68 | + # default databricks resource id |
| 69 | + return AzureAppId.PROD.value[1] |
| 70 | + |
| 71 | + |
| 72 | +def get_azure_tenant_id_from_host(host: str, http_client=None) -> str: |
| 73 | + """ |
| 74 | + Load the Azure tenant ID from the Azure Databricks login page. |
| 75 | +
|
| 76 | + This function retrieves the Azure tenant ID by making a request to the Databricks |
| 77 | + Azure Active Directory (AAD) authentication endpoint. The endpoint redirects to |
| 78 | + the Azure login page, and the tenant ID is extracted from the redirect URL. |
| 79 | + """ |
| 80 | + |
| 81 | + if http_client is None: |
| 82 | + http_client = DatabricksHttpClient.get_instance() |
| 83 | + |
| 84 | + login_url = f"{host}/aad/auth" |
| 85 | + logger.debug("Loading tenant ID from %s", login_url) |
| 86 | + with http_client.execute(HttpMethod.GET, login_url, allow_redirects=False) as resp: |
| 87 | + if resp.status_code // 100 != 3: |
| 88 | + raise ValueError( |
| 89 | + f"Failed to get tenant ID from {login_url}: expected status code 3xx, got {resp.status_code}" |
| 90 | + ) |
| 91 | + entra_id_endpoint = resp.headers.get("Location") |
| 92 | + if entra_id_endpoint is None: |
| 93 | + raise ValueError(f"No Location header in response from {login_url}") |
| 94 | + # The Location header has the following form: https://login.microsoftonline.com/<tenant-id>/oauth2/authorize?... |
| 95 | + # The domain may change depending on the Azure cloud (e.g. login.microsoftonline.us for US Government cloud). |
| 96 | + url = urlparse(entra_id_endpoint) |
| 97 | + path_segments = url.path.split("/") |
| 98 | + if len(path_segments) < 2: |
| 99 | + raise ValueError(f"Invalid path in Location header: {url.path}") |
| 100 | + return path_segments[1] |
0 commit comments