|
| 1 | +"""Tests for the Google Cloud OIDC detector.""" |
| 2 | + |
| 3 | +import sys |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from cloudsmith_cli.core.credentials.models import CredentialContext |
| 9 | +from cloudsmith_cli.core.credentials.oidc.detectors.gcp import GCPDetector |
| 10 | + |
| 11 | +compute_engine = pytest.importorskip("google.auth.compute_engine") |
| 12 | +exceptions = pytest.importorskip("google.auth.exceptions") |
| 13 | +oauth2_creds = pytest.importorskip("google.oauth2.credentials") |
| 14 | + |
| 15 | +METADATA_GET = "google.auth.compute_engine._metadata.get" |
| 16 | +METADATA_IS_ON_GCE = "google.auth.compute_engine._metadata.is_on_gce" |
| 17 | +FETCH_ID_TOKEN_CREDS = "google.oauth2.id_token.fetch_id_token_credentials" |
| 18 | + |
| 19 | + |
| 20 | +def make_detector(**ctx): |
| 21 | + return GCPDetector(context=CredentialContext(**ctx)) |
| 22 | + |
| 23 | + |
| 24 | +class TestDetect: |
| 25 | + def test_not_detected_when_google_auth_missing(self): |
| 26 | + with mock.patch.dict(sys.modules, {"google.auth": None}): |
| 27 | + assert make_detector().detect() is False |
| 28 | + |
| 29 | + def test_detected_when_adc_resolves(self): |
| 30 | + with mock.patch("google.auth.default", return_value=(mock.MagicMock(), "proj")): |
| 31 | + assert make_detector().detect() is True |
| 32 | + |
| 33 | + def test_detected_via_metadata_when_adc_unavailable(self): |
| 34 | + with mock.patch( |
| 35 | + "google.auth.default", |
| 36 | + side_effect=exceptions.DefaultCredentialsError("no adc"), |
| 37 | + ), mock.patch(METADATA_IS_ON_GCE, return_value=True): |
| 38 | + assert make_detector().detect() is True |
| 39 | + |
| 40 | + def test_not_detected_when_nothing_available(self): |
| 41 | + with mock.patch( |
| 42 | + "google.auth.default", |
| 43 | + side_effect=exceptions.DefaultCredentialsError("no adc"), |
| 44 | + ), mock.patch(METADATA_IS_ON_GCE, return_value=False): |
| 45 | + assert make_detector().detect() is False |
| 46 | + |
| 47 | + def test_not_detected_on_google_auth_error(self): |
| 48 | + with mock.patch( |
| 49 | + "google.auth.default", side_effect=exceptions.GoogleAuthError("boom") |
| 50 | + ): |
| 51 | + assert make_detector().detect() is False |
| 52 | + |
| 53 | + |
| 54 | +class TestGetTokenMetadata: |
| 55 | + def test_compute_credentials_use_metadata_with_format_full(self): |
| 56 | + compute_creds = mock.Mock(spec=compute_engine.Credentials) |
| 57 | + with mock.patch( |
| 58 | + "google.auth.default", return_value=(compute_creds, "proj") |
| 59 | + ), mock.patch(METADATA_GET, return_value="meta-jwt\n") as get: |
| 60 | + token = make_detector().get_token() |
| 61 | + assert token == "meta-jwt" |
| 62 | + args, kwargs = get.call_args |
| 63 | + assert args[1] == "instance/service-accounts/default/identity" |
| 64 | + assert kwargs["params"] == {"audience": "cloudsmith", "format": "full"} |
| 65 | + |
| 66 | + def test_uses_configured_audience(self): |
| 67 | + compute_creds = mock.Mock(spec=compute_engine.Credentials) |
| 68 | + with mock.patch( |
| 69 | + "google.auth.default", return_value=(compute_creds, "proj") |
| 70 | + ), mock.patch(METADATA_GET, return_value="meta-jwt") as get: |
| 71 | + make_detector(oidc_audience="custom-aud").get_token() |
| 72 | + _, kwargs = get.call_args |
| 73 | + assert kwargs["params"]["audience"] == "custom-aud" |
| 74 | + |
| 75 | + def test_falls_back_to_metadata_when_default_raises(self): |
| 76 | + with mock.patch( |
| 77 | + "google.auth.default", |
| 78 | + side_effect=exceptions.DefaultCredentialsError("no adc"), |
| 79 | + ), mock.patch(METADATA_GET, return_value="meta-jwt"): |
| 80 | + assert make_detector().get_token() == "meta-jwt" |
| 81 | + |
| 82 | + def test_raises_when_metadata_returns_blank(self): |
| 83 | + compute_creds = mock.Mock(spec=compute_engine.Credentials) |
| 84 | + with mock.patch( |
| 85 | + "google.auth.default", return_value=(compute_creds, "proj") |
| 86 | + ), mock.patch(METADATA_GET, return_value=" "): |
| 87 | + with pytest.raises(ValueError): |
| 88 | + make_detector().get_token() |
| 89 | + |
| 90 | + def test_raises_when_metadata_errors(self): |
| 91 | + compute_creds = mock.Mock(spec=compute_engine.Credentials) |
| 92 | + with mock.patch( |
| 93 | + "google.auth.default", return_value=(compute_creds, "proj") |
| 94 | + ), mock.patch(METADATA_GET, side_effect=exceptions.TransportError("boom")): |
| 95 | + with pytest.raises(ValueError): |
| 96 | + make_detector().get_token() |
| 97 | + |
| 98 | + |
| 99 | +class TestGetTokenUserCredentials: |
| 100 | + def test_returns_refreshed_id_token(self): |
| 101 | + user_creds = mock.Mock(spec=oauth2_creds.Credentials) |
| 102 | + user_creds.id_token = "adc-jwt" |
| 103 | + with mock.patch("google.auth.default", return_value=(user_creds, "proj")): |
| 104 | + token = make_detector().get_token() |
| 105 | + assert token == "adc-jwt" |
| 106 | + user_creds.refresh.assert_called_once() |
| 107 | + |
| 108 | + def test_raises_when_user_credentials_have_no_id_token(self): |
| 109 | + user_creds = mock.Mock(spec=oauth2_creds.Credentials) |
| 110 | + user_creds.id_token = None |
| 111 | + with mock.patch("google.auth.default", return_value=(user_creds, "proj")): |
| 112 | + with pytest.raises(ValueError): |
| 113 | + make_detector().get_token() |
| 114 | + |
| 115 | + |
| 116 | +class TestGetTokenIdTokenCredentials: |
| 117 | + def test_delegates_to_fetch_id_token_credentials(self): |
| 118 | + # A non-compute, non-user credential (e.g. service-account key or |
| 119 | + # Workload Identity Federation) is delegated to google-auth's dispatch. |
| 120 | + other_creds = mock.Mock() |
| 121 | + id_creds = mock.Mock(token="sa-jwt") |
| 122 | + with mock.patch( |
| 123 | + "google.auth.default", return_value=(other_creds, "proj") |
| 124 | + ), mock.patch(FETCH_ID_TOKEN_CREDS, return_value=id_creds) as fetch: |
| 125 | + token = make_detector(oidc_audience="aud").get_token() |
| 126 | + assert token == "sa-jwt" |
| 127 | + args, _ = fetch.call_args |
| 128 | + assert args[0] == "aud" |
| 129 | + id_creds.refresh.assert_called_once() |
| 130 | + |
| 131 | + def test_raises_when_dispatch_fails(self): |
| 132 | + other_creds = mock.Mock() |
| 133 | + with mock.patch( |
| 134 | + "google.auth.default", return_value=(other_creds, "proj") |
| 135 | + ), mock.patch( |
| 136 | + FETCH_ID_TOKEN_CREDS, side_effect=exceptions.DefaultCredentialsError("x") |
| 137 | + ): |
| 138 | + with pytest.raises(ValueError): |
| 139 | + make_detector().get_token() |
| 140 | + |
| 141 | + |
| 142 | +class TestRegistration: |
| 143 | + def test_registered_after_aws(self): |
| 144 | + from cloudsmith_cli.core.credentials.oidc.detectors import ( |
| 145 | + _DETECTORS, |
| 146 | + AWSDetector, |
| 147 | + ) |
| 148 | + |
| 149 | + assert _DETECTORS == [AWSDetector, GCPDetector] |
0 commit comments