Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pyiceberg/catalog/rest/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"


Expand Down
1 change: 1 addition & 0 deletions tests/catalog/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
44 changes: 44 additions & 0 deletions tests/catalog/test_rest_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")

Expand All @@ -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")

Expand All @@ -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"]
Expand All @@ -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}):
Expand Down
Loading