Fix opaque AttributeError on malformed CloudFormation templates (#2307)#10271
Open
SAY-5 wants to merge 1 commit intoaws:developfrom
Open
Fix opaque AttributeError on malformed CloudFormation templates (#2307)#10271SAY-5 wants to merge 1 commit intoaws:developfrom
SAY-5 wants to merge 1 commit intoaws:developfrom
Conversation
The cloudformation package command crashed with errors like 'NoneType object has no attribute items' or '... has no attribute get' when a template was malformed (typically inconsistent indentation that caused a section or resource body to parse as null), instead of telling the user where the problem was. Issue aws#2307. Add an InvalidTemplateError that carries a clear message, and guard every site in artifact_exporter.Template that previously dereferenced a section or resource body without checking it. Eight surfaces are now covered: top-level template body, Metadata, Resources, individual resource bodies, the Resources mapping shape, the resource-group shape in export_resources, the Fn::Transform handler, and its Parameters sub-section. Add nine regression tests covering the empty-template, null-Resources, null-resource-body, wrong-shape, and Fn::Transform null cases. Fixes aws#2307 Signed-off-by: saiasish <saiasish.cnp@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2307.
When a CloudFormation template is malformed (typically inconsistent indentation that causes a section or resource body to parse as null),
aws cloudformation packagecrashed with messages like'NoneType' object has no attribute 'items'and'NoneType' object has no attribute 'get'that gave the user no clue where the problem was.This change adds a new
InvalidTemplateErrorand guards every place inartifact_exporter.Templatethat previously dereferenced a template section or resource body without first checking it. Eight surfaces are covered:yaml_parsereturnsNone)Metadatapresent but null, or not a mappingResourcesmissing, present but null, or not a mappingexport_resources(coversFn::ForEach::payloads too)'NoneType' object has no attribute 'get')Fn::Transformhandler called with a non-dict fragmentParameterssub-section being nullThe result: the exact malformed YAML from the original issue now produces
Nine regression tests are added covering the empty-template, null-Resources, null-resource-body, wrong-shape, null-Metadata, and
Fn::Transformnull cases.Relation to PR #8169
PR #8169 (open since Sep 2023) addresses the same issue but only patches one surface (the
Resources.items()call) and raises a genericValueError. This PR is broader: it covers all eight surfaces I could find, raises a CFN-specific exception that fits the existingexceptions.pyhierarchy, and adds tests for each surface. I have not closed #8169 - happy to leave that decision to maintainers.Test plan
python -m pytest tests/unit/customizations/cloudformation/test_artifact_exporter.py- 86 passed (10 new, 76 pre-existing)