Repository navigation
fix(iam,ec2): the bundled policies' condition keys (#730) - #751
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This was referenced Aug 22, 2026
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 #730.
Three parts, one PR, because they are one issue and the docs change once.
ec2:ResourceTag/<key>, and making it observableTwo 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,StringLikeonec2:ResourceTag/aws:cloudformation:stack-name) andAmazonEKSClusterPolicyENIDelete(ec2:DeleteNetworkInterface,StringEqualsonec2:ResourceTag/eks:eni:owner). Substrate published a resource's tags underaws:ResourceTag/only, so neither condition could be satisfied. Both prefixes now report thesame 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 divergencein 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 wasdecided against a bare
*with no tags.ec2AuthzNamedResourceis now the single resolver forInstanceId,GroupId,RouteTableIdandInternetGatewayId, shared bybuildResourceARNand
resourceTagsForso the tags a condition is evaluated against always belong to the ARN thedecision is made about.
NetworkInterfaceId(substrate stores no standalone taggable ENIrecord),
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
*:resourceMatchesuses thestatement's own
Resourceas the pattern, so a request resource of*matched only a statementwhose
Resourcebegan 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": {…}}wasstored 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, whichasks
condEvaluateitself, so an operator the evaluator learns is accepted at write time in thesame commit — the drift #714 was filed about, in the other direction. Two further rules mirror
the evaluator's:
NullIfExistsis refused (AWS: "You can addIfExiststo the end of anycondition operator name except the
Nullcondition"), and a set-qualifiedNullis 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:NumericLessThanwould be substrate inventing arestriction.
Refusals are ordered deterministically. A statement's
Conditionis a Go map and the messagenames 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 therewould make a pre-existing stored document unloadable.
iam_set_qualifiers_test.go's"forallvalues:StringEquals"assertion stays at evaluation and stays green: AWS'scase-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 —
NotResourceand negatedoperators, where AWS says an unresolved variable does match — and the ordering constraint
that
aws:usernameneeds 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:usernameasabsent).
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="#…"vsid="…"diff overdist/services.html(dangling: none),and
go vet/go testintest/e2e. Both new functions and the new file are at 100% statementcoverage.
Mutation testing, 8 mutations, all red then reverted: drop the
ec2:ResourceTag/prefix;give every service a
<service>:ResourceTag/prefix; revertbuildResourceARNto"*";revert
resourceTagsForto instances-only; drop the refusal sort; disable each of the threeoperator rules.
Live SDK run against a built binary.
PutUserPolicywith{"NotAnOperator": …}→MalformedPolicyDocument, "Condition operator "NotAnOperator" is not valid.", andGetUserPolicythen answersNoSuchEntity— nothing was stored.CreateRolewithNullIfExistsandCreatePolicywith the same bad operator refuse through their own doors;ForSomeValues:StringEqualsrefuses naming the qualifier;ForAllValues:StringLikeIfExistsisaccepted and readable back. On the EC2 half, a user whose only grant is a
StringLikeonec2:ResourceTag/Stackdeletes the matching security group and route table and is denied on thenon-matching ones, and the same shape under
aws:ResourceTag/Envallows theEnv=prodinternetgateway and denies the
Env=devone — 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-nametag it names.CreateTagsrefuses a caller-suppliedaws:key exactly as AWS does, and substrate's CloudFormation deployer does not tag theresources a stack creates. Recorded in
docs/services.mdand filed as #746 rather than paperedover. Also filed from #730's survey: #747 (
CreateServiceLinkedRoleforiam:AWSServiceName)and #748 (ELB tagging for
elasticloadbalancing:CreateAction).