-
Notifications
You must be signed in to change notification settings - Fork 114
[PECOBLR-587] Azure Service Principal Credential Provider #621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5e2b792
basic setup
jprakash-db 05ab3e8
Nit
jprakash-db bef7ac6
working
jprakash-db 0877b6f
moved pyjwt to code dependency
jprakash-db 83d6a9e
nit
jprakash-db b21d015
nit
jprakash-db 6d85d19
nit
jprakash-db f518085
nit
jprakash-db 814d1cb
nit
jprakash-db ce4543c
nit
jprakash-db 42841f1
testing sdk
jprakash-db a377ce7
Refractor
jprakash-db bf5c565
logging
jprakash-db 3f7d476
nit
jprakash-db 601b08e
nit
jprakash-db 9a948eb
nit
jprakash-db 959d137
nit
jprakash-db 66a4244
nit
jprakash-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from enum import Enum | ||
import logging | ||
from typing import Optional, List | ||
from urllib.parse import urlparse | ||
from databricks.sql.common.http import DatabricksHttpClient, HttpMethod | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class AuthType(Enum): | ||
DATABRICKS_OAUTH = "databricks-oauth" | ||
AZURE_OAUTH = "azure-oauth" | ||
AZURE_SP_M2M = "azure-sp-m2m" | ||
|
||
|
||
class AzureAppId(Enum): | ||
DEV = (".dev.azuredatabricks.net", "62a912ac-b58e-4c1d-89ea-b2dbfc7358fc") | ||
STAGING = (".staging.azuredatabricks.net", "4a67d088-db5c-48f1-9ff2-0aace800ae68") | ||
PROD = (".azuredatabricks.net", "2ff814a6-3304-4ab8-85cb-cd0e6f879c1d") | ||
|
||
|
||
class ClientContext: | ||
def __init__( | ||
self, | ||
hostname: str, | ||
access_token: Optional[str] = None, | ||
auth_type: Optional[str] = None, | ||
oauth_scopes: Optional[List[str]] = None, | ||
oauth_client_id: Optional[str] = None, | ||
azure_client_id: Optional[str] = None, | ||
azure_client_secret: Optional[str] = None, | ||
azure_tenant_id: Optional[str] = None, | ||
azure_workspace_resource_id: Optional[str] = None, | ||
oauth_redirect_port_range: Optional[List[int]] = None, | ||
use_cert_as_auth: Optional[str] = None, | ||
tls_client_cert_file: Optional[str] = None, | ||
oauth_persistence=None, | ||
credentials_provider=None, | ||
): | ||
self.hostname = hostname | ||
self.access_token = access_token | ||
self.auth_type = auth_type | ||
self.oauth_scopes = oauth_scopes | ||
self.oauth_client_id = oauth_client_id | ||
self.azure_client_id = azure_client_id | ||
self.azure_client_secret = azure_client_secret | ||
self.azure_tenant_id = azure_tenant_id | ||
self.azure_workspace_resource_id = azure_workspace_resource_id | ||
self.oauth_redirect_port_range = oauth_redirect_port_range | ||
self.use_cert_as_auth = use_cert_as_auth | ||
self.tls_client_cert_file = tls_client_cert_file | ||
self.oauth_persistence = oauth_persistence | ||
self.credentials_provider = credentials_provider | ||
|
||
|
||
def get_effective_azure_login_app_id(hostname) -> str: | ||
""" | ||
Get the effective Azure login app ID for a given hostname. | ||
This function determines the appropriate Azure login app ID based on the hostname. | ||
If the hostname does not match any of these domains, it returns the default Databricks resource ID. | ||
|
||
""" | ||
for azure_app_id in AzureAppId: | ||
domain, app_id = azure_app_id.value | ||
if domain in hostname: | ||
return app_id | ||
|
||
# default databricks resource id | ||
return AzureAppId.PROD.value[1] | ||
|
||
|
||
def get_azure_tenant_id_from_host(host: str, http_client=None) -> str: | ||
""" | ||
Load the Azure tenant ID from the Azure Databricks login page. | ||
jprakash-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This function retrieves the Azure tenant ID by making a request to the Databricks | ||
Azure Active Directory (AAD) authentication endpoint. The endpoint redirects to | ||
the Azure login page, and the tenant ID is extracted from the redirect URL. | ||
""" | ||
|
||
if http_client is None: | ||
http_client = DatabricksHttpClient.get_instance() | ||
|
||
login_url = f"{host}/aad/auth" | ||
logger.debug("Loading tenant ID from %s", login_url) | ||
with http_client.execute(HttpMethod.GET, login_url, allow_redirects=False) as resp: | ||
if resp.status_code // 100 != 3: | ||
raise ValueError( | ||
f"Failed to get tenant ID from {login_url}: expected status code 3xx, got {resp.status_code}" | ||
) | ||
entra_id_endpoint = resp.headers.get("Location") | ||
if entra_id_endpoint is None: | ||
raise ValueError(f"No Location header in response from {login_url}") | ||
# The Location header has the following form: https://login.microsoftonline.com/<tenant-id>/oauth2/authorize?... | ||
# The domain may change depending on the Azure cloud (e.g. login.microsoftonline.us for US Government cloud). | ||
url = urlparse(entra_id_endpoint) | ||
path_segments = url.path.split("/") | ||
if len(path_segments) < 2: | ||
raise ValueError(f"Invalid path in Location header: {url.path}") | ||
return path_segments[1] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.