-
Notifications
You must be signed in to change notification settings - Fork 118
[PECOBLR-727] Add kerberos support for proxy auth #675
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
+690
−98
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ab3410f
unify ssl proxy
vikrantpuppala 5895b57
unify ssl proxy
vikrantpuppala 65e89ce
simplify change
vikrantpuppala 2b228c3
add utils class
vikrantpuppala 4d54557
Allow per request proxy decision
vikrantpuppala 14651e9
Add kerberos auth support
vikrantpuppala 466bfed
update dependencies
vikrantpuppala 046501f
update dependencies
vikrantpuppala 9d6c565
update dependencies
vikrantpuppala cdc1643
update dependencies
vikrantpuppala e4c05f7
update dependencies
vikrantpuppala 74dffbe
update dependencies
vikrantpuppala 341d1c5
update dependencies
vikrantpuppala 7e34a35
fix mypy
vikrantpuppala 7e30d28
fix lint
vikrantpuppala 3fcd6b9
fix lint
vikrantpuppala 0e01443
lazy logging
vikrantpuppala 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
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 @@ | ||
import ssl | ||
import urllib.parse | ||
import urllib.request | ||
import logging | ||
from typing import Dict, Any, Optional, Tuple, Union | ||
|
||
from urllib3 import HTTPConnectionPool, HTTPSConnectionPool, ProxyManager | ||
from urllib3.util import make_headers | ||
|
||
from databricks.sql.auth.retry import DatabricksRetryPolicy | ||
from databricks.sql.types import SSLOptions | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def detect_and_parse_proxy( | ||
scheme: str, | ||
host: Optional[str], | ||
skip_bypass: bool = False, | ||
proxy_auth_method: Optional[str] = None, | ||
) -> Tuple[Optional[str], Optional[Dict[str, str]]]: | ||
""" | ||
Detect system proxy and return proxy URI and headers using standardized logic. | ||
Args: | ||
scheme: URL scheme (http/https) | ||
host: Target hostname (optional, only needed for bypass checking) | ||
skip_bypass: If True, skip proxy bypass checking and return proxy config if found | ||
proxy_auth_method: Authentication method ('basic', 'negotiate', or None) | ||
Returns: | ||
Tuple of (proxy_uri, proxy_headers) or (None, None) if no proxy | ||
""" | ||
try: | ||
# returns a dictionary of scheme -> proxy server URL mappings. | ||
# https://docs.python.org/3/library/urllib.request.html#urllib.request.getproxies | ||
proxy = urllib.request.getproxies().get(scheme) | ||
except (KeyError, AttributeError): | ||
# No proxy found or getproxies() failed - disable proxy | ||
proxy = None | ||
else: | ||
# Proxy found, but check if this host should bypass proxy (unless skipped) | ||
if not skip_bypass and host and urllib.request.proxy_bypass(host): | ||
proxy = None # Host bypasses proxy per system rules | ||
|
||
if not proxy: | ||
return None, None | ||
|
||
parsed_proxy = urllib.parse.urlparse(proxy) | ||
|
||
# Generate appropriate auth headers based on method | ||
if proxy_auth_method == "negotiate": | ||
proxy_headers = _generate_negotiate_headers(parsed_proxy.hostname) | ||
elif proxy_auth_method == "basic" or proxy_auth_method is None: | ||
# Default to basic if method not specified (backward compatibility) | ||
proxy_headers = create_basic_proxy_auth_headers(parsed_proxy) | ||
else: | ||
raise ValueError(f"Unsupported proxy_auth_method: {proxy_auth_method}") | ||
|
||
return proxy, proxy_headers | ||
|
||
|
||
def _generate_negotiate_headers( | ||
proxy_hostname: Optional[str], | ||
) -> Optional[Dict[str, str]]: | ||
"""Generate Kerberos/SPNEGO authentication headers""" | ||
try: | ||
from requests_kerberos import HTTPKerberosAuth | ||
|
||
logger.debug( | ||
vikrantpuppala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Attempting to generate Kerberos SPNEGO token for proxy: %s", proxy_hostname | ||
) | ||
auth = HTTPKerberosAuth() | ||
negotiate_details = auth.generate_request_header( | ||
None, proxy_hostname, is_preemptive=True | ||
) | ||
if negotiate_details: | ||
return {"proxy-authorization": negotiate_details} | ||
else: | ||
logger.debug("Unable to generate kerberos proxy auth headers") | ||
except Exception as e: | ||
logger.error("Error generating Kerberos proxy auth headers: %s", e) | ||
|
||
return None | ||
|
||
|
||
def create_basic_proxy_auth_headers(parsed_proxy) -> Optional[Dict[str, str]]: | ||
""" | ||
Create basic auth headers for proxy if credentials are provided. | ||
Args: | ||
parsed_proxy: Parsed proxy URL from urllib.parse.urlparse() | ||
Returns: | ||
Dictionary of proxy auth headers or None if no credentials | ||
""" | ||
if parsed_proxy is None or not parsed_proxy.username: | ||
return None | ||
ap = f"{urllib.parse.unquote(parsed_proxy.username)}:{urllib.parse.unquote(parsed_proxy.password)}" | ||
vikrantpuppala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return make_headers(proxy_basic_auth=ap) |
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.