Repository navigation
fix(ec2,iam): resolve the default VPC before the decision, and authorize a fleet's launches (#673) - #683
Merged
Conversation
…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 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 #673
A
RunInstancesthat omitsSubnetIddoes 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 afterCheckAccess, so the decision named neither: the launch resolved to the literal"*", and"*"as a request resource matches only a statement whose ownResourcestarts 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
ec2AuthzResolveLaunchalready makes this move for launch templates, and its comment records why:CheckAccessruns before the handler, so the authorizer resolves through a free function made free for exactly that reason.ensureDefaultVPCcannot 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-onlyec2LookupDefaultVPCand the create, withec2LookupDefaultSecurityGroupbeside 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.
resolveDefaultVPCruns 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/*andsecurity-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 whyinstance/*andnetwork-interface/*are already wildcards in the same file. ADenyonsubnet/*still matches, and a least-privilegeAllownaming 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:
RunInstancesis no longer ever decided against the literal"*". Every launch names at leastinstance/*, and one that named nothing else still resolves its subnet, soec2AuthzLaunchMembers.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
CreateFleetlaunches throughrunInstancesWithTags, which the server's authorization pipeline never sees: the pipeline decides theCreateFleetrequest the caller sent, not theRunInstancesrequests the fleet synthesizes. A caller deniedec2:RunInstanceson a subnet reached it by asking for a fleet instead. Real AWS does not answer that way — itsCreateFleetreference requires permission for the launch's own resources — soEC2Pluginnow holds the*AuthController(threaded throughWithPluginAuth, 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 toCheckAccessis built byec2FleetLaunchRequest, the same builder the launch itself uses. The in-repo precedent isStackDeployer.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
SubnetIdand named a nonexistentSecurityGroupIdcommitted 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### Fixedline.Provenance
Authorizing a subnet-less launch against its default subnet is substrate's reading, not AWS parity, and
docs/services.mdsays so: the Service Authorization Reference'sRunInstancesscenario rows requiresubnet*only inEC2-VPC-EBS-SubnetandEC2-VPC-InstanceStore-Subnet, and AWS's own recommended subnet guardrail is theec2:Subnetcondition key onnetwork-interface/*(filed as a follow-up — it needs the same lookup this PR adds and would be substrate's firstec2:-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'sCreateFleetreference states the requirement outright.Verification
Fail-before, at the tier each defect lives at (mutate → run → restore, by file copy):
m.resolveDefaultVPC(state, reqCtx)journey_ec2_default_vpc_authz_test.go:171withgot <nil> (<nil>), want an API errorCheckAccess(|| trueon its guard):342withgot <nil>Mutation testing — 7 mutants, green baseline, anchor count asserted over the whole file,
gofmt -w && go vetso a non-compiling mutant is BROKEN rather than a result, full-packagego test -race -count=1 ./emulator/plus the e2e module with no-runfilter, restored and re-run green:ec2LookupDefaultVPCnever recognizes a default VPCM5 survived the first pass and the test added to kill it is the interesting one. The obvious test — a
Denyonsecurity-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-privilegeAllownaming the group and not the wildcard: a specific ARN in a statement does not match a request resource ofsecurity-group/*.Live run through the AWS CLI against
substrate server --address :4678(AWS_MAX_ATTEMPTS=1, torn down withlsof -ti:4678 | xargs kill), which also confirmsmain.goalready wiresWithPluginAuth, so the fleet fix is live in the real server:AccessDenied … on resource: …:subnet/*; 0 instances; still only the caller's 1 VPCsubnet/*+security-group/*→ launches, creatingsubnet-b347…/sg-3b44…Denyon the resolved default subnet →AccessDenied … subnet/subnet-b347…; instance count unchangedsubnet-b347…AccessDenied … security-group/sg-3b44…ec2:CreateFleetalone →AccessDenied … on resource: arn:aws:ec2:us-east-1::image/ami-0abcdef1234567890; 0 new instances; 0 fleet recordsDenyon the pool's subnet →AccessDenied … subnet/subnet-c74d…; 0 new instancesec2:RunInstanceson the launch's resources → the fleet launches 2, both in the pool's subnetGate:
gofmt -l .clean;go build ./...;go vet ./...;golangci-lint run ./...→ 0 issues;make test(race) ok;make coveragetotal 82.1% (emulator 82.8%);make docs-reference-check docs-versions;go test -C test/e2e ./...ok;npm --prefix docs run build.Compatibility
A
RunInstancesthat omitsSubnetIdand was permitted by a subnet-scoped policy may now be denied — including the first launch in a fresh account, which is decided againstsubnet/*. ACreateFleetmay now be denied unless the policy also permitsec2:RunInstanceson the launch's own resources, which is what real AWS requires. Neither affects a caller with no IAM configured, or one whose policy allowsec2:RunInstanceson"*".