From 43a0d737e90e99e41e8cb2fb4386e6e24572306a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Tue, 15 Sep 2026 20:42:56 +0900 Subject: [PATCH] perf(iam): cache parsed policy and lowercase patterns once (#3635) * perf(iam): cache parsed policy documents in IamPolicyEvaluator Every evaluate call parsed each policy document with Jackson again. A caller with a large managed policy such as ReadOnlyAccess (94 KB) paid that parse on every request under IAM enforcement. The evaluator now keeps the parsed statements per document text in a ConcurrentHashMap. Parsing is a pure function of the text, so entries never go stale. The map is bounded at 2048 entries and cleared when the bound is reached, which only matters for many distinct session policies. A document that fails to parse is cached as failed, so the warning is logged once instead of on every request. AWS managed policy catalog has 1566 documents. All 1566 parsed take about 9.5 MB of heap, and AWS caps a customer managed policy at 6144 characters and a session policy at 2048, so 2048 entries stay within tens of MBs in the worst case. * perf(iam): store policy patterns lowercased in the parsed statement cache Action and Resource matching is case-insensitive. globMatches lowercased both the pattern and the request value on every comparison, which for a policy like ReadOnlyAccess meant about 5800 new strings per request. The patterns are now lowercased once when the document is parsed and cached, and the request action and resource are lowercased once per evaluation. The public globMatches helper used by the trust policy and S3 public access evaluators is unchanged. * fix(iam): compare resource ARNs case-sensitively AWS documents the Action element as case-insensitive, but the Resource element is not: the IAM user name in a Resource ARN is case sensitive, and so are S3 object keys. The evaluator lowercased both sides of every Resource and NotResource comparison, so a grant on user/Bob also matched user/bob and a grant on bucket/Private/* also matched bucket/private/*. Resource and NotResource patterns are now kept as written in the parsed statement cache and the request resource is compared as is. Action and NotAction patterns stay lowercased once at parse time. --- docs/services/iam.md | 1 + .../services/iam/IamPolicyEvaluator.java | 92 ++++++++++++++---- .../services/iam/IamPolicyEvaluatorTest.java | 97 +++++++++++++++++++ 3 files changed, 170 insertions(+), 20 deletions(-) diff --git a/docs/services/iam.md b/docs/services/iam.md index aec08e6f8c..89341bb1a0 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -396,6 +396,7 @@ account key carries no identity policies of its own. - **Session policies**: inline policies passed during `sts:AssumeRole`. - **Permission boundaries**: managed policies used to cap maximum permissions. - **Action/Resource patterns**: literal matches, wildcards (`*`, `?`), and `NotAction`/`NotResource` blocks. + Action names match without regard to case, resource ARNs match case-sensitively, as on AWS. - **Conditions**: support for `Condition` blocks with multiple operators. - **Effects**: `Allow` and `Deny`. diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamPolicyEvaluator.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamPolicyEvaluator.java index 50cc092007..5e9310e445 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/IamPolicyEvaluator.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamPolicyEvaluator.java @@ -13,6 +13,7 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * Evaluates IAM policy documents against a requested action and resource. @@ -70,7 +71,12 @@ public class IamPolicyEvaluator { private static final Logger LOG = Logger.getLogger(IamPolicyEvaluator.class); + // Parsing is a pure function of the document text, so entries never go stale. The bound + // only guards against growth from many distinct session policies. + static final int MAX_CACHED_DOCUMENTS = 2048; + private final ObjectMapper objectMapper; + private final ConcurrentHashMap cachedDocuments = new ConcurrentHashMap<>(); @Inject public IamPolicyEvaluator(ObjectMapper objectMapper) { @@ -93,6 +99,7 @@ public class IamPolicyEvaluator { String resource, Map> conditionCtx) { Map> ctx = normalizeConditionContext(conditionCtx); + String loweredAction = lowercase(action); List identityStmts = parseAll(caller.identityPolicies()); List resourceStmts = resourcePolicies == null ? List.of() : parseAll(resourcePolicies); @@ -101,12 +108,12 @@ public class IamPolicyEvaluator { List boundaryStmts = caller.boundaryPolicyDocument() == null ? null : parseAll(List.of(caller.boundaryPolicyDocument())); - boolean resourceExplicitDeny = anyExplicitDeny(resourceStmts, action, resource, ctx); - boolean resourceAllow = anyExplicitAllow(resourceStmts, action, resource, ctx); + boolean resourceExplicitDeny = anyExplicitDeny(resourceStmts, loweredAction, resource, ctx); + boolean resourceAllow = anyExplicitAllow(resourceStmts, loweredAction, resource, ctx); return evaluateParsed( caller, identityStmts, sessionStmts, boundaryStmts, resourceExplicitDeny, resourceAllow, false, - ResourceAccountRelationship.SAME_ACCOUNT, action, resource, ctx); + ResourceAccountRelationship.SAME_ACCOUNT, loweredAction, resource, ctx); } /** @@ -157,7 +164,7 @@ public class IamPolicyEvaluator { resolvedDecision == ResourcePolicyDecision.ALLOW_DIRECT_IAM_USER, accountRelationship == null ? ResourceAccountRelationship.CROSS_ACCOUNT : accountRelationship, - action, resource, ctx); + lowercase(action), resource, ctx); } private Decision evaluateParsed( @@ -241,24 +248,25 @@ public class IamPolicyEvaluator { String resource, Map> conditionCtx) { Map> ctx = normalizeConditionContext(conditionCtx); + String loweredAction = lowercase(action); List identityStmts = parseAll(caller.identityPolicies()); List sessionStmts = caller.sessionPolicyDocument() == null ? null : parseAll(List.of(caller.sessionPolicyDocument())); List boundaryStmts = caller.boundaryPolicyDocument() == null ? null : parseAll(List.of(caller.boundaryPolicyDocument())); - if (anyExplicitDeny(identityStmts, action, resource, ctx) - || (sessionStmts != null && anyExplicitDeny(sessionStmts, action, resource, ctx)) - || (boundaryStmts != null && anyExplicitDeny(boundaryStmts, action, resource, ctx))) { + if (anyExplicitDeny(identityStmts, loweredAction, resource, ctx) + || (sessionStmts != null && anyExplicitDeny(sessionStmts, loweredAction, resource, ctx)) + || (boundaryStmts != null && anyExplicitDeny(boundaryStmts, loweredAction, resource, ctx))) { return SimulationDecision.EXPLICIT_DENY; } - if (!anyExplicitAllow(identityStmts, action, resource, ctx)) { + if (!anyExplicitAllow(identityStmts, loweredAction, resource, ctx)) { return SimulationDecision.IMPLICIT_DENY; } - if (sessionStmts != null && !anyExplicitAllow(sessionStmts, action, resource, ctx)) { + if (sessionStmts != null && !anyExplicitAllow(sessionStmts, loweredAction, resource, ctx)) { return SimulationDecision.IMPLICIT_DENY; } - if (boundaryStmts != null && !anyExplicitAllow(boundaryStmts, action, resource, ctx)) { + if (boundaryStmts != null && !anyExplicitAllow(boundaryStmts, loweredAction, resource, ctx)) { return SimulationDecision.IMPLICIT_DENY; } return SimulationDecision.ALLOWED; @@ -373,18 +381,27 @@ public class IamPolicyEvaluator { return false; } + /** + * Case-sensitive glob over the given patterns. Action names are case-insensitive on AWS, so + * both sides arrive lowercased: the patterns at parse time and the request action once per + * evaluation. Resource ARNs are case-sensitive on AWS and are compared as written. + */ private boolean matchesAny(List patterns, String value) { - if (patterns == null) { + if (patterns == null || value == null) { return false; } for (String pattern : patterns) { - if (globMatches(pattern, value)) { + if (globMatchesHelper(pattern, value, 0, 0)) { return true; } } return false; } + private static String lowercase(String value) { + return value == null ? null : value.toLowerCase(); + } + // ----------------------------------------------------------------------- // Condition evaluation (Phase 4) // ----------------------------------------------------------------------- @@ -670,12 +687,9 @@ public class IamPolicyEvaluator { } boolean anyFailed = false; for (String doc : documents) { - try { - result.addAll(parseStatements(doc)); - } catch (Exception e) { - anyFailed = true; - LOG.warnv("Failed to parse policy document: {0}", e.getMessage()); - } + CachedDocument parsed = parseDocument(doc); + result.addAll(parsed.statements()); + anyFailed |= parsed.failed(); } return new ParsedDocuments(result, anyFailed); } @@ -683,6 +697,34 @@ public class IamPolicyEvaluator { private record ParsedDocuments(List statements, boolean anyFailed) { } + private CachedDocument parseDocument(String document) { + if (document == null) { + return parseUncached(null); + } + CachedDocument cached = cachedDocuments.get(document); + if (cached != null) { + return cached; + } + CachedDocument parsed = parseUncached(document); + if (cachedDocuments.size() >= MAX_CACHED_DOCUMENTS) { + cachedDocuments.clear(); + } + cachedDocuments.put(document, parsed); + return parsed; + } + + private CachedDocument parseUncached(String document) { + try { + return new CachedDocument(List.copyOf(parseStatements(document)), false); + } catch (Exception e) { + LOG.warnv("Failed to parse policy document: {0}", e.getMessage()); + return new CachedDocument(List.of(), true); + } + } + + private record CachedDocument(List statements, boolean failed) { + } + private List parseStatements(String document) throws Exception { JsonNode root = objectMapper.readTree(document); JsonNode stmtNode = root.path("Statement"); @@ -699,8 +741,10 @@ public class IamPolicyEvaluator { private PolicyStatement parseStatement(JsonNode stmt) { String effect = stmt.path("Effect").asText("Allow"); - List actions = nodeToList(stmt.get("Action")); - List notActions = nodeToList(stmt.get("NotAction")); + // Action names are case-insensitive on AWS, so they are lowercased once here instead of + // on every request. Resource ARNs are case-sensitive on AWS and are kept as written. + List actions = lowercaseAll(nodeToList(stmt.get("Action"))); + List notActions = lowercaseAll(nodeToList(stmt.get("NotAction"))); List resources = nodeToList(stmt.get("Resource")); List notResources= nodeToList(stmt.get("NotResource")); Map>> conditions = parseConditions(stmt.get("Condition")); @@ -727,6 +771,14 @@ public class IamPolicyEvaluator { return result.isEmpty() ? null : result; } + private static List lowercaseAll(List values) { + List lowered = new ArrayList<>(values.size()); + for (String value : values) { + lowered.add(value.toLowerCase()); + } + return lowered; + } + private List nodeToList(JsonNode node) { List list = new ArrayList<>(); if (node == null) { diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamPolicyEvaluatorTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamPolicyEvaluatorTest.java index 847e4ebe9e..a6072e42ff 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/IamPolicyEvaluatorTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamPolicyEvaluatorTest.java @@ -11,6 +11,10 @@ import java.util.List; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; /** * SCP semantics in policy evaluation: service control policies gate the decision before @@ -415,4 +419,97 @@ class IamPolicyEvaluatorTest { List.of(policy), "dynamodb:GetItem", "*", Map.of("dynamodb:LeadingKeys", List.of("USER_alice")))); } + + @Test + void sameDocumentIsParsedOnce() throws Exception { + ObjectMapper mapper = spy(new ObjectMapper()); + IamPolicyEvaluator cachingEvaluator = new IamPolicyEvaluator(mapper); + CallerContext caller = CallerContext.of(List.of(ALLOW_S3_ONLY)); + + assertEquals(Decision.ALLOW, cachingEvaluator.evaluate(caller, null, "s3:GetObject", "*", null)); + assertEquals(Decision.DENY, cachingEvaluator.evaluate(caller, null, "sqs:SendMessage", "*", null)); + + verify(mapper, times(1)).readTree(anyString()); + } + + @Test + void malformedDocumentIsParsedOnceAndStillDeniesScpLevel() throws Exception { + ObjectMapper mapper = spy(new ObjectMapper()); + IamPolicyEvaluator cachingEvaluator = new IamPolicyEvaluator(mapper); + CallerContext caller = adminWithScps(List.of(List.of(MALFORMED, ALLOW_ALL))); + + assertEquals(Decision.DENY, cachingEvaluator.evaluate(caller, null, "s3:GetObject", "*", null)); + assertEquals(Decision.DENY, cachingEvaluator.evaluate(caller, null, "s3:GetObject", "*", null)); + + verify(mapper, times(1)).readTree(MALFORMED); + verify(mapper, times(1)).readTree(ALLOW_ALL); + } + + @Test + void cacheIsClearedWhenBoundIsExceeded() throws Exception { + ObjectMapper mapper = spy(new ObjectMapper()); + IamPolicyEvaluator cachingEvaluator = new IamPolicyEvaluator(mapper); + String first = sidDocument(0); + cachingEvaluator.evaluate(CallerContext.of(List.of(first)), null, "s3:GetObject", "*", null); + for (int i = 1; i <= IamPolicyEvaluator.MAX_CACHED_DOCUMENTS; i++) { + cachingEvaluator.evaluate(CallerContext.of(List.of(sidDocument(i))), null, "s3:GetObject", "*", null); + } + + cachingEvaluator.evaluate(CallerContext.of(List.of(first)), null, "s3:GetObject", "*", null); + + verify(mapper, times(2)).readTree(first); + } + + @Test + void actionMatchingIsCaseInsensitive() { + String mixedCase = "{\"Version\":\"2012-10-17\",\"Statement\":[" + + "{\"Effect\":\"Allow\",\"Action\":\"S3:GetObject\",\"Resource\":\"arn:aws:s3:::bucket/*\"}," + + "{\"Effect\":\"Deny\",\"NotAction\":\"s3:*\",\"Resource\":\"*\"}]}"; + CallerContext caller = CallerContext.of(List.of(mixedCase)); + + assertEquals(Decision.ALLOW, + evaluator.evaluate(caller, null, "s3:getobject", "arn:aws:s3:::bucket/key", null)); + assertEquals(Decision.ALLOW, + evaluator.evaluate(caller, null, "S3:GETOBJECT", "arn:aws:s3:::bucket/key", null)); + assertEquals(Decision.DENY, + evaluator.evaluate(caller, null, "SQS:SendMessage", "arn:aws:sqs:us-east-1:000000000000:q", null)); + } + + @Test + void resourceMatchingIsCaseSensitive() { + String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[" + + "{\"Effect\":\"Allow\",\"Action\":\"iam:GetUser\"," + + "\"Resource\":\"arn:aws:iam::000000000000:user/Bob\"}," + + "{\"Effect\":\"Allow\",\"Action\":\"s3:GetObject\"," + + "\"Resource\":\"arn:aws:s3:::bucket/Private/*\"}]}"; + CallerContext caller = CallerContext.of(List.of(policy)); + + assertEquals(Decision.ALLOW, + evaluator.evaluate(caller, null, "iam:GetUser", "arn:aws:iam::000000000000:user/Bob", null)); + assertEquals(Decision.DENY, + evaluator.evaluate(caller, null, "iam:GetUser", "arn:aws:iam::000000000000:user/bob", null)); + assertEquals(Decision.ALLOW, + evaluator.evaluate(caller, null, "s3:GetObject", "arn:aws:s3:::bucket/Private/report.csv", null)); + assertEquals(Decision.DENY, + evaluator.evaluate(caller, null, "s3:GetObject", "arn:aws:s3:::bucket/private/report.csv", null)); + } + + @Test + void notResourceMatchingIsCaseSensitive() { + String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[" + + "{\"Effect\":\"Deny\",\"Action\":\"s3:*\"," + + "\"NotResource\":\"arn:aws:s3:::bucket/Public/*\"}," + + "{\"Effect\":\"Allow\",\"Action\":\"s3:*\",\"Resource\":\"*\"}]}"; + CallerContext caller = CallerContext.of(List.of(policy)); + + assertEquals(Decision.ALLOW, + evaluator.evaluate(caller, null, "s3:GetObject", "arn:aws:s3:::bucket/Public/index.html", null)); + assertEquals(Decision.DENY, + evaluator.evaluate(caller, null, "s3:GetObject", "arn:aws:s3:::bucket/public/index.html", null)); + } + + private static String sidDocument(int sid) { + return "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"S" + sid + "\",\"Effect\":\"Allow\"," + + "\"Action\":\"s3:*\",\"Resource\":\"*\"}]}"; + } }