diff --git a/docs/development/testing.md b/docs/development/testing.md index d2be103c7..97b939494 100644 --- a/docs/development/testing.md +++ b/docs/development/testing.md @@ -76,6 +76,30 @@ Missing required credentials fail setup. The launcher copies ADC into its temporary directory and preserves its external token-source references, which must remain accessible for the run. +Direct local BigQuery tests also require an explicit +`GOOGLE_APPLICATION_CREDENTIALS` file. Supported local ADC types are +`service_account`, `authorized_user`, `impersonated_service_account`, and +`external_account`. Enabling `KUMO_GCP_WORKLOAD_EXCHANGE_ENABLED` requires +`external_account` even outside a profile. BigQuery test setup does not fetch +AWS secrets, search home-directory credentials, or write credential files. +Missing or invalid inputs fail before dependent resources start. + +For local restricted-permission tests using a service-account key, provide +separate key files through `GCP_BIGQUERY_NO_TABLE_CREDENTIALS` and +`GCP_BIGQUERY_NO_JOB_CREDENTIALS`. For user, impersonated, or federated ADC, +provide the two `GCP_BIGQUERY_NO_*_SERVICE_ACCOUNT` targets instead; the source +identity needs permission to impersonate them. The test identities must have +the intended restricted permissions. The helper validates the credential type +and explicit configuration; the live tests verify the actual permissions. + +Use `@withBigQueryCredentials` for the default identity, or +`@withBigQueryCredentials(identity=BigQueryIdentity.NO_TABLE)` / +`BigQueryIdentity.NO_JOB` for restricted tests. These decorators only declare +requirements. Shared pytest fixtures prepare the base identity before session +resources and scope each test's identity until its dependent fixtures finish. +Explicit input files remain unchanged, and environment changes are restored +after success, failure, or skip. + The GCP-only runtime removes AWS secret fallback and uses a local artifact directory. The GCP+AWS profile retains S3 because the training test actually uses it. Credentials are prepared before worker startup; function-specific diff --git a/kumo/testing/bigquery.py b/kumo/testing/bigquery.py index e7811b8c3..29fbd595a 100644 --- a/kumo/testing/bigquery.py +++ b/kumo/testing/bigquery.py @@ -2,38 +2,28 @@ # All rights reserved. # SPDX-License-Identifier: Apache-2.0 -import asyncio -import functools import json import os from collections.abc import Callable, Generator from contextlib import contextmanager from enum import Enum -from typing import Optional import pytest from io_utils import fsutil -from io_utils.secrets import get_secrets from kumo.connector.bigquery_connector import ( ENV_GCS_KUMO_BUCKET, - ENV_GOOGLE_CREDENTIALS_DEFAULT_PATH, ENV_GOOGLE_CREDENTIALS_JSON, ) +from service_framework.env_var import EnvVar from tools.ci_test_profiles import TestProfile, profile_for -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' - 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_BIGQUERY_NO_TABLE_CREDENTIALS = 'GCP_BIGQUERY_NO_TABLE_CREDENTIALS' +ENV_BIGQUERY_NO_JOB_CREDENTIALS = 'GCP_BIGQUERY_NO_JOB_CREDENTIALS' -_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, @@ -43,6 +33,12 @@ ) +class BigQueryIdentity(Enum): + DEFAULT = 'default' + NO_TABLE = 'no_table' + NO_JOB = 'no_job' + + class CredentialKind(Enum): FEDERATED = 'external_account' USER = 'authorized_user' @@ -68,89 +64,87 @@ def credential_kind(path: str) -> CredentialKind: 'unsupported credential type') from error +def resolve_credentials(identity: BigQueryIdentity) -> tuple[str, str | None]: + """Resolve explicit inputs without changing the environment or files.""" + if not isinstance(identity, BigQueryIdentity): + raise TypeError('BigQuery credentials require a BigQueryIdentity') + path = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS') + if not path: + raise RuntimeError('BigQuery tests require explicit ' + 'GOOGLE_APPLICATION_CREDENTIALS') + kind = credential_kind(path) + profile = configured_profile() + require_wif = (profile is not None and 'gcp' in profile.capabilities) or EnvVar( + 'KUMO_GCP_WORKLOAD_EXCHANGE_ENABLED', bool, default_value=False).parse_value() + if require_wif and kind != CredentialKind.FEDERATED: + raise RuntimeError('Workload exchange BigQuery tests require ' + 'external_account ADC') + if identity == BigQueryIdentity.DEFAULT: + return path, None + + if kind == CredentialKind.KEY: + variable = { + BigQueryIdentity.NO_TABLE: ENV_BIGQUERY_NO_TABLE_CREDENTIALS, + BigQueryIdentity.NO_JOB: ENV_BIGQUERY_NO_JOB_CREDENTIALS, + }[identity] + restricted_path = os.environ.get(variable) + if not restricted_path: + raise RuntimeError(f'{variable} must identify the restricted ' + 'BigQuery service-account credential file') + if credential_kind(restricted_path) != CredentialKind.KEY: + raise RuntimeError(f'{variable} must contain service_account ' + 'credentials') + return restricted_path, None + + variable = { + BigQueryIdentity.NO_TABLE: ENV_BIGQUERY_NO_TABLE_SERVICE_ACCOUNT, + BigQueryIdentity.NO_JOB: ENV_BIGQUERY_NO_JOB_SERVICE_ACCOUNT, + }[identity] + target = os.environ.get(variable) + if not target: + raise RuntimeError(f'{variable} must be configured for ' + 'the restricted BigQuery test identity') + return path, target + + @contextmanager def setup_bigquery_credentials( - service_account_key: Optional[str] = None, + identity: BigQueryIdentity = BigQueryIdentity.DEFAULT, ) -> 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 + """Scope explicit ADC to a test or session; input files remain read-only.""" + path, target = resolve_credentials(identity) 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) - profile = configured_profile() - profiled_gcp = profile is not None and 'gcp' in profile.capabilities - path = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS') - if profiled_gcp and not path: - raise RuntimeError('GCP test profiles require explicit ' - 'GOOGLE_APPLICATION_CREDENTIALS') - if not profiled_gcp: - path = path or os.environ.get(ENV_GOOGLE_CREDENTIALS_JSON) - if (not profiled_gcp and not path - and fsutil.exists(ENV_GOOGLE_CREDENTIALS_DEFAULT_PATH)): - path = ENV_GOOGLE_CREDENTIALS_DEFAULT_PATH - kind = credential_kind(path) if path else None - if profiled_gcp and kind != CredentialKind.FEDERATED: - raise RuntimeError('GCP test profiles require external_account ADC') - 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) + 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 @pytest.fixture(scope='session', autouse=True) -def configure_profile_credentials(): +def configure_profile_credentials(request: pytest.FixtureRequest): """Prepare ADC before session resources and worker processes start.""" profile = configured_profile() - if profile is None or 'gcp' not in profile.capabilities: + selected = any(item.get_closest_marker('bigquery_credentials') is not None + and item.get_closest_marker('skip') is None + for item in request.session.items) + if not selected and (profile is None or 'gcp' not in profile.capabilities): yield return with setup_bigquery_credentials(): @@ -163,39 +157,24 @@ def configure_bigquery_credentials( configure_profile_credentials, ): marker = request.node.get_closest_marker('bigquery_credentials') - if configured_profile() is None or marker is None: + if marker is None: yield return - key, = marker.args - if key is not None and not isinstance(key, str): - raise TypeError('bigquery_credentials requires a string identity key') - with setup_bigquery_credentials(key): + identity, = marker.args + if not isinstance(identity, BigQueryIdentity): + raise TypeError('bigquery_credentials requires a BigQueryIdentity') + with setup_bigquery_credentials(identity): yield -def withBigQueryCredentials(func: Optional[Callable] = None, - service_account_key: Optional[str] = None): +def withBigQueryCredentials( + func: Callable | None = None, + *, + identity: BigQueryIdentity = BigQueryIdentity.DEFAULT, +): + """Declare credentials without performing auth or wrapping the test body.""" def decorator(inner_func: Callable): - marked = pytest.mark.requires_credentials('gcp')( - pytest.mark.bigquery_credentials(service_account_key)(inner_func)) - if configured_profile() is not None: - return marked - # Temporary unprofiled path: existing workflows retain phase-one - # behavior until all invocations use the profile launcher. - if asyncio.iscoroutinefunction(inner_func): - - async def async_wrapper(*args, **kwargs): - with setup_bigquery_credentials(service_account_key): - return await inner_func(*args, **kwargs) - - wrapper = async_wrapper - else: - - def sync_wrapper(*args, **kwargs): - with setup_bigquery_credentials(service_account_key): - return inner_func(*args, **kwargs) - - wrapper = sync_wrapper - return functools.wraps(marked)(wrapper) + return pytest.mark.requires_credentials('gcp')( + pytest.mark.bigquery_credentials(identity)(inner_func)) return decorator if func is None else decorator(func) diff --git a/test/connector/test_bigquery_connector.py b/test/connector/test_bigquery_connector.py index 23a9eb21a..3f3e6f718 100644 --- a/test/connector/test_bigquery_connector.py +++ b/test/connector/test_bigquery_connector.py @@ -27,11 +27,7 @@ SourceTableNotFoundError, ) from kumo.stats.compute_stats_python import compute_col_stats_python -from kumo.testing.bigquery import ( - FAIL_PROJECT_PERMISSION_BIGQUERY_KEY, - FAIL_TABLE_PERMISSION_BIGQUERY_KEY, - withBigQueryCredentials, -) +from kumo.testing.bigquery import BigQueryIdentity, withBigQueryCredentials def _write_df_to_bigquery(connector, table_name, df, replace=False): @@ -367,7 +363,7 @@ def test_set_credentials_and_open(): connector.open() -@withBigQueryCredentials(service_account_key=FAIL_TABLE_PERMISSION_BIGQUERY_KEY) +@withBigQueryCredentials(identity=BigQueryIdentity.NO_TABLE) def test_bigquery_table_permission_fail_read_table(): connector = BigQueryConnector( name='bigquery_fail', @@ -385,8 +381,7 @@ def test_bigquery_table_permission_fail_read_table(): assert 'LOAN' not in message -@withBigQueryCredentials( - service_account_key=FAIL_PROJECT_PERMISSION_BIGQUERY_KEY) +@withBigQueryCredentials(identity=BigQueryIdentity.NO_JOB) def test_bigquery_project_permission_fail_read_table(): connector = BigQueryConnector( name='bigquery_fail', diff --git a/test/testing/test_bigquery_credentials.py b/test/testing/test_bigquery_credentials.py index 9483fa067..19dbeaa11 100644 --- a/test/testing/test_bigquery_credentials.py +++ b/test/testing/test_bigquery_credentials.py @@ -10,10 +10,11 @@ import kumoapi.data_source as data_source_api import pytest -from io_utils import fsutil +from io_utils import fsutil, secrets from kumo.connector import bigquery_connector from kumo.connector.bigquery_connector import BigQueryConnector from kumo.testing import bigquery as testing_bigquery +from kumo.testing.bigquery import BigQueryIdentity from kumo.testing.rest import ( bigquery_test_identity, invalid_bigquery_key_test_identity_config, @@ -21,101 +22,114 @@ _ENV_NAMES = ( *testing_bigquery._CREDENTIAL_ENV_VARS, - 'GOOGLE_CLOUD_PROJECT', + 'GOOGLE_CLOUD_PROJECT', 'KUMO_TEST_PROFILE', + 'KUMO_GCP_WORKLOAD_EXCHANGE_ENABLED', testing_bigquery.ENV_BIGQUERY_NO_TABLE_SERVICE_ACCOUNT, testing_bigquery.ENV_BIGQUERY_NO_JOB_SERVICE_ACCOUNT, - 'KUMO_TEST_PROFILE', + testing_bigquery.ENV_BIGQUERY_NO_TABLE_CREDENTIALS, + testing_bigquery.ENV_BIGQUERY_NO_JOB_CREDENTIALS, ) -_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), +_RESTRICTED_IDENTITIES = ( + (BigQueryIdentity.NO_TABLE, + testing_bigquery.ENV_BIGQUERY_NO_TABLE_SERVICE_ACCOUNT, + testing_bigquery.ENV_BIGQUERY_NO_TABLE_CREDENTIALS), + (BigQueryIdentity.NO_JOB, + testing_bigquery.ENV_BIGQUERY_NO_JOB_SERVICE_ACCOUNT, + testing_bigquery.ENV_BIGQUERY_NO_JOB_CREDENTIALS), ) @pytest.fixture(autouse=True) -def isolated_credentials(monkeypatch, tmp_path): +def isolated_credentials(monkeypatch): 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'))) + secret_read = mock.Mock(side_effect=AssertionError('unexpected secret access')) + monkeypatch.setattr(secrets, 'get_secrets', secret_read) + monkeypatch.setattr(secrets.AWSSecretProvider, 'get_secret', secret_read) + yield + secret_read.assert_not_called() -def write_credentials(tmp_path, kind): - path = str(tmp_path / 'input-credentials.json') +def write_credentials(tmp_path, kind, filename='input-credentials.json'): + path = str(tmp_path / filename) 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): +@pytest.mark.parametrize(('identity', 'target_env', 'file_env'), _RESTRICTED_IDENTITIES) +def test_adc_identity_sequence(monkeypatch, tmp_path, kind, identity, + target_env, file_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(testing_bigquery.ENV_GOOGLE_CREDENTIALS_JSON, 'original-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): + for selected in (BigQueryIdentity.DEFAULT, identity, BigQueryIdentity.DEFAULT): 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) + None if selected == BigQueryIdentity.DEFAULT + else 'restricted@example.test') 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): +@pytest.mark.parametrize(('identity', 'target_env', 'file_env'), _RESTRICTED_IDENTITIES) +def test_key_identity_sequence(monkeypatch, tmp_path, identity, target_env, file_env): path = write_credentials(tmp_path, 'service_account') - original = fsutil.read_text(path) + restricted = write_credentials(tmp_path, 'service_account', 'restricted.json') 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) + monkeypatch.setenv(file_env, restricted) + monkeypatch.setenv(target_env, 'must-not-impersonate@example.test') before = {name: os.environ.get(name) for name in _ENV_NAMES} - temporary_paths = [] - for selected in (None, key, None): + original_files = {path: fsutil.read_text(path), restricted: fsutil.read_text(restricted)} + for selected in (BigQueryIdentity.DEFAULT, identity, BigQueryIdentity.DEFAULT): with testing_bigquery.setup_bigquery_credentials(selected): - configured = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] + expected = path if selected == BigQueryIdentity.DEFAULT else restricted + assert os.environ['GOOGLE_APPLICATION_CREDENTIALS'] == expected + assert os.environ[bigquery_connector.ENV_GOOGLE_CREDENTIALS_JSON] == expected 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 + assert {path: fsutil.read_text(path) for path in original_files} == original_files -@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') +@pytest.mark.parametrize('kind', ['external_account', 'service_account']) +@pytest.mark.parametrize(('identity', 'target_env', 'file_env'), _RESTRICTED_IDENTITIES) +def test_restricted_requires_explicit_input(monkeypatch, tmp_path, kind, identity, + target_env, file_env): + path = write_credentials(tmp_path, kind) 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): + required = file_env if kind == 'service_account' else target_env + with pytest.raises(RuntimeError, match=required): + with testing_bigquery.setup_bigquery_credentials(identity): 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('restricted_kind', [None, 'external_account']) +def test_restricted_key_requires_valid_file(monkeypatch, tmp_path, restricted_kind): + path = write_credentials(tmp_path, 'service_account') + restricted = str(tmp_path / 'restricted.json') + if restricted_kind: + fsutil.write_text(restricted, json.dumps({'type': restricted_kind})) + monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', path) + monkeypatch.setenv(testing_bigquery.ENV_BIGQUERY_NO_JOB_CREDENTIALS, restricted) + before = {name: os.environ.get(name) for name in _ENV_NAMES} + message = 'service_account credentials' if restricted_kind else 'does not exist' + with pytest.raises(RuntimeError, match=message): + with testing_bigquery.setup_bigquery_credentials(BigQueryIdentity.NO_JOB): + pytest.fail('invalid restricted key must fail during setup') + assert {name: os.environ.get(name) for name in _ENV_NAMES} == before @pytest.mark.parametrize('contents', ['not-json', '[]', '{}', @@ -128,152 +142,89 @@ def test_invalid_adc_fails(monkeypatch, tmp_path, contents): 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')) +@pytest.mark.parametrize('profile', [None, 'gcp', 'gcp-aws']) +def test_requires_explicit_adc(monkeypatch, tmp_path, profile): + if profile: + monkeypatch.setenv('KUMO_TEST_PROFILE', profile) + path = write_credentials(tmp_path, 'external_account') + monkeypatch.setenv(testing_bigquery.ENV_GOOGLE_CREDENTIALS_JSON, path) + with pytest.raises(RuntimeError, match='require explicit GOOGLE_APPLICATION_CREDENTIALS'): + with testing_bigquery.setup_bigquery_credentials(): + pytest.fail('other credential inputs must not activate BigQuery') + + +def test_missing_adc_file(monkeypatch, tmp_path): + monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', str(tmp_path / 'missing')) 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.fail('missing file must fail') + + +@pytest.mark.parametrize('mode', ['gcp', 'gcp-aws', 'workload']) +@pytest.mark.parametrize('kind', ['service_account', 'authorized_user', + 'impersonated_service_account']) +def test_wif_rejects_nonfederated(monkeypatch, tmp_path, mode, kind): + if mode == 'workload': + monkeypatch.setenv('KUMO_GCP_WORKLOAD_EXCHANGE_ENABLED', 'true') + else: + monkeypatch.setenv('KUMO_TEST_PROFILE', mode) + path = write_credentials(tmp_path, kind) + monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', path) + before = {name: os.environ.get(name) for name in _ENV_NAMES} + with pytest.raises(RuntimeError, match='external_account ADC'): + with testing_bigquery.setup_bigquery_credentials(): + pytest.fail('nonfederated input must fail') + assert {name: os.environ.get(name) for name in _ENV_NAMES} == before @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') +def test_explicit_scope_restores(monkeypatch, tmp_path, asynchronous, outcome): + path = write_credentials(tmp_path, 'external_account') + original = fsutil.read_text(path) + monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', path) + monkeypatch.setenv(testing_bigquery.ENV_GOOGLE_CREDENTIALS_JSON, 'previous-input') 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 = [] + writes = mock.Mock(side_effect=AssertionError('input files must be read-only')) + monkeypatch.setattr(fsutil, 'write_text', writes) 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') + raise ValueError('test failure') 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) + async def invoke_async(): + with testing_bigquery.setup_bigquery_credentials(): + await asyncio.sleep(0) + body() def invoke(): - return asyncio.run(wrapped()) if asynchronous else wrapped() + if asynchronous: + asyncio.run(invoke_async()) + else: + with testing_bigquery.setup_bigquery_credentials(): + body() if outcome == 'pass': - assert invoke() == 'result' + invoke() 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] - - -@pytest.mark.parametrize('profile', ['gcp', 'gcp-aws']) -@pytest.mark.parametrize('kind', ['service_account', 'authorized_user', - 'impersonated_service_account']) -def test_profile_rejects_nonfederated(monkeypatch, tmp_path, profile, kind): - monkeypatch.setenv('KUMO_TEST_PROFILE', profile) - path = write_credentials(tmp_path, kind) - monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', path) - before = {name: os.environ.get(name) for name in _ENV_NAMES} - with pytest.raises(RuntimeError, match='external_account ADC'): - with testing_bigquery.setup_bigquery_credentials(): - pytest.fail('nonfederated ADC must fail before test setup') - testing_bigquery.get_secrets.assert_not_called() - assert {name: os.environ.get(name) for name in _ENV_NAMES} == before - - -@pytest.mark.parametrize('profile', ['gcp', 'gcp-aws']) -def test_profile_requires_explicit_adc(monkeypatch, tmp_path, profile): - monkeypatch.setenv('KUMO_TEST_PROFILE', profile) - path = write_credentials(tmp_path, 'external_account') - monkeypatch.setenv(testing_bigquery.ENV_GOOGLE_CREDENTIALS_JSON, path) - monkeypatch.setattr(testing_bigquery, 'ENV_GOOGLE_CREDENTIALS_DEFAULT_PATH', - path) - with pytest.raises(RuntimeError, match='GOOGLE_APPLICATION_CREDENTIALS'): - with testing_bigquery.setup_bigquery_credentials(): - pytest.fail('ambient files must not activate profiled tests') - testing_bigquery.get_secrets.assert_not_called() + assert fsutil.read_text(path) == original + writes.assert_not_called() -def test_profile_decorator_only_marks(monkeypatch): - monkeypatch.setenv('KUMO_TEST_PROFILE', 'gcp') +@pytest.mark.parametrize('profile', [None, 'gcp']) +def test_decorator_only_marks(monkeypatch, profile): + if profile: + monkeypatch.setenv('KUMO_TEST_PROFILE', profile) read = mock.Mock(side_effect=AssertionError('credential I/O during import')) monkeypatch.setattr(fsutil, 'read_text', read) @@ -285,7 +236,6 @@ def test_body(): assert {mark.name for mark in decorated.pytestmark} == { 'requires_credentials', 'bigquery_credentials'} read.assert_not_called() - testing_bigquery.get_secrets.assert_not_called() def test_bigquery_rest_identity_uses_workload_exchange(monkeypatch): diff --git a/test/testing/test_gcs_credentials.py b/test/testing/test_gcs_credentials.py index bb29dde8a..96b9ac78b 100644 --- a/test/testing/test_gcs_credentials.py +++ b/test/testing/test_gcs_credentials.py @@ -20,6 +20,7 @@ 'GOOGLE_CREDENTIALS_JSON', 'GOOGLE_CREDENTIALS_STR_JSON', 'GCS_TEST_BUCKET', 'GCP_PROJECT_ID', 'GOOGLE_IMPERSONATE_SERVICE_ACCOUNT', 'GCP_BIGQUERY_NO_JOB_SERVICE_ACCOUNT', 'GCP_STORAGE_TRANSFER_BUCKET', + 'KUMO_GCP_WORKLOAD_EXCHANGE_ENABLED', 'GCP_BIGQUERY_NO_JOB_CREDENTIALS', ) @@ -99,10 +100,12 @@ def invoke(): @pytest.mark.parametrize('failure', [False, True]) -def test_profile_fixture_order(pytester, monkeypatch, tmp_path, failure): +@pytest.mark.parametrize('profile', [None, 'gcp']) +def test_profile_fixture_order(pytester, monkeypatch, tmp_path, failure, profile): path = str(tmp_path / 'adc.json') fsutil.write_text(path, '{"type":"external_account"}') - monkeypatch.setenv('KUMO_TEST_PROFILE', 'gcp') + if profile: + monkeypatch.setenv('KUMO_TEST_PROFILE', profile) monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', path) monkeypatch.setenv('GOOGLE_CREDENTIALS_JSON', 'before-scope') monkeypatch.setenv('GCP_BIGQUERY_NO_JOB_SERVICE_ACCOUNT', 'no-job@example.test') @@ -117,7 +120,7 @@ def test_profile_fixture_order(pytester, monkeypatch, tmp_path, failure): pytester.makepyfile(''' import os import pytest -from kumo.testing.bigquery import withBigQueryCredentials, FAIL_PROJECT_PERMISSION_BIGQUERY_KEY +from kumo.testing.bigquery import withBigQueryCredentials, BigQueryIdentity from kumo.testing.gcs import withGCSCredentials @pytest.fixture(scope='session') @@ -133,7 +136,7 @@ def cloud_resource(workers): yield assert os.environ['GOOGLE_IMPERSONATE_SERVICE_ACCOUNT'] == 'no-job@example.test' -@withBigQueryCredentials(service_account_key=FAIL_PROJECT_PERMISSION_BIGQUERY_KEY) +@withBigQueryCredentials(identity=BigQueryIdentity.NO_JOB) def test_restricted(cloud_resource): os.environ['GOOGLE_CREDENTIALS_JSON'] = 'test mutation' ''' + ("raise ValueError('expected failure')" if failure else "pass") + ''' @@ -239,3 +242,77 @@ def test_local(): result = pytester.runpytest_inprocess( '-q', '-o', 'addopts=', plugins=[testing_bigquery, testing_gcs]) result.assert_outcomes(passed=1, skipped=1) + + +def test_local_key_fixture_order(pytester, monkeypatch, tmp_path): + default = str(tmp_path / 'default.json') + restricted = str(tmp_path / 'restricted.json') + fsutil.write_text(default, '{"type":"service_account"}') + fsutil.write_text(restricted, '{"type":"service_account"}') + monkeypatch.setenv('GOOGLE_APPLICATION_CREDENTIALS', default) + monkeypatch.setenv('GCP_BIGQUERY_NO_JOB_CREDENTIALS', restricted) + before = {name: os.environ.get(name) for name in _ENV_NAMES} + pytester.makeini(''' +[pytest] +markers = + requires_credentials: external capabilities + bigquery_credentials: BigQuery identity +''') + pytester.makepyfile(''' +import os +import pytest +from kumo.testing.bigquery import BigQueryIdentity, withBigQueryCredentials + +@pytest.fixture(scope='session') +def workers(configure_profile_credentials): + base = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] + assert base != os.environ['GCP_BIGQUERY_NO_JOB_CREDENTIALS'] + yield base + assert os.environ['GOOGLE_APPLICATION_CREDENTIALS'] == base + +@pytest.fixture +def resource(workers): + assert os.environ['GOOGLE_APPLICATION_CREDENTIALS'] == os.environ['GCP_BIGQUERY_NO_JOB_CREDENTIALS'] + assert 'GOOGLE_IMPERSONATE_SERVICE_ACCOUNT' not in os.environ + yield + assert os.environ['GOOGLE_APPLICATION_CREDENTIALS'] == os.environ['GCP_BIGQUERY_NO_JOB_CREDENTIALS'] + +@withBigQueryCredentials(identity=BigQueryIdentity.NO_JOB) +def test_restricted(resource): + pass + +@withBigQueryCredentials +def test_default(workers): + assert os.environ['GOOGLE_APPLICATION_CREDENTIALS'] == workers +''') + result = pytester.runpytest_inprocess( + '-q', '-o', 'addopts=', plugins=[testing_bigquery]) + result.assert_outcomes(passed=2) + assert {name: os.environ.get(name) for name in _ENV_NAMES} == before + assert fsutil.read_text(default) == '{"type":"service_account"}' + assert fsutil.read_text(restricted) == '{"type":"service_account"}' + + +def test_local_bigquery_missing_input(pytester): + pytester.makeini(''' +[pytest] +markers = + requires_credentials: external capabilities + bigquery_credentials: BigQuery identity +''') + pytester.makepyfile(''' +import pytest +from kumo.testing.bigquery import withBigQueryCredentials + +@pytest.fixture(scope='session') +def workers(configure_profile_credentials): + raise AssertionError('worker must not start without explicit credentials') + +@withBigQueryCredentials +def test_bigquery(workers): + pass +''') + result = pytester.runpytest_inprocess( + '-q', '-o', 'addopts=', plugins=[testing_bigquery]) + result.assert_outcomes(errors=1) + result.stdout.fnmatch_lines(['*require explicit*GOOGLE_APPLICATION_CREDENTIALS*'])