From 006eecfef597337f207ef89d76b9c30a67c43ddb Mon Sep 17 00:00:00 2001 From: Colin Date: Tue, 10 Dec 2024 20:08:01 -0800 Subject: [PATCH] Allow users to suppress urllib3 warnings --- .../unreleased/Features-20241211-095408.yaml | 7 +++++++ dbt/adapters/trino/connections.py | 12 ++++++++++++ tests/unit/test_adapter.py | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 .changes/unreleased/Features-20241211-095408.yaml diff --git a/.changes/unreleased/Features-20241211-095408.yaml b/.changes/unreleased/Features-20241211-095408.yaml new file mode 100644 index 00000000..19fadd18 --- /dev/null +++ b/.changes/unreleased/Features-20241211-095408.yaml @@ -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" diff --git a/dbt/adapters/trino/connections.py b/dbt/adapters/trino/connections.py index 86d7eeec..3b89aa20 100644 --- a/dbt/adapters/trino/connections.py +++ b/dbt/adapters/trino/connections.py @@ -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): @@ -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): @@ -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): @@ -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): @@ -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): @@ -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): @@ -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): @@ -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( diff --git a/tests/unit/test_adapter.py b/tests/unit/test_adapter.py index 40c5578a..c56802bd 100644 --- a/tests/unit/test_adapter.py +++ b/tests/unit/test_adapter.py @@ -180,6 +180,7 @@ def test_none_authentication(self): "exchange_compression": True, }, "timezone": "UTC", + "suppress_cert_warning": False, } ) credentials = connection.credentials @@ -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( @@ -209,6 +211,7 @@ def test_none_authentication_with_method(self): "exchange_compression": True, }, "timezone": "UTC", + "suppress_cert_warning": False, } ) credentials = connection.credentials @@ -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( @@ -237,6 +241,7 @@ def test_none_authentication_without_http_scheme(self): "exchange_compression": True, }, "timezone": "UTC", + "suppress_cert_warning": False, } ) credentials = connection.credentials @@ -246,6 +251,7 @@ def test_none_authentication_without_http_scheme(self): self.assertEqual(credentials.cert, True) 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")] @@ -269,6 +275,7 @@ def test_ldap_authentication(self): "exchange_compression": True, }, "timezone": "UTC", + "suppress_cert_warning": True, } ) credentials = connection.credentials @@ -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( @@ -300,6 +308,7 @@ def test_kerberos_authentication(self): "exchange_compression": True, }, "timezone": "UTC", + "suppress_cert_warning": False, } ) credentials = connection.credentials @@ -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( @@ -329,6 +339,7 @@ def test_certificate_authentication(self): "exchange_compression": True, }, "timezone": "UTC", + "suppress_cert_warning": False, } ) credentials = connection.credentials @@ -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( @@ -362,6 +374,7 @@ def test_jwt_authentication(self): "exchange_compression": True, }, "timezone": "UTC", + "suppress_cert_warning": False, } ) credentials = connection.credentials @@ -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( @@ -389,6 +403,7 @@ def test_oauth_authentication(self): "exchange_compression": True, }, "timezone": "UTC", + "suppress_cert_warning": False, } ) credentials = connection.credentials @@ -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( @@ -417,6 +433,7 @@ def test_oauth_console_authentication(self): "exchange_compression": True, }, "timezone": "UTC", + "suppress_cert_warning": False, } ) credentials = connection.credentials @@ -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):