|
| 1 | +# ABOUTME: Tests for OIDC id_token caching and silent credential refresh (issue #153) |
| 2 | +# ABOUTME: Verifies that expired AWS credentials can be refreshed without browser popup |
| 3 | +"""Tests for silent credential refresh using cached OIDC id_token.""" |
| 4 | + |
| 5 | +import json |
| 6 | +import time |
| 7 | +from unittest.mock import MagicMock, patch |
| 8 | + |
| 9 | +import jwt as pyjwt |
| 10 | +import pytest |
| 11 | + |
| 12 | + |
| 13 | +def _make_id_token(exp_offset=3600, email="test@example.com"): |
| 14 | + """Create a minimal JWT id_token for testing. |
| 15 | +
|
| 16 | + Args: |
| 17 | + exp_offset: Seconds from now until expiration (positive = future). |
| 18 | + email: Email claim to embed. |
| 19 | + """ |
| 20 | + claims = { |
| 21 | + "sub": "user-123", |
| 22 | + "email": email, |
| 23 | + "iss": "https://test.okta.com", |
| 24 | + "aud": "test-client-id", |
| 25 | + "exp": int(time.time()) + exp_offset, |
| 26 | + "iat": int(time.time()), |
| 27 | + "nonce": "test-nonce", |
| 28 | + } |
| 29 | + # Encode without signing (matches how the provider decodes with verify_signature=False) |
| 30 | + return pyjwt.encode(claims, "secret", algorithm="HS256"), claims |
| 31 | + |
| 32 | + |
| 33 | +def _make_config(): |
| 34 | + """Return a minimal config dict for MultiProviderAuth.""" |
| 35 | + return { |
| 36 | + "profiles": { |
| 37 | + "TestProfile": { |
| 38 | + "provider_domain": "test.okta.com", |
| 39 | + "client_id": "test-client-id", |
| 40 | + "identity_pool_id": "us-east-1:test-pool", |
| 41 | + "aws_region": "us-east-1", |
| 42 | + "credential_storage": "session", |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | +def _make_aws_credentials(exp_offset=900): |
| 49 | + """Return fake AWS credentials dict.""" |
| 50 | + from datetime import datetime, timezone, timedelta |
| 51 | + |
| 52 | + exp = datetime.now(timezone.utc) + timedelta(seconds=exp_offset) |
| 53 | + return { |
| 54 | + "Version": 1, |
| 55 | + "AccessKeyId": "AKIAIOSFODNN7EXAMPLE", |
| 56 | + "SecretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", |
| 57 | + "SessionToken": "FwoGZXIvYXdzEBYaDH...", |
| 58 | + "Expiration": exp.isoformat(), |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture |
| 63 | +def auth_instance(tmp_path): |
| 64 | + """Create a MultiProviderAuth instance with mocked config.""" |
| 65 | + config_file = tmp_path / "config.json" |
| 66 | + config_file.write_text(json.dumps(_make_config())) |
| 67 | + |
| 68 | + with patch("credential_provider.__main__.Path") as mock_path_cls: |
| 69 | + # Make _load_config find our temp config |
| 70 | + mock_home = MagicMock() |
| 71 | + mock_path_cls.home.return_value = mock_home |
| 72 | + mock_home.__truediv__ = lambda self, key: tmp_path / key if key == "claude-code-with-bedrock" else MagicMock() |
| 73 | + |
| 74 | + # Also mock __file__ parent for binary dir config lookup |
| 75 | + mock_file_parent = MagicMock() |
| 76 | + mock_file_parent.__truediv__ = lambda self, key: MagicMock(exists=lambda: False) |
| 77 | + mock_path_cls.return_value = mock_file_parent |
| 78 | + |
| 79 | + # Simpler approach: just patch _load_config and _init_credential_storage |
| 80 | + with patch("credential_provider.__main__.MultiProviderAuth._load_config") as mock_load, \ |
| 81 | + patch("credential_provider.__main__.MultiProviderAuth._init_credential_storage"): |
| 82 | + mock_load.return_value = { |
| 83 | + "provider_domain": "test.okta.com", |
| 84 | + "client_id": "test-client-id", |
| 85 | + "identity_pool_id": "us-east-1:test-pool", |
| 86 | + "aws_region": "us-east-1", |
| 87 | + "credential_storage": "session", |
| 88 | + "provider_type": "okta", |
| 89 | + "federation_type": "cognito", |
| 90 | + "max_session_duration": 28800, |
| 91 | + } |
| 92 | + |
| 93 | + from credential_provider.__main__ import MultiProviderAuth |
| 94 | + instance = MultiProviderAuth(profile="TestProfile") |
| 95 | + instance.cache_dir = tmp_path / "cache" |
| 96 | + instance.cache_dir.mkdir(parents=True, exist_ok=True) |
| 97 | + return instance |
| 98 | + |
| 99 | + |
| 100 | +class TestSilentRefresh: |
| 101 | + """Tests for _try_silent_refresh method.""" |
| 102 | + |
| 103 | + def test_silent_refresh_succeeds_with_valid_id_token(self, auth_instance): |
| 104 | + """When a valid id_token is cached, silent refresh should return new AWS creds.""" |
| 105 | + id_token, claims = _make_id_token(exp_offset=3600) |
| 106 | + aws_creds = _make_aws_credentials() |
| 107 | + |
| 108 | + with patch.object(auth_instance, "get_monitoring_token", return_value=id_token), \ |
| 109 | + patch.object(auth_instance, "get_aws_credentials", return_value=aws_creds) as mock_get_creds, \ |
| 110 | + patch.object(auth_instance, "save_credentials") as mock_save, \ |
| 111 | + patch.object(auth_instance, "save_monitoring_token") as mock_save_token: |
| 112 | + |
| 113 | + creds, returned_token, returned_claims = auth_instance._try_silent_refresh() |
| 114 | + |
| 115 | + assert creds is not None |
| 116 | + assert creds["AccessKeyId"] == aws_creds["AccessKeyId"] |
| 117 | + assert returned_token == id_token |
| 118 | + assert returned_claims["sub"] == claims["sub"] |
| 119 | + mock_get_creds.assert_called_once() |
| 120 | + mock_save.assert_called_once_with(aws_creds) |
| 121 | + # Verify the id_token is re-persisted so the next refresh also works |
| 122 | + mock_save_token.assert_called_once_with(id_token, claims) |
| 123 | + |
| 124 | + def test_silent_refresh_returns_none_when_id_token_expired(self, auth_instance): |
| 125 | + """When cached id_token is within the 60-second expiry buffer, get_monitoring_token |
| 126 | + returns None and silent refresh must not attempt an STS exchange.""" |
| 127 | + with patch.object(auth_instance, "get_monitoring_token", return_value=None) as mock_get_token, \ |
| 128 | + patch.object(auth_instance, "get_aws_credentials") as mock_get_creds: |
| 129 | + |
| 130 | + creds, id_token, token_claims = auth_instance._try_silent_refresh() |
| 131 | + |
| 132 | + assert creds is None |
| 133 | + assert id_token is None |
| 134 | + assert token_claims is None |
| 135 | + mock_get_token.assert_called_once() |
| 136 | + # STS must never be called when the token is expired |
| 137 | + mock_get_creds.assert_not_called() |
| 138 | + |
| 139 | + def test_silent_refresh_returns_none_when_no_cached_token(self, auth_instance): |
| 140 | + """When no id_token is cached, silent refresh should return None.""" |
| 141 | + with patch.object(auth_instance, "get_monitoring_token", return_value=None): |
| 142 | + creds, id_token, token_claims = auth_instance._try_silent_refresh() |
| 143 | + assert creds is None |
| 144 | + assert id_token is None |
| 145 | + assert token_claims is None |
| 146 | + |
| 147 | + def test_silent_refresh_returns_none_when_sts_exchange_fails(self, auth_instance): |
| 148 | + """When id_token is valid but STS exchange fails, should return None (fallback to browser).""" |
| 149 | + id_token, _ = _make_id_token(exp_offset=3600) |
| 150 | + |
| 151 | + with patch.object(auth_instance, "get_monitoring_token", return_value=id_token), \ |
| 152 | + patch.object(auth_instance, "get_aws_credentials", side_effect=Exception("STS error")): |
| 153 | + |
| 154 | + creds, returned_token, returned_claims = auth_instance._try_silent_refresh() |
| 155 | + assert creds is None |
| 156 | + assert returned_token is None |
| 157 | + assert returned_claims is None |
| 158 | + |
| 159 | + def test_silent_refresh_not_called_when_aws_creds_valid(self, auth_instance): |
| 160 | + """When AWS credentials are still valid, silent refresh should not be attempted.""" |
| 161 | + aws_creds = _make_aws_credentials(exp_offset=3600) |
| 162 | + |
| 163 | + with patch.object(auth_instance, "get_cached_credentials", return_value=aws_creds), \ |
| 164 | + patch.object(auth_instance, "_try_silent_refresh") as mock_silent, \ |
| 165 | + patch.object(auth_instance, "_should_recheck_quota", return_value=False): |
| 166 | + |
| 167 | + # Capture stdout |
| 168 | + with patch("builtins.print"): |
| 169 | + auth_instance.run() |
| 170 | + |
| 171 | + mock_silent.assert_not_called() |
| 172 | + |
| 173 | + def test_run_uses_silent_refresh_before_browser(self, auth_instance): |
| 174 | + """When AWS creds expired but id_token valid, run() should use silent refresh.""" |
| 175 | + aws_creds = _make_aws_credentials(exp_offset=3600) |
| 176 | + |
| 177 | + with patch.object(auth_instance, "get_cached_credentials", return_value=None), \ |
| 178 | + patch("socket.socket") as mock_socket_cls, \ |
| 179 | + patch.object(auth_instance, "_try_silent_refresh", return_value=(aws_creds, None, None)), \ |
| 180 | + patch.object(auth_instance, "_should_check_quota", return_value=False), \ |
| 181 | + patch.object(auth_instance, "authenticate_oidc") as mock_browser, \ |
| 182 | + patch("builtins.print"): |
| 183 | + |
| 184 | + # Mock socket to simulate port available |
| 185 | + mock_socket = MagicMock() |
| 186 | + mock_socket_cls.return_value = mock_socket |
| 187 | + |
| 188 | + result = auth_instance.run() |
| 189 | + |
| 190 | + assert result == 0 |
| 191 | + mock_browser.assert_not_called() |
| 192 | + |
| 193 | + def test_run_falls_back_to_browser_when_silent_refresh_fails(self, auth_instance): |
| 194 | + """When silent refresh fails, run() should fall back to browser auth.""" |
| 195 | + id_token, claims = _make_id_token(exp_offset=3600) |
| 196 | + aws_creds = _make_aws_credentials(exp_offset=3600) |
| 197 | + |
| 198 | + with patch.object(auth_instance, "get_cached_credentials", return_value=None), \ |
| 199 | + patch("socket.socket") as mock_socket_cls, \ |
| 200 | + patch.object(auth_instance, "_try_silent_refresh", return_value=(None, None, None)), \ |
| 201 | + patch.object(auth_instance, "authenticate_oidc", return_value=(id_token, claims)) as mock_browser, \ |
| 202 | + patch.object(auth_instance, "_should_check_quota", return_value=False), \ |
| 203 | + patch.object(auth_instance, "get_aws_credentials", return_value=aws_creds), \ |
| 204 | + patch.object(auth_instance, "save_credentials"), \ |
| 205 | + patch.object(auth_instance, "save_monitoring_token"), \ |
| 206 | + patch("builtins.print"): |
| 207 | + |
| 208 | + mock_socket = MagicMock() |
| 209 | + mock_socket_cls.return_value = mock_socket |
| 210 | + |
| 211 | + result = auth_instance.run() |
| 212 | + |
| 213 | + assert result == 0 |
| 214 | + mock_browser.assert_called_once() |
| 215 | + |
0 commit comments