diff --git a/pyiceberg/catalog/rest/auth.py b/pyiceberg/catalog/rest/auth.py index b24822cfa7..05ef4b5db0 100644 --- a/pyiceberg/catalog/rest/auth.py +++ b/pyiceberg/catalog/rest/auth.py @@ -251,7 +251,9 @@ def __init__(self, credentials_path: str | None = None, scopes: list[str] | None self._auth_request = google.auth.transport.requests.Request() def auth_header(self) -> str: - self.credentials.refresh(self._auth_request) + # Only fetch a new token when there is none yet or the current one is close to expiry + if not self.credentials.valid: + self.credentials.refresh(self._auth_request) return f"Bearer {self.credentials.token}" diff --git a/tests/catalog/test_rest.py b/tests/catalog/test_rest.py index 901b66ef4e..3d2b64da92 100644 --- a/tests/catalog/test_rest.py +++ b/tests/catalog/test_rest.py @@ -3226,6 +3226,7 @@ def test_rest_catalog_with_google_credentials_path( mock_load_creds: mock.MagicMock, mock_google_request: mock.MagicMock, rest_mock: Mocker ) -> None: mock_credentials = mock.MagicMock() + mock_credentials.valid = False mock_credentials.token = "file_token" mock_load_creds.return_value = (mock_credentials, "test_project_file") diff --git a/tests/catalog/test_rest_auth.py b/tests/catalog/test_rest_auth.py index 3202fa7d8f..8cf667f85d 100644 --- a/tests/catalog/test_rest_auth.py +++ b/tests/catalog/test_rest_auth.py @@ -16,7 +16,9 @@ # under the License. import base64 +import json import pickle +from pathlib import Path from unittest.mock import MagicMock, patch import pytest @@ -57,6 +59,23 @@ def google_mock(requests_mock: Mocker) -> Mocker: return requests_mock +@pytest.fixture +def google_default_credentials(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + credentials_path = tmp_path / "credentials.json" + credentials_path.write_text( + json.dumps( + { + "type": "authorized_user", + "client_id": "client", + "client_secret": "secret", + "refresh_token": "refresh", + "token_uri": GOOGLE_CREDS_URI, + } + ) + ) + monkeypatch.setenv("GOOGLE_APPLICATION_CREDENTIALS", str(credentials_path)) + + def test_noop_auth_header(rest_mock: Mocker) -> None: auth_manager = NoopAuthManager() session = requests.Session() @@ -93,6 +112,7 @@ def test_google_auth_manager_default_credentials( ) -> None: """Test GoogleAuthManager with default application credentials.""" mock_credentials = MagicMock() + mock_credentials.valid = False mock_credentials.token = "test_token" mock_google_auth_default.return_value = (mock_credentials, "test_project") @@ -116,6 +136,7 @@ def test_google_auth_manager_with_credentials_file( ) -> None: """Test GoogleAuthManager with a credentials file path.""" mock_credentials = MagicMock() + mock_credentials.valid = False mock_credentials.token = "file_token" mock_load_creds.return_value = (mock_credentials, "test_project_file") @@ -139,6 +160,7 @@ def test_google_auth_manager_with_credentials_file_and_scopes( ) -> None: """Test GoogleAuthManager with a credentials file path and scopes.""" mock_credentials = MagicMock() + mock_credentials.valid = False mock_credentials.token = "scoped_token" mock_load_creds.return_value = (mock_credentials, "test_project_scoped") scopes = ["https://www.googleapis.com/auth/bigquery"] @@ -156,6 +178,28 @@ def test_google_auth_manager_with_credentials_file_and_scopes( assert actual_headers["Authorization"] == "Bearer scoped_token" +def test_google_auth_manager_reuses_valid_token(google_mock: Mocker, google_default_credentials: None) -> None: + session = requests.Session() + session.auth = AuthManagerAdapter(GoogleAuthManager()) + for _ in range(3): + session.get(TEST_URI) + + history = google_mock.request_history + assert [request.url for request in history].count(GOOGLE_CREDS_URI) == 1 + assert [request.headers["Authorization"] for request in history if request.url == TEST_URI] == ["Bearer aaaabbb"] * 3 + + +def test_google_auth_manager_refreshes_expiring_token(google_mock: Mocker, google_default_credentials: None) -> None: + # A token that expires within google-auth's refresh threshold is not valid + google_mock.post(GOOGLE_CREDS_URI, json={"access_token": "aaaabbb", "expires_in": 60}, status_code=200) + session = requests.Session() + session.auth = AuthManagerAdapter(GoogleAuthManager()) + for _ in range(3): + session.get(TEST_URI) + + assert [request.url for request in google_mock.request_history].count(GOOGLE_CREDS_URI) == 3 + + def test_google_auth_manager_import_error() -> None: """Test GoogleAuthManager raises ImportError if google-auth is not installed.""" with patch.dict("sys.modules", {"google.auth": None, "google.auth.transport.requests": None}):