From 5a3bff3939f1a2573bc2163af3ef4959aaedda37 Mon Sep 17 00:00:00 2001 From: Oleksandr Nikolaichuk <57410473+Sekator778@users.noreply.github.com> Date: Sun, 29 Nov 2026 23:35:55 +0300 Subject: [PATCH] feat(iam): report EvalDecisionDetails per policy type for cross-account simulations (#9766) --- docs/services/iam.md | 10 +- .../aws/services/iam/IamPolicyEvaluator.java | 42 +++++++- .../aws/services/iam/IamQueryHandler.java | 37 +++++++- ...rincipalInputDocumentsIntegrationTest.java | 95 ++++++++++++++++--- 4 files changed, 160 insertions(+), 24 deletions(-) diff --git a/docs/services/iam.md b/docs/services/iam.md index e787246c5c..7dd7bcb1eb 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -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 diff --git a/src/main/java/io/floci/aws/services/iam/IamPolicyEvaluator.java b/src/main/java/io/floci/aws/services/iam/IamPolicyEvaluator.java index 69148082ff..1f1c228346 100644 --- a/src/main/java/io/floci/aws/services/iam/IamPolicyEvaluator.java +++ b/src/main/java/io/floci/aws/services/iam/IamPolicyEvaluator.java @@ -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 matchedStatements, List missingContextValues, Boolean allowedByOrganizations, - Boolean allowedByPermissionsBoundary) { + Boolean allowedByPermissionsBoundary, + Map 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 boundary = policies.boundary(); + SimulationDecision boundaryDecision = boundary == null ? null + : simulationPolicyDecision(boundary, loweredAction, resource, ctx); + Map 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 crossAccountDecisionDetails( + SimulationDecision identityDecision, ResourcePolicyDecision resourceDecision, + SimulationDecision boundaryDecision) { + Map 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 documents, String action, + String resource, Map> 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 diff --git a/src/main/java/io/floci/aws/services/iam/IamQueryHandler.java b/src/main/java/io/floci/aws/services/iam/IamQueryHandler.java index a1cd770706..25a18585c8 100644 --- a/src/main/java/io/floci/aws/services/iam/IamQueryHandler.java +++ b/src/main/java/io/floci/aws/services/iam/IamQueryHandler.java @@ -2527,10 +2527,13 @@ public class IamQueryHandler { * "the deduplicated set of missing context values across all requested resources". * *

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 resourceArns, Map> 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 reportedDecisionDetails( + List reports) { + Map 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 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 diff --git a/src/test/java/io/floci/aws/services/iam/IamSimulatePrincipalInputDocumentsIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamSimulatePrincipalInputDocumentsIntegrationTest.java index 713c014022..9d1b7b36af 100644 --- a/src/test/java/io/floci/aws/services/iam/IamSimulatePrincipalInputDocumentsIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamSimulatePrincipalInputDocumentsIntegrationTest.java @@ -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 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 details) {} + List 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)