Skip to content

Core: Extract REST SigV4 signing into a dedicated module - #4493

Open
Anurag-S1ngh wants to merge 3 commits into
apache:mainfrom
Anurag-S1ngh:fix/3410-extract-sigv4
Open

Anurag-S1ngh wants to merge 3 commits into
apache:mainfrom
Anurag-S1ngh:fix/3410-extract-sigv4

Conversation

@Anurag-S1ngh

@Anurag-S1ngh Anurag-S1ngh commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

RestCatalog._init_sigv4 currently contains the SigV4 adapter, signing constants, and boto session setup inside an already large catalog module.

This extracts that code into catalog/rest/sigv4.py and replaces the method with an init_sigv4(session, uri, properties) helper. The SigV4 constants and SigV4Adapter now live in catalog/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 passed
  • ruff check and git diff --check pass
  • ty check reports nothing for the new module; existing catalog diagnostics are unchanged

Are there any user-facing changes?

No behavior change.

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.
Comment thread pyiceberg/catalog/rest/__init__.py Outdated
ScanTasks,
StorageCredential,
)
from pyiceberg.catalog.rest.sigv4 import (

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Fokko Fokko Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

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. With implicit_reexport = false, mypy needs the redundant alias. make lint will split this into one statement per name, which is fine:

Suggested change
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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

Comment on lines +74 to +77
if "connection" in request.headers:
del request.headers["connection"]
if not request.body:
request.headers["x-amz-content-sha256"] = EMPTY_BODY_SHA256

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants