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
Open
Anurag-S1ngh
wants to merge
1
commit into
apache:main
Choose a base branch
from
Anurag-S1ngh:fix/3410-extract-sigv4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−73
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): | ||
| """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)) | ||
|
|
||
| if "connection" in request.headers: | ||
| del request.headers["connection"] | ||
| if not request.body: | ||
| request.headers["x-amz-content-sha256"] = EMPTY_BODY_SHA256 | ||
|
|
||
| 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)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@Anurag-S1ngh These imports should be grouped. I appreciate all the fixes you're pushing, but I would expect anyone to do a first pass themselves before opening it up for review. Thanks