Skip to content

fix(cloudformation): deploy a stack into the calling account and region (#517) - #524

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/517-cfn-caller-identity
Aug 3, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/517-cfn-caller-identity

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

The defect

StackDeployer.dispatch synthesized its own RequestContext from substrate's
package defaults — testAccountID (123456789012) and defaultRegion
(us-east-1) — regardless of who created the stack. Most plugins embed the
account, and some the region, in their state keys:

plugin key
EC2 instance:<account>/<region>/<id>
DynamoDB table:<account>/<name>
SQS queue:<account>/<name>
SNS topic:<account>/<region>/<name>
S3 bucket:<name> — unpartitioned
IAM role:<name> — unpartitioned

So a caller in any other partition got a stack ARN naming their own account
whose resources were written where they could not read them. #517's
reproduction: an unsigned client (which resolves to 000000000000) created a
stack reporting CREATE_COMPLETE with a PhysicalResourceId of i-…, and
DescribeInstances then returned nothing. Every read scoped to the caller was
right; the write was in the wrong place.

The reporter read that as AWS::EC2::* being stubbed while AWS::IAM::*
worked. The asymmetry is real but the diagnosis was not — the two families that
appeared to work are exactly the two whose keys carry no account. That is also
why this survived #483's review: its shared-state test asserted a bucket
through S3 and a role through IAM.

The fix

Identity rides on the deployer as an explicit account and region, defaulting to
today's constants, set through WithDeployerIdentity. Deploy has 145 test
call sites; an option touches none of them, and because the default is unchanged
no existing caller behaves differently.

Two strings rather than a whole *RequestContext: a deployer holding one would
carry a single request's ID and timestamp across every resource it deploys, and
would invite a future reader to propagate Principal, which is #411's decision.

CloudFormationPlugin.deployerFor(reqCtx) builds a deployer per deploying
request — six pointer copies with no I/O, cheaper than making one deployer's
identity mutable and therefore racy. p.deployer stays for the paths that touch
no resource (the stack and change-set records, which are unpartitioned;
CreateChangeSet is among them since it only parses and diffs two templates).

Six sites read that identity, and threading dispatch alone would have left a
half-fix:

  • dispatch — the write path.
  • both buildCFNContext calls — AWS::AccountId and AWS::Region. Before this,
    a bucket named {"Fn::Sub": "b-${AWS::AccountId}"} got substrate's account
    rather than the caller's: a wrong physical name, not a misplaced resource.
    Neither the issue nor the reporter named this.
  • the drift existence checkers, plus the DynamoDB, SQS and SNS comparators'
    own state reads. A wrong-partition existence check reports DELETED for a
    resource that is fine; a wrong-partition comparator finds nothing to compare
    and reports no difference at all — drift silently blind, which no IN_SYNC
    assertion can distinguish from drift working.

BettyClient keeps the defaults deliberately — it is the in-process validation
client and its callers never sign a request, so there is no caller identity to
take. Its deployer now goes through the constructor rather than a struct
literal, which had left both fields empty; harmless only for as long as nothing
read them.

The comment #483 left on cfnStackID called this a pseudo-parameter
discrepancy. That understated it and has been rewritten.

Tests

emulator/cloudformation_identity_test.go, 11 tests using only partitioned
services
— the test-design lesson from #483:

  • CloudFormation: StackDeployer.dispatch hardcodes account/region, so stack-created EC2 and ECS resources are invisible to the caller #517's reproduction: an unsigned caller finds its own instance, by
    DescribeInstances and by ID.
  • One resource per family the reporter named: ECS::Cluster, Logs::LogGroup,
    EC2::LaunchTemplate.
  • A non-default region: present in eu-west-1, absent from us-east-1.
  • Signed and unsigned callers deploying the same template see one instance each.
  • Pseudo-parameters agree with the stack ARN.
  • All three deploying entry points: CreateStack, UpdateStack,
    ExecuteChangeSet. UpdateStack gets its own case because the change-set
    test reaches it indirectly and would pass with it left unthreaded.
  • Drift, tabled over all three partitioned drift-checkable types, in
    eu-west-1, asserting IN_SYNC and then MODIFIED after a mutation —
    both reads are separately hardcoded. Both DescribeStackResourceDrifts and
    the DetectStackDrift / DescribeStackDriftDetectionStatus pair. The SNS
    case's DisplayName is {"Fn::Sub": "${AWS::AccountId} in ${AWS::Region}"},
    which is the only place either pseudo-parameter is resolved twice.
  • S3 and IAM still work — the unpartitioned keys must not regress.
  • BettyClient deploys into the default partition; the option overrides it.

Mutation testing: 18 mutants, 18 CAUGHT, no survivors. Three of them were
survivors first and drove test changes: the SQS and SNS comparators (fixed by
tabling drift over all three types), updateStack (its own test), and
cfnDriftResourceProps's context — which needed both pseudo-parameters in a
drift-compared property, since a comparator resolving the expected value
against the wrong identity reports MODIFIED on a resource nobody touched.
StartStackDriftDetection also survived until DetectStackDrift was asserted
over the wire. Mutating the constants default fails 23 pre-existing tests,
confirming it is load-bearing.

Verified over the wire

Against substrate server, two callers in us-west-2:

# non-AKIA key → 000000000000
create-stack acct   → arn:aws:cloudformation:us-west-2:000000000000:stack/acct/…
describe-instances                    → i-f824919e3601d6a3   (was 0 reservations)
describe-instances --instance-ids …   → found                (was InvalidInstanceID.NotFound)
--region us-east-1 describe-instances → []                   — partitioning still holds

# AKIA key → 123456789012, same template
describe-instances → i-e9e26543b96942f1 only

Pseudo-parameters in eu-west-1 produced q-000000000000-eu-west-1, and
sqs get-queue-url found that queue. Drift reported IN_SYNC /
DETECTION_COMPLETE / 0 drifted.

gofmt, go build, go vet, golangci-lint (0 issues), make test (race),
the docs targets, the VitePress build and the test/e2e module all pass.

Closes #517

…on (#517)

The deployer synthesized its own request context from substrate's package
defaults (123456789012 / us-east-1) regardless of who created the stack. Most
plugins embed the account, and some the region, in their state keys, so a caller
in any other partition got a stack ARN naming their own account whose resources
were written where they could not read them: an unsigned client created a stack
reporting CREATE_COMPLETE with a PhysicalResourceId of i-…, and DescribeInstances
then correctly returned nothing.

Identity now rides on the deployer as an explicit account and region defaulting
to today's constants, and the plugin builds a deployer carrying the requesting
caller's identity for every path that deploys or reads a resource. Two strings
rather than a whole *RequestContext: one would carry a single request's ID and
timestamp across every resource, and would invite propagating Principal, which
is #411's decision.

Six sites read that identity. Threading dispatch alone would have left a half
fix: both buildCFNContext calls resolve AWS::AccountId and AWS::Region, and
three drift comparators (DynamoDB, SQS, SNS) read partitioned state keys
directly — a wrong-partition existence check reports DELETED for a resource
that is fine, and a wrong-partition comparator finds nothing to compare and
reports no difference at all, which no IN_SYNC assertion can distinguish from
drift working.

BettyClient keeps the defaults deliberately: it is the in-process validation
client and its callers never sign a request, so there is no caller identity to
take. Its deployer is now built through the constructor rather than as a struct
literal, which had left both fields empty — harmless only for as long as
nothing read them.

The #483 comment on cfnStackID described this as a pseudo-parameter
discrepancy. That understated it and has been rewritten.

The new tests use only partitioned services, because #483's shared-state test
asserted a bucket through S3 and a role through IAM — the two unpartitioned
plugins, and the reason this survived review.

Closes #517
@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.55172% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/cloudformation_plugin.go 90.90% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 3ae048a into main Aug 3, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/517-cfn-caller-identity 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

Development

Successfully merging this pull request may close these issues.

CloudFormation: StackDeployer.dispatch hardcodes account/region, so stack-created EC2 and ECS resources are invisible to the caller

1 participant