Skip to content
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

feat(event_handler): allow multiple CORS origins #2279

Merged
merged 5 commits into from
May 18, 2023

Conversation

rubenfonseca
Copy link
Contributor

@rubenfonseca rubenfonseca commented May 16, 2023

Issue number: #1006

Summary

Changes

Please provide a summary of what's being changed

This PR adds support for additional origins for CORS. It also changes the behavior to never return Origin information outside of a CORS request.

User experience

Please share what the user experience looks like before and after this change

carbon (8)

NOTE: this PR changes the default behavior when a non-CORS request is sent. Previously, we would return the configured Origin as part of the response. Now we only do it if there's an Origin in the request and it matches one of the allowed origins. Should we do something about this?

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation event_handlers tests labels May 16, 2023
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 16, 2023
@github-actions github-actions bot added the feature New feature or functionality label May 16, 2023
@rubenfonseca rubenfonseca marked this pull request as ready for review May 16, 2023 14:53
@rubenfonseca rubenfonseca requested a review from a team as a code owner May 16, 2023 14:53
@rubenfonseca rubenfonseca requested review from heitorlessa and removed request for a team May 16, 2023 14:53
@rubenfonseca rubenfonseca linked an issue May 16, 2023 that may be closed by this pull request
@codecov-commenter
Copy link

codecov-commenter commented May 16, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (eb3e795) 97.46% compared to head (3ca9aaf) 97.46%.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #2279   +/-   ##
========================================
  Coverage    97.46%   97.46%           
========================================
  Files          149      149           
  Lines         6895     6901    +6     
  Branches       506      509    +3     
========================================
+ Hits          6720     6726    +6     
  Misses         137      137           
  Partials        38       38           
Impacted Files Coverage Δ
aws_lambda_powertools/event_handler/api_gateway.py 100.00% <100.00%> (ø)
...lambda_powertools/utilities/data_classes/common.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

I tested this with several scenarios, resolvers and origins and in all of them it worked perfectly! You are truly amazing, Ruben!

I made some comments and after you answer them I think we are ready to merge!!

Comment on lines 314 to 316
???+ tip "Multiple allowed origins?"
If you require multiple allowed origins, pass the additional origins using the `extra_origins` key.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ruben, please add the extra_origins field in the table below.

How about adding a new tab with an example to show how to use this new field? What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe I'm dizzy in the taxi but this reads odd, perhaps what you meant was:

Multiple origins?

If you need to allow multiple origins ...

As in, Allow-Origins is an explicit CORS terminology, but here we can be more flexible in wording

docs/core/event_handler/api_gateway.md Show resolved Hide resolved
aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

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

Some tiny suggestions but looks great!

aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
self.allow_headers = set(self._REQUIRED_HEADERS + (allow_headers or []))
self.expose_headers = expose_headers or []
self.max_age = max_age
self.allow_credentials = allow_credentials

def to_dict(self) -> Dict[str, str]:
def to_dict(self, origin: Optional[str]) -> Dict[str, str]:
Copy link
Contributor

Choose a reason for hiding this comment

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

origin: str = ""?

Annotation says it's optional but we are not setting a default value.

You can drop the Optional (None), and simply set to an empty str

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this mean I would have to compare if origin == "" later on instead of not origin? Doesn't it sound wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TIL not will eventually translate to __bool__, so it works for empty strings too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed my mind again and I believe it's better to keep the Optional. Reason is, the caller will try to fetch an Origin from the headers. The Origin is not always present, so mypy would complain that I can't pass an Optional to the to_dict. So I think in this case the Optional makes sense.

docs/core/event_handler/api_gateway.md Show resolved Hide resolved
Comment on lines 314 to 316
???+ tip "Multiple allowed origins?"
If you require multiple allowed origins, pass the additional origins using the `extra_origins` key.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe I'm dizzy in the taxi but this reads odd, perhaps what you meant was:

Multiple origins?

If you need to allow multiple origins ...

As in, Allow-Origins is an explicit CORS terminology, but here we can be more flexible in wording

tests/functional/event_handler/test_api_gateway.py Outdated Show resolved Hide resolved
tests/functional/event_handler/test_api_gateway.py Outdated Show resolved Hide resolved
tests/functional/event_handler/test_api_gateway.py Outdated Show resolved Hide resolved
@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 17, 2023
@rubenfonseca
Copy link
Contributor Author

As promised, added E2E tests.

Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

I ran the end2end test on this branch and everything is working as expected, Ruben! There are some random bugs at specific runtimes, but they are known bugs and nothing related to this change. - https://github.com/awslabs/aws-lambda-powertools-python/actions/runs/5006899451/jobs/8972788300

One more amazing work, dude!!!

@leandrodamascena leandrodamascena merged commit 042e83a into develop May 18, 2023
6 checks passed
@leandrodamascena leandrodamascena deleted the rf/multiple-origins branch May 18, 2023 15:57
@leandrodamascena leandrodamascena changed the title feat(event_source): allow multiple CORS origins feat(event_handler): allow multiple CORS origins May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation event_handlers feature New feature or functionality size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support multiple Allowed Origins in CORS
4 participants