|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -"""Tests for the shared base model behind the auth credential models.""" |
| 15 | +"""Tests for the auth credential models and their shared base model.""" |
16 | 16 |
|
17 | 17 | from __future__ import annotations |
18 | 18 |
|
| 19 | +from google.adk.auth.auth_credential import AuthCredential |
| 20 | +from google.adk.auth.auth_credential import AuthCredentialTypes |
19 | 21 | from google.adk.auth.auth_credential import BaseModelWithConfig |
| 22 | +from google.adk.auth.auth_credential import HttpAuth |
| 23 | +from google.adk.auth.auth_credential import HttpCredentials |
| 24 | +from google.adk.auth.auth_credential import OAuth2Auth |
| 25 | +from google.adk.auth.auth_credential import ServiceAccountCredential |
| 26 | +import pydantic |
| 27 | +import pytest |
20 | 28 |
|
21 | 29 |
|
22 | 30 | class _Sample(BaseModelWithConfig): |
@@ -46,3 +54,146 @@ def test_base_model_with_config_dumps_camel_case_only_when_asked(): |
46 | 54 | model = _Sample(access_token='abc') |
47 | 55 | assert model.model_dump()['access_token'] == 'abc' |
48 | 56 | assert model.model_dump(by_alias=True)['accessToken'] == 'abc' |
| 57 | + |
| 58 | + |
| 59 | +def test_api_key_redacted_in_repr_and_str(): |
| 60 | + """An API key is not rendered, but is still readable on the model.""" |
| 61 | + cred = AuthCredential( |
| 62 | + auth_type=AuthCredentialTypes.API_KEY, |
| 63 | + api_key='sk-live-secret-api-key-12345', |
| 64 | + ) |
| 65 | + repr_str = repr(cred) |
| 66 | + str_str = str(cred) |
| 67 | + assert 'sk-live-secret-api-key-12345' not in repr_str |
| 68 | + assert 'sk-live-secret-api-key-12345' not in str_str |
| 69 | + # Only the rendering is redacted; the value itself is untouched. |
| 70 | + assert cred.api_key == 'sk-live-secret-api-key-12345' |
| 71 | + |
| 72 | + |
| 73 | +def test_http_credentials_redacted_in_repr_and_str(): |
| 74 | + """HTTP passwords, tokens and auth headers are not rendered.""" |
| 75 | + cred = AuthCredential( |
| 76 | + auth_type=AuthCredentialTypes.HTTP, |
| 77 | + http=HttpAuth( |
| 78 | + scheme='basic', |
| 79 | + credentials=HttpCredentials( |
| 80 | + username='my_user', |
| 81 | + password='secret_password_999', |
| 82 | + token='secret_token_abc', |
| 83 | + ), |
| 84 | + additional_headers={'Authorization': 'Bearer secret_bearer_token'}, |
| 85 | + ), |
| 86 | + ) |
| 87 | + repr_str = repr(cred) |
| 88 | + str_str = str(cred) |
| 89 | + assert 'secret_password_999' not in repr_str |
| 90 | + assert 'secret_token_abc' not in repr_str |
| 91 | + assert 'secret_bearer_token' not in repr_str |
| 92 | + assert 'secret_password_999' not in str_str |
| 93 | + assert 'secret_token_abc' not in str_str |
| 94 | + |
| 95 | + |
| 96 | +def test_oauth2_credentials_redacted_in_repr_and_str(): |
| 97 | + """OAuth2 secrets, tokens and the auth response URI are not rendered.""" |
| 98 | + cred = AuthCredential( |
| 99 | + auth_type=AuthCredentialTypes.OAUTH2, |
| 100 | + oauth2=OAuth2Auth( |
| 101 | + client_id='my_client_id', |
| 102 | + client_secret='top_secret_client_secret', |
| 103 | + access_token='secret_access_token', |
| 104 | + refresh_token='secret_refresh_token', |
| 105 | + id_token='secret_id_token', |
| 106 | + auth_code='secret_auth_code', |
| 107 | + auth_response_uri=( |
| 108 | + 'https://example.com/callback?code=secret_response_code' |
| 109 | + ), |
| 110 | + code_verifier='secret_code_verifier', |
| 111 | + ), |
| 112 | + ) |
| 113 | + repr_str = repr(cred) |
| 114 | + str_str = str(cred) |
| 115 | + assert 'top_secret_client_secret' not in repr_str |
| 116 | + assert 'secret_access_token' not in repr_str |
| 117 | + assert 'secret_refresh_token' not in repr_str |
| 118 | + assert 'secret_id_token' not in repr_str |
| 119 | + assert 'secret_auth_code' not in repr_str |
| 120 | + assert 'secret_response_code' not in repr_str |
| 121 | + assert 'secret_code_verifier' not in repr_str |
| 122 | + assert 'top_secret_client_secret' not in str_str |
| 123 | + assert 'secret_response_code' not in str_str |
| 124 | + |
| 125 | + |
| 126 | +def test_service_account_redacted_in_repr_and_str(): |
| 127 | + """A service account private key and its ID are not rendered.""" |
| 128 | + sa_cred = ServiceAccountCredential( |
| 129 | + type_='service_account', |
| 130 | + project_id='test_project', |
| 131 | + private_key_id='secret_private_key_id', |
| 132 | + private_key=( |
| 133 | + '-----BEGIN PRIVATE KEY-----\nsecret_key_data\n-----END PRIVATE' |
| 134 | + ' KEY-----' |
| 135 | + ), |
| 136 | + client_email='test@iam.gserviceaccount.com', |
| 137 | + client_id='12345', |
| 138 | + auth_uri='https://example.com/o/oauth2/auth', |
| 139 | + token_uri='https://example.com/token', |
| 140 | + auth_provider_x509_cert_url='https://example.com/oauth2/v1/certs', |
| 141 | + client_x509_cert_url='https://example.com/robot/v1/metadata/x509/test', |
| 142 | + universe_domain='example.com', |
| 143 | + ) |
| 144 | + repr_str = repr(sa_cred) |
| 145 | + str_str = str(sa_cred) |
| 146 | + assert 'secret_key_data' not in repr_str |
| 147 | + assert 'secret_private_key_id' not in repr_str |
| 148 | + assert 'secret_key_data' not in str_str |
| 149 | + assert 'secret_private_key_id' not in str_str |
| 150 | + |
| 151 | + |
| 152 | +def test_extra_fields_redacted_in_repr_and_str(): |
| 153 | + """A secret under an undeclared key is redacted, not rendered.""" |
| 154 | + # `extra="allow"` means a secret can arrive under a key the model does not |
| 155 | + # declare, which pydantic would otherwise render in repr unconditionally. |
| 156 | + cred = AuthCredential.model_validate({ |
| 157 | + 'auth_type': AuthCredentialTypes.API_KEY, |
| 158 | + 'undeclared_secret': 'secret_extra_value', |
| 159 | + }) |
| 160 | + repr_str = repr(cred) |
| 161 | + str_str = str(cred) |
| 162 | + assert 'secret_extra_value' not in repr_str |
| 163 | + assert 'secret_extra_value' not in str_str |
| 164 | + # The key is still surfaced so the redaction is visible when debugging, and |
| 165 | + # the value remains readable programmatically. |
| 166 | + assert 'undeclared_secret' in repr_str |
| 167 | + assert cred.undeclared_secret == 'secret_extra_value' |
| 168 | + |
| 169 | + |
| 170 | +def test_nested_extra_fields_redacted_in_repr_and_str(): |
| 171 | + """Undeclared keys on a nested credential model are redacted too.""" |
| 172 | + # Mirrors an OAuth2 provider returning a non-standard token field. |
| 173 | + cred = AuthCredential( |
| 174 | + auth_type=AuthCredentialTypes.OAUTH2, |
| 175 | + oauth2=OAuth2Auth.model_validate({ |
| 176 | + 'client_id': 'my_client_id', |
| 177 | + 'unexpected_token': 'secret_unexpected_token', |
| 178 | + }), |
| 179 | + ) |
| 180 | + repr_str = repr(cred) |
| 181 | + str_str = str(cred) |
| 182 | + assert 'secret_unexpected_token' not in repr_str |
| 183 | + assert 'secret_unexpected_token' not in str_str |
| 184 | + assert 'my_client_id' in repr_str |
| 185 | + |
| 186 | + |
| 187 | +def test_validation_error_does_not_echo_secret_value(): |
| 188 | + """A rejected value is not echoed back in the ValidationError text.""" |
| 189 | + # Pydantic reports the rejected value as `input_value=...` by default, which |
| 190 | + # would put the secret into the error string surfaced to the LLM. |
| 191 | + with pytest.raises(pydantic.ValidationError) as exc_info: |
| 192 | + AuthCredential.model_validate({ |
| 193 | + 'auth_type': AuthCredentialTypes.API_KEY, |
| 194 | + 'api_key': ['sk-live-secret-api-key-12345'], |
| 195 | + }) |
| 196 | + message = str(exc_info.value) |
| 197 | + assert 'sk-live-secret-api-key-12345' not in message |
| 198 | + # The field and the reason are still reported. |
| 199 | + assert 'api_key' in message |
0 commit comments