Skip to content

fix(cloudformation): expand YAML short-form intrinsics, report a refused resource (#516) - #523

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/516-cfn-yaml-shortform
Aug 3, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/516-cfn-yaml-shortform

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #516
Closes #519

parseCFNTemplate unmarshalled YAML straight into the template struct, and
go.yaml.in/yaml/v3 has no notion of CloudFormation's tag shorthands: it drops
the tag and keeps the node value. So !Sub 'short-${P}' reached the resolver as
the literal short-${P} — indistinguishable from a string the template really
did intend — and !If [C, a, b] arrived as the raw array ["C","a","b"], which
is what the reporting consumer saw stamped verbatim into an ECS task-definition
family. Nothing downstream was ever handed anything to resolve. The Fn:: long
forms worked correctly throughout.

The fix decodes into a yaml.Node, whose .Tag survives, rewrites every
recognized shorthand into its long-form mapping, and decodes that. All eighteen
are handled, and the walk is depth-first, children before parents, because
the tags nest — !Not [!Equals [!Ref VpcId, '']] is three levels and is the most
common condition idiom in real templates. !GetAtt is the one irregular form
(dotted string, split on the first period only, per AWS's own
myELB.SourceSecurityGroup.OwnerAlias example). An unrecognized tag keeps its
value and logs a WARN: dropping it silently is the defect being fixed, and
refusing the template would reject one real CloudFormation accepts.

Three defects behind it, each reachable only once the tags expand

  • Fn::Sub's ${!Literal} escape was never implemented. The reporter's
    ec2_worker.yml contains ${!Count.Index}, which would have resolved as a ref
    named !Count.Index. Unreachable before the expander, which is why it ships
    here.
  • Default: '' was treated as no default at all, so the parameter was left
    undeclared and Ref echoed its own name back — which inverts the
    conventional !Not [!Equals [!Ref X, '']] optional-parameter test: a parameter
    the caller never set reads as set. 21 occurrences across the reporter's five
    templates. Found by deploying one of them as a checked-in fixture, not by
    reading the code.
  • Conditions were evaluated by iterating a Go map, so a condition referencing
    another via {"Condition": "Other"} read the referent's zero value whenever it
    was reached first: the same template deployed differently from one run to the
    next, which is the one outcome an emulator built on deterministic replay must
    never produce. They now resolve on demand, and a cycle resolves to false.
    Names are additionally walked sorted, which matters only for a cycle running
    through Fn::Not — that resolves its two members to opposite values, so
    absent the sort the answer would still depend on map order.

#519 — a refused resource was reported CREATE_COMPLETE

The deployer set a resource's error only when the dispatched request returned a
non-nil error, and never read the response status. Plugins signal a client
error two ways, both in wide use: S3 (58 sites) and IAM (162) return a 4xx
response with a nil error — the shape a real endpoint puts on the wire — while
EC2 (49), ECS (27), Logs (20) and SQS (5) return an *AWSError. Only the second
was inspected, so every S3 and IAM resource failure in a stack was swallowed.
That is #516's own reproduction: invalid bucket name, no bucket, and a stack
reporting success.

cfnDispatchError now derives the failure from either convention, applied
centrally in dispatch so all 31 recording sites became correct without being
individually edited, and lifts the plugin's own code out of the body so the
reason reads InvalidBucketName: The specified bucket is not valid. rather than
a status number. deployS3Bucket also stops discarding its response and returns
early rather than configuring versioning on a bucket that was refused — that
follow-up could not corrupt the recorded reason (its result was discarded too),
but it did put a PUT ?versioning refused with NoSuchBucket into the log
substrate replays.

Rollback is deliberately not changed: substrate records the failure per
resource and carries on where real CloudFormation would roll back. Filed as #520.

Compatibility

A resource that genuinely fails now reports CREATE_FAILED where it reported
CREATE_COMPLETE.
A test asserting the old status was asserting a defect, but
it will still turn red. Stack status is unchanged.

Expansion is not resolution: !FindInMap, !ImportValue, !Cidr and !GetAZs
now expand to long forms the resolver still ignores and hit the same fallback the
long forms already did (#522 — Fn::FindInMap needs a Mappings model the
template struct does not have). Fn::Split still yields only its first element
(#521). The docs say both, so "expanded" is not read as "resolved". None of the
reporter's templates use the four unresolved intrinsics; one does use !Split.

Verification

gofmt -l ., go build ./..., go vet ./..., golangci-lint run ./... → 0
issues, make test (race), the docs targets, npm --prefix docs run build, and
the test/e2e module — all green. Every new or changed function is at 100%
statement coverage.

Mutation testing, 24 mutants: 23 caught. Four were worth more than a tally.
native-tag-always-str survived because the branch it mutated was unreachable —
resolved by deleting the code, not by adding a test. conditions-unsorted-lazy-walk
survived twice: the lazy accessor, not the sort, is what makes a chain
order-independent, so the sort is load-bearing only for a negated cycle, and
even with that case added the divergence appears in the member reached second,
which the test was not asserting. The remaining survivor is a genuine equivalence
with a written proof at the call site, settled by the AWS docs stating Fn::If is
not supported inside a Conditions entry.

End to end over the wire against both a fixed and a pre-fix binary, so the claims
are demonstrated in both directions: pre-fix gives short-${p},
["HasP","role-${P}","role-none"] and CREATE_COMPLETE for the bad bucket;
post-fix gives short-live, role-live, the GetAtt ARN, the ${Count.Index}
literal, and CREATE_FAILED / InvalidBucketName.

…sed resource (#516)

A template written with !Ref, !Sub or !If deployed with unresolved literals
where a value should be. go.yaml.in/yaml/v3 has no notion of CloudFormation's
tag shorthands, so it dropped the tag and kept the node value: `!Sub 'x-${P}'`
reached the resolver as the plain string "x-${P}", indistinguishable from a
literal the template really did intend, and `!If [C, a, b]` arrived as the raw
array ["C", "a", "b"]. The Fn::-prefixed long forms were unaffected throughout,
so this was purely tag resolution.

The template is now decoded into a yaml.Node, whose .Tag survives, and every
recognized shorthand is rewritten into its long form before the node is decoded.
The walk is depth-first, children before parents, because the tags nest:
`!Not [!Equals [!Ref VpcId, '']]` is three levels deep and is the most common
condition idiom in real templates. !GetAtt is the one irregular expansion, split
on the first period only. An unrecognized tag keeps its value and logs a WARN
rather than being dropped, since a macro may introduce one substrate has never
heard of.

Three defects surfaced behind it, each reachable only once the tags expand:

- Fn::Sub's `${!Literal}` escape was never implemented, so `${!Count.Index}`
  would have resolved as a ref named "!Count.Index".
- A parameter declared `Default: ''` was treated as having no default, leaving
  it undeclared so Ref echoed its own name back — which inverts the
  conventional `!Not [!Equals [!Ref X, '']]` optional-parameter test. Found by
  deploying a real template as a fixture, not by reading the code.
- Conditions were evaluated by iterating a Go map, so a condition referencing
  another read the referent's zero value whenever it was reached first: the same
  template deployed differently from run to run. They now resolve on demand, and
  a cycle resolves to false.

Separately (#519), the deployer never looked at a response status, only at the
error return. S3 and IAM report a refusal as a 4xx response with a nil error —
the shape a real endpoint puts on the wire — while EC2/ECS/Logs/SQS return an
*AWSError. Only the second was inspected, so every S3 and IAM resource failure
in a stack was swallowed and reported CREATE_COMPLETE for a resource that does
not exist. That is #516's own reproduction. One helper in dispatch now derives
the failure from either convention, lifting the plugin's error code out of the
body so the reason names what went wrong.

Compatibility: a resource that genuinely fails now reports CREATE_FAILED where
it reported CREATE_COMPLETE.
@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.30508% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/betty_cfn.go 96.82% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit f0d7d2a into main Aug 3, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/516-cfn-yaml-shortform branch August 3, 2026 03:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant