Skip to content

Commit 86014bd

Browse files
rahulsmahadevIsaac
andcommitted
Send the pyiceberg User-Agent on S3 remote signing requests
S3V4RestSigner posts to the REST catalog's signing endpoint from its own requests session, so those calls went out as python-requests/<version> instead of the pyiceberg User-Agent that the catalog sends. Share the value through a small _user_agent() helper and set it on signer requests the same way the catalog session does, after the header properties. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 9eb8395 commit 86014bd

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

‎pyiceberg/catalog/rest/__init__.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ def _retry_hook(retry_state: RetryCallState) -> None:
361361
rest_catalog._refresh_token() # pylint: disable=protected-access
362362

363363

364+
def _user_agent() -> str:
365+
"""Return the User-Agent recommended by the REST catalog spec, for example `pyiceberg/0.11.0 (cpython 3.11.4)`."""
366+
return f"pyiceberg/{__version__} ({platform.python_implementation().lower()} {platform.python_version()})"
367+
368+
364369
_RETRY_ARGS = {
365370
"retry": retry_if_exception_type((AuthorizationExpiredError, UnauthorizedError)),
366371
"stop": stop_after_attempt(2),
@@ -1232,9 +1237,7 @@ def _config_headers(self, session: Session) -> None:
12321237
header_properties = get_header_properties(self.properties)
12331238
session.headers.update(header_properties)
12341239
session.headers["Content-type"] = "application/json"
1235-
session.headers["User-Agent"] = (
1236-
f"pyiceberg/{__version__} ({platform.python_implementation().lower()} {platform.python_version()})"
1237-
)
1240+
session.headers["User-Agent"] = _user_agent()
12381241
session.headers["X-Client-Version"] = f"PyIceberg {__version__}"
12391242
session.headers.setdefault("X-Iceberg-Access-Delegation", ACCESS_DELEGATION_DEFAULT)
12401243

‎pyiceberg/io/fsspec.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from typing_extensions import override
3939

4040
from pyiceberg.catalog import TOKEN, URI
41+
from pyiceberg.catalog.rest import _user_agent
4142
from pyiceberg.catalog.rest.auth import AUTH_MANAGER
4243
from pyiceberg.exceptions import SignError
4344
from pyiceberg.io import (
@@ -141,6 +142,7 @@ def __call__(self, request: "AWSRequest", operation_name: str | None = None, **_
141142
signer_headers["Authorization"] = auth_header
142143

143144
signer_headers.update(get_header_properties(self.properties))
145+
signer_headers["User-Agent"] = _user_agent()
144146

145147
signer_body = {
146148
"method": request.method,

‎tests/io/test_fsspec.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from fsspec.spec import AbstractFileSystem
3333
from requests_mock import Mocker
3434

35+
from pyiceberg.catalog.rest import _user_agent
3536
from pyiceberg.catalog.rest.auth import AUTH_MANAGER
3637
from pyiceberg.exceptions import SignError
3738
from pyiceberg.io import fsspec
@@ -1028,6 +1029,7 @@ def test_s3v4_rest_signer(requests_mock: Mocker) -> None:
10281029
"x-amz-content-sha256": "UNSIGNED-PAYLOAD",
10291030
"X-Custom-Header": "value",
10301031
}
1032+
assert requests_mock.last_request.headers["User-Agent"] == _user_agent()
10311033

10321034

10331035
@pytest.mark.s3

0 commit comments

Comments
 (0)