diff --git a/pyiceberg/catalog/rest/__init__.py b/pyiceberg/catalog/rest/__init__.py index 2ad3684c9a..63b933ebf4 100644 --- a/pyiceberg/catalog/rest/__init__.py +++ b/pyiceberg/catalog/rest/__init__.py @@ -1655,14 +1655,26 @@ def commit_table( table_identifier = TableIdentifier(namespace=identifier[:-1], name=identifier[-1]) table_request = CommitTableRequest(identifier=table_identifier, requirements=requirements, updates=updates) - headers = self._session.headers + post_kwargs: dict[str, Any] = { + "data": table_request.model_dump_json().encode(UTF8), + "headers": self._mutation_headers(), + } if table_token := table.config.get(TOKEN): - headers[AUTHORIZATION_HEADER] = f"{BEARER_PREFIX} {table_token}" + table_auth = f"{BEARER_PREFIX} {table_token}" + post_kwargs["headers"] = {**post_kwargs["headers"], AUTHORIZATION_HEADER: table_auth} + + # Session.auth would otherwise overwrite Authorization with the + # catalog token. Apply the table-scoped token on this request only + # and leave the session headers untouched. + def _apply_table_token(request: PreparedRequest) -> PreparedRequest: + request.headers[AUTHORIZATION_HEADER] = table_auth + return request + + post_kwargs["auth"] = _apply_table_token response = self._session.post( self.url(Endpoints.update_table, prefixed=True, **self._split_identifier_for_path(table_request.identifier)), - data=table_request.model_dump_json().encode(UTF8), - headers={**headers, **self._mutation_headers()}, + **post_kwargs, ) try: response.raise_for_status() diff --git a/tests/catalog/test_rest.py b/tests/catalog/test_rest.py index 3d2b64da92..b4921cca20 100644 --- a/tests/catalog/test_rest.py +++ b/tests/catalog/test_rest.py @@ -36,7 +36,7 @@ from requests_mock import Mocker import pyiceberg -from pyiceberg.catalog import PropertiesUpdateSummary, load_catalog +from pyiceberg.catalog import TOKEN, PropertiesUpdateSummary, load_catalog from pyiceberg.catalog.rest import ( DEFAULT_ENDPOINTS, EMPTY_BODY_SHA256, @@ -2977,6 +2977,34 @@ def test_table_identifier_in_commit_table_request( ) +def test_commit_table_does_not_mutate_session_headers(rest_mock: Mocker, example_table_metadata_v2: dict[str, Any]) -> None: + metadata_location = "s3://some_bucket/metadata.json" + table_token = "table-scoped-token" + rest_mock.post( + url=f"{TEST_URI}v1/namespaces/namespace/tables/table_name", + json={ + "metadata": example_table_metadata_v2, + "metadata-location": metadata_location, + }, + status_code=200, + ) + catalog = RestCatalog("catalog_name", uri=TEST_URI, token=TEST_TOKEN) + session_auth = catalog._session.headers.get("Authorization") + table = Table( + identifier=("namespace", "table_name"), + metadata=None, # type: ignore + metadata_location=metadata_location, + io=None, # type: ignore + catalog=catalog, + config={TOKEN: table_token}, + ) + + catalog.commit_table(table, (), ()) + + assert catalog._session.headers.get("Authorization") == session_auth + assert rest_mock.last_request.headers["Authorization"] == f"Bearer {table_token}" + + def test_register_view_200(rest_mock: Mocker, example_view_metadata_rest_json: dict[str, Any]) -> None: rest_mock.head( f"{TEST_URI}v1/namespaces/default/tables/registered_view", diff --git a/tests/io/test_fsspec.py b/tests/io/test_fsspec.py index 37c0bd979f..9516c05dec 100644 --- a/tests/io/test_fsspec.py +++ b/tests/io/test_fsspec.py @@ -18,6 +18,7 @@ import asyncio import os import pickle +import sys import tempfile import threading import uuid @@ -37,8 +38,8 @@ from pyiceberg.catalog.rest import _user_agent from pyiceberg.catalog.rest.auth import AUTH_MANAGER from pyiceberg.exceptions import SignError -from pyiceberg.io import fsspec -from pyiceberg.io.fsspec import FsspecFileIO, S3V4RestSigner +from pyiceberg.io import S3_SIGNER, fsspec +from pyiceberg.io.fsspec import FsspecFileIO, S3V4RestSigner, _s3 from pyiceberg.io.pyarrow import PyArrowFileIO from pyiceberg.typedef import Properties from tests.conftest import UNIFIED_AWS_SESSION_PROPERTIES