Skip to content

EC2 plugin's injected/real errors use <ErrorResponse> shape; AWS SDK v2's ec2query deserializer expects <Errors><Error> wrapper #591

Closed

Description

@scttfrdmn

Context

Found while wiring calque (a Go CLI at github.com/spore-host/calque) to use
Substrate as an offline test harness for its EC2 acquisition code
(internal/plan/acquire.go's Acquirer, wrapping spawn's
*spawnaws.Client/launcher.Provision).

Repro

Seed a fault rule and call RunInstances via a real AWS SDK v2 EC2 client
pointed at a Substrate TestServer:

ts := emulator.StartTestServer(t)
// POST /v1/fault/rules: {"enabled":true,"rules":[{"service":"ec2","operation":"RunInstances","fault_type":"error","error_code":"InsufficientInstanceCapacity","http_status":500,"probability":1.0,"times":-1}]}

cfg, _ := config.LoadDefaultConfig(ctx,
    config.WithBaseEndpoint(ts.URL),
    config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("test", "test", "")),
)
client := ec2.NewFromConfig(cfg)
_, err := client.RunInstances(ctx, &ec2.RunInstancesInput{...})

A raw HTTP POST to the same endpoint (bypassing the SDK) confirms Substrate
sends:

<ErrorResponse><Error><Type>Sender</Type><Code>InsufficientInstanceCapacity</Code><Message>injected fault</Message></Error></ErrorResponse>

The mismatch

The AWS SDK v2 EC2 client uses the ec2query protocol for error
deserialization. Its GetErrorResponseComponents
(aws-sdk-go-v2/aws/protocol/ec2query/error_utils.go) declares:

type ErrorComponents struct {
	Code      string `xml:"Errors>Error>Code"`
	Message   string `xml:"Errors>Error>Message"`
	RequestID string `xml:"RequestID"`
}

Note the XPath: Errors>Error>Code — a plural <Errors> wrapping
element. Substrate's response has <ErrorResponse><Error><Code> — no
<Errors> wrapper at all (it looks like the REST-XML / query-protocol
success-response shape, or possibly modeled after S3/IAM's error shape
rather than EC2's specifically).

Because the XPath doesn't match, Code decodes as "", and the SDK's
error handling falls back to a generic smithy.GenericAPIError with code
"UnknownError" — the real injected code (or a real EC2 error code on a
genuine AWS-side failure) never reaches the caller.

Impact

Any consumer using the real AWS SDK v2 EC2 client against Substrate
(not just fault-injection scenarios — this would affect ANY EC2 error
response Substrate returns, including organically-triggered ones from
state/quota/consistency checks) cannot classify EC2 errors by their real
AWS error code. For calque specifically, this means our
Acquirer.classify()'s capacity-vs-terminal-vs-unknown error taxonomy
(keyed on codes like InsufficientInstanceCapacity,
InvalidAMIID.NotFound, etc.) can't be exercised end-to-end against
Substrate — it always sees UnknownError instead of the real code.

Suggested fix

Confirmed against a real AWS EC2 RunInstances error response — EC2's
actual wire format DOES use the <Errors><Error>...</Errors> wrapper
(this is standard EC2 Query-protocol error shape, distinct from S3/other
REST-XML services' unwrapped <Error>). writeError
(emulator/server.go) should emit the EC2/query-protocol shape
specifically for EC2 (and any other Query-protocol service Substrate
emulates), rather than the current one-size-fits-all
<ErrorResponse><Error> shape that seems to target REST-XML services.

Files referenced

  • emulator/server.go's writeError (the response-writing code)
  • emulator/fault.go/emulator/fault_control.go (where I found this,
    via fault injection, but it's not specific to faults — any EC2 error
    path hits the same writeError)

Version

Found against github.com/scttfrdmn/substrate v0.94.0 (via go get),
though the same code appears present in the local dev checkout too.

Activity

  1. added
    type: bug
    on Aug 8, 2026
  2. added this to the v0.95.0 milestone on Aug 8, 2026
  3. added
    area: server
    priority: high
    on Aug 8, 2026
  4. scttfrdmn commented on Aug 8, 2026

    @scttfrdmn
    OwnerAuthor

    Confirmed on main (9c5f6e8, latest tag v0.94.0). Two findings go beyond what the issue tests, and one of them changes the scope of the fix.

    1. It is not fault-specific — organic errors are broken identically. The issue notes this as a likelihood but only exercises the injected fault. Verified with the real SDK (service/[email protected]) against a live server: DescribeInstances on i-0000000000000dead returns the same <ErrorResponse><Error> wrapper carrying InvalidInstanceID.NotFound, and the SDK reports code="UnknownError" msg="UnknownError" — exactly as the injected InsufficientInstanceCapacity does. That is because writeError (emulator/server.go:722) delegates the shape entirely to marshalAWSError, and all 8 of its call sites — parse, signature, presigned expiry, auth, quota, consistency, fault, and the plugin route — go through it. So the fix belongs in the serializer, and it covers both paths at once.

    2. botocore tolerates both shapes, which is why every AWS-CLI gate here passed. EC2QueryParser._get_error_root walks the root's children for an Errors element and returns original_root when it is absent, so the wrong document and the documented one both parse to the right code. Measured: aws ec2 describe-instances --instance-ids i-0000000000000dead reports InvalidInstanceID.NotFound correctly today, against the broken shape. Substrate's EC2 coverage is CLI-driven, so no existing test could have caught this — only a real-SDK test can, and the fix is CLI-safe by the same token.

    Pre-verified, not inferred. A stand-in server serving the AWS-documented shape —

    <Response><Errors><Error><Code>…</Code><Message>…</Message></Error></Errors><RequestID>…</RequestID></Response>
    

    — made the same SDK probe report code="InsufficientInstanceCapacity" msg="injected fault" for both the injected and the organic call.

    Scope. ec2 is the only service on the ec2 protocol (checked the protocol field of every botocore service model), and the two documents are mutually exclusive: ec2query.ErrorComponents reads Code at the XPath Errors>Error>Code while the shared awsxml.wrappedErrorResponse reads Error>Code, and each yields "" for the other's document. So this is one new awsErrorProtocol arm plus one moved map entry — following the structure #480 established for S3 — not a change to the shared Query shape, which IAM and every other query service still need. CFN's in-process stack deploys are unaffected: cfnDispatchError reads the code with extractXMLField(resp.Body, "Code"), a substring scan at any depth rather than a struct decode.

    One correction to make along the way: the serviceErrorProtocols doc comment currently asserts that "every one of their parsers recovers the code from an <ErrorResponse><Error><Code> document, so a single shape serves all three". This issue is that sentence being false for the ec2 protocol.

    Milestoned to v0.95.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: ec2area: serverpriority: hightype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions