Skip to content

Commit

Permalink
Allow users to suppress urllib3 warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-rogers-dbt authored and damian3031 committed Dec 11, 2024
1 parent ca86392 commit 51a67e7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Features-20241211-095408.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Features
body: Add support for suppressing urllib3 cert warnings from logs
time: 2024-12-11T09:54:08.828716-08:00
custom:
Author: colin-rogers-dbt
Issue: "459"
PR: "459"
12 changes: 12 additions & 0 deletions dbt/adapters/trino/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class TrinoNoneCredentials(TrinoCredentials):
prepared_statements_enabled: bool = PREPARED_STATEMENTS_ENABLED_DEFAULT
retries: Optional[int] = trino.constants.DEFAULT_MAX_ATTEMPTS
timezone: Optional[str] = None
suppress_cert_warning: Optional[bool] = None

@property
def method(self):
Expand All @@ -130,6 +131,7 @@ class TrinoCertificateCredentials(TrinoCredentials):
prepared_statements_enabled: bool = PREPARED_STATEMENTS_ENABLED_DEFAULT
retries: Optional[int] = trino.constants.DEFAULT_MAX_ATTEMPTS
timezone: Optional[str] = None
suppress_cert_warning: Optional[bool] = None

@property
def http_scheme(self):
Expand Down Expand Up @@ -160,6 +162,7 @@ class TrinoLdapCredentials(TrinoCredentials):
prepared_statements_enabled: bool = PREPARED_STATEMENTS_ENABLED_DEFAULT
retries: Optional[int] = trino.constants.DEFAULT_MAX_ATTEMPTS
timezone: Optional[str] = None
suppress_cert_warning: Optional[bool] = None

@property
def http_scheme(self):
Expand Down Expand Up @@ -195,6 +198,7 @@ class TrinoKerberosCredentials(TrinoCredentials):
prepared_statements_enabled: bool = PREPARED_STATEMENTS_ENABLED_DEFAULT
retries: Optional[int] = trino.constants.DEFAULT_MAX_ATTEMPTS
timezone: Optional[str] = None
suppress_cert_warning: Optional[bool] = None

@property
def http_scheme(self):
Expand Down Expand Up @@ -233,6 +237,7 @@ class TrinoJwtCredentials(TrinoCredentials):
prepared_statements_enabled: bool = PREPARED_STATEMENTS_ENABLED_DEFAULT
retries: Optional[int] = trino.constants.DEFAULT_MAX_ATTEMPTS
timezone: Optional[str] = None
suppress_cert_warning: Optional[bool] = None

@property
def http_scheme(self):
Expand Down Expand Up @@ -262,6 +267,7 @@ class TrinoOauthCredentials(TrinoCredentials):
OAUTH = trino.auth.OAuth2Authentication(
redirect_auth_url_handler=trino.auth.WebBrowserRedirectHandler()
)
suppress_cert_warning: Optional[bool] = None

@property
def http_scheme(self):
Expand Down Expand Up @@ -291,6 +297,7 @@ class TrinoOauthConsoleCredentials(TrinoCredentials):
OAUTH = trino.auth.OAuth2Authentication(
redirect_auth_url_handler=trino.auth.ConsoleRedirectHandler()
)
suppress_cert_warning: Optional[bool] = None

@property
def http_scheme(self):
Expand Down Expand Up @@ -478,6 +485,11 @@ def open(cls, connection):
if req_cert_val_flag:
credentials.cert = True

if credentials.suppress_cert_warning:
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# it's impossible for trino to fail here as 'connections' are actually
# just cursor factories.
trino_conn = trino.dbapi.connect(
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def test_none_authentication(self):
"exchange_compression": True,
},
"timezone": "UTC",
"suppress_cert_warning": False,
}
)
credentials = connection.credentials
Expand All @@ -189,6 +190,7 @@ def test_none_authentication(self):
self.assertEqual(credentials.cert, "/path/to/cert")
self.assertEqual(credentials.client_tags, ["dev", "none"])
self.assertEqual(credentials.timezone, "UTC")
self.assertEqual(credentials.suppress_cert_warning, False)

def test_none_authentication_with_method(self):
connection = self.acquire_connection_with_profile(
Expand All @@ -209,6 +211,7 @@ def test_none_authentication_with_method(self):
"exchange_compression": True,
},
"timezone": "UTC",
"suppress_cert_warning": False,
}
)
credentials = connection.credentials
Expand All @@ -218,6 +221,7 @@ def test_none_authentication_with_method(self):
self.assertEqual(credentials.cert, "/path/to/cert")
self.assertEqual(credentials.client_tags, ["dev", "none_with_method"])
self.assertEqual(credentials.timezone, "UTC")
self.assertEqual(credentials.suppress_cert_warning, False)

