Skip to content

Commit 5d4dd14

Browse files
authored
Reuse a valid Google access token in GoogleAuthManager (#4376)
GoogleAuthManager refreshed its credentials before every REST catalog request, so each call made an extra round trip to Google's token endpoint. Refresh only when the credentials are not valid, which is what google-auth does itself before a request.
1 parent fce8185 commit 5d4dd14

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎pyiceberg/catalog/rest/auth.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ def __init__(self, credentials_path: str | None = None, scopes: list[str] | None
251251
self._auth_request = google.auth.transport.requests.Request()
252252

253253
def auth_header(self) -> str:
254-
self.credentials.refresh(self._auth_request)
254+
# Only fetch a new token when there is none yet or the current one is close to expiry
255+
if not self.credentials.valid:
256+
self.credentials.refresh(self._auth_request)
255257
return f"Bearer {self.credentials.token}"
256258

257259

‎tests/catalog/test_rest.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,6 +3226,7 @@ def test_rest_catalog_with_google_credentials_path(
32263226
mock_load_creds: mock.MagicMock, mock_google_request: mock.MagicMock, rest_mock: Mocker
32273227
) -> None:
32283228
mock_credentials = mock.MagicMock()
3229+
mock_credentials.valid = False
32293230
mock_credentials.token = "file_token"
32303231
mock_load_creds.return_value = (mock_credentials, "test_project_file")
32313232

‎tests/catalog/test_rest_auth.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
# under the License.
1717

1818
import base64
19+
import json
1920
import pickle
21+
from pathlib import Path
2022
from unittest.mock import MagicMock, patch
2123

2224
import pytest
@@ -57,6 +59,23 @@ def google_mock(requests_mock: Mocker) -> Mocker:
5759
return requests_mock
5860

5961

62+
@pytest.fixture
63+
def google_default_credentials(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
64+
credentials_path = tmp_path / "credentials.json"
65+
credentials_path.write_text(
66+
json.dumps(
67+
{
68+
"type": "authorized_user",
69+
"client_id": "client",
70+
"client_secret": "secret",
71+
"refresh_token": "refresh",
72+
"token_uri": GOOGLE_CREDS_URI,
73+
}
74+
)
75+
)
76+
monkeypatch.setenv("GOOGLE_APPLICATION_CREDENTIALS", str(credentials_path))
77+
78+
6079
def test_noop_auth_header(rest_mock: Mocker) -> None:
6180
auth_manager = NoopAuthManager()
6281
session = requests.Session()
@@ -93,6 +112,7 @@ def test_google_auth_manager_default_credentials(
93112
) -> None:
94113
"""Test GoogleAuthManager with default application credentials."""
95114
mock_credentials = MagicMock()
115+
mock_credentials.valid = False
96116
mock_credentials.token = "test_token"
97117
mock_google_auth_default.return_value = (mock_credentials, "test_project")
98118

@@ -116,6 +136,7 @@ def test_google_auth_manager_with_credentials_file(
116136
) -> None:
117137
"""Test GoogleAuthManager with a credentials file path."""
118138
mock_credentials = MagicMock()
139+
mock_credentials.valid = False
119140
mock_credentials.token = "file_token"
120141
mock_load_creds.return_value = (mock_credentials, "test_project_file")
121142

@@ -139,6 +160,7 @@ def test_google_auth_manager_with_credentials_file_and_scopes(
139160
) -> None:
140161
"""Test GoogleAuthManager with a credentials file path and scopes."""
141162
mock_credentials = MagicMock()
163+
mock_credentials.valid = False
142164
mock_credentials.token = "scoped_token"
143165
mock_load_creds.return_value = (mock_credentials, "test_project_scoped")
144166
scopes = ["https://www.googleapis.com/auth/bigquery"]
@@ -156,6 +178,28 @@ def test_google_auth_manager_with_credentials_file_and_scopes(
156178
assert actual_headers["Authorization"] == "Bearer scoped_token"
157179

158180

181+
def test_google_auth_manager_reuses_valid_token(google_mock: Mocker, google_default_credentials: None) -> None:
182+
session = requests.Session()
183+
session.auth = AuthManagerAdapter(GoogleAuthManager())
184+
for _ in range(3):
185+
session.get(TEST_URI)
186+
187+
history = google_mock.request_history
188+
assert [request.url for request in history].count(GOOGLE_CREDS_URI) == 1
189+
assert [request.headers["Authorization"] for request in history if request.url == TEST_URI] == ["Bearer aaaabbb"] * 3
190+
191+
192+
def test_google_auth_manager_refreshes_expiring_token(google_mock: Mocker, google_default_credentials: None) -> None:
193+
# A token that expires within google-auth's refresh threshold is not valid
194+
google_mock.post(GOOGLE_CREDS_URI, json={"access_token": "aaaabbb", "expires_in": 60}, status_code=200)
195+
session = requests.Session()
196+
session.auth = AuthManagerAdapter(GoogleAuthManager())
197+
for _ in range(3):
198+
session.get(TEST_URI)
199+
200+
assert [request.url for request in google_mock.request_history].count(GOOGLE_CREDS_URI) == 3
201+
202+
159203
def test_google_auth_manager_import_error() -> None:
160204
"""Test GoogleAuthManager raises ImportError if google-auth is not installed."""
161205
with patch.dict("sys.modules", {"google.auth": None, "google.auth.transport.requests": None}):

0 commit comments

Comments
 (0)