Skip to content

Fix: Top SAM crashes - #1671

Closed
prenx4x wants to merge 6 commits into
aws:developfrom
prenx4x:sam-crashes
Closed

Fix: Top SAM crashes#1671
prenx4x wants to merge 6 commits into
aws:developfrom
prenx4x:sam-crashes

Conversation

@prenx4x

@prenx4x prenx4x commented Jul 29, 2020

Copy link
Copy Markdown
Contributor

Issue #, if available:

Description of changes:
Fixed some common SAM crashes

Description of how you validated changes:
Changes validated using unit tests

Checklist:

  • Write/update tests
  • make pr passes
  • Update documentation
  • Verify transformed template deploys and application functions as expected

Examples?

Please reach out in the comments, if you want to add an example. Examples will be
added to sam init through https://github.com/awslabs/aws-sam-cli-app-templates/

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

@prenx4x
prenx4x requested review from c2tarun and jfuss July 29, 2020 19:53
@codecov-commenter

codecov-commenter commented Jul 29, 2020

Copy link
Copy Markdown

Codecov Report

Merging #1671 into develop will decrease coverage by 0.24%.
The diff coverage is 34.78%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1671      +/-   ##
===========================================
- Coverage    94.10%   93.85%   -0.25%     
===========================================
  Files           86       86              
  Lines         5408     5434      +26     
  Branches      1080     1091      +11     
===========================================
+ Hits          5089     5100      +11     
- Misses         148      155       +7     
- Partials       171      179       +8     
Impacted Files Coverage Δ
samtranslator/model/eventsources/push.py 89.88% <12.50%> (-0.99%) ⬇️
samtranslator/model/api/api_generator.py 94.11% <33.33%> (-0.96%) ⬇️
samtranslator/swagger/swagger.py 91.80% <33.33%> (-0.68%) ⬇️
samtranslator/intrinsics/resolver.py 100.00% <100.00%> (ø)
samtranslator/model/lambda_.py 93.10% <0.00%> (ø)
samtranslator/plugins/globals/globals.py 99.05% <0.00%> (ø)
samtranslator/model/sam_resources.py 93.37% <0.00%> (+0.01%) ⬆️
samtranslator/model/apigateway.py 97.12% <0.00%> (+0.02%) ⬆️
samtranslator/model/apigatewayv2.py 94.28% <0.00%> (+0.34%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d168f37...0b95c0a. Read the comment docs.

@jfuss jfuss 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.

Overall this is a good start. The major piece of feedback is, if we are going to call/say a template is invalid we should make sure the details are enough for the customer to see that and update/investigate the template accordingly without too much effort. The more descriptive we can be the better experience the customer will have.

Comment thread samtranslator/intrinsics/resolver.py Outdated

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.

Why is this an error? I can understand raising this for when parameters is not a dict but why if parameters is None? We create this in a couple places (one being here). This Resolver gets created with the Parameters and Mappings, which means this error message is not correct. In the current state, the customer could send an invalid Mapping and we would report that the Parameters section is invalid.

Two things we need to do here:

  1. Understand why parameters is None is important and document this for the future.
  2. Update this error message to be more accurate. We should make sure information we return can be used by the customer to debug/fix the problem themselves with only that information.

Comment thread samtranslator/intrinsics/resolver.py Outdated

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.

If this was raise to the customer, they would have no idea what this means as it is an implementation detail. We should understand deeper if this error is meant as a guard in the code or is something customers will run into. I don't think InvalidTemplateException is the right thing we want to throw here, as that will suggests customers template was wrong. Maybe a custom Exception and still fail with that Error is a better approach, as this looks like a guard in the code when SAM is used as a library not run in the service.

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.

We should add what is valid to help the customer understand what they need to change it too.

Comment thread samtranslator/model/api/api_generator.py Outdated

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.

Is it that it wasn't defined properly or that we couldn't resolve something or something else?

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.

Instead of adding an explicit check for api_auth here, you should update line 1112 to be api_auth = api.get("Auth", {}). This will default api_auth to be {}. Typically, this is preferred when dealing with dicts in python.

Comment thread samtranslator/swagger/swagger.py Outdated

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.

Why is it invalid?

Comment thread samtranslator/swagger/swagger.py Outdated

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.

Let's be more descriptive.

Comment thread samtranslator/swagger/swagger.py Outdated

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.

Same here

Comment thread samtranslator/swagger/swagger.py Outdated

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.

Make sure to provide some details about where this is in the template. We generally do this through adding the logical id, so the customer at least knows what resource they need to dig into. This goes for all the exceptions, not just this one.

@prenx4x
prenx4x requested a review from jfuss October 15, 2020 19:57
@codecov-io

codecov-io commented Oct 15, 2020

Copy link
Copy Markdown

Codecov Report

Merging #1671 into develop will decrease coverage by 0.34%.
The diff coverage is 33.33%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1671      +/-   ##
===========================================
- Coverage    94.15%   93.80%   -0.35%     
===========================================
  Files           86       87       +1     
  Lines         5439     5618     +179     
  Branches      1087     1130      +43     
===========================================
+ Hits          5121     5270     +149     
- Misses         147      163      +16     
- Partials       171      185      +14     
Impacted Files Coverage Δ
samtranslator/model/api/api_generator.py 93.72% <14.28%> (-1.20%) ⬇️
samtranslator/model/eventsources/push.py 89.88% <25.00%> (-0.99%) ⬇️
samtranslator/swagger/swagger.py 92.03% <25.00%> (-0.99%) ⬇️
samtranslator/intrinsics/resolver.py 100.00% <100.00%> (ø)
samtranslator/model/api/http_api_generator.py 91.60% <0.00%> (ø)
samtranslator/feature_toggle/feature_toggle.py 87.14% <0.00%> (ø)
samtranslator/translator/translator.py 98.46% <0.00%> (+0.03%) ⬆️
samtranslator/open_api/open_api.py 92.00% <0.00%> (+0.40%) ⬆️
samtranslator/model/apigatewayv2.py 94.95% <0.00%> (+0.67%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c963c93...f81fe56. Read the comment docs.

@jfuss jfuss 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.

Can we add additional test cases in test_transform_invalid_document to verify we have fixed the issue.

if not default_authorizer:
return

if not isinstance(default_authorizer, Hashable):

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 think this should be isinstance(default_authorizer, str) because this if will catch other cases that are hashable but we only expect a string.

"""

if parameters is None or not isinstance(parameters, dict):
if parameters is None:

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 be throwing TypeError for no parameters? This will crash SAM. We should validate through a test or if parameters can be None here, lets default parameters to {}.

raise TypeError("parameters must be a valid dictionary")

if not isinstance(parameters, dict):
raise InvalidTemplateException("'Mappings' is not a valid dictionary")

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.

Mappings or Parameters

def resources_to_link(self, resources):
if isinstance(self.Bucket, dict) and "Ref" in self.Bucket:
bucket_id = self.Bucket["Ref"]
if not isinstance(bucket_id, collections.Hashable):

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 this check for hashable or string?

),
)

if not isinstance(method_authorizer, collections.Hashable):

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.

same here?

model=method_model, method=self.Method, path=self.Path
),
)
if not isinstance(method_model, collections.Hashable):

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.

same here?

@prenx4x

prenx4x commented Oct 27, 2020

Copy link
Copy Markdown
Contributor Author

We can close this PR as I am breaking it in favor of #1757 and others to come.

@prenx4x prenx4x closed this Oct 27, 2020
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.

4 participants