def test_none_authentication_without_http_scheme(self):
connection = self.acquire_connection_with_profile(
Expand All @@ -237,6 +241,7 @@ def test_none_authentication_without_http_scheme(self):
"exchange_compression": True,
},
"timezone": "UTC",
"suppress_cert_warning": False,
}
)
credentials = connection.credentials
Expand All @@ -246,6 +251,7 @@ def test_none_authentication_without_http_scheme(self):
self.assertEqual(credentials.cert, "/path/to/cert")
self.assertEqual(credentials.client_tags, ["dev", "without_http_scheme"])
self.assertEqual(credentials.timezone, "UTC")
self.assertEqual(credentials.suppress_cert_warning, False)

def test_ldap_authentication(self):
test_cases = [(False, "trino_user"), (True, "impersonated_user")]
Expand All @@ -269,6 +275,7 @@ def test_ldap_authentication(self):
"exchange_compression": True,
},
"timezone": "UTC",
"suppress_cert_warning": True,
}
)
credentials = connection.credentials
Expand All @@ -280,6 +287,7 @@ def test_ldap_authentication(self):
self.assertEqual(connection.handle.handle.user, expected_user)
self.assertEqual(credentials.client_tags, ["dev", "ldap"])
self.assertEqual(credentials.timezone, "UTC")
self.assertEqual(credentials.suppress_cert_warning, True)

def test_kerberos_authentication(self):
connection = self.acquire_connection_with_profile(
Expand All @@ -300,6 +308,7 @@ def test_kerberos_authentication(self):
"exchange_compression": True,
},
"timezone": "UTC",
"suppress_cert_warning": False,
}
)
credentials = connection.credentials
Expand All @@ -309,6 +318,7 @@ def test_kerberos_authentication(self):
self.assertEqual(credentials.cert, "/path/to/cert")
self.assertEqual(credentials.client_tags, ["dev", "kerberos"])
self.assertEqual(credentials.timezone, "UTC")
self.assertEqual(credentials.suppress_cert_warning, False)

def test_certificate_authentication(self):
connection = self.acquire_connection_with_profile(
Expand All @@ -329,6 +339,7 @@ def test_certificate_authentication(self):
"exchange_compression": True,
},
"timezone": "UTC",
"suppress_cert_warning": False,
}
)
credentials = connection.credentials
Expand All @@ -343,6 +354,7 @@ def test_certificate_authentication(self):
self.assertEqual(credentials.cert, "/path/to/cert")
self.assertEqual(credentials.client_tags, ["dev", "certificate"])
self.assertEqual(credentials.timezone, "UTC")
self.assertEqual(credentials.suppress_cert_warning, False)

def test_jwt_authentication(self):
connection = self.acquire_connection_with_profile(
Expand All @@ -362,6 +374,7 @@ def test_jwt_authentication(self):
"exchange_compression": True,
},
"timezone": "UTC",
"suppress_cert_warning": False,
}
)
credentials = connection.credentials
Expand All @@ -371,6 +384,7 @@ def test_jwt_authentication(self):
self.assertEqual(credentials.cert, "/path/to/cert")
self.assertEqual(credentials.client_tags, ["dev", "jwt"])
self.assertEqual(credentials.timezone, "UTC")
self.assertEqual(credentials.suppress_cert_warning, False)

def test_oauth_authentication(self):
connection = self.acquire_connection_with_profile(
Expand All @@ -389,6 +403,7 @@ def test_oauth_authentication(self):
"exchange_compression": True,
},
"timezone": "UTC",
"suppress_cert_warning": False,
}
)
credentials = connection.credentials
Expand All @@ -399,6 +414,7 @@ def test_oauth_authentication(self):
self.assertEqual(connection.credentials.prepared_statements_enabled, True)
self.assertEqual(credentials.client_tags, ["dev", "oauth"])
self.assertEqual(credentials.timezone, "UTC")
self.assertEqual(credentials.suppress_cert_warning, False)

def test_oauth_console_authentication(self):
connection = self.acquire_connection_with_profile(
Expand All @@ -417,6 +433,7 @@ def test_oauth_console_authentication(self):
"exchange_compression": True,
},
"timezone": "UTC",
"suppress_cert_warning": False,
}
)
credentials = connection.credentials
Expand All @@ -427,6 +444,7 @@ def test_oauth_console_authentication(self):
self.assertEqual(connection.credentials.prepared_statements_enabled, True)
self.assertEqual(credentials.client_tags, ["dev", "oauth_console"])
self.assertEqual(credentials.timezone, "UTC")
self.assertEqual(credentials.suppress_cert_warning, False)


class TestPreparedStatementsEnabled(TestCase):
Expand Down

0 comments on commit 51a67e7

Please sign in to comment.