diff --git a/dbt-bigquery/src/dbt/adapters/bigquery/credentials.py b/dbt-bigquery/src/dbt/adapters/bigquery/credentials.py index 28aee2104..01f5cb70b 100644 --- a/dbt-bigquery/src/dbt/adapters/bigquery/credentials.py +++ b/dbt-bigquery/src/dbt/adapters/bigquery/credentials.py @@ -10,6 +10,7 @@ from google.oauth2.credentials import Credentials as GoogleCredentials from google.oauth2.service_account import Credentials as ServiceAccountCredentials from google.auth.identity_pool import Credentials as IdentityPoolCredentials +from google.auth.credentials import AnonymousCredentials from mashumaro import pass_through from dbt_common.clients.system import run_cmd @@ -42,6 +43,7 @@ class BigQueryConnectionMethod(StrEnum): SERVICE_ACCOUNT_JSON = "service-account-json" # WIF in this context refers to Workload Identity Federation https://cloud.google.com/iam/docs/workload-identity-federation EXTERNAL_OAUTH_WIF = "external-oauth-wif" + ANONYMOUS = "anonymous" @dataclass @@ -248,6 +250,9 @@ def _create_google_credentials(credentials: BigQueryCredentials) -> GoogleCreden elif credentials.method == BigQueryConnectionMethod.EXTERNAL_OAUTH_WIF: creds = _create_identity_pool_credentials(credentials=credentials) + elif credentials.method == BigQueryConnectionMethod.ANONYMOUS: + creds = AnonymousCredentials() + else: raise FailedToConnectError(f"Invalid `method` in profile: '{credentials.method}'") diff --git a/dbt-bigquery/tests/unit/test_bigquery_adapter.py b/dbt-bigquery/tests/unit/test_bigquery_adapter.py index 5edcb9416..b09f8fa1b 100644 --- a/dbt-bigquery/tests/unit/test_bigquery_adapter.py +++ b/dbt-bigquery/tests/unit/test_bigquery_adapter.py @@ -166,6 +166,21 @@ def setUp(self): "compute_region": "europe-west1", "submission_method": "serverless", }, + "anonymous_ok": { + "type": "bigquery", + "method": "anonymous", + "project": "dbt-unit-000000", + "schema": "dummy_schema", + "threads": 1, + "api_endpoint": "http://localhost:9050", + }, + "anonymous_missing_api_endpoint": { + "type": "bigquery", + "method": "anonymous", + "project": "dbt-unit-000000", + "schema": "dummy_schema", + "threads": 1, + }, }, "target": "oauth", } @@ -407,6 +422,20 @@ def test_acquire_connection_maximum_bytes_billed(self, mock_open_connection): connection.handle mock_open_connection.assert_called_once() + @patch("dbt.adapters.bigquery.BigQueryConnectionManager.open", return_value=_bq_conn()) + def test_acquire_connection_anonymous_validations(self, mock_open_connection): + adapter = self.get_adapter("anonymous_ok") + try: + connection = adapter.acquire_connection("dummy") + self.assertEqual(connection.type, "bigquery") + + except dbt_common.exceptions.base.DbtValidationError as e: + self.fail("got DbtValidationError: {}".format(str(e))) + + mock_open_connection.assert_not_called() + connection.handle + mock_open_connection.assert_called_once() + def test_cancel_open_connections_empty(self): adapter = self.get_adapter("oauth") self.assertEqual(len(list(adapter.cancel_open_connections())), 0)