-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat(handler): Add custom header to all AWS responses from LocalStack #12769
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
Conversation
S3 Image Test Results (AMD64 / ARM64) 2 files 2 suites 8m 25s ⏱️ Results for commit 6c75361. ♻️ This comment has been updated with latest results. |
Test Results - Alternative Providers988 tests +1 622 ✅ +33 34m 23s ⏱️ + 4m 59s Results for commit f3f9475. ± Comparison against base commit 67b3da6. This pull request skips 1 and un-skips 34 tests.
♻️ This comment has been updated with latest results. |
Test Results (amd64) - Integration, Bootstrap 5 files 5 suites 2h 20m 51s ⏱️ Results for commit 6c75361. ♻️ This comment has been updated with latest results. |
e3c1dcf
to
0447cfd
Compare
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.
Reviewed and tested env variable in a pairing session.
@@ -1245,6 +1245,9 @@ def use_custom_dns(): | |||
# This flag enables `connect_to` to be in-memory only and not do networking calls | |||
IN_MEMORY_CLIENT = is_env_true("IN_MEMORY_CLIENT") | |||
|
|||
# This flag enables all responses from LocalStack to contain a `x-localstack` HTTP header. |
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.
dismiss: would rephrase this that flag "disables" this header from being set in response. default: True.
just a heads up to add the env var in public docs: https://docs.localstack.cloud/references/configuration/
tests/aws/conftest.py
Outdated
@@ -107,6 +108,26 @@ def _infrastructure_setup( | |||
return _infrastructure_setup | |||
|
|||
|
|||
@pytest.hookimpl | |||
def pytest_runtest_setup(item): |
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.
would be nice to add a small high-level docstring/comment what this fixture was added for, so one can understand it in the code without having to open the PR where it was introduced for additional context.
e.g: """ fixture that skips header x-localstack
from snapshot evaluation"""
tests/aws/conftest.py
Outdated
|
||
# Otherwise, dynamically inject a path to the custom header inside all skip_snapshot_verify markers paths. | ||
# If a path parameter is None, it's assumed that all paths are skipped. | ||
for mark in skip_markers: |
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.
question: there are some snapshot tests example where path
parameter is not explicitly set, but just the list with the item is passed. Is that covered by these conditions?
@markers.snapshot.skip_snapshot_verify( [ "$..Invalidation.Status", # In real AWS it can be still in progress "$..Location", # not currently supported ] )
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.
Huh that is a good question 🤔 Let me see if the kwargs
accounts for this
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.
OK damn. You're correct. This is not accounted for. Nice catch!
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.
Update on this... looks like paths are not skipped when added as args as opposed to kwargs 💀
That means the test you linked actually skips all paths.
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.
nice and clean, good job @gregfurman 👏🏼
I liked the logic for skipping the x-localstack header check from snapshot matching.
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.
I like the new approach way more!
Just one question: Did you run an -ext run as well? And did you check if there are any lists of header keys where this could end up? (your logic only removes them from dicts)
f3f9475
to
6c75361
Compare
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.
LGTM!
@@ -118,6 +118,9 @@ def snapshot(request, _snapshot_session: SnapshotSession, account_id, region_nam | |||
RegexTransformer(f"arn:{get_partition(region_name)}:", "arn:<partition>:"), priority=2 | |||
) | |||
|
|||
# Removes the 'x-localstack' header from all responses | |||
_snapshot_session.add_transformer(_snapshot_session.transform.remove_key("x-localstack")) |
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.
Nit: We could reference the constant for the header here
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.
Yeah fair. I originally did this but realised it's more explicit to add the string literal over referencing the constant. Will add in a follow-up.
Motivation
This PR adds a custom header to all service responses -- allowing us to distinguish whether a request orignates from LocalStack or AWS.
Changes
LOCALSTACK_RESPONSE_HEADER_ENABLED
environment variable that is opt-out by default.LOCALSTACK_RESPONSE_HEADER_ENABLED
is truthy, a handler is added to the response chain that adds ax-localstack: true
header to all responses from LocalStack gateway.