Skip to content

Commit ee86b4b

Browse files
authored
fix(io): pass GCS token expiry to gcsfs (#4089)
1 parent f3e735e commit ee86b4b

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎pyiceberg/io/fsspec.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
GCS_SERVICE_HOST,
6767
GCS_SESSION_KWARGS,
6868
GCS_TOKEN,
69+
GCS_TOKEN_EXPIRES_AT_MS,
6970
GCS_VERSION_AWARE,
7071
HF_ENDPOINT,
7172
HF_TOKEN,
@@ -95,6 +96,7 @@
9596
)
9697
from pyiceberg.typedef import Properties
9798
from pyiceberg.types import strtobool
99+
from pyiceberg.utils.datetime import millis_to_datetime
98100
from pyiceberg.utils.properties import get_first_property_value, get_header_properties, property_as_bool
99101
from pyiceberg.utils.transient import _TransientStateMixin
100102

@@ -258,11 +260,16 @@ def _s3(properties: Properties) -> AbstractFileSystem:
258260
def _gs(properties: Properties) -> AbstractFileSystem:
259261
# https://gcsfs.readthedocs.io/en/latest/api.html#gcsfs.core.GCSFileSystem
260262
from gcsfs import GCSFileSystem
263+
from google.oauth2.credentials import Credentials
264+
265+
token: str | Credentials | None = properties.get(GCS_TOKEN)
266+
if token and (expires_at := properties.get(GCS_TOKEN_EXPIRES_AT_MS)):
267+
token = Credentials(token, expiry=millis_to_datetime(int(expires_at)))
261268

262269
return GCSFileSystem(
263270
project=properties.get(GCS_PROJECT_ID),
264271
access=properties.get(GCS_ACCESS, "full_control"),
265-
token=properties.get(GCS_TOKEN),
272+
token=token,
266273
consistency=properties.get(GCS_CONSISTENCY, "none"),
267274
cache_timeout=properties.get(GCS_CACHE_TIMEOUT),
268275
requester_pays=property_as_bool(properties, GCS_REQUESTER_PAYS, False),

‎tests/io/test_fsspec.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import tempfile
2222
import threading
2323
import uuid
24+
from datetime import datetime
2425
from typing import Any
2526
from unittest import mock
2627

@@ -32,6 +33,7 @@
3233
from botocore.credentials import Credentials
3334
from fsspec.implementations.local import LocalFileSystem
3435
from fsspec.spec import AbstractFileSystem
36+
from google.oauth2.credentials import Credentials as GoogleCredentials
3537
from requests_mock import Mocker
3638

3739
from pyiceberg.catalog.rest import _user_agent
@@ -763,6 +765,29 @@ def test_adls_account_name_not_overridden_when_in_properties() -> None:
763765
)
764766

765767

768+
def test_fsspec_gcs_token_with_expiry() -> None:
769+
"""Test that a GCS token with an expiry is passed to gcsfs as google-auth credentials."""
770+
properties: Properties = {"gcs.oauth2.token": "token", "gcs.oauth2.token-expires-at": "1700000000000"}
771+
772+
with mock.patch("gcsfs.GCSFileSystem") as mock_gcsfs:
773+
FsspecFileIO(properties=properties).new_input(location="gs://bucket/path/file.parquet")
774+
775+
token = mock_gcsfs.call_args.kwargs["token"]
776+
assert isinstance(token, GoogleCredentials)
777+
assert token.token == "token"
778+
assert token.expiry == datetime(2023, 11, 14, 22, 13, 20)
779+
780+
781+
def test_fsspec_gcs_token_without_expiry() -> None:
782+
"""Test that a GCS token without an expiry is passed to gcsfs unchanged."""
783+
properties: Properties = {"gcs.oauth2.token": "token"}
784+
785+
with mock.patch("gcsfs.GCSFileSystem") as mock_gcsfs:
786+
FsspecFileIO(properties=properties).new_input(location="gs://bucket/path/file.parquet")
787+
788+
assert mock_gcsfs.call_args.kwargs["token"] == "token"
789+
790+
766791
@pytest.mark.gcs
767792
def test_fsspec_new_input_file_gcs(fsspec_fileio_gcs: FsspecFileIO) -> None:
768793
"""Test creating a new input file from a fsspec file-io"""

0 commit comments

Comments
 (0)