Repository navigation
fix(cfn): resolve Fn::ImportValue against a cross-stack export registry (#522) - #537
Merged
Merged
Conversation
…ry (#522) Fn::ImportValue was the last intrinsic falling through to its JSON encoding, so a template importing another stack's subnet ID deployed a resource whose SubnetId was the literal {"Fn::ImportValue":"net-SubnetID"} and reported CREATE_COMPLETE. That is the silent-success shape this release is about, and the worst instance of it: the two-stack split is how every non-trivial template is organised. An output may now declare Export: {Name: …}. The name is an expression rather than a string because the conventional form is Export: {Name: !Sub '${AWS::StackName}-SubnetID'}; it resolves before the first resource deploys, which the API permits since an export name may not depend on a resource. Exports are scoped per account and Region, per the documented restriction that cross-stack references are limited to the same account and Region. Getting that wrong permissively would be worse than not resolving at all: a template would pass under test and fail in AWS. deleteStack on the wire now uses the caller's deployer for the same reason. Four rules are enforced, which is why exports are modeled rather than faked — an import that resolves against nothing enforceable is a lookup, not a reference: an import of an unpublished name fails the resource rather than resolving to "" or to JSON; a name another stack already exports is refused; a stack whose export is imported cannot be deleted; and an imported export's value cannot be changed, nor dropped, which is the same thing from the importer's side. Both refusals are ValidationError at 400 naming the export and every importing stack. What counts as an import is recorded when the resolver walks the template, not derived from the template text: an Fn::ImportValue in the branch of an Fn::If that was not taken never happened, and neither did one that failed to resolve. Only the resolver knows which branch it walked. Adds ListExports and ListImports, which were InvalidAction before there was anything to list, and reports each output's ExportName in DescribeStacks. Closes #522
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.
Completes #522 by resolving its last intrinsic. The
Mappings/Fn::FindInMap,Fn::GetAZs,Fn::Cidrand pseudo-parameter halves shipped in PR #535; the plancalled for splitting
Fn::ImportValueout if PR 3 grew, because the exportregistry plus the delete-refusal is a new cross-stack model rather than another
resolver. It did, and this is that split.
The defect
Fn::ImportValuewas the last intrinsic falling throughresolveValue'sJSON-encoding fallback, so a template importing another stack's subnet ID deployed
a resource whose
SubnetIdwas the literal{"Fn::ImportValue":"net-SubnetID"}and reported
CREATE_COMPLETE. Same shape as the rest of v0.88.0 — substratereports success where the observable is wrong — and the worst instance of it,
because the two-stack split is how every non-trivial template is organised.
The model
An output may now declare
Export: {Name: …}, absent fromcfnOutputuntil now.The name is an expression rather than a string, because the conventional form
is
Export: {Name: !Sub '${AWS::StackName}-SubnetID'}— an export name must beunique per account and Region, so a template hard-coding one cannot be deployed
twice. It resolves before the first resource deploys, which the API permits: an
export name may not depend on a resource, so it is knowable from the template and
its parameters alone.
Exports are scoped per account and Region, per the documented restriction that
cross-stack references are limited to the same account and Region. Getting this
wrong in the permissive direction would be worse than not resolving at all: a
template would pass under test and fail in AWS.
deleteStackon the wire now usesthe caller's deployer for the same reason — the stack record is unpartitioned, but
the export visibility deciding the refusal is not.
Four rules are enforced, and they are why exports are modeled rather than faked —
an import that resolves against nothing enforceable is a lookup, not a reference:
ResourceStatusReason, rather than resolving to""or to JSON;same thing from the importer's side, so a removal is refused on the same terms.
Re-deploying an unchanged value is not a change, so an idempotent redeploy still
works.
Both refusals are a
ValidationErrorat 400 naming the export and every importingstack, because the API's own remedy is "first find out which stacks are importing
them" and a refusal that withheld the answer would send the caller to
ListImportsfor something substrate already knows.
The one decision a reader would otherwise get wrong
What counts as an import is recorded when the resolver walks the template, not
derived from the template's text. An
Fn::ImportValuein the branch of anFn::Ifthat was not taken never happened, and neither did one that failed to resolve, so
neither pins an exporting stack. Only the resolver knows which branch it walked.
Relatedly:
Fn::ImportValuewas deliberately kept out ofcfnIntrinsicNamesuntilit resolved.
resolveNested(#526) consults that table to decide what is anintrinsic, and admitting an unresolvable one would have resolved every nested
import to
""— strictly worse than the JSON fallback, which at least left theintrinsic visible in the request the plugin refused. The table now carries it, with
that reasoning recorded in place.
Added
ListExportsandListImports, which wereInvalidActionbefore there wasanything to list.
ListExportsreports theExportingStackIdARNDescribeStacksreports for the same stack, in one page with noNextToken;ListImportsreports stack names, and an export nothing imports — includingone that does not exist — is an empty list rather than an error, since the
reference documents no service-specific errors for it.
DescribeStacksadditionally reports each output's
ExportNamebeside it.Provenance
DeleteStackdocuments onlyTokenAlreadyExists, so theValidationError/400 forboth export refusals is substrate's own choice — the same code the service uses
for every other unsatisfiable request on it. The messages are substrate's; no
capture of real wording was taken. Everything else here (the scope restriction, the
uniqueness rule, the modify-while-imported prohibition, the permitted inner
functions,
ListExports/ListImportsshapes and their absence ofservice-specific errors) is from the API reference.
Substrate does not enforce "an export name can't use Ref or GetAtt functions that
depend on a resource": by the time export names resolve, the resources have
deployed. That is strictly more permissive rather than differently behaved.
Tests
16 new tests over both seams. The plan's stated gate — an
Fn::ImportValueofanother stack's export resolves, and deleting the exporting stack is refused — plus
the account/Region scoping in both directions, the name conflict, the
change-and-drop refusals, the resolution-time import recording (including an import
in a false
Fn::Ifbranch not counting), resolution at depth inside a structuredproperty, legacy-snapshot scope back-compatibility, determinism of which conflict is
reported, and the
ListExports/ListImports/DescribeStacks-ExportNamewireshapes.
Mutation testing: 27 mutants, 27 CAUGHT, 0 survivors. Five survived the first
round and each was a real test-design gap, closed with a test rather than argued
equivalent:
sameScoperequiring an exact account match → a v0.87 snapshot's stacks vanishfrom
ListExportsrather than exporting nothing (TestCFN_LegacySnapshotStacksStayInScope)loadExportsincluding the deploying stack → a redeploy imports the name it isin the middle of redefining (
TestCFN_AStackDoesNotImportItsOwnExport)between runs (
TestCFN_TheReportedExportConflictIsDeterministic)export is permanently undeletable (
TestCFN_AStacksOwnImportDoesNotPinIt)deleteStackusing the default identity → a non-default caller's own import isinvisible, so a delete that must be refused is allowed
(
TestCFNPlugin_DeleteRefusalUsesTheCallersOwnNamespace)Verification
gofmt -l .clean,go build ./...,go vet ./...,golangci-lint run ./...→0 issues,
make test(race) green,make docs-reference docs-reference-check docs-versions,npm --prefix docs run build,go vet -C test/e2e ./...,go test -C test/e2e ./...all green.End to end through the real AWS CLI against
substrate server: the importresolves to
e2e-net-bucketandsqs list-queuesreads it back;list-exportsreports the ARN
describe-stacksreports;list-importsnames the importer;deleting the exporter is refused with the
ValidationErrorabove; an unknownexport gives
CREATE_FAILEDwithFn::ImportValue: no exported output named "nope-Name" in this account and region;describe-stacksshowsExportNamebeside the output; deleting the importer first releases the exporter.
Closes #522