2020
2121# Configure logging to see proxy activity
2222logging .basicConfig (
23- level = logging .INFO ,
24- format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
23+ level = logging .INFO , format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
2524)
2625
2726# Uncomment for detailed debugging (shows HTTP requests/responses)
2827# logging.getLogger("urllib3").setLevel(logging.DEBUG)
2928# logging.getLogger("urllib3.connectionpool").setLevel(logging.DEBUG)
3029
30+
3131def check_proxy_environment ():
3232 """Check if proxy environment variables are configured."""
33- proxy_vars = ['HTTP_PROXY' , 'HTTPS_PROXY' , 'http_proxy' , 'https_proxy' ]
34- configured_proxies = {var : os .environ .get (var ) for var in proxy_vars if os .environ .get (var )}
35-
33+ proxy_vars = ["HTTP_PROXY" , "HTTPS_PROXY" , "http_proxy" , "https_proxy" ]
34+ configured_proxies = {
35+ var : os .environ .get (var ) for var in proxy_vars if os .environ .get (var )
36+ }
37+
3638 if configured_proxies :
3739 print ("✓ Proxy environment variables found:" )
3840 for var , value in configured_proxies .items ():
3941 # Hide credentials in output for security
40- safe_value = value .split ('@' )[- 1 ] if '@' in value else value
42+ safe_value = value .split ("@" )[- 1 ] if "@" in value else value
4143 print (f" { var } : { safe_value } " )
4244 return True
4345 else :
4446 print ("⚠ No proxy environment variables found" )
4547 print (" Set HTTP_PROXY and/or HTTPS_PROXY if using a proxy" )
4648 return False
4749
50+
4851def test_connection (connection_params , test_name ):
4952 """Test a database connection with given parameters."""
5053 print (f"\n --- Testing { test_name } ---" )
51-
54+
5255 try :
5356 with sql .connect (** connection_params ) as connection :
5457 print ("✓ Successfully connected!" )
55-
58+
5659 with connection .cursor () as cursor :
5760 # Test basic query
58- cursor .execute ("SELECT current_user() as user, current_database() as database" )
61+ cursor .execute (
62+ "SELECT current_user() as user, current_database() as database"
63+ )
5964 result = cursor .fetchone ()
6065 print (f"✓ Connected as user: { result .user } " )
6166 print (f"✓ Default database: { result .database } " )
62-
67+
6368 # Test a simple computation
6469 cursor .execute ("SELECT 1 + 1 as result" )
6570 result = cursor .fetchone ()
6671 print (f"✓ Query result: 1 + 1 = { result .result } " )
67-
72+
6873 return True
69-
74+
7075 except Exception as e :
7176 print (f"✗ Connection failed: { e } " )
7277 return False
7378
79+
7480def main ():
7581 print ("Databricks SQL Connector - Proxy Authentication Examples" )
7682 print ("=" * 60 )
77-
83+
7884 # Check proxy configuration
7985 has_proxy = check_proxy_environment ()
80-
86+
8187 # Get Databricks connection parameters
82- server_hostname = os .environ .get (' DATABRICKS_SERVER_HOSTNAME' )
83- http_path = os .environ .get (' DATABRICKS_HTTP_PATH' )
84- access_token = os .environ .get (' DATABRICKS_TOKEN' )
85-
88+ server_hostname = os .environ .get (" DATABRICKS_SERVER_HOSTNAME" )
89+ http_path = os .environ .get (" DATABRICKS_HTTP_PATH" )
90+ access_token = os .environ .get (" DATABRICKS_TOKEN" )
91+
8692 if not all ([server_hostname , http_path , access_token ]):
8793 print ("\n ✗ Missing required environment variables:" )
8894 print (" DATABRICKS_SERVER_HOSTNAME" )
89- print (" DATABRICKS_HTTP_PATH" )
95+ print (" DATABRICKS_HTTP_PATH" )
9096 print (" DATABRICKS_TOKEN" )
9197 return 1
92-
98+
9399 print (f"\n Connecting to: { server_hostname } " )
94-
100+
95101 # Base connection parameters
96102 base_params = {
97- ' server_hostname' : server_hostname ,
98- ' http_path' : http_path ,
99- ' access_token' : access_token
103+ " server_hostname" : server_hostname ,
104+ " http_path" : http_path ,
105+ " access_token" : access_token ,
100106 }
101-
107+
102108 success_count = 0
103109 total_tests = 0
104-
110+
105111 # Test 1: Default proxy behavior (no _proxy_auth_method specified)
106112 # This uses basic auth if credentials are in proxy URL, otherwise no auth
107- print ("\n " + "=" * 60 )
113+ print ("\n " + "=" * 60 )
108114 print ("Test 1: Default Proxy Behavior" )
109115 print ("Uses basic authentication if credentials are in proxy URL" )
110116 total_tests += 1
111117 if test_connection (base_params , "Default Proxy Behavior" ):
112118 success_count += 1
113-
119+
114120 # Test 2: Explicit basic authentication
115- print ("\n " + "=" * 60 )
121+ print ("\n " + "=" * 60 )
116122 print ("Test 2: Explicit Basic Authentication" )
117123 print ("Explicitly requests basic authentication (same as default)" )
118124 total_tests += 1
119125 basic_params = base_params .copy ()
120- basic_params [' _proxy_auth_method' ] = ' basic'
126+ basic_params [" _proxy_auth_method" ] = " basic"
121127 if test_connection (basic_params , "Basic Proxy Authentication" ):
122128 success_count += 1
123-
129+
124130 # Test 3: Kerberos/Negotiate authentication
125- print ("\n " + "=" * 60 )
131+ print ("\n " + "=" * 60 )
126132 print ("Test 3: Kerberos/Negotiate Authentication" )
127133 print ("Uses Kerberos tickets for proxy authentication" )
128134 print ("Note: Requires valid Kerberos tickets (run 'kinit' first)" )
129135 total_tests += 1
130136 kerberos_params = base_params .copy ()
131- kerberos_params [' _proxy_auth_method' ] = ' negotiate'
137+ kerberos_params [" _proxy_auth_method" ] = " negotiate"
132138 if test_connection (kerberos_params , "Kerberos Proxy Authentication" ):
133139 success_count += 1
134-
140+
135141 # Summary
136- print (f"\n { '=' * 60 } " )
142+ print (f"\n { '=' * 60 } " )
137143 print (f"Summary: { success_count } /{ total_tests } tests passed" )
138-
144+
139145 if success_count == total_tests :
140146 print ("✓ All proxy authentication methods working!" )
141147 return 0
@@ -149,5 +155,6 @@ def main():
149155 print ("Consider checking your proxy configuration" )
150156 return 1
151157
158+
152159if __name__ == "__main__" :
153160 exit (main ())
0 commit comments