google-auth: Local developer mTLS environment leaks into unit tests, causing mock failures
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 76/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 静か
- 技術スタック
- python
- 領域
- testing-qa
調査の方向性
tests/conftest.py から始め、既存の pytest fixture と mTLS 関連テストを確認します。ローカルの mTLS 環境変数または証明書設定が存在する状態でユニットテストスイートを実行し、通常のテストが標準エンドポイントを使用する一方で、mTLS を明示的に有効化するテストも引き続きパスすることを確認します。各テストごとに環境が分離され、テスト後に復元されれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Determine this is the right repository
- I determined this is the correct repository in which to report this bug.
Summary of the issue
When running unit tests locally on developer machines that have context-aware access or enterprise mTLS configured (such as Google-managed corporate MacBooks), several tests fail unexpectedly.
This happens because the test suite's global configuration (tests/conftest.py) does not sanitize the environment. Global environment variables (like GOOGLE_API_CERTIFICATE_CONFIG or GOOGLE_API_USE_CLIENT_CERTIFICATE) and default configuration files (like ~/.config/gcloud/certificate_config.json) leak directly into the pytest session.
Any test that does not explicitly mock _mtls_helper.check_use_client_cert() or clear these environment variables will transition to using mTLS hostnames (e.g. expecting iamcredentials.googleapis.com but getting iamcredentials.mtls.googleapis.com). Since the mock networks in the tests are configured with standard endpoints, the requests fail to match the mock setups, causing transport exceptions and test failures.
Proposed fix
Introduce a function-scoped, autouse fixture in tests/conftest.py to establish a clean, hermetic environment for every unit test. Using pytest's standard monkeypatch fixture ensures all local environmental variables and mocks are automatically cleaned up and restored back to the developer's original workstation state after each test executes. This can be accomplished by something like:
# tests/conftest.py
@pytest.fixture(autouse=True)
def clean_mtls_environment(monkeypatch):
from google.auth.transport import _mtls_helper
# Pop all mTLS-related environment variables
for var in [
"GOOGLE_API_USE_CLIENT_CERTIFICATE",
"GOOGLE_API_CERTIFICATE_CONFIG",
"CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE",
"CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH",
]:
monkeypatch.delenv(var, raising=False)
# Mock check_use_client_cert to return False by default
monkeypatch.setattr(_mtls_helper, "check_use_client_cert", lambda: False)
This way tests specifically designed to verify mTLS behaviors can easily override the global mock locally by re-patching the method in their own bodies (which runs after the global autouse setup):
def test_mtls_behavior(monkeypatch):
from google.auth.transport import _mtls_helper
monkeypatch.setattr(_mtls_helper, "check_use_client_cert", lambda: True)
# Test runs with mTLS enabled cleanly
- 主要言語
- Python
- スター
- 5.4k
- フォーク
- 1.8k
- 平均マージ
- 1日 17時間
- マージ済み PR(30日)
- 93
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
googleapis/google-cloud-python のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
googleapis/google-cloud-python#18428 ·
-
priority: p2 type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
googleapis/google-cloud-python#18375 · コメント 1 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 76/100
googleapis/google-cloud-python#18339 ·
-
auth priority: p2
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
googleapis/google-cloud-python#18315 ·
-
priority: p2 type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
googleapis/google-cloud-python#18260 ·
googleapis/google-cloud-python の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100