From e48bd3eb8f92e0e9de56e0cb54c10f88acb2e714 Mon Sep 17 00:00:00 2001 From: danielmawhirter Date: Tue, 27 Oct 2026 15:11:07 -0500 Subject: [PATCH] Fix BigQuery test credential selection and cleanup --- kumo/testing/bigquery.py | 221 +++++++++-------- test/testing/test_bigquery_credentials.py | 276 ++++++++++++++++++---- 2 files changed, 333 insertions(+), 164 deletions(-) diff --git a/kumo/testing/bigquery.py b/kumo/testing/bigquery.py index db7ad10be..da91deaa5 100644 --- a/kumo/testing/bigquery.py +++ b/kumo/testing/bigquery.py @@ -6,10 +6,14 @@ import functools import json import os -from collections.abc import Callable +from collections.abc import Callable, Generator +from contextlib import contextmanager +from enum import Enum from typing import Optional -from io_utils import logging +import pytest + +from io_utils import fsutil from io_utils.secrets import get_secrets from kumo.connector.bigquery_connector import ( ENV_GCS_KUMO_BUCKET, @@ -17,146 +21,133 @@ ENV_GOOGLE_CREDENTIALS_JSON, ) -logger = logging.getLogger(__name__) - -DEFAULT_BIGQUERY_KEY = "kumo-test-bigquery-service-account-key" +DEFAULT_BIGQUERY_KEY = 'kumo-test-bigquery-service-account-key' +FAIL_TABLE_PERMISSION_BIGQUERY_KEY = 'kumo-test-permission-fail-bigquery-service-account-key-2' +FAIL_PROJECT_PERMISSION_BIGQUERY_KEY = 'kumo-test-permission-fail-bigquery-service-account-key' -# Missing required table read permission -FAIL_TABLE_PERMISSION_BIGQUERY_KEY = "kumo-test-permission-fail-bigquery-service-account-key-2" -# Missing required jobs.create permission -FAIL_PROJECT_PERMISSION_BIGQUERY_KEY = "kumo-test-permission-fail-bigquery-service-account-key" - -# Keyless CI maps the two legacy negative-test identities to deliberately -# restricted service accounts. The names remain keyed by the old secret -# identifiers so callers do not need to know how credentials are sourced. -ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT = "GOOGLE_IMPERSONATE_SERVICE_ACCOUNT" -ENV_BIGQUERY_NO_TABLE_SERVICE_ACCOUNT = ( - "GCP_BIGQUERY_NO_TABLE_SERVICE_ACCOUNT") -ENV_BIGQUERY_NO_JOB_SERVICE_ACCOUNT = "GCP_BIGQUERY_NO_JOB_SERVICE_ACCOUNT" +ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT = 'GOOGLE_IMPERSONATE_SERVICE_ACCOUNT' +ENV_BIGQUERY_NO_TABLE_SERVICE_ACCOUNT = 'GCP_BIGQUERY_NO_TABLE_SERVICE_ACCOUNT' +ENV_BIGQUERY_NO_JOB_SERVICE_ACCOUNT = 'GCP_BIGQUERY_NO_JOB_SERVICE_ACCOUNT' _IMPERSONATION_ENV_BY_KEY = { FAIL_TABLE_PERMISSION_BIGQUERY_KEY: ENV_BIGQUERY_NO_TABLE_SERVICE_ACCOUNT, FAIL_PROJECT_PERMISSION_BIGQUERY_KEY: ENV_BIGQUERY_NO_JOB_SERVICE_ACCOUNT, } +_CREDENTIAL_ENV_VARS = ( + 'GOOGLE_APPLICATION_CREDENTIALS', + ENV_GOOGLE_CREDENTIALS_JSON, + ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT, + ENV_GCS_KUMO_BUCKET.name, + 'GCP_PROJECT_ID', +) -def _configure_application_default_credentials( - service_account_key: str) -> bool: - """Configure an existing ADC file for BigQuery connector tests. +class CredentialKind(Enum): + FEDERATED = 'external_account' + USER = 'authorized_user' + IMPERSONATED = 'impersonated_service_account' + KEY = 'service_account' - GitHub's OIDC authentication action writes an ``external_account`` ADC - file and exports ``GOOGLE_APPLICATION_CREDENTIALS``. Negative permission - tests select a restricted service account through IAM impersonation rather - than loading a second private key. - """ - credentials_path = os.getenv('GOOGLE_APPLICATION_CREDENTIALS') - if not credentials_path or not os.path.exists(credentials_path): - return False - - impersonation_env = _IMPERSONATION_ENV_BY_KEY.get(service_account_key) - if impersonation_env is None: - os.environ.pop(ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT, None) - else: - target = os.getenv(impersonation_env) - if not target: - raise RuntimeError( - f"{impersonation_env} must be configured for the restricted " - "BigQuery permission test identity") - os.environ[ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT] = target - - os.environ[ENV_GOOGLE_CREDENTIALS_JSON] = credentials_path - project_id = (os.getenv('GCP_PROJECT_ID') - or os.getenv('GOOGLE_CLOUD_PROJECT')) - if project_id: - os.environ['GCP_PROJECT_ID'] = project_id - logger.info("Using Application Default Credentials for BigQuery tests") - return True - - -def setup_bigquery_credentials(service_account_key: Optional[str] = None): - # Set env var for GCS kumo bucket for testing. - os.environ[ENV_GCS_KUMO_BUCKET.name] = 'kumo-test-bucket' - service_account_key = service_account_key or DEFAULT_BIGQUERY_KEY - - # Prefer short-lived ADC supplied by Workload Identity Federation. This - # keeps GitHub Actions from reading any stored GCP service-account key. - if _configure_application_default_credentials(service_account_key): - return +def credential_kind(path: str) -> CredentialKind: + if not fsutil.exists(path) or fsutil.isdir(path): + raise RuntimeError('Configured BigQuery credential file does not exist') try: - # Get the GCloud secrets from AWS secrets manager and store it in the - # default file. This remains a local/deployment fallback; CI supplies - # ADC above and never reaches this path. - secrets = get_secrets(service_account_key) - file_path = os.path.join(os.getcwd(), ".google_credential.json") - with open(file_path, 'w') as f: - json.dump(secrets, f) - - with open(ENV_GOOGLE_CREDENTIALS_DEFAULT_PATH, 'w') as f: - # This is a hack. allows big query connector - # to fall back to default credentials - json.dump(secrets, f) - os.environ[ENV_GOOGLE_CREDENTIALS_JSON] = file_path - # Also set the standard ADC env var so any google-cloud-* client - # built without explicit credentials (e.g. fsutil's GCS client - # used by integration tests to verify EXPORT DATA output) picks - # up the same service account. - os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = file_path - except Exception as e: - # Use built-in credentials if this env doesn't have access to read the - # secrets from AWS - logger.exception(e) + document = json.loads(fsutil.read_text(path)) + if not isinstance(document, dict): + raise ValueError('credential document must be a JSON object') + return CredentialKind(document.get('type')) + except (ValueError, TypeError) as error: + raise RuntimeError('BigQuery credential file has an invalid or ' + 'unsupported credential type') from error + + +@contextmanager +def setup_bigquery_credentials( + service_account_key: Optional[str] = None, +) -> Generator[None, None, None]: + """Scope test credentials, including transitional stored-key fallback. + + An ADC file's type determines whether restricted tests use impersonation. + A service-account key instead needs the existing per-identity secret. The + fallback remains until hosted workflows and local setup migrate together; + it never overwrites input files or writes shared home/repository files. + """ + key = service_account_key or DEFAULT_BIGQUERY_KEY + with pytest.MonkeyPatch.context() as environment: + # Register all touched variables even when absent, including values a + # decorated test may overwrite before raising or skipping. + for name in _CREDENTIAL_ENV_VARS: + previous = os.environ.get(name) + environment.setenv(name, previous or '') + if previous is None: + environment.delenv(name) + + path = (os.environ.get('GOOGLE_APPLICATION_CREDENTIALS') + or os.environ.get(ENV_GOOGLE_CREDENTIALS_JSON)) + if not path and fsutil.exists(ENV_GOOGLE_CREDENTIALS_DEFAULT_PATH): + path = ENV_GOOGLE_CREDENTIALS_DEFAULT_PATH + kind = credential_kind(path) if path else None + target_variable = _IMPERSONATION_ENV_BY_KEY.get(key) + target = None + if kind is not None and kind != CredentialKind.KEY and target_variable: + target = os.environ.get(target_variable) + if not target: + raise RuntimeError(f'{target_variable} must be configured for ' + 'the restricted BigQuery test identity') + + temporary_dir = None + try: + if path is None or (kind == CredentialKind.KEY and target_variable): + try: + credentials = get_secrets(key) + except Exception: + pytest.skip('No BigQuery credentials found') + temporary_dir = fsutil.mkdtemp() + path = os.path.join(temporary_dir, 'credentials.json') + fsutil.write_text(path, json.dumps(credentials)) + if credential_kind(path) != CredentialKind.KEY: + raise RuntimeError('Stored BigQuery test credentials must ' + 'be service_account credentials') + + environment.setenv('GOOGLE_APPLICATION_CREDENTIALS', path) + environment.setenv(ENV_GOOGLE_CREDENTIALS_JSON, path) + if target: + environment.setenv(ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT, + target) + else: + environment.delenv(ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT, + raising=False) + environment.setenv( + ENV_GCS_KUMO_BUCKET.name, + os.environ.get(ENV_GCS_KUMO_BUCKET.name) or 'kumo-test-bucket') + project = (os.environ.get('GCP_PROJECT_ID') + or os.environ.get('GOOGLE_CLOUD_PROJECT')) + if project: + environment.setenv('GCP_PROJECT_ID', project) + yield + finally: + if temporary_dir is not None: + fsutil.rm(temporary_dir) def withBigQueryCredentials(func: Optional[Callable] = None, service_account_key: Optional[str] = None): - import pytest - def decorator(inner_func: Callable): - def _setup(): - setup_bigquery_credentials(service_account_key) - if (not os.getenv(ENV_GOOGLE_CREDENTIALS_JSON) and - not os.path.exists(ENV_GOOGLE_CREDENTIALS_DEFAULT_PATH)): - pytest.skip("No BigQuery credentials found") - if asyncio.iscoroutinefunction(inner_func): async def async_wrapper(*args, **kwargs): - previous_target = os.environ.get( - ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT) - try: - _setup() + with setup_bigquery_credentials(service_account_key): return await inner_func(*args, **kwargs) - finally: - if previous_target is None: - os.environ.pop(ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT, - None) - else: - os.environ[ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT] = ( - previous_target) wrapper = async_wrapper else: def sync_wrapper(*args, **kwargs): - previous_target = os.environ.get( - ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT) - try: - _setup() + with setup_bigquery_credentials(service_account_key): return inner_func(*args, **kwargs) - finally: - if previous_target is None: - os.environ.pop(ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT, - None) - else: - os.environ[ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT] = ( - previous_target) wrapper = sync_wrapper - return functools.wraps(inner_func)(wrapper) - if func is None: - return decorator - else: - return decorator(func) + return decorator if func is None else decorator(func) diff --git a/test/testing/test_bigquery_credentials.py b/test/testing/test_bigquery_credentials.py index a87107373..156e3422a 100644 --- a/test/testing/test_bigquery_credentials.py +++ b/test/testing/test_bigquery_credentials.py @@ -2,12 +2,15 @@ # All rights reserved. # SPDX-License-Identifier: Apache-2.0 +import asyncio +import json import os from unittest import mock import kumoapi.data_source as data_source_api import pytest +from io_utils import fsutil from kumo.connector import bigquery_connector from kumo.connector.bigquery_connector import BigQueryConnector from kumo.testing import bigquery as testing_bigquery @@ -16,6 +19,229 @@ invalid_bigquery_key_test_identity_config, ) +_ENV_NAMES = ( + *testing_bigquery._CREDENTIAL_ENV_VARS, + 'GOOGLE_CLOUD_PROJECT', + testing_bigquery.ENV_BIGQUERY_NO_TABLE_SERVICE_ACCOUNT, + testing_bigquery.ENV_BIGQUERY_NO_JOB_SERVICE_ACCOUNT, +) +_RESTRICTED_KEYS = ( + (testing_bigquery.FAIL_TABLE_PERMISSION_BIGQUERY_KEY, + testing_bigquery.ENV_BIGQUERY_NO_TABLE_SERVICE_ACCOUNT), + (testing_bigquery.FAIL_PROJECT_PERMISSION_BIGQUERY_KEY, + testing_bigquery.ENV_BIGQUERY_NO_JOB_SERVICE_ACCOUNT), +) + + +@pytest.fixture(autouse=True) +def isolated_credentials(monkeypatch, tmp_path): + for name in _ENV_NAMES: + monkeypatch.setenv(name, '') + monkeypatch.delenv(name) + monkeypatch.setattr(testing_bigquery, 'ENV_GOOGLE_CREDENTIALS_DEFAULT_PATH', + str(tmp_path / 'absent-home-credentials.json')) + # No focused test may reach a real secret provider. + monkeypatch.setattr(testing_bigquery, 'get_secrets', mock.Mock( + side_effect=AssertionError('unexpected secret access'))) + + +def write_credentials(tmp_path, kind): + path = str(tmp_path / 'input-credentials.json') + fsutil.write_text(path, json.dumps({'type': kind})) + return path + + +@pytest.mark.parametrize('kind', ['external_account', 'authorized_user', + 'impersonated_service_account']) +@pytest.mark.parametrize(('key', 'target_env'), _RESTRICTED_KEYS) +def test_adc_identity_sequence(monkeypatch, tmp_path, kind, key, target_env): + path = write_credentials(tmp_path, kind) + original = fsutil.read_text(path) + monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', path) + monkeypatch.setenv(testing_bigquery.ENV_GOOGLE_CREDENTIALS_JSON, + 'original-connector-input') + monkeypatch.setenv(target_env, 'restricted@example.test') + monkeypatch.setenv('GOOGLE_CLOUD_PROJECT', 'example-project') + monkeypatch.setenv(testing_bigquery.ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT, + 'original@example.test') + before = {name: os.environ.get(name) for name in _ENV_NAMES} + + for selected in (None, key, None): + with testing_bigquery.setup_bigquery_credentials(selected): + assert os.environ[bigquery_connector.ENV_GOOGLE_CREDENTIALS_JSON] == path + assert os.environ['GCP_PROJECT_ID'] == 'example-project' + assert os.environ.get( + testing_bigquery.ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT) == ( + 'restricted@example.test' if selected else None) + assert {name: os.environ.get(name) for name in _ENV_NAMES} == before + assert fsutil.read_text(path) == original + testing_bigquery.get_secrets.assert_not_called() + + +@pytest.mark.parametrize(('key', 'target_env'), _RESTRICTED_KEYS) +def test_key_identity_sequence(monkeypatch, tmp_path, key, target_env): + path = write_credentials(tmp_path, 'service_account') + original = fsutil.read_text(path) + monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', path) + # An unrelated WIF mode flag must not relabel an explicit stored key. + monkeypatch.setenv('KUMO_GCP_WORKLOAD_EXCHANGE_ENABLED', 'true') + secrets = mock.Mock(return_value={'type': 'service_account', 'identity': key}) + monkeypatch.setattr(testing_bigquery, 'get_secrets', secrets) + before = {name: os.environ.get(name) for name in _ENV_NAMES} + temporary_paths = [] + for selected in (None, key, None): + with testing_bigquery.setup_bigquery_credentials(selected): + configured = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] + assert testing_bigquery.ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT not in os.environ + if selected: + assert configured != path + assert json.loads(fsutil.read_text(configured))['identity'] == key + temporary_paths.append(configured) + else: + assert configured == path + assert {name: os.environ.get(name) for name in _ENV_NAMES} == before + secrets.assert_called_once_with(key) + assert all(not fsutil.exists(path) for path in temporary_paths) + assert fsutil.read_text(path) == original + + +@pytest.mark.parametrize(('key', 'target_env'), _RESTRICTED_KEYS) +def test_adc_requires_restricted_target(monkeypatch, tmp_path, key, target_env): + path = write_credentials(tmp_path, 'external_account') + monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', path) + before = {name: os.environ.get(name) for name in _ENV_NAMES} + with pytest.raises(RuntimeError, match=target_env): + with testing_bigquery.setup_bigquery_credentials(key): + pytest.fail('missing identity must fail during setup') + assert {name: os.environ.get(name) for name in _ENV_NAMES} == before + testing_bigquery.get_secrets.assert_not_called() + + +@pytest.mark.parametrize('contents', ['not-json', '[]', '{}', + '{"type": "unsupported"}']) +def test_invalid_adc_fails(monkeypatch, tmp_path, contents): + path = str(tmp_path / 'bad.json') + fsutil.write_text(path, contents) + monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', path) + with pytest.raises(RuntimeError, match='invalid or unsupported'): + with testing_bigquery.setup_bigquery_credentials(): + pytest.fail('invalid input must fail during setup') + assert fsutil.read_text(path) == contents + testing_bigquery.get_secrets.assert_not_called() + + +def test_missing_adc_fails(monkeypatch, tmp_path): + monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', + str(tmp_path / 'missing.json')) + with pytest.raises(RuntimeError, match='does not exist'): + with testing_bigquery.setup_bigquery_credentials(): + pytest.fail('missing input must fail during setup') + testing_bigquery.get_secrets.assert_not_called() + + +@pytest.mark.parametrize('asynchronous', [False, True]) +@pytest.mark.parametrize('outcome', ['pass', 'fail', 'skip']) +@pytest.mark.parametrize('prior_values', [False, True]) +def test_fallback_lifetime(monkeypatch, tmp_path, asynchronous, outcome, + prior_values): + # Preserve a preexisting legacy home file without letting it select ADC. + untouched = str(tmp_path / 'home-key.json') + fsutil.write_text(untouched, 'leave unchanged') + if prior_values: + for name in (testing_bigquery.ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT, + testing_bigquery.ENV_GCS_KUMO_BUCKET.name, 'GCP_PROJECT_ID'): + monkeypatch.setenv(name, 'original') + before = {name: os.environ.get(name) for name in _ENV_NAMES} + secrets = mock.Mock(return_value={'type': 'service_account'}) + monkeypatch.setattr(testing_bigquery, 'get_secrets', secrets) + created = [] + + def body(): + path = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] + created.append(path) + assert fsutil.exists(path) + assert os.environ[bigquery_connector.ENV_GOOGLE_CREDENTIALS_JSON] == path + for name in testing_bigquery._CREDENTIAL_ENV_VARS: + os.environ[name] = 'test mutation' + if outcome == 'fail': + raise ValueError('test failed') + if outcome == 'skip': + pytest.skip('intentional skip') + return 'result' + + async def async_body(): + return body() + + wrapped = testing_bigquery.withBigQueryCredentials( + async_body if asynchronous else body) + + def invoke(): + return asyncio.run(wrapped()) if asynchronous else wrapped() + + if outcome == 'pass': + assert invoke() == 'result' + else: + error = ValueError if outcome == 'fail' else pytest.skip.Exception + with pytest.raises(error): + invoke() + assert {name: os.environ.get(name) for name in _ENV_NAMES} == before + assert len(created) == 1 + assert not fsutil.exists(os.path.dirname(created[0])) + assert fsutil.read_text(untouched) == 'leave unchanged' + secrets.assert_called_once_with(testing_bigquery.DEFAULT_BIGQUERY_KEY) + + +def test_fallback_setup_failure_cleans(monkeypatch): + secrets = mock.Mock(return_value={'type': 'unsupported'}) + monkeypatch.setattr(testing_bigquery, 'get_secrets', secrets) + original_mkdtemp = fsutil.mkdtemp + created = [] + + def record_tempdir(): + path = original_mkdtemp() + created.append(path) + return path + + monkeypatch.setattr(fsutil, 'mkdtemp', record_tempdir) + before = {name: os.environ.get(name) for name in _ENV_NAMES} + with pytest.raises(RuntimeError, match='unsupported'): + with testing_bigquery.setup_bigquery_credentials(): + pytest.fail('bad stored key must fail during setup') + assert len(created) == 1 + assert not fsutil.exists(created[0]) + assert {name: os.environ.get(name) for name in _ENV_NAMES} == before + + +def test_missing_credentials_skip(monkeypatch): + monkeypatch.setattr(testing_bigquery, 'get_secrets', mock.Mock( + side_effect=RuntimeError('secret unavailable'))) + before = {name: os.environ.get(name) for name in _ENV_NAMES} + with pytest.raises(pytest.skip.Exception, match='No BigQuery credentials'): + with testing_bigquery.setup_bigquery_credentials(): + pytest.fail('missing credentials must skip') + assert {name: os.environ.get(name) for name in _ENV_NAMES} == before + + +def test_fallback_identity_sequence(monkeypatch): + secrets = mock.Mock(side_effect=lambda key: { + 'type': 'service_account', 'identity': key}) + monkeypatch.setattr(testing_bigquery, 'get_secrets', secrets) + keys = [testing_bigquery.DEFAULT_BIGQUERY_KEY, + testing_bigquery.FAIL_TABLE_PERMISSION_BIGQUERY_KEY, + testing_bigquery.DEFAULT_BIGQUERY_KEY] + before = {name: os.environ.get(name) for name in _ENV_NAMES} + paths = [] + for key in keys: + with testing_bigquery.setup_bigquery_credentials(key): + path = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] + paths.append(path) + assert json.loads(fsutil.read_text(path))['identity'] == key + assert testing_bigquery.ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT not in os.environ + assert not fsutil.exists(path) + assert {name: os.environ.get(name) for name in _ENV_NAMES} == before + assert len(set(paths)) == len(keys) + assert secrets.call_args_list == [mock.call(key) for key in keys] + def test_bigquery_rest_identity_uses_workload_exchange(monkeypatch): service_account = ( @@ -37,58 +263,10 @@ def test_invalid_bigquery_identity_remains_key_backed(): data_source_api.GoogleServiceAccountKeyAuthentication) -def test_setup_prefers_application_default_credentials(monkeypatch, tmp_path): - credentials_path = tmp_path / "gha-creds.json" - credentials_path.write_text('{"type": "external_account"}') - monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', str(credentials_path)) - monkeypatch.setenv('GOOGLE_CLOUD_PROJECT', 'example-project') - get_secrets = mock.Mock(side_effect=AssertionError( - "Workload Identity must not read a stored service-account key")) - monkeypatch.setattr(testing_bigquery, 'get_secrets', get_secrets) - - testing_bigquery.setup_bigquery_credentials() - - assert os.environ[bigquery_connector.ENV_GOOGLE_CREDENTIALS_JSON] == str( - credentials_path) - assert os.environ['GCP_PROJECT_ID'] == 'example-project' - assert testing_bigquery.ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT not in ( - os.environ) - get_secrets.assert_not_called() - - -def test_restricted_identity_uses_iam_impersonation(monkeypatch, tmp_path): - credentials_path = tmp_path / "gha-creds.json" - credentials_path.write_text('{"type": "external_account"}') - target = 'gha-kumo-bq-no-table@example-project.iam.gserviceaccount.com' - monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', str(credentials_path)) - monkeypatch.setenv(testing_bigquery.ENV_BIGQUERY_NO_TABLE_SERVICE_ACCOUNT, - target) - - testing_bigquery.setup_bigquery_credentials( - testing_bigquery.FAIL_TABLE_PERMISSION_BIGQUERY_KEY) - - assert os.environ[ - testing_bigquery.ENV_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT] == target - - -def test_restricted_identity_requires_target_variable(monkeypatch, tmp_path): - credentials_path = tmp_path / "gha-creds.json" - credentials_path.write_text('{"type": "external_account"}') - monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', str(credentials_path)) - monkeypatch.delenv( - testing_bigquery.ENV_BIGQUERY_NO_TABLE_SERVICE_ACCOUNT, - raising=False, - ) - - with pytest.raises(RuntimeError, match='GCP_BIGQUERY_NO_TABLE'): - testing_bigquery.setup_bigquery_credentials( - testing_bigquery.FAIL_TABLE_PERMISSION_BIGQUERY_KEY) - - def test_connector_loads_external_account_and_impersonates( monkeypatch, tmp_path): credentials_path = tmp_path / "gha-creds.json" - credentials_path.write_text('{"type": "external_account"}') + fsutil.write_text(str(credentials_path), '{"type": "external_account"}') target = 'gha-kumo-bq-no-job@example-project.iam.gserviceaccount.com' source_credential = mock.Mock(name='source_credential') target_credential = mock.Mock(name='target_credential')