Repository navigation
Jwt access token auth #747
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
base: main
Are you sure you want to change the base?
Changes from all commits
f1aa190
8789d98
0b41044
96228a5
42e25a8
9dcf52f
e0174c7
bbd7401
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,7 +38,12 @@ | |
| from dbt.adapters.sql import SQLAdapter | ||
| from dbt_common.contracts.constraints import ConstraintType, ModelLevelConstraint | ||
| from dbt_common.events.functions import warn_or_error | ||
| from dbt_common.exceptions import DbtInternalError, DbtRuntimeError, NotImplementedError | ||
| from dbt_common.exceptions import ( | ||
| DbtConfigError, | ||
| DbtInternalError, | ||
| DbtRuntimeError, | ||
| NotImplementedError, | ||
| ) | ||
| from dbt_common.utils import filter_null_values | ||
|
|
||
| if TYPE_CHECKING: | ||
|
|
@@ -630,6 +635,11 @@ def get_credentials(self, connection_overrides) -> Dict: | |
| conn = self.connections.get_if_exists() | ||
| if conn is None or conn.credentials is None: | ||
| return dict() | ||
| if conn.credentials.uses_token_auth and 'user' not in connection_overrides: | ||
| raise DbtConfigError( | ||
| 'Dictionary sources cannot authenticate with a JWT; set user/password for the ' | ||
| 'dictionary via the connection_overrides config.' | ||
| ) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dictionary JWT guard allows empty userMedium Severity The new dictionary JWT check only tests whether Additional Locations (1)Reviewed by Cursor Bugbot for commit 96228a5. Configure here. |
||
| credentials = { | ||
| 'user': conn.credentials.user, | ||
| 'password': conn.credentials.password, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,7 @@ dependencies = [ | |
| "agate>=1.0,<2.0", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @canbekley, what is this used for?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these are unrelated changes, not introduced with this PR but with #776. we've set them because there are direct imports from those packages |
||
| # add dbt-core to ensure backwards compatibility of installation, this is not a functional dependency | ||
| "dbt-core>=1.9,<2.0", | ||
| "clickhouse-connect>=0.10.0", | ||
| "clickhouse-connect>=1.2.0", | ||
| "clickhouse-driver>=0.2.10" | ||
| ] | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| """JWT authentication via `access_token` and `access_token_provider`. | ||
|
|
||
| ClickHouse only validates JWTs in ClickHouse Cloud, so the end-to-end runs need | ||
| `DBT_CH_TEST_CLOUD=true` and a valid token in `DBT_CH_TEST_ACCESS_TOKEN`. Against OSS | ||
| ClickHouse we can still verify the wiring: the server must see a `Bearer` header | ||
| (rather than a Basic auth fallback) and reject it as an unsupported scheme, and the | ||
| provider must be called a second time after that rejection. | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| import pytest | ||
| from dbt.adapters.clickhouse.credentials import ClickHouseCredentials | ||
| from dbt.adapters.clickhouse.httpclient import ChHttpClient | ||
| from dbt.tests.util import run_dbt | ||
|
|
||
| from tests.integration.adapter.clickhouse import token_provider_stub | ||
|
|
||
| _CLOUD = os.environ.get('DBT_CH_TEST_CLOUD', '').lower() in ('1', 'true', 'yes') | ||
| _TOKEN = os.environ.get('DBT_CH_TEST_ACCESS_TOKEN') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't set |
||
| _PROVIDER = f'{token_provider_stub.__name__}:rejected_then_real' | ||
|
|
||
|
|
||
| def _credentials(test_config, **auth): | ||
| return ClickHouseCredentials( | ||
| driver='http', | ||
| host=test_config['host'], | ||
| port=test_config['port'], | ||
| secure=test_config['secure'], | ||
| schema='default', | ||
| **auth, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.skipif(_CLOUD, reason='OSS ClickHouse only; Cloud validates JWTs') | ||
| class TestAccessTokenRejectedByOSS: | ||
| @pytest.fixture(autouse=True) | ||
| def _http_only(self, test_config): | ||
| if test_config['driver'] != 'http': | ||
| pytest.skip('JWT authentication is only supported by the http driver') | ||
|
|
||
| def test_token_sent_as_bearer(self, test_config): | ||
| with pytest.raises(Exception, match='(?i)bearer'): | ||
| ChHttpClient(_credentials(test_config, access_token='not-a-real-jwt')) | ||
|
|
||
| def test_provider_called_again_after_rejection(self, test_config): | ||
| token_provider_stub.calls.clear() | ||
| with pytest.raises(Exception, match='(?i)bearer'): | ||
| ChHttpClient(_credentials(test_config, access_token_provider=_PROVIDER)) | ||
| # initial token, then one refresh after the server rejected it | ||
| assert len(token_provider_stub.calls) == 2 | ||
|
|
||
|
|
||
| def _jwt_target(dbt_profile_target, **auth): | ||
| if not (_CLOUD and _TOKEN): | ||
| pytest.skip('requires DBT_CH_TEST_CLOUD=true and DBT_CH_TEST_ACCESS_TOKEN') | ||
| if dbt_profile_target['driver'] != 'http': | ||
| pytest.skip('JWT authentication is only supported by the http driver') | ||
| target = {k: v for k, v in dbt_profile_target.items() if k not in ('user', 'password')} | ||
| target.update(auth) | ||
| return target | ||
|
|
||
|
|
||
| class TestAccessTokenCloud: | ||
| @pytest.fixture(scope="class") | ||
| def dbt_profile_target(self, dbt_profile_target): | ||
| return _jwt_target(dbt_profile_target, access_token=_TOKEN) | ||
|
|
||
| @pytest.fixture(scope="class") | ||
| def models(self): | ||
| return {'jwt_model.sql': 'select 1 as id'} | ||
|
|
||
| def test_run_with_access_token(self, project): | ||
| run_dbt(['debug']) | ||
| run_dbt(['run']) | ||
| assert project.run_sql('select id from jwt_model', fetch='one')[0] == 1 | ||
| assert project.run_sql('select currentUser()', fetch='one')[0].startswith('JWT::') | ||
|
|
||
|
|
||
| class TestAccessTokenProviderCloud: | ||
| @pytest.fixture(scope="class") | ||
| def dbt_profile_target(self, dbt_profile_target): | ||
| token_provider_stub.calls.clear() | ||
| return _jwt_target(dbt_profile_target, access_token_provider=_PROVIDER) | ||
|
|
||
| @pytest.fixture(scope="class") | ||
| def models(self): | ||
| return {'jwt_model.sql': 'select 1 as id'} | ||
|
|
||
| def test_run_with_refreshed_token(self, project): | ||
| run_dbt(['run']) | ||
| assert project.run_sql('select id from jwt_model', fetch='one')[0] == 1 | ||
| assert project.run_sql('select currentUser()', fetch='one')[0].startswith('JWT::') | ||
| # The first token was rejected, so the provider must have been asked again. | ||
| assert len(token_provider_stub.calls) >= 2 | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you might also want to check whether the function is allowed to be called with zero arguments (as stated by the contract). but it's not that critical, as a wrong signature will probably raise when clickhouse-connect tries to first call it.