Repository navigation
fix(ec2): a block device mapping can only name a completed snapshot (#732) - #743
Merged
Merged
Conversation
…732) CreateVolume refused a snapshot that was not usable yet from #715 onward, but a launch declaring the same snapshot in a BlockDeviceMapping materialized a volume from it and reported success. One rule, two doors, disagreeing: a consumer restoring through a launch — which is what CDK's and Terraform's snapshot-backed volumes do — never reached the error branch it had written. RegisterImage, whose own second AWS example registers an AMI from a root-device snapshot, had the same hole. The rule now lives in ec2CheckMappingSnapshot, so the launch path (and CreateFleet through it) and RegisterImage inherit it from one place: IncorrectState/400 naming the snapshot and its state, for everything except completed. The state has to come from an observation rather than the record: EC2Snapshot.State is "completed" for every snapshot substrate writes, so a record-reading check would be dead code. It also has to peek rather than observe — the countdown advances in exactly one place, so an observing read would spend one observation per mapping per request. The observer is injected as a second function parameter, keeping the validator free of a StateManager as its doc comment promises, and a nil observer skips the state rule for the two CreateLaunchTemplate paths.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…an permitting it Covers the InternalError branch of the state rule, which is the one path a seeded progression cannot reach: it needs the control-plane namespace to fail a Get while the snapshot record itself still resolves, so the double fails only that namespace and only once armed. A store failing every Get would stop at the record and answer InvalidSnapshot.NotFound — the collapse the assertion rules out.
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.
Closes #732
CreateVolumehas refused a snapshot that is not usable yet since #715. A launch declaring thesame snapshot in a
BlockDeviceMappingmaterialized a volume from it and reported success.So one rule had two doors that disagreed:
A consumer restoring through a launch — which is what CDK's and Terraform's snapshot-backed
volumes do — never reached the error branch it had written.
RegisterImage, whose own secondAWS example registers an AMI from a root-device snapshot, had the same hole, and an AMI recorded
against a snapshot that never completes is one every later launch fails on.
The change
The rule goes into
ec2CheckMappingSnapshot, so the launch path (andCreateFleetthrough it)and
RegisterImageinherit it from one place:IncorrectState/ 400, naming the snapshot andthe state it is in so a caller can decide to wait. Ordering inside the validator is existence →
state → size, which is
CreateVolume's order.Everything except
completedis refused, not justpendinganderror. AWS'ssnapshot-states table covers all four in its own words: a
pendingsnapshot "can't be usedwhile it is in the
pendingstate", anerrorone "can't be used", arecoverableone "mustfirst [be recovered] from the Recycle Bin", and a
recoveringone becomes "ready for use" onlyafter it reaches
completed.The state comes from an observation, not the record.
EC2Snapshot.Stateis"completed"forevery snapshot substrate writes, so
if snap.State != "completed"would be dead code — theobservable state lives only behind the seed accessors.
And it peeks rather than observes. The countdown advances in exactly one place, so an
observing read would spend one observation per mapping per request:
pendingObservations: 2would mean two polls in a test that only describes and fewer in one that also attempts a
three-mapping launch, and a consumer's wait loop — which alternates a launch attempt and a
describe — would race its own budget. A test pins this by asserting a seeded one-observation
budget survives two three-mapping launch attempts; swapping
peekforobservereddens exactlythat test and nothing else.
ec2CheckMappingSnapshotis deliberately pure — its doc comment records that it takes aresolver and no
StateManager. That is preserved by injecting a secondfunc(EC2Snapshot) (ec2SnapshotObservation, error)rather than promoting the function to amethod. A nil observer skips the state rule, which is how the exemption below is expressed.
The launch-template exemption
CreateLaunchTemplateandCreateLaunchTemplateVersionpass nil, with the reason recorded atboth call sites: a template records a mapping rather than consuming one, both report mapping
problems through AWS's documented
warningmember and refuse nothing (#693), and a snapshotpendingwhen a template is written may legitimately becompletedby the time the template isused — so refusing at write time would forbid an ordering AWS permits. A launch from such a
template is refused, so the rule is not escapable by routing a mapping through one; a test
asserts both halves.
Provenance
Substrate's reading.
API_RegisterImage.html's Errors section contains only the pointer to theerrors common to all actions — it is empty — and the page says nothing about a snapshot's
required state. The rule is AWS's snapshot-states rule, quoted above; the code is
substrate's choice from EC2's client-error table, the one
CreateVolumealready answers with,which is the same provenance shape that carries.
DeleteSnapshotis not analogous, and the issue's premise that #715 settled it is inverted:#715 records the opposite, that AWS permits deleting a snapshot still in progress. It still
refuses nothing on state grounds — the live run below shows a seeded
pendingsnapshot refusedonly with
InvalidSnapshot.InUse, because an AMI referenced it, a rule that has nothing to dowith the state.
A dedicated test covers the unreadable-state branch with a state double that fails Gets only in
the control-plane namespace, which is what keeps the refusal from collapsing into
InvalidSnapshot.NotFound.An unreadable seed answers
InternalError/ 500 rather than treating the snapshot as usable,which is deliberately the opposite of
ec2SnapshotResolver's absent-on-error rule and isdocumented as such: silently permitting a mapping because the seed could not be read would make
the rule's absence indistinguishable from its satisfaction.
Verification
Full gate green:
gofmt,go build,go vet,golangci-lint(0 issues),make test(race),make coverage(emulator 83.3%, total 82.6%),make docs-reference{,-check},make docs-versions,npm --prefix docs run buildwith no dangling anchors, andgo vet/go testin
test/e2e.Fail-before: nulling the injected observer at both call sites reddens all seven new tests.
Mutation: swapping
peekSnapshotStatusforobserveSnapshotStatusreddens only the poll-budgettest.
Live SDK run against a built binary, which is how the issue was found, all twelve parts
verified — the AMI resolved through the SSM parameter generated IaC uses, the unseeded launch
and
RegisterImagesucceeding, both refused once seeded with the identical message,CreateVolumeagreeing on the code, three mappings plus aRegisterImageplus aCreateVolumeleaving the seeded three-poll budget exactly intact (
pending 0% → 33% → 66% → completed 100%),both template operations accepting the pending mapping while a launch from the template is
refused, and the same template launching both volumes once the seed is cleared.
Compatibility
A launch or a
RegisterImagewhose mapping names a snapshot under a seeded progression nowfails until that snapshot reports
completed. Only a seeded snapshot is affected: every snapshotsubstrate writes is born
completed, so an unseeded mapping behaves exactly as before.