Skip to content

fix(iam,ec2): the bundled policies' condition keys (#730) - #751

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/730-condition-keys
Aug 22, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/730-condition-keys

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #730.

Three parts, one PR, because they are one issue and the docs change once.

ec2:ResourceTag/<key>, and making it observable

Two of the bundled AWS-authored managed policies scope an EC2 delete by tag under EC2's own
prefix
rather than the global one: ManagedCloudformationResourcesCleanupPolicy
(ec2:DeleteInternetGateway, ec2:DeleteRoute, ec2:DeleteRouteTable,
ec2:DeleteSecurityGroup, StringLike on ec2:ResourceTag/aws:cloudformation:stack-name) and
AmazonEKSClusterPolicyENIDelete (ec2:DeleteNetworkInterface, StringEquals on
ec2:ResourceTag/eks:eni:owner). Substrate published a resource's tags under
aws:ResourceTag/ only, so neither condition could be satisfied. Both prefixes now report the
same tag, from the resource's one tag set, because a policy may write the condition either way
and AWS answers both. The prefix is specific to EC2: reporting an S3 bucket's tags under an
s3:ResourceTag/ key AWS does not publish would honor a policy real AWS ignores — a divergence
in the granting direction.

The prefix alone would have been inert, which is the issue's one wrong prediction. Authorization
resolved an EC2 request to a real ARN, and to that resource's tags, only when the request
named InstanceId
— every operation the two statements govern names something else, and was
decided against a bare * with no tags. ec2AuthzNamedResource is now the single resolver for
InstanceId, GroupId, RouteTableId and InternetGatewayId, shared by buildResourceARN
and resourceTagsFor so the tags a condition is evaluated against always belong to the ARN the
decision is made about. NetworkInterfaceId (substrate stores no standalone taggable ENI
record), VpcId/SubnetId (a container, not the resource acted on) and the plural forms
(#744) stay at *, each with its reason in the docs.

Safe in one direction only, which is why the fallback is *: resourceMatches uses the
statement's own Resource as the pattern, so a request resource of * matched only a statement
whose Resource began with *. Substituting a concrete ARN can narrow a grant, never widen it.

Write-time operator-name validation

All four IAM submission paths — CreateRole, UpdateAssumeRolePolicy, CreatePolicy,
putInlinePolicy — checked JSON syntax and nothing else, so {"NotAnOperator": {…}} was
stored and then discarded at evaluation, leaving a policy that looked attached and granted
nothing. They now answer MalformedPolicyDocument / 400, naming the operator.

The vocabulary is not restated: recognition goes through condOperatorRecognized, which
asks condEvaluate itself, so an operator the evaluator learns is accepted at write time in the
same commit — the drift #714 was filed about, in the other direction. Two further rules mirror
the evaluator's: NullIfExists is refused (AWS: "You can add IfExists to the end of any
condition operator name except the Null condition"), and a set-qualified Null is refused.
A set qualifier over any other recognized operator is accepted, including combinations AWS's
condition-operator page does not tabulate — its set-operator reference describes the qualifiers
generally, so refusing ForAllValues:NumericLessThan would be substrate inventing a
restriction.

Refusals are ordered deterministically. A statement's Condition is a Go map and the message
names one operator, so without sorting two submissions of the same bad document would be refused
with different sentences and a recorded run would not replay to the same response. Pinned with
20 iterations.

Placement, stated explicitly: validation is on the submission paths only, never in
PolicyDocument.UnmarshalJSON, which also runs on every read from state — validating there
would make a pre-existing stored document unloadable. iam_set_qualifiers_test.go's
"forallvalues:StringEquals" assertion stays at evaluation and stays green: AWS's
case-insensitivity covers key names, not operator names. Predicted blast radius across the
whole suite was 0 test failures; observed 0.

Policy variables: a recorded non-implementation

Documented as a deliberate choice with the divergence that remains — NotResource and negated
operators, where AWS says an unresolved variable does match — and the ordering constraint
that aws:username needs an honest producer first, which cannot be the ARN's last component
(that yields the session name for an assumed role, where AWS documents aws:username as
absent).

Verification

Full gate green: gofmt, go build, go vet, golangci-lint (0 issues), make test (race),
make coverage (emulator 83.3%, total 82.7%), make docs-reference-check, make docs-versions,
the docs build plus an href="#…" vs id="…" diff over dist/services.html (dangling: none),
and go vet/go test in test/e2e. Both new functions and the new file are at 100% statement
coverage.

Mutation testing, 8 mutations, all red then reverted: drop the ec2:ResourceTag/ prefix;
give every service a <service>:ResourceTag/ prefix; revert buildResourceARN to "*";
revert resourceTagsFor to instances-only; drop the refusal sort; disable each of the three
operator rules.

Live SDK run against a built binary. PutUserPolicy with {"NotAnOperator": …} →
MalformedPolicyDocument, "Condition operator "NotAnOperator" is not valid.", and
GetUserPolicy then answers NoSuchEntity — nothing was stored. CreateRole with
NullIfExists and CreatePolicy with the same bad operator refuse through their own doors;
ForSomeValues:StringEquals refuses naming the qualifier; ForAllValues:StringLikeIfExists is
accepted and readable back. On the EC2 half, a user whose only grant is a StringLike on
ec2:ResourceTag/Stack deletes the matching security group and route table and is denied on the
non-matching ones, and the same shape under aws:ResourceTag/Env allows the Env=prod internet
gateway and denies the Env=dev one — both prefixes answering from one tag set.

Found while verifying, filed not fixed

The bundled cleanup statement stays unsatisfiable for a second reason: nothing in substrate
produces the aws:cloudformation:stack-name tag it names. CreateTags refuses a caller-supplied
aws: key exactly as AWS does, and substrate's CloudFormation deployer does not tag the
resources a stack creates. Recorded in docs/services.md and filed as #746 rather than papered
over. Also filed from #730's survey: #747 (CreateServiceLinkedRole for iam:AWSServiceName)
and #748 (ELB tagging for elasticloadbalancing:CreateAction).

Three parts of one issue: the condition key two AWS-authored bundled
policies write about but substrate never published, the operator names
substrate accepted and then discarded, and the policy-variable
substitution it does not do.

ec2:ResourceTag/<key>. Substrate reported a resource's tags under
aws:ResourceTag/ only, so a statement conditioned on EC2's own prefix
could never be satisfied — which is the prefix AWS's own
ManagedCloudformationResourcesCleanupPolicy and
AmazonEKSClusterPolicyENIDelete use. Both prefixes now report the same
tag from the resource's one tag set. The prefix alone would have been
inert: authorization resolved an EC2 request to a real ARN and to that
resource's tags only when the request named InstanceId, and every
operation the two statements govern names something else. One resolver,
shared by the ARN builder and the tag reader so the tags a condition is
evaluated against always belong to the ARN the decision is made about,
now covers GroupId, RouteTableId and InternetGatewayId as well.
NetworkInterfaceId, VpcId/SubnetId and the plural forms stay at "*",
each with its reason recorded.

Write-time operator validation. All four IAM submission paths —
CreateRole, UpdateAssumeRolePolicy, CreatePolicy, putInlinePolicy —
checked JSON syntax and nothing else, so a misspelled operator was
stored and silently ignored at evaluation, leaving a policy that looked
attached and granted nothing. They now answer MalformedPolicyDocument /
400 naming the operator. The vocabulary is not restated: recognition
goes through condOperatorRecognized, which asks the evaluator itself, so
the two cannot drift. Refusals are ordered deterministically, because a
statement's Condition is a map and the message names one operator.

Validation is on the submission paths only, never in
PolicyDocument.UnmarshalJSON, which also runs on every read from state:
validating there would make a pre-existing stored document unloadable.

Policy variables are documented as a deliberate non-implementation, with
the divergence that remains (NotResource and negated operators, where
AWS says an unresolved variable does match) and the ordering constraint
that aws:username needs an honest producer first.
@codecov

codecov Bot commented Aug 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 93affea into main Aug 22, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/730-condition-keys branch August 22, 2026 19:55
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.

iam: the keys the bundled policies condition on have no producer

1 participant