diff --git a/google/auth/impersonated_credentials.py b/google/auth/impersonated_credentials.py index ddafc08ee..7c2f18d74 100644 --- a/google/auth/impersonated_credentials.py +++ b/google/auth/impersonated_credentials.py @@ -438,11 +438,19 @@ def refresh(self, request): self._target_credentials._source_credentials, auth_request=request ) - response = authed_session.post( - url=iam_sign_endpoint, - headers=headers, - data=json.dumps(body).encode("utf-8"), - ) + try: + response = authed_session.post( + url=iam_sign_endpoint, + headers=headers, + data=json.dumps(body).encode("utf-8"), + ) + finally: + authed_session.close() + + if response.status_code != http_client.OK: + raise exceptions.RefreshError( + "Error getting ID token: {}".format(response.json()) + ) id_token = response.json()["token"] self.token = id_token diff --git a/system_tests/secrets.tar.enc b/system_tests/secrets.tar.enc index 8b9e40871..04a24e425 100644 Binary files a/system_tests/secrets.tar.enc and b/system_tests/secrets.tar.enc differ diff --git a/tests/test_impersonated_credentials.py b/tests/test_impersonated_credentials.py index dc091fe61..0c6ca0ce9 100644 --- a/tests/test_impersonated_credentials.py +++ b/tests/test_impersonated_credentials.py @@ -318,6 +318,27 @@ def test_refresh_failure_unauthorzed(self, mock_donor_credentials): assert not credentials.valid assert credentials.expired + def test_refresh_failure(self): + credentials = self.make_credentials(lifetime=None) + credentials.expiry = None + credentials.token = "token" + id_creds = impersonated_credentials.IDTokenCredentials( + credentials, target_audience="audience" + ) + + response = mock.create_autospec(transport.Response, instance=False) + response.status_code = http_client.UNAUTHORIZED + response.json = mock.Mock(return_value="failed to get ID token") + + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.post", + return_value=response, + ): + with pytest.raises(exceptions.RefreshError) as excinfo: + id_creds.refresh(None) + + assert excinfo.match("Error getting ID token") + def test_refresh_failure_http_error(self, mock_donor_credentials): credentials = self.make_credentials(lifetime=None)