-
Notifications
You must be signed in to change notification settings - Fork 126
Add Token Federation Support for Databricks SQL Python Driver #691
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 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
02d16d2
token federation for python driver
madhav-db ae5ee50
address comment
madhav-db 63a7f82
address comments
madhav-db 1e604a1
lint
madhav-db b79af03
lint fix
madhav-db 010b58e
nit
madhav-db 3398ce6
change import
madhav-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
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,117 @@ | ||
| import logging | ||
| import jwt | ||
| from datetime import datetime, timedelta | ||
| from typing import Optional, Dict, Tuple | ||
| from urllib.parse import urlparse | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def parse_hostname(hostname: str) -> str: | ||
| """ | ||
| Normalize the hostname to include scheme and trailing slash. | ||
|
|
||
| Args: | ||
| hostname: The hostname to normalize | ||
|
|
||
| Returns: | ||
| Normalized hostname with scheme and trailing slash | ||
| """ | ||
| if not hostname.startswith("http://") and not hostname.startswith("https://"): | ||
| hostname = f"https://{hostname}" | ||
| if not hostname.endswith("/"): | ||
| hostname = f"{hostname}/" | ||
| return hostname | ||
|
|
||
|
|
||
| def decode_token(access_token: str) -> Optional[Dict]: | ||
| """ | ||
| Decode a JWT token without verification to extract claims. | ||
|
|
||
| Args: | ||
| access_token: The JWT access token to decode | ||
|
|
||
| Returns: | ||
| Decoded token claims or None if decoding fails | ||
| """ | ||
| try: | ||
| return jwt.decode(access_token, options={"verify_signature": False}) | ||
| except Exception as e: | ||
| logger.debug("Failed to decode JWT token: %s", e) | ||
| return None | ||
|
|
||
|
|
||
| def is_same_host(url1: str, url2: str) -> bool: | ||
| """ | ||
| Check if two URLs have the same host. | ||
|
|
||
| Args: | ||
| url1: First URL | ||
| url2: Second URL | ||
|
|
||
| Returns: | ||
| True if hosts are the same, False otherwise | ||
| """ | ||
| try: | ||
| host1 = urlparse(url1).netloc | ||
| host2 = urlparse(url2).netloc | ||
| # Handle port differences (e.g., example.com vs example.com:443) | ||
| host1_without_port = host1.split(":")[0] | ||
| host2_without_port = host2.split(":")[0] | ||
| return host1_without_port == host2_without_port | ||
| except Exception as e: | ||
| logger.debug("Failed to parse URLs: %s", e) | ||
| return False | ||
|
|
||
|
|
||
| class Token: | ||
| """ | ||
| Represents an OAuth token with expiration management. | ||
| """ | ||
|
|
||
| def __init__(self, access_token: str, token_type: str = "Bearer"): | ||
| """ | ||
| Initialize a token. | ||
|
|
||
| Args: | ||
| access_token: The access token string | ||
| token_type: The token type (default: Bearer) | ||
| """ | ||
| self.access_token = access_token | ||
| self.token_type = token_type | ||
| self.expiry_time = self._calculate_expiry() | ||
|
|
||
| def _calculate_expiry(self) -> datetime: | ||
| """ | ||
| Calculate the token expiry time from JWT claims. | ||
|
|
||
| Returns: | ||
| The token expiry datetime | ||
| """ | ||
| decoded = decode_token(self.access_token) | ||
| if decoded and "exp" in decoded: | ||
| # Use JWT exp claim with 1 minute buffer | ||
| return datetime.fromtimestamp(decoded["exp"]) - timedelta(minutes=1) | ||
| # Default to 1 hour if no expiry info | ||
| return datetime.now() + timedelta(hours=1) | ||
|
|
||
| def is_expired(self) -> bool: | ||
| """ | ||
| Check if the token is expired. | ||
|
|
||
| Returns: | ||
| True if token is expired, False otherwise | ||
| """ | ||
| return datetime.now() >= self.expiry_time | ||
|
|
||
| def to_dict(self) -> Dict[str, str]: | ||
| """ | ||
| Convert token to dictionary format. | ||
|
|
||
| Returns: | ||
| Dictionary with access_token and token_type | ||
| """ | ||
| return { | ||
| "access_token": self.access_token, | ||
| "token_type": self.token_type, | ||
| } | ||
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,153 @@ | ||
| import logging | ||
| import json | ||
| from typing import Optional, Dict, Tuple | ||
| from urllib.parse import urlencode | ||
|
|
||
| from databricks.sql.auth.authenticators import AuthProvider | ||
| from databricks.sql.auth.auth_utils import ( | ||
| Token, | ||
| parse_hostname, | ||
| decode_token, | ||
| is_same_host, | ||
| ) | ||
| from databricks.sql.common.http import HttpMethod | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class TokenFederationProvider(AuthProvider): | ||
| """ | ||
| Implementation of Token Federation for Databricks SQL Python driver. | ||
|
|
||
| This provider exchanges third-party access tokens for Databricks in-house tokens | ||
| when the token issuer is different from the Databricks host. | ||
| """ | ||
|
|
||
| TOKEN_EXCHANGE_ENDPOINT = "/oidc/v1/token" | ||
| TOKEN_EXCHANGE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:token-exchange" | ||
| TOKEN_EXCHANGE_SUBJECT_TYPE = "urn:ietf:params:oauth:token-type:jwt" | ||
|
|
||
| def __init__( | ||
| self, | ||
| hostname: str, | ||
| external_provider: AuthProvider, | ||
| http_client, | ||
| identity_federation_client_id: Optional[str] = None, | ||
| ): | ||
| """ | ||
| Initialize the Token Federation Provider. | ||
|
|
||
| Args: | ||
| hostname: The Databricks workspace hostname | ||
| external_provider: The external authentication provider | ||
| http_client: HTTP client for making requests (required) | ||
| identity_federation_client_id: Optional client ID for token federation | ||
| """ | ||
| if not http_client: | ||
| raise ValueError("http_client is required for TokenFederationProvider") | ||
|
|
||
| self.hostname = parse_hostname(hostname) | ||
| self.external_provider = external_provider | ||
| self.http_client = http_client | ||
| self.identity_federation_client_id = identity_federation_client_id | ||
|
|
||
| self._cached_token: Optional[Token] = None | ||
| self._external_headers: Dict[str, str] = {} | ||
|
|
||
| def add_headers(self, request_headers: Dict[str, str]): | ||
| """Add authentication headers to the request.""" | ||
|
|
||
| if self._cached_token and not self._cached_token.is_expired(): | ||
| request_headers[ | ||
| "Authorization" | ||
| ] = f"{self._cached_token.token_type} {self._cached_token.access_token}" | ||
| return | ||
|
|
||
| # Get the external headers first to check if we need token federation | ||
| self._external_headers = {} | ||
| self.external_provider.add_headers(self._external_headers) | ||
|
|
||
| # If no Authorization header from external provider, pass through all headers | ||
| if "Authorization" not in self._external_headers: | ||
| request_headers.update(self._external_headers) | ||
| return | ||
|
|
||
| token = self._get_token() | ||
| request_headers["Authorization"] = f"{token.token_type} {token.access_token}" | ||
|
|
||
| def _get_token(self) -> Token: | ||
| """Get or refresh the authentication token.""" | ||
| # Check if cached token is still valid | ||
| if self._cached_token and not self._cached_token.is_expired(): | ||
| return self._cached_token | ||
|
|
||
| # Extract token from already-fetched headers | ||
| auth_header = self._external_headers.get("Authorization", "") | ||
| token_type, access_token = self._extract_token_from_header(auth_header) | ||
|
|
||
| # Check if token exchange is needed | ||
| if self._should_exchange_token(access_token): | ||
| try: | ||
| token = self._exchange_token(access_token) | ||
| self._cached_token = token | ||
| return token | ||
| except Exception as e: | ||
| logger.warning("Token exchange failed, using external token: %s", e) | ||
|
|
||
| # Use external token directly | ||
| token = Token(access_token, token_type) | ||
| self._cached_token = token | ||
| return token | ||
|
|
||
| def _should_exchange_token(self, access_token: str) -> bool: | ||
| """Check if the token should be exchanged based on issuer.""" | ||
| decoded = decode_token(access_token) | ||
| if not decoded: | ||
| return False | ||
|
|
||
| issuer = decoded.get("iss", "") | ||
| # Check if issuer host is different from Databricks host | ||
| return not is_same_host(issuer, self.hostname) | ||
|
|
||
| def _exchange_token(self, access_token: str) -> Token: | ||
| """Exchange the external token for a Databricks token.""" | ||
| token_url = f"{self.hostname.rstrip('/')}{self.TOKEN_EXCHANGE_ENDPOINT}" | ||
|
|
||
| data = { | ||
| "grant_type": self.TOKEN_EXCHANGE_GRANT_TYPE, | ||
| "subject_token": access_token, | ||
| "subject_token_type": self.TOKEN_EXCHANGE_SUBJECT_TYPE, | ||
| "scope": "sql", | ||
| "return_original_token_if_authenticated": "true", | ||
| } | ||
|
|
||
| if self.identity_federation_client_id: | ||
| data["client_id"] = self.identity_federation_client_id | ||
|
|
||
| headers = { | ||
| "Content-Type": "application/x-www-form-urlencoded", | ||
| "Accept": "*/*", | ||
| } | ||
|
|
||
| body = urlencode(data) | ||
|
|
||
| response = self.http_client.request( | ||
| HttpMethod.POST, url=token_url, body=body, headers=headers | ||
| ) | ||
|
|
||
| token_response = json.loads(response.data.decode()) | ||
|
|
||
| return Token( | ||
| token_response["access_token"], token_response.get("token_type", "Bearer") | ||
| ) | ||
|
|
||
| def _extract_token_from_header(self, auth_header: str) -> Tuple[str, str]: | ||
| """Extract token type and access token from Authorization header.""" | ||
| if not auth_header: | ||
| raise ValueError("Authorization header is missing") | ||
|
|
||
| parts = auth_header.split(" ", 1) | ||
| if len(parts) != 2: | ||
| raise ValueError("Invalid Authorization header format") | ||
|
|
||
| return parts[0], parts[1] |
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
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.