Fokko commented on code in PR #4493:
URL: https://github.com/apache/iceberg-python/pull/4493#discussion_r4770215151


##########
pyiceberg/catalog/rest/__init__.py:
##########
@@ -54,6 +54,7 @@
     ScanTasks,
     StorageCredential,
 )
+from pyiceberg.catalog.rest.sigv4 import init_sigv4

Review Comment:
   **Blocking:** Could we bring back the re-exports? These names were 
importable from `pyiceberg.catalog.rest`, so removing them breaks users without 
a deprecation cycle. With `implicit_reexport = false`, mypy needs the redundant 
alias. `make lint` will split this into one statement per name, which is fine:
   ```suggestion
   from pyiceberg.catalog.rest.sigv4 import (
       EMPTY_BODY_SHA256 as EMPTY_BODY_SHA256,
       SIGV4_MAX_RETRIES as SIGV4_MAX_RETRIES,
       SIGV4_MAX_RETRIES_DEFAULT as SIGV4_MAX_RETRIES_DEFAULT,
       SIGV4_REGION as SIGV4_REGION,
       SIGV4_SERVICE as SIGV4_SERVICE,
       init_sigv4,
   )
   ```



##########
pyiceberg/catalog/rest/sigv4.py:
##########
@@ -0,0 +1,96 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from __future__ import annotations
+
+from typing import Any
+from urllib import parse
+
+from requests import PreparedRequest, Session
+from requests.adapters import HTTPAdapter
+
+from pyiceberg.catalog import BOTOCORE_SESSION
+from pyiceberg.io import (
+    AWS_ACCESS_KEY_ID,
+    AWS_PROFILE_NAME,
+    AWS_REGION,
+    AWS_SECRET_ACCESS_KEY,
+    AWS_SESSION_TOKEN,
+)
+from pyiceberg.typedef import Properties
+from pyiceberg.utils.properties import get_first_property_value, 
property_as_int
+
+SIGV4_REGION = "rest.signing-region"
+SIGV4_SERVICE = "rest.signing-name"
+SIGV4_MAX_RETRIES = "rest.sigv4.max-retries"
+SIGV4_MAX_RETRIES_DEFAULT = 10
+EMPTY_BODY_SHA256: str = 
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
+
+
+class SigV4Adapter(HTTPAdapter):

Review Comment:
   Should we keep these internal for now, e.g. `_SigV4Adapter` and 
`_init_sigv4`? Before this PR, the adapter was a local class and `_init_sigv4` 
was private, so neither was part of the API. #3120 is going to rework this 
code, and the less we expose publicly, the easier it is to evolve without a 
deprecation cycle. We can always make them public later :)



##########
pyiceberg/catalog/rest/sigv4.py:
##########
@@ -0,0 +1,96 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from __future__ import annotations
+
+from typing import Any
+from urllib import parse
+
+from requests import PreparedRequest, Session
+from requests.adapters import HTTPAdapter
+
+from pyiceberg.catalog import BOTOCORE_SESSION
+from pyiceberg.io import (
+    AWS_ACCESS_KEY_ID,
+    AWS_PROFILE_NAME,
+    AWS_REGION,
+    AWS_SECRET_ACCESS_KEY,
+    AWS_SESSION_TOKEN,
+)
+from pyiceberg.typedef import Properties
+from pyiceberg.utils.properties import get_first_property_value, 
property_as_int
+
+SIGV4_REGION = "rest.signing-region"
+SIGV4_SERVICE = "rest.signing-name"
+SIGV4_MAX_RETRIES = "rest.sigv4.max-retries"
+SIGV4_MAX_RETRIES_DEFAULT = 10
+EMPTY_BODY_SHA256: str = 
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
+
+
+class SigV4Adapter(HTTPAdapter):
+    """HTTP adapter that signs REST requests with AWS Signature Version 4."""
+
+    def __init__(self, **properties: str) -> None:
+        import boto3
+
+        self._properties = properties
+        max_retries = property_as_int(self._properties, SIGV4_MAX_RETRIES, 
SIGV4_MAX_RETRIES_DEFAULT)
+        super().__init__(max_retries=max_retries)
+        self._boto_session = boto3.Session(
+            profile_name=get_first_property_value(self._properties, 
AWS_PROFILE_NAME),
+            region_name=get_first_property_value(self._properties, AWS_REGION),
+            botocore_session=self._properties.get(BOTOCORE_SESSION),
+            aws_access_key_id=get_first_property_value(self._properties, 
AWS_ACCESS_KEY_ID),
+            aws_secret_access_key=get_first_property_value(self._properties, 
AWS_SECRET_ACCESS_KEY),
+            aws_session_token=get_first_property_value(self._properties, 
AWS_SESSION_TOKEN),
+        )
+
+    def add_headers(self, request: PreparedRequest, **kwargs: Any) -> None:
+        from botocore.auth import SigV4Auth
+        from botocore.awsrequest import AWSRequest
+
+        credentials = 
self._boto_session.get_credentials().get_frozen_credentials()
+        region = self._properties.get(SIGV4_REGION, 
self._boto_session.region_name)
+        service = self._properties.get(SIGV4_SERVICE, "execute-api")
+
+        url = str(request.url).split("?")[0]
+        query = str(parse.urlsplit(request.url).query)
+        params = dict(parse.parse_qsl(query, keep_blank_values=True))
+
+        if "connection" in request.headers:
+            del request.headers["connection"]
+        if not request.body:
+            request.headers["x-amz-content-sha256"] = EMPTY_BODY_SHA256

Review Comment:
   Nit: The move dropped the comments that explain why we do this. Can we keep 
them, along with the "relocate headers if there is a conflict with signed 
headers" comment above the loop on line 86?
   ```suggestion
           # remove the connection header as it will be updated after signing
           if "connection" in request.headers:
               del request.headers["connection"]
           # For empty bodies, explicitly set the content hash header to the 
SHA256 of an empty string
           if not request.body:
               request.headers["x-amz-content-sha256"] = EMPTY_BODY_SHA256
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to