Skip to content

fix(ec2,iam): resolve the default VPC before the decision, and authorize a fleet's launches (#673) - #683

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/673-default-vpc-authz
Aug 20, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/673-default-vpc-authz

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #673

A RunInstances that omits SubnetId does not run without a subnet — substrate resolves the default VPC's default subnet and default security group, creating them if the account has none. Until now that happened after CheckAccess, so the decision named neither: the launch resolved to the literal "*", and "*" as a request resource matches only a statement whose own Resource starts with "*", so no specific ARN in the policy was ever compared against it. A policy scoped to one subnet permitted a launch into a subnet it never mentioned, and "launch anywhere except the default subnet" was defeated by leaving the parameter out. That is the shape every getting-started example and most CDK-generated launches take.

Resolve before authorizing

ec2AuthzResolveLaunch already makes this move for launch templates, and its comment records why: CheckAccess runs before the handler, so the authorizer resolves through a free function made free for exactly that reason. ensureDefaultVPC cannot be that function — it is a create-if-absent committing nine records, and calling it from the authorizer would let a request the policy is about to refuse create a VPC. So it is split into a read-only ec2LookupDefaultVPC and the create, with ec2LookupDefaultSecurityGroup beside it; the handler calls the same two, so the decision and the launch cannot disagree about where an instance lands.

Precedence: the default VPC applies only to a launch that nothing else gave a subnet. resolveDefaultVPC runs last, after the template merge, and yields to a request-named subnet, a nested interface's subnet and a template's subnet alike.

