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

Validate API Gateway Response Keys #1154

Merged
merged 6 commits into from
May 7, 2019
Merged

Validate API Gateway Response Keys #1154

merged 6 commits into from
May 7, 2019

Conversation

awood45
Copy link
Member

@awood45 awood45 commented Apr 25, 2019

*Issue #, if available: Resolves Issue #522

Description of changes: Per my testing, API Gateway has 4 allowable response keys: statusCode, body, headers, and isBase64Encoded. None of these keys are required, but any extra keys will result in a 502 error from the service. Currently, the local API Gateway implementation will ignore extra keys, this change will cause those keys to raise an error, matching service behavior.

Checklist:

  • [N/A] Write Design Document (Do I need to write a design document?)
  • [Y] Write unit tests
  • [N/A] Write/update functional tests
  • [Y] Write/update integration tests
  • [Y] make pr passes
  • [N/A] Write documentation

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

This adds a failing test for an example of invalid API Gateway output,
and proposes a fix of disallowing missing status codes. Need to
investigate possible regressions of this fix before finalizing and
merging, may change the fix approach.
Copy link
Contributor

@sanathkr sanathkr left a comment

Choose a reason for hiding this comment

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

Just a couple of small Pythonic nits

samcli/local/apigw/local_apigw_service.py Outdated Show resolved Hide resolved
samcli/local/apigw/local_apigw_service.py Outdated Show resolved Hide resolved
More explicit definition of the intended behavior.
The empty set is falsy, and we're checking for a non-empty set in this
method. Therefore, can return directly as boolean.
samcli/local/apigw/local_apigw_service.py Outdated Show resolved Hide resolved
tests/integration/local/start_api/test_start_api.py Outdated Show resolved Hide resolved
@@ -63,6 +63,10 @@ def invalid_response_returned(event, context):
return "This is invalid"


def invalid_hash_response(event, context):
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe just json response?

samcli/local/apigw/local_apigw_service.py Outdated Show resolved Hide resolved
Python 2.7 appears to treat Dictionary's keys() method as an array, and
then does not support the `-` operator with a set. Adding an explicit
set() casting works in my Python 2.7 local testing.
@awood45
Copy link
Member Author

awood45 commented Apr 26, 2019

Python 2.7 incompatibility is fixed, so this is ready for review again.

Copy link
Contributor

@sriram-mv sriram-mv left a comment

Choose a reason for hiding this comment

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

just minor comments, will approve after those are addressed.

# API Gateway only accepts statusCode, body, headers, and isBase64Encoded in
# a response shape.
if LocalApigwService._invalid_apig_response(json_output):
msg = "Invalid Response: " + str(json_output)
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to be more explicit in the response about accepted keys?

Copy link
Contributor

Choose a reason for hiding this comment

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

we can possibly move log statement up here just before throwing an exception.

Copy link
Member Author

Choose a reason for hiding this comment

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

To do this I'd probably want to actually return the invalid keys and then perform the boolean if at this top level. Should we move to that approach?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that sounds good, provides more context as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed in most recent change.

"headers",
"isBase64Encoded"
}
# In Python 2.7, need to explicitly make the Dictionary keys into a set
Copy link
Contributor

Choose a reason for hiding this comment

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

Good to know!

Has the checker method return keys instead of a boolean, so that the
exception message can be more detailed.
Copy link
Contributor

@sriram-mv sriram-mv left a comment

Choose a reason for hiding this comment

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

🥇

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.

None yet

3 participants