|
1 | | -"""Importing the connector must not import requests or the OAuth client libraries. |
| 1 | +"""Importing the connector must not import the Python HTTP and OAuth client stack. |
2 | 2 |
|
3 | | -They are only needed by the auth flows and HTTP paths that use them. Each check runs |
4 | | -in a fresh interpreter because ``sys.modules`` is process-global. |
| 3 | +requests, urllib3, oauthlib, jwt and pybreaker are only needed by the auth flows, |
| 4 | +the Python HTTP client and telemetry export that use them; a kernel connection uses |
| 5 | +none of them. Each check runs in a fresh interpreter because ``sys.modules`` is |
| 6 | +process-global. |
5 | 7 | """ |
6 | 8 |
|
7 | 9 | import subprocess |
|
17 | 19 | "databricks.sql", |
18 | 20 | "databricks.sql.client", |
19 | 21 | "databricks.sql.session", |
| 22 | + "databricks.sql.auth.auth", |
20 | 23 | "databricks.sql.auth.authenticators", |
21 | 24 | "databricks.sql.common.http", |
| 25 | + "databricks.sql.cloudfetch.downloader", |
| 26 | + "databricks.sql.telemetry.telemetry_client", |
| 27 | + "databricks.sql.backend.kernel.client", |
22 | 28 | ] |
23 | | -UNWANTED = ["requests", "oauthlib"] |
| 29 | +UNWANTED = ["requests", "urllib3", "oauthlib", "jwt", "pybreaker"] |
24 | 30 |
|
25 | 31 |
|
26 | 32 | @pytest.mark.parametrize("module_name", MODULES) |
@@ -67,3 +73,37 @@ def test_oauth_provider_still_builds_its_manager(): |
67 | 73 | http_client=MagicMock(), |
68 | 74 | ) |
69 | 75 | manager.assert_called_once() |
| 76 | + |
| 77 | + |
| 78 | +def test_token_federation_check_does_not_import_it(): |
| 79 | + script = """ |
| 80 | + import sys |
| 81 | + from databricks.sql.auth.common import is_token_federation_provider |
| 82 | + from databricks.sql.auth.authenticators import AccessTokenAuthProvider |
| 83 | + assert not is_token_federation_provider(AccessTokenAuthProvider("t")) |
| 84 | + assert "databricks.sql.auth.token_federation" not in sys.modules |
| 85 | + """ |
| 86 | + proc = subprocess.run( |
| 87 | + [sys.executable, "-c", textwrap.dedent(script)], |
| 88 | + capture_output=True, |
| 89 | + text=True, |
| 90 | + ) |
| 91 | + assert proc.returncode == 0, proc.stderr |
| 92 | + |
| 93 | + |
| 94 | +def test_token_federation_check_matches_real_providers(): |
| 95 | + from unittest.mock import MagicMock |
| 96 | + |
| 97 | + from databricks.sql.auth.authenticators import AccessTokenAuthProvider |
| 98 | + from databricks.sql.auth.common import is_token_federation_provider |
| 99 | + from databricks.sql.auth.token_federation import TokenFederationProvider |
| 100 | + from databricks.sql.telemetry.models.enums import AuthMech |
| 101 | + from databricks.sql.telemetry.telemetry_client import TelemetryHelper |
| 102 | + |
| 103 | + wrapped = TokenFederationProvider( |
| 104 | + hostname="example.cloud.databricks.com", |
| 105 | + external_provider=AccessTokenAuthProvider("t"), |
| 106 | + http_client=MagicMock(), |
| 107 | + ) |
| 108 | + assert is_token_federation_provider(wrapped) |
| 109 | + assert TelemetryHelper.get_auth_mechanism(wrapped) == AuthMech.PAT |
0 commit comments