feat(iam): report EvalDecisionDetails per policy type for cross-account simulations (#9766)

This commit is contained in:
Oleksandr Nikolaichuk authored and GitHub committed 2026-11-29 16:35:55 -04:00
1 parent e0a57623c2
commit 5a3bff3939
4 files changed
+160 -24

No files matched your search

+8 -2
View File
@@ -1182,8 +1182,14 @@ the resource policy must allow access. Identity-only simulations use same-accoun
Resource policy condition keys count toward `MissingContextValues`. Resource policy statements
are not listed in `MatchedStatements` yet, and `SimulateCustomPolicy` ignores `ResourcePolicy`.
AWS returns a decision per policy type in `EvalDecisionDetails` for cross-account simulations.
Floci leaves it empty for now. For same-account simulations, AWS returns the empty member when
the request includes a resource ARN and omits it when the request specifies all resources (`*`).
For each named resource that passes the SCP walk, a cross-account simulation reports `IAM Policy`
and `Resource Policy` decisions, plus `Permissions Boundary Policy` when a boundary is supplied.
The top-level result reports the most restrictive value per policy type across the resources that
report details. For same-account simulations, AWS returns the empty member when the request
includes a resource ARN and omits it when the request specifies all resources (`*`). Floci also
returns an empty top-level member when the call names resources, none report details, and at least
one was not denied by an SCP. Floci omits the top-level member when an SCP denies every named
resource.
A result names the statements that decided it. `MatchedStatements` reports each one by the policy
it came from and the place it sits in that policy's text: a supplied document is
@@ -169,10 +169,13 @@ public class IamPolicyEvaluator {
* the simulation was handed none
* @param allowedByPermissionsBoundary whether the permissions boundary allowed it, or null when
* there is no boundary in the simulation
* @param decisionDetails decisions by policy type for a cross-account simulation,
* or null when same-account or blocked by the SCP walk
*/
public record SimulationReport(SimulationDecision decision, List<MatchedStatement> matchedStatements,
List<String> missingContextValues, Boolean allowedByOrganizations,
Boolean allowedByPermissionsBoundary) {
Boolean allowedByPermissionsBoundary,
Map<String, SimulationDecision> decisionDetails) {
}
private static final Logger LOG = Logger.getLogger(IamPolicyEvaluator.class);
@@ -522,14 +525,38 @@ public class IamPolicyEvaluator {
DecisionRecord record = decide(policies, resourceDecision, relationship, loweredAction, resource, ctx);
SimulationDecision decision = SimulationDecision.from(record.outcome());
List<CachedDocument> boundary = policies.boundary();
SimulationDecision boundaryDecision = boundary == null ? null
: simulationPolicyDecision(boundary, loweredAction, resource, ctx);
Map<String, SimulationDecision> decisionDetails = null;
if (relationship == ResourceAccountRelationship.CROSS_ACCOUNT && policies.scp().allows()) {
decisionDetails = crossAccountDecisionDetails(
simulationPolicyDecision(policies.identity(), loweredAction, resource, ctx),
resourceDecision, boundaryDecision);
}
return new SimulationReport(decision,
matchedStatements(decision, caller, policies.identity(), loweredAction, resource, ctx),
missingContextValues(policies, resourcePolicy == null
? List.of() : List.of(parseDocument(resourcePolicy.document())), loweredAction, resource, ctx),
caller.scpLevels() == null
? null : policies.scp().firstDeny() == null && policies.scp().allows(),
boundary == null ? null : !anyMatch(boundary, true, loweredAction, resource, ctx)
&& anyMatch(boundary, false, loweredAction, resource, ctx));
boundaryDecision == null ? null : boundaryDecision == SimulationDecision.ALLOWED,
decisionDetails);
}
private static Map<String, SimulationDecision> crossAccountDecisionDetails(
SimulationDecision identityDecision, ResourcePolicyDecision resourceDecision,
SimulationDecision boundaryDecision) {
Map<String, SimulationDecision> details = new LinkedHashMap<>();
details.put("IAM Policy", identityDecision);
details.put("Resource Policy", switch (resourceDecision) {
case EXPLICIT_DENY -> SimulationDecision.EXPLICIT_DENY;
case ALLOW, ALLOW_DIRECT_IAM_USER, ALLOW_ACCOUNT -> SimulationDecision.ALLOWED;
case NEUTRAL -> SimulationDecision.IMPLICIT_DENY;
});
if (boundaryDecision != null) {
details.put("Permissions Boundary Policy", boundaryDecision);
}
return details;
}
/**
@@ -566,6 +593,15 @@ public class IamPolicyEvaluator {
return false;
}
private SimulationDecision simulationPolicyDecision(List<CachedDocument> documents, String action,
String resource, Map<String, List<String>> ctx) {
if (anyMatch(documents, true, action, resource, ctx)) {
return SimulationDecision.EXPLICIT_DENY;
}
return anyMatch(documents, false, action, resource, ctx)
? SimulationDecision.ALLOWED : SimulationDecision.IMPLICIT_DENY;
}
/**
* The statements a result reports as having decided this scenario: "a list of the statements in
* the input policies that determine the result", where "even if multiple statements allow the
@@ -2527,10 +2527,13 @@ public class IamQueryHandler {
* "the deduplicated set of missing context values across all requested resources".
*
* <p>AWS returns a decision per policy type in {@code EvalDecisionDetails} for cross-account
* simulations. Floci leaves it empty for now. For same-account simulations, AWS returns the
* empty member when the request includes a resource ARN and omits it when the request specifies
* all resources ({@code *}). Floci includes the empty member when the call names resources
* and omits it when the call names none.
* simulations. Floci reports those decisions for each named resource that passes the SCP walk
* and the most restrictive decision per policy type at the top level. For same-account
* simulations, AWS returns the empty member when the request includes a resource ARN and
* omits it when the request specifies all resources ({@code *}). Floci includes the empty
* member at the top level when the call names resources, none report details, and at least one
* was not denied by an SCP. It omits the member when the call names none or when an SCP denies
* every named resource.
*/
private void evaluationResult(XmlBuilder xml, CallerContext caller, SimulatedResourcePolicy resourcePolicy,
String actionName, List<String> resourceArns, Map<String, List<String>> context) {
@@ -2550,7 +2553,9 @@ public class IamQueryHandler {
decisionDetail(xml, "PermissionsBoundaryDecisionDetail", "AllowedByPermissionsBoundary",
aggregateFlag(reports, IamPolicyEvaluator.SimulationReport::allowedByPermissionsBoundary));
if (!List.of("*").equals(resourceArns)) {
xml.start("EvalDecisionDetails").end("EvalDecisionDetails");
if (reports.stream().anyMatch(report -> !Boolean.FALSE.equals(report.allowedByOrganizations()))) {
evalDecisionDetails(xml, reportedDecisionDetails(reports));
}
xml.start("ResourceSpecificResults");
for (int resource = 0; resource < resourceArns.size(); resource++) {
IamPolicyEvaluator.SimulationReport report = reports.get(resource);
@@ -2561,6 +2566,9 @@ public class IamQueryHandler {
missingContextValues(xml, report.missingContextValues());
decisionDetail(xml, "PermissionsBoundaryDecisionDetail", "AllowedByPermissionsBoundary",
report.allowedByPermissionsBoundary());
if (report.decisionDetails() != null) {
evalDecisionDetails(xml, report.decisionDetails());
}
xml.end("member");
}
xml.end("ResourceSpecificResults");
@@ -2568,6 +2576,25 @@ public class IamQueryHandler {
xml.end("member");
}
private static Map<String, IamPolicyEvaluator.SimulationDecision> reportedDecisionDetails(
List<IamPolicyEvaluator.SimulationReport> reports) {
Map<String, IamPolicyEvaluator.SimulationDecision> reported = new LinkedHashMap<>();
for (IamPolicyEvaluator.SimulationReport report : reports) {
if (report.decisionDetails() != null) {
report.decisionDetails().forEach((type, decision) -> reported.merge(type, decision,
(previous, current) -> mostRestrictive(List.of(previous, current))));
}
}
return reported;
}
private static void evalDecisionDetails(XmlBuilder xml, Map<String, IamPolicyEvaluator.SimulationDecision> details) {
xml.start("EvalDecisionDetails");
details.forEach((type, decision) ->
xml.start("entry").elem("key", type).elem("value", decision.awsValue()).end("entry"));
xml.end("EvalDecisionDetails");
}
/**
* The statements the top-level result reports: "the union of matched statements across all
* requested resources", held to the sentence beside it, "only statements that contributed to
@@ -3,13 +3,17 @@ package io.floci.aws.services.iam;
import io.floci.aws.testing.PartitionCleanup;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.xml.XmlPath;
import io.restassured.path.xml.element.Node;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static io.floci.aws.testing.IamSimulationRequests.answer;
import static io.floci.aws.testing.IamSimulationRequests.level;
import static io.floci.aws.testing.IamSimulationRequests.strings;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
@@ -40,8 +44,10 @@ class IamSimulatePrincipalInputDocumentsIntegrationTest {
private static final String CODE = "ErrorResponse.Error.Code";
private static final String MESSAGE = "ErrorResponse.Error.Message";
private static final String DECISION = "SimulatePrincipalPolicyResponse"
+ ".SimulatePrincipalPolicyResult.EvaluationResults.member.EvalDecision";
private static final String RESULT = "SimulatePrincipalPolicyResponse"
+ ".SimulatePrincipalPolicyResult.EvaluationResults.member";
private static final String DECISION = RESULT + ".EvalDecision";
private static final String DECISION_DETAILS = ".EvalDecisionDetails";
private static final String CUSTOM_DECISION = "SimulateCustomPolicyResponse"
+ ".SimulateCustomPolicyResult.EvaluationResults.member.EvalDecision";
@@ -165,6 +171,12 @@ class IamSimulatePrincipalInputDocumentsIntegrationTest {
.extract().xmlPath().getString(DECISION);
}
private static Map<String, String> decisionDetails(XmlPath result, String path) {
return result.getList(path + DECISION_DETAILS + ".entry", Node.class).stream()
.collect(Collectors.toMap(entry -> entry.getNode("key").value(),
entry -> entry.getNode("value").value()));
}
/**
* AWS's own example for this operation: a user whose policies do not allow the action is
* allowed it on the strength of an input document alone.
@@ -173,7 +185,9 @@ class IamSimulatePrincipalInputDocumentsIntegrationTest {
void anInputDocumentAllowsWhatThePrincipalHasNoPolicyFor() {
String user = createUser("input-allow");
assertEquals("implicitDeny", decision(simulate(user, "s3:PutObject")));
XmlPath wildcard = answer(simulate(user, "s3:PutObject"));
assertEquals("implicitDeny", wildcard.getString(DECISION));
assertEquals(List.of(), strings(wildcard, RESULT + DECISION_DETAILS));
assertEquals("allowed", decision(simulate(user, "s3:PutObject")
.formParam(INPUT + "1", allow("s3:PutObject"))));
}
@@ -269,6 +283,7 @@ class IamSimulatePrincipalInputDocumentsIntegrationTest {
String object = "arn:aws:s3:::bucket/k";
String getObject = "s3:GetObject";
String queue = "arn:aws:sqs:us-east-1:222222222222:q";
String otherQueue = "arn:aws:sqs:us-east-1:222222222222:other-q";
String sendMessage = "sqs:SendMessage";
String otherOwner = "arn:aws:iam::222222222222:root";
String objectAllowForDavid = RESOURCE_POLICY.formatted("Allow", davidArn, getObject, object);
@@ -281,34 +296,86 @@ class IamSimulatePrincipalInputDocumentsIntegrationTest {
String bobWithBobsGrant = decision(simulate(david, getObject, object)
.formParam("CallerArn", bobArn)
.formParam("ResourcePolicy", objectAllowForBob));
String queueWithResourceAllow = decision(simulate(david, sendMessage, queue)
XmlPath queueWithResourceAllow = answer(simulate(david, sendMessage, queue)
.formParam("ResourcePolicy", queueAllowForDavid));
String objectWithOtherOwner = decision(simulate(david, getObject, object)
XmlPath queuesWithOneResourceAllow = answer(simulate(david, sendMessage, queue)
.formParam("ResourceArns.member.2", otherQueue)
.formParam("ResourcePolicy", queueAllowForDavid));
XmlPath objectWithOtherOwner = answer(simulate(david, getObject, object)
.formParam("ResourceOwner", otherOwner)
.formParam("ResourcePolicy", objectAllowForDavid));
String objectWithDefaultOwner = decision(simulate(david, getObject, object)
XmlPath objectWithDefaultOwner = answer(simulate(david, getObject, object)
.formParam("ResourcePolicy", objectAllowForDavid));
attachNewPolicy(david, "input-sqs-sender", sendMessage);
String queueWithBothAllows = decision(simulate(david, sendMessage, queue)
XmlPath queueWithBothAllows = answer(simulate(david, sendMessage, queue)
.formParam("ResourcePolicy", queueAllowForDavid));
XmlPath queueWithScpDeny = answer(simulate(david, sendMessage, queue)
.formParam("ResourcePolicy", queueAllowForDavid)
.formParam(level(1), deny(sendMessage)));
String queueWithIdentityAllow = decision(simulate(david, sendMessage, queue));
XmlPath queueWithIdentityDeny = answer(simulate(david, sendMessage, queue)
.formParam(INPUT + "1", deny(sendMessage))
.formParam("ResourcePolicy", queueAllowForDavid));
XmlPath queueWithResourceDeny = answer(simulate(david, sendMessage, queue)
.formParam("ResourcePolicy", RESOURCE_POLICY.formatted("Deny", davidArn, sendMessage, queue)));
String putObject = "s3:PutObject";
XmlPath objectWithBoundary = answer(simulate(david, putObject, object)
.formParam("ResourceOwner", otherOwner)
.formParam("ResourcePolicy", RESOURCE_POLICY.formatted("Allow", davidArn, putObject, object))
.formParam(BOUNDARY + "1", allow("iam:*")));
record CrossAccountCase(String description, XmlPath result, String decision, Map<String, String> details) {}
List<CrossAccountCase> crossAccountCases = List.of(
new CrossAccountCase("SQS resource allow without an identity policy", queueWithResourceAllow,
"implicitDeny", Map.of("IAM Policy", "implicitDeny", "Resource Policy", "allowed")),
new CrossAccountCase("SQS identity and resource allow", queueWithBothAllows,
"allowed", Map.of("IAM Policy", "allowed", "Resource Policy", "allowed")),
new CrossAccountCase("S3 resource allow with a cross-account ResourceOwner", objectWithOtherOwner,
"implicitDeny", Map.of("IAM Policy", "implicitDeny", "Resource Policy", "allowed")),
new CrossAccountCase("SQS identity deny with a resource allow", queueWithIdentityDeny,
"explicitDeny", Map.of("IAM Policy", "explicitDeny", "Resource Policy", "allowed")),
new CrossAccountCase("SQS resource deny with an identity allow", queueWithResourceDeny,
"explicitDeny", Map.of("IAM Policy", "allowed", "Resource Policy", "explicitDeny")),
new CrossAccountCase("S3 PutObject resource allow with a boundary", objectWithBoundary,
"implicitDeny", Map.of("IAM Policy", "implicitDeny", "Resource Policy", "allowed",
"Permissions Boundary Policy", "implicitDeny")));
String resources = RESULT + ".ResourceSpecificResults.member";
assertAll(
() -> assertEquals("implicitDeny", bobWithDavidsGrant,
"CallerArn Bob with a resource policy that grants access to David"),
() -> assertEquals("allowed", bobWithBobsGrant,
"CallerArn Bob with a resource policy that grants access to Bob"),
() -> assertEquals("implicitDeny", queueWithResourceAllow,
"Cross-account SQS ARN with a resource allow and no identity policy"),
() -> assertEquals("allowed", queueWithBothAllows,
"Cross-account SQS ARN with both identity and resource allows"),
() -> assertEquals("implicitDeny", objectWithOtherOwner,
"S3 object with a resource allow and a cross-account ResourceOwner"),
() -> assertEquals("allowed", objectWithDefaultOwner,
() -> assertEquals("allowed", objectWithDefaultOwner.getString(DECISION),
"S3 object with a resource allow and the default ResourceOwner"),
() -> assertEquals(List.of(""), strings(objectWithDefaultOwner, RESULT + DECISION_DETAILS),
"Same-account named resource has empty decision details"),
() -> assertEquals("allowed", queueWithIdentityAllow,
"Cross-account SQS ARN with an identity allow and no ResourcePolicy"),
() -> assertAll("Two cross-account queues with one resource policy grant",
() -> assertEquals(List.of("implicitDeny"), strings(queuesWithOneResourceAllow, DECISION)),
() -> assertEquals(Map.of("IAM Policy", "implicitDeny", "Resource Policy", "implicitDeny"),
decisionDetails(queuesWithOneResourceAllow, RESULT), "top-level details"),
() -> assertEquals("allowed", decisionDetails(queuesWithOneResourceAllow,
resources + ".find { it.EvalResourceName == '" + queue + "' }")
.get("Resource Policy"), "resource policy decision for the granted queue"),
() -> assertEquals("implicitDeny", decisionDetails(queuesWithOneResourceAllow,
resources + ".find { it.EvalResourceName == '" + otherQueue + "' }")
.get("Resource Policy"), "resource policy decision for the other queue")),
() -> assertAll("Cross-account queue denied by an SCP",
() -> assertEquals(List.of("explicitDeny"), strings(queueWithScpDeny, DECISION)),
() -> assertEquals(List.of(), strings(queueWithScpDeny, RESULT + DECISION_DETAILS),
"top-level details element is absent"),
() -> assertEquals(List.of(), strings(queueWithScpDeny, resources + DECISION_DETAILS),
"per-resource details element is absent")),
() -> assertAll(crossAccountCases.stream().map(row -> () -> assertAll(row.description(),
() -> assertEquals(row.decision(), row.result().getString(DECISION), "overall decision"),
() -> assertEquals(row.details(), decisionDetails(row.result(), RESULT), "top-level details"),
() -> assertEquals(row.details(),
decisionDetails(row.result(), RESULT + ".ResourceSpecificResults.member"),
"per-resource details")))),
() -> simulate(david, getObject)
.formParam("CallerArn", "")
.when().post("/").then().statusCode(400)