Repository navigation
Core: Extract REST SigV4 signing into a dedicated module #4493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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): | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we keep these internal for now, e.g. |
||||||||||||||||||||||
| """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 | ||||||||||||||||||||||
|
Comment on lines
+74
to
+77
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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?
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| aws_request = AWSRequest(method=request.method, url=url, params=params, data=request.body, headers=dict(request.headers)) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| SigV4Auth(credentials, service, region).add_auth(aws_request) | ||||||||||||||||||||||
| original_header = request.headers | ||||||||||||||||||||||
| signed_headers = aws_request.headers | ||||||||||||||||||||||
| relocated_headers = {} | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| for header, value in original_header.items(): | ||||||||||||||||||||||
| if header in signed_headers and signed_headers[header] != value: | ||||||||||||||||||||||
| relocated_headers[f"Original-{header}"] = value | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| request.headers.update(relocated_headers) | ||||||||||||||||||||||
| request.headers.update(signed_headers) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def init_sigv4(session: Session, uri: str, properties: Properties) -> None: | ||||||||||||||||||||||
| """Mount the SigV4 adapter on a requests session for the catalog URI.""" | ||||||||||||||||||||||
| session.mount(uri, SigV4Adapter(**properties)) | ||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. Withimplicit_reexport = false, mypy needs the redundant alias.make lintwill split this into one statement per name, which is fine: