Repository navigation
Core: Extract REST SigV4 signing into a dedicated module - #4493
Anurag-S1ngh wants to merge 3 commits into
Conversation
Move SigV4Adapter, its constants, and the initialization helper out of RestCatalog._init_sigv4. Re-export the existing public names from catalog.rest for compatibility. Part of apache#3410; signing and retry fixes remain separate.
| ScanTasks, | ||
| StorageCredential, | ||
| ) | ||
| from pyiceberg.catalog.rest.sigv4 import ( |
There was a problem hiding this comment.
@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
There was a problem hiding this comment.
Grouped them into a single import. I also merged main, which had moved the query parsing behind keep_blank_values, so I carried that into the new module.
There was a problem hiding this comment.
I missed that it is because of keeping the imports, I think that's more important than esthetics
Resolve the conflict in pyiceberg/catalog/rest/__init__.py by keeping the upstream pyiceberg.io imports and dropping the extracted SigV4 code. Group the sigv4 re-exports into a single import, and carry over the keep_blank_values=True query parsing from apache#4511 into sigv4.py.
catalog/rest/__init__.py now imports only init_sigv4, and the SigV4 tests import the constants from pyiceberg.catalog.rest.sigv4. That removes the redundant alias imports mypy required for the re-exports.
Fokko
left a comment
There was a problem hiding this comment.
Thanks @Anurag-S1ngh for picking this up, this makes the REST catalog module a lot easier to navigate 👍 There is one thing that needs fixing before we can merge: the move removes SIGV4_REGION, SIGV4_SERVICE, SIGV4_MAX_RETRIES, SIGV4_MAX_RETRIES_DEFAULT and EMPTY_BODY_SHA256 from pyiceberg.catalog.rest, so from pyiceberg.catalog.rest import SIGV4_REGION now raises an ImportError. Sorry, that's partly on me: ruff splits X as X imports into one statement per name, so the re-exports in your first commit were fine, and my comment about grouping them was misleading. The rest are small suggestions. Once this is in, #4586 needs a small rebase to move its change into sigv4.py.
| ScanTasks, | ||
| StorageCredential, | ||
| ) | ||
| from pyiceberg.catalog.rest.sigv4 import init_sigv4 |
There was a problem hiding this 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:
| from pyiceberg.catalog.rest.sigv4 import init_sigv4 | |
| 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, | |
| ) |
| EMPTY_BODY_SHA256: str = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" | ||
|
|
||
|
|
||
| class SigV4Adapter(HTTPAdapter): |
There was a problem hiding this 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 :)
| if "connection" in request.headers: | ||
| del request.headers["connection"] | ||
| if not request.body: | ||
| request.headers["x-amz-content-sha256"] = EMPTY_BODY_SHA256 |
There was a problem hiding this 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?
| if "connection" in request.headers: | |
| del request.headers["connection"] | |
| if not request.body: | |
| request.headers["x-amz-content-sha256"] = EMPTY_BODY_SHA256 | |
| # 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 |
Rationale for this change
RestCatalog._init_sigv4currently contains the SigV4 adapter, signing constants, and boto session setup inside an already large catalog module.This extracts that code into
catalog/rest/sigv4.pyand replaces the method with aninit_sigv4(session, uri, properties)helper. The SigV4 constants andSigV4Adapternow live incatalog/rest/sigv4.py. Signing and retry behavior are unchanged.The retry behavior in #3008 and signing fixes in #3120 remain separate.
Part of #3410.
Are these changes tested?
Yes. The existing REST SigV4 tests exercise the extracted adapter through
RestCatalog.pytest tests/catalog/test_rest.py -m unit: 204 passedruff checkandgit diff --checkpassty checkreports nothing for the new module; existing catalog diagnostics are unchangedAre there any user-facing changes?
No behavior change.