Repository navigation
fix(ec2): raise Invalid*.NotFound/Malformed for explicit resource IDs (#391) - #414
Merged
Merged
Conversation
…#391) Naming a resource ID explicitly on a Describe* call is an assertion that the ID exists, and EC2 answers it with an error when it does not. Substrate instead filtered the ID out and returned 200 with an empty set, so a consumer's error branch was unreachable and its test passed while verifying nothing. The mutating counterparts (Terminate/Stop/Start, Delete*) swallowed the lookup entirely and reported success for IDs that were never there. Ten describes (DescribeInstances, DescribeInstanceStatus, DescribeVpcs, DescribeSubnets, DescribeSecurityGroups, DescribeInternetGateways, DescribeRouteTables, DescribeSnapshots, DescribeAddresses, DescribeNatGateways) and eight mutators now validate syntax before existence and raise the resource's own Invalid*.Malformed or Invalid*.NotFound code. AWS spells these inconsistently (InvalidGroupId.Malformed vs InvalidGroup.NotFound, InvalidRouteTableId.Malformed vs InvalidRouteTableID.NotFound); every code is mirrored verbatim from the EC2 error reference because SDK callers match the literal string. eipalloc- and nat- have no published Malformed variant, so a malformed ID of those kinds surfaces as NotFound. Three orderings are pinned by tests: Malformed precedes NotFound, an omitted ID reports MissingParameter rather than an empty-string Malformed, and an ID excluded by a filter still counts as resolved (200, empty set — not NotFound). Describes with no explicit ID keep returning an empty set, which is the "absent vs. filtered" distinction the fix turns on. The Malformed check deliberately does not pin ID length: substrate's generators emit 16 hex characters where AWS emits 8 or 17, so a length check would reject substrate's own IDs. TestEC2_SubstrateMintedIDsAreWellFormed guards that.
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.
Closes #391
The defect
Naming a resource ID explicitly on a
Describe*call is an assertion that the IDexists, and EC2 answers it with an error when it does not. Substrate instead
filtered the ID out and returned
200with an empty set:So a consumer's
InvalidVpcID.NotFoundbranch was unreachable and its testpassed while verifying nothing — the failure mode
CLAUDE.mdcalls out as worsethan an incomplete emulator. The mutating counterparts were worse still:
TerminateInstancesswallowed the lookup andDeleteVpcran_ = p.state.Delete(...),both reporting success for IDs that were never there.
This is not about empty results in general.
DescribeVpcs()with no argumentslegitimately returns an empty set — that distinction ("absent vs. filtered") is
pinned by
TestEC2_DescribeWithNoIDs_ReturnsEmpty.What changed
New
emulator/ec2_resourceid.goholds the mechanism: anec2IDKindper resourcecarrying its wire prefix and its two error codes, plus an
ec2IDFilterwith anewEC2IDFilter→validate()→match()→unresolved()contract, andec2RequireResourcefor single-ID mutators.Ten describes now use it —
DescribeInstances,DescribeInstanceStatus,DescribeVpcs,DescribeSubnets,DescribeSecurityGroups,DescribeInternetGateways,DescribeRouteTables,DescribeSnapshots,DescribeAddresses,DescribeNatGateways— as do eight mutators(
Terminate/Stop/StartInstances,DeleteVpc/Subnet/SecurityGroup/InternetGateway/RouteTable).The five deletes are now Get-then-check-then-Delete, with the
Deleteerrorwrapped rather than discarded.
Error codes
Every code is mirrored verbatim from the EC2 error reference
rather than from memory, because SDK callers match the literal string and AWS is
inconsistent about casing:
i-InvalidInstanceID.NotFoundInvalidInstanceID.Malformedvpc-InvalidVpcID.NotFoundInvalidVpcID.Malformedsubnet-InvalidSubnetID.NotFoundInvalidSubnetID.Malformedsg-InvalidGroup.NotFoundInvalidGroupId.Malformedigw-InvalidInternetGatewayID.NotFoundInvalidInternetGatewayId.Malformedrtb-InvalidRouteTableID.NotFoundInvalidRouteTableId.Malformedsnap-InvalidSnapshot.NotFoundInvalidSnapshotID.Malformedeipalloc-InvalidAllocationID.NotFoundnat-InvalidNatGatewayID.NotFoundeipalloc-andnat-have no publishedMalformedvariant, so a malformed IDof those kinds falls back to
NotFound.Three orderings, each pinned by a test
TestEC2_MalformedBeatsNotFound).MissingParameter, not an empty-stringMalformedthat would name a parameter the caller never sent (TestEC2_MutateWithoutID_MissingParameter).200with an empty set, notNotFound(TestEC2_DescribeIDWithNonMatchingFilter_ReturnsEmpty).Also deliberate: one present + one absent ID fails the whole call, since EC2 does
not return the partial set.
The length trap
The
Malformedcheck validates prefix and hex-ness but not length.Substrate's generators emit 16 hex characters (
randomHex(8)) where AWS emits 8or 17, so a length check would have rejected substrate's own minted IDs.
TestEC2_SubstrateMintedIDsAreWellFormedcreates a VPC, subnet, SG, IGW, routetable, instance, EIP and NAT gateway, then describes each by the ID its create
returned — a standing guard against that regression.
Out of scope
DescribeVolumes(InvalidVolume.NotFound),DescribeImages(InvalidAMIID.*),DescribeLaunchTemplatesandDescribeKeyPairswant the same treatment but aredifferent code families; worth a follow-up issue.
DeleteSnapshotis left as-is —its comment documents idempotent delete-on-missing as intentional and an existing
test asserts it.
Verification
emulator/ec2_notfound_test.goadds 9 table-driven tests (allt.Parallel()),reusing the existing
newEC2TestServer/ec2Requestharness.go build ./...,go vet ./...— cleangolangci-lint run ./...— 0 issuesmake test(race) — passtest/e2e— passmake docs-reference-check— passmake scan-fs(Trivy) — cleanec2_resourceid.go— 100% on all 9 functionsdocs/services.mdgains an Explicit resource IDs section documenting the sixsemantics and the full code table, and links it from 17 operation rows.