Skip to content

fix(ec2): raise Invalid*.NotFound/Malformed for explicit resource IDs (#391) - #414

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/391-ec2-notfound
Jul 31, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/391-ec2-notfound

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #391

The defect

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:

if len(ids) > 0 && !containsStr(ids, vpc.VpcID) {
    continue
}

So a consumer's InvalidVpcID.NotFound branch was unreachable and its test
passed while verifying nothing — the failure mode CLAUDE.md calls out as worse
than an incomplete emulator. The mutating counterparts were worse still:
TerminateInstances swallowed the lookup and DeleteVpc ran _ = p.state.Delete(...),
both reporting success for IDs that were never there.

This is not about empty results in general. DescribeVpcs() with no arguments
legitimately returns an empty set — that distinction ("absent vs. filtered") is
pinned by TestEC2_DescribeWithNoIDs_ReturnsEmpty.

What changed

New emulator/ec2_resourceid.go holds the mechanism: an ec2IDKind per resource
carrying its wire prefix and its two error codes, plus an ec2IDFilter with a
newEC2IDFilter → validate() → match() → unresolved() contract, and
ec2RequireResource for 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 Delete error
wrapped 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:

Resource NotFound Malformed
i- InvalidInstanceID.NotFound InvalidInstanceID.Malformed
vpc- InvalidVpcID.NotFound InvalidVpcID.Malformed
subnet- InvalidSubnetID.NotFound InvalidSubnetID.Malformed
sg- InvalidGroup.NotFound InvalidGroupId.Malformed
igw- InvalidInternetGatewayID.NotFound InvalidInternetGatewayId.Malformed
rtb- InvalidRouteTableID.NotFound InvalidRouteTableId.Malformed
snap- InvalidSnapshot.NotFound InvalidSnapshotID.Malformed
eipalloc- InvalidAllocationID.NotFound (none published)
nat- InvalidNatGatewayID.NotFound (none published)

eipalloc- and nat- have no published Malformed variant, so a malformed ID
of those kinds falls back to NotFound.

Three orderings, each pinned by a test

  • Malformed precedes NotFound. Syntax is validated before any lookup, matching EC2 (TestEC2_MalformedBeatsNotFound).
  • An omitted ID is MissingParameter, not an empty-string Malformed that would name a parameter the caller never sent (TestEC2_MutateWithoutID_MissingParameter).
  • An ID excluded by a filter still counts as resolved — 200 with an empty set, not NotFound (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 Malformed check validates prefix and hex-ness but not length.
Substrate's generators emit 16 hex characters (randomHex(8)) where AWS emits 8
or 17, so a length check would have rejected substrate's own minted IDs.
TestEC2_SubstrateMintedIDsAreWellFormed creates a VPC, subnet, SG, IGW, route
table, 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.*),
DescribeLaunchTemplates and DescribeKeyPairs want the same treatment but are
different code families; worth a follow-up issue. DeleteSnapshot is left as-is —
its comment documents idempotent delete-on-missing as intentional and an existing
test asserts it.

Verification

emulator/ec2_notfound_test.go adds 9 table-driven tests (all t.Parallel()),
reusing the existing newEC2TestServer/ec2Request harness.

  • go build ./..., go vet ./... — clean
  • golangci-lint run ./... — 0 issues
  • make test (race) — pass
  • test/e2e — pass
  • make docs-reference-check — pass
  • make scan-fs (Trivy) — clean
  • Coverage on ec2_resourceid.go — 100% on all 9 functions

docs/services.md gains an Explicit resource IDs section documenting the six
semantics and the full code table, and links it from 17 operation rows.

…#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.
@scttfrdmn scttfrdmn added this to the v0.78.0 milestone Jul 31, 2026
@scttfrdmn scttfrdmn added type: bug Something is not working correctly area: ec2 labels Jul 31, 2026
@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.33708% with 35 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/ec2_plugin.go 70.58% 16 Missing and 19 partials ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 71dc6cb into main Jul 31, 2026
15 checks passed
@scttfrdmn
scttfrdmn deleted the feat/391-ec2-notfound branch July 31, 2026 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ec2 type: bug Something is not working correctly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EC2: describe_* by explicit ID returns empty list instead of Invalid*ID.NotFound

1 participant