A launch whose default VPC does not exist yet is decided against subnet/* and security-group/*. This is not the skip-don't-widen case: that rule is about a resource the request omits, whereas this is a resource the request will create — which is precisely why instance/* and network-interface/* are already wildcards in the same file. A Deny on subnet/* still matches, and a least-privilege Allow naming one subnet correctly refuses a launch that will mint a different one. Substrate's fixtures all start from empty state, so this is the common case rather than a corner. A failed state read also resolves to the wildcards, so a storage fault cannot turn a guarded launch into an unguarded one.

One consequence worth stating: RunInstances is no longer ever decided against the literal "*". Every launch names at least instance/*, and one that named nothing else still resolves its subnet, so ec2AuthzLaunchMembers.empty() became structurally unreachable. It is deleted rather than left as a branch nothing can reach, with a comment where the guard was.

The fleet path is authorized, not exempted

CreateFleet launches through runInstancesWithTags, which the server's authorization pipeline never sees: the pipeline decides the CreateFleet request the caller sent, not the RunInstances requests the fleet synthesizes. A caller denied ec2:RunInstances on a subnet reached it by asking for a fleet instead. Real AWS does not answer that way — its CreateFleet reference requires permission for the launch's own resources — so EC2Plugin now holds the *AuthController (threaded through WithPluginAuth, whose doc comment asserting only CloudFormation takes one is rewritten) and authorizes every pool before the first one launches, so a refusal writes nothing at all rather than leaving the pools ahead of it behind. The request handed to CheckAccess is built by ec2FleetLaunchRequest, the same builder the launch itself uses. The in-repo precedent is StackDeployer.dispatch, which holds a controller for the same reason; like it, this is a no-op when no controller is wired or the principal is nil, so substrate's own internal callers are unaffected.

The partial-write fix, which stands on its own

A launch that omitted SubnetId and named a nonexistent SecurityGroupId committed a VPC, a default security group, an internet gateway, a route table, a subnet and four index mutations before being refused — and two of those writes swallow their own errors, so the state a refused request left behind was not even guaranteed to be complete. Reachable with no IAM configured at all. Group existence needs nothing and now runs before the first write; membership, which needs the target subnet's VPC, keeps its place. Its own ### Fixed line.

Provenance

Authorizing a subnet-less launch against its default subnet is substrate's reading, not AWS parity, and docs/services.md says so: the Service Authorization Reference's RunInstances scenario rows require subnet* only in EC2-VPC-EBS-Subnet and EC2-VPC-InstanceStore-Subnet, and AWS's own recommended subnet guardrail is the ec2:Subnet condition key on network-interface/* (filed as a follow-up — it needs the same lookup this PR adds and would be substrate's first ec2:-prefixed condition key). The reading is made because a guardrail a caller defeats by omitting a parameter is useless for the purpose substrate exists for. The fleet half is the opposite: AWS's CreateFleet reference states the requirement outright.

Verification

Fail-before, at the tier each defect lives at (mutate → run → restore, by file copy):

Removed Result
m.resolveDefaultVPC(state, reqCtx) 5 unit test functions fail (9 subtest failures); the e2e journey fails at journey_ec2_default_vpc_authz_test.go:171 with got <nil> (<nil>), want an API error
the fleet CheckAccess (|| true on its guard) the fleet journey fails at :342 with got <nil>
the security-group existence pre-check the partial-write test fails on VPC, subnet and security group all non-empty

Mutation testing — 7 mutants, green baseline, anchor count asserted over the whole file, gofmt -w && go vet so a non-compiling mutant is BROKEN rather than a result, full-package go test -race -count=1 ./emulator/ plus the e2e module with no -run filter, restored and re-run green:

# Mutant Verdict
M1 ec2LookupDefaultVPC never recognizes a default VPC KILLED (11)
M2 the default subnet is never found, so the subnet stays a wildcard KILLED (8)
M3 the security-group existence pre-check never refuses KILLED (1)
M4 the fleet's launches are not authorized KILLED (1, e2e only)
M5 the default group is wildcarded even when the request named groups KILLED (1)
M6 the default group overwrites the groups the request named KILLED (1)
M7 the default VPC overrides a subnet the request or template supplied KILLED (5)

M5 survived the first pass and the test added to kill it is the interesting one. The obvious test — a Deny on security-group/* must not block a launch that named its own group — asserts nothing, because that pattern matches the named group's own ARN too and refuses the launch either way. The discriminator is a least-privilege Allow naming the group and not the wildcard: a specific ARN in a statement does not match a request resource of security-group/*.

Live run through the AWS CLI against substrate server --address :4678 (AWS_MAX_ATTEMPTS=1, torn down with lsof -ti:4678 | xargs kill), which also confirms main.go already wires WithPluginAuth, so the fleet fix is live in the real server:

  1. policy allowing one subnet → AccessDenied … on resource: …:subnet/*; 0 instances; still only the caller's 1 VPC
  2. naming subnet/* + security-group/* → launches, creating subnet-b347… / sg-3b44…
  3. Deny on the resolved default subnet → AccessDenied … subnet/subnet-b347…; instance count unchanged
  4. least privilege naming both → launches, and lands in subnet-b347…
  5. dropping the group → AccessDenied … security-group/sg-3b44…
  6. ec2:CreateFleet alone → AccessDenied … on resource: arn:aws:ec2:us-east-1::image/ami-0abcdef1234567890; 0 new instances; 0 fleet records
  7. Deny on the pool's subnet → AccessDenied … subnet/subnet-c74d…; 0 new instances
  8. adding ec2:RunInstances on the launch's resources → the fleet launches 2, both in the pool's subnet

Gate: gofmt -l . clean; go build ./...; go vet ./...; golangci-lint run ./... → 0 issues; make test (race) ok; make coverage total 82.1% (emulator 82.8%); make docs-reference-check docs-versions; go test -C test/e2e ./... ok; npm --prefix docs run build.

Compatibility

A RunInstances that omits SubnetId and was permitted by a subnet-scoped policy may now be denied — including the first launch in a fresh account, which is decided against subnet/*. A CreateFleet may now be denied unless the policy also permits ec2:RunInstances on the launch's own resources, which is what real AWS requires. Neither affects a caller with no IAM configured, or one whose policy allows ec2:RunInstances on "*".

…ize a fleet's launches (#673)

A RunInstances that omits SubnetId does not run without a subnet: substrate
resolves the default VPC's default subnet and default security group, creating
them if the account has none. That happened after CheckAccess, so the decision
named neither and the launch resolved to the literal "*" — which as a *request*
resource matches only a statement whose own Resource starts with "*". A policy
scoped to one subnet therefore permitted a launch into a subnet it never
mentioned, and "launch anywhere except the default subnet" was defeated by
leaving the parameter out.

The authorizer now resolves the default VPC itself, through a read-only free
function rather than ensureDefaultVPC, which commits nine records and would let
a request the policy is about to refuse create a VPC. A launch whose default VPC
does not exist yet is decided against subnet/* and security-group/*, because it
is about to create both — the same footing instance/* and network-interface/*
already rest on. RunInstances is no longer ever decided against "*", so
ec2AuthzLaunchMembers.empty() became unreachable and is deleted.

CreateFleet launched through the plugin's own runInstances, which the server's
authorization pipeline never sees, so a caller denied ec2:RunInstances on a
subnet reached it by asking for a fleet instead. Every pool is now authorized
before the first one launches, using the same request builder the launch itself
uses, so a refused fleet leaves no instances and no fleet record behind.

Separately, a launch that omitted SubnetId and named a nonexistent security
group committed a VPC, subnet, security group, internet gateway, route table and
four index mutations before being refused — two of those writes swallow their
own errors. Group *existence* needs nothing and now runs before the first write;
membership, which needs the target subnet's VPC, keeps its place.
@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.32787% with 24 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/ec2_plugin.go 78.26% 10 Missing and 5 partials ⚠️
emulator/ec2_fleet.go 57.89% 7 Missing and 1 partial ⚠️
emulator/ec2_authz.go 96.66% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit fba22c6 into main Aug 20, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the feat/673-default-vpc-authz branch August 20, 2026 23:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ec2: the default VPC subnet and security group are resolved after the authorization decision

1 participant