Skip to content

Commit 7d5c58d

Browse files
rinzoolQuentin FLEURENT NAMBOT
andauthored
API: fix: inject header prior to AuthManager (#2656)
<!-- Thanks for opening a pull request! --> <!-- In the case this PR will resolve an issue, please replace ${GITHUB_ISSUE_ID} below with the actual Github issue id. --> Closes #2655 # Rationale for this change Headers should be injected before creating auth manager in order for Polaris catalog to work. ## Are these changes tested? Yes, with following code: ```python from pyiceberg.catalog import load_catalog param = { "type": "rest", "uri": "http://localhost:8181/api/catalog", "warehouse": "s3://...", "s3.access-key-id": "***", "s3.secret-access-key": "***", "s3.path-style-access": True, "header.X-Iceberg-Access-Delegation": "", "scope": "PRINCIPAL_ROLE:ALL", "credential": "***:***", } catalog = load_catalog("default", **param) print(catalog.list_namespaces()) ``` * With version 0.10.0 I have: `pyiceberg.exceptions.RESTError: MissingOrInvalidRealm` * With this development (using `poetry run ...`) I can connect to the catalog ## Are there any user-facing changes? No <!-- In the case of user-facing changes, please add the changelog label. --> --------- Co-authored-by: Quentin FLEURENT NAMBOT <[email protected]>
1 parent 284e479 commit 7d5c58d

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

pyiceberg/catalog/rest/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ def _create_session(self) -> Session:
238238
"""Create a request session with provided catalog configuration."""
239239
session = Session()
240240

241+
# Set HTTP headers
242+
self._config_headers(session)
243+
241244
# Sets the client side and server side SSL cert verification, if provided as properties.
242245
if ssl_config := self.properties.get(SSL):
243246
if ssl_ca_bundle := ssl_config.get(CA_BUNDLE):
@@ -265,9 +268,6 @@ def _create_session(self) -> Session:
265268
else:
266269
session.auth = AuthManagerAdapter(self._create_legacy_oauth2_auth_manager(session))
267270

268-
# Set HTTP headers
269-
self._config_headers(session)
270-
271271
# Configure SigV4 Request Signing
272272
if property_as_bool(self.properties, SIGV4, False):
273273
self._init_sigv4(session)

tests/catalog/test_rest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,6 +1899,28 @@ def test_rest_catalog_with_google_credentials_path(
18991899
assert actual_headers["Authorization"] == expected_auth_header
19001900

19011901

1902+
@pytest.mark.filterwarnings(
1903+
"ignore:Deprecated in 0.8.0, will be removed in 1.0.0. Iceberg REST client is missing the OAuth2 server URI:DeprecationWarning"
1904+
)
1905+
def test_auth_header(rest_mock: Mocker) -> None:
1906+
mock_request = rest_mock.post(
1907+
f"{TEST_URI}v1/oauth/tokens",
1908+
json={
1909+
"access_token": TEST_TOKEN,
1910+
"token_type": "Bearer",
1911+
"expires_in": 86400,
1912+
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
1913+
"scope": "openid offline",
1914+
"refresh_token": "refresh_token",
1915+
},
1916+
status_code=200,
1917+
request_headers={**OAUTH_TEST_HEADERS, "Custom": "Value"},
1918+
)
1919+
1920+
RestCatalog("rest", uri=TEST_URI, credential=TEST_CREDENTIALS, audience="", resource="", **{"header.Custom": "Value"})
1921+
assert mock_request.last_request.text == "grant_type=client_credentials&client_id=client&client_secret=secret&scope=catalog"
1922+
1923+
19021924
class TestRestCatalogClose:
19031925
"""Tests RestCatalog close functionality"""
19041926

0 commit comments

Comments
 (0)