Repository navigation
fix(cfn): read Mappings and resolve FindInMap, GetAZs, Cidr and the pseudo-parameters (#522) - #535
Merged
Merged
Conversation
…seudo-parameters (#522) A template's Mappings section was discarded at parse time — cfnTemplate had no field for it — so Fn::FindInMap fell back to its JSON encoding and the near-universal `ImageId: !FindInMap [RegionMap, !Ref 'AWS::Region', AMI]` reached RunInstances as the literal `{"Fn::FindInMap":[...]}` while the instance launched and the resource reported CREATE_COMPLETE. Mappings is now read and all three levels resolve, including the nested-key form the reference leads with, a list-valued leaf (spliced in a list context, rejoined on commas in a scalar one), and the optional {DefaultValue: ...} fourth argument. A lookup that misses fails the resource — CREATE_FAILED with a ResourceStatusReason naming the intrinsic and the key — rather than falling back to the literal, which is the whole point of modelling Mappings rather than tolerating it. Reporting it needed a new channel, since a resolver returns a string in which "no such key" and "the key held an empty string" are the same value: cfnContext accumulates failures, drained per resource onto DeployedResource.Error, which DescribeStackResources already derives CREATE_FAILED from. deployResource is split into that drain plus dispatchResource so the ~100-arm type switch is untouched. Fn::GetAZs derives from the same seeded zone list EC2's DescribeAvailabilityZones answers from rather than carrying a second one, so a subnet placed with !Select over !GetAZs names a zone the caller can then query. Fn::Cidr handles IPv4 and IPv6 via net/netip — the mask comes from the parsed block's address family — and refuses an impossible request rather than returning a short list a !Select would read an empty string out of. The four remaining pseudo-parameters resolve: AWS::Partition and AWS::URLSuffix follow the region, AWS::NotificationARNs is an empty list, and AWS::StackId shares its builder with the wire's cfnStackID so the ARN a template reads about itself is the one CreateStack returned. Fn::ImportValue remains unresolved and #522 stays open for it: it needs an export registry across stacks and a refusal to delete an imported-from stack, which is a cross-stack model rather than another resolver. 21 mutants run against the new code; all caught, no survivors. Three test-design gaps the pass exposed are closed by tests: the resolved *shape* inside a structured property (invisible in a scalar context, which rejoins), a failure not being inherited by a later resource, and a condition's failure not being blamed on the first resource.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
Part 1 of #522. Does not close it —
Fn::ImportValueis deliberately split out; see What this leaves below.The defect
cfnTemplatehad noMappingsfield, so the section every region→AMI template carries was discarded at parse time andFn::FindInMapfell through to the JSON-encoding fallback. The near-universalreached
RunInstancesas the literal string{"Fn::FindInMap":["RegionMap",...]}and the instance launched, the resource reportingCREATE_COMPLETE. Same shape as #516/#517/#521/#526/#527: substrate returns 200 while the observable a caller parses is wrong.What resolves now
Mappings+Fn::FindInMap— all three levels, including the nested-key form the reference leads with (!FindInMap [Arch2AMI, !Ref 'AWS::Region', !FindInMap [Type2Arch, !Ref InstanceType, Arch]]). A leaf may be a string or a list, since "the values can be of type String or List", so a list-valued lookup splices into a list-valued property and is rejoined on commas in a scalar one. The optional fourth argument is honored, spelled exactly{DefaultValue: …}— a map with any other key is not a default and the lookup fails as it would without one, rather than substrate guessing at the intent.A lookup that misses fails the resource.
CREATE_FAILEDwith aResourceStatusReasonnaming the intrinsic and the key that missed; the rest of the stack still deploys, per #519. That is the point of modellingMappingsrather than tolerating it — a nonsenseImageIdreported as success is the defect.Reporting it needed a new channel, because a resolver returns a
stringand in that type "no such key" and "the key held an empty string" are the same value.cfnContextnow accumulates failures, drained per resource ontoDeployedResource.Error— whichDescribeStackResourcesalready derivesCREATE_FAILEDfrom (cloudformation_plugin.go:474).deployResourceis split into that drain plusdispatchResource, so the ~100-arm type switch is renamed rather than edited.Fn::GetAZsderives fromec2SeededAZSuffixes, the same list EC2'sDescribeAvailabilityZonesanswers from, rather than carrying a second one — a subnet in a zone the emulator does not report is not something a caller can query, and two independent lists is how that happens.Fn::Cidrfor IPv4 and IPv6 vianet/netip(the mask comes from the parsed block's address family; both of the reference's examples are asserted). An impossible request fails the resource rather than returning a short list, since!Select [3, !Cidr [...]]reads an empty string out of a short list and deploys.The four remaining pseudo-parameters.
AWS::Partition/AWS::URLSuffixfollow the region;AWS::NotificationARNsis an empty list (accurate — substrate has no notification model);AWS::StackIdnow shares its builder with the wire'scfnStackID, so a consumer that capturesStackIdfromCreateStackand compares it against a property built fromAWS::StackIdfinds one ARN for one stack. Two builders would have been the same divergence #517 fixed for the caller's account.What this leaves
Fn::ImportValueis still unresolved and #522 stays open for it. Per the plan's own stated trigger — "if PR 3 grows past PR 1+2 combined, splitFn::ImportValueout" — it needs an export registry populated fromOutputs.*.Export.Name(Exportis absent fromcfnOutputtoday) plus a refusal to delete a stack whose export is imported. That is a new cross-stack model, not another resolver, and it ships as a follow-up PR.Fn::ImportValueis deliberately not admitted tocfnIntrinsicNameshere: doing so would makeresolveNestedresolve it to the empty string, which is worse than the current JSON-literal fallback.Verification
gofmt,go build,go vet,golangci-lint(0 issues),make test(race), docs reference/versions checks,npm --prefix docs run build,test/e2evet+test — all green.End to end over the wire against
substrate server, region matched to the signed request:And the failure path, with the rest of the stack unaffected:
Mutation testing
21 mutants, all CAUGHT, no survivors and no equivalents — including the plan's nominated highest-value one (
Fn::FindInMapfalling back to the JSON literal instead of failing the resource), the failure channel going silent,Fn::GetAZsinventing its own zone list,Fn::Cidrassuming IPv4, andcfnStackIDdiverging fromcfnStackARN.The pass exposed three test-design gaps, each closed by a test rather than argued away as equivalent:
cfnListValuedIntrinsiccould return a constant and nothing failed.TestCFN_ListValuedShapeInsideAStructuredPropertyasserts the JSON shape a member receives, which is also whereAWS::NotificationARNsbeing an empty array rather than a one-element[""]becomes visible.CREATE_FAILEDand send a consumer chasing the wrong one.!Not [!Equals [!FindInMap [M, K1, K2], '']]is conventional — so a failing lookup there records against a context with nothing to blame. Dropping it is deliberate (DescribeStackResourceshas no row for a condition); blaming the first resource is the bug the pre-drain prevents.Refs #522