Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

perf(iam): cache parsed policy and lowercase patterns once (#3635)

TATSUNO “Taz” Yasuhiro43a0d737e90e
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<String, CachedDocument> cachedDocuments = new ConcurrentHashMap<>();
@Inject
public IamPolicyEvaluator(ObjectMapper objectMapper) {
@@ -93,6 +99,7 @@ public class IamPolicyEvaluator {
String resource,
Map<String, List<String>> conditionCtx) {
Map<String, List<String>> ctx = normalizeConditionContext(conditionCtx);
+ String loweredAction = lowercase(action);
List<PolicyStatement> identityStmts = parseAll(caller.identityPolicies());
List<PolicyStatement> resourceStmts = resourcePolicies == null ? List.of() : parseAll(resourcePolicies);
@@ -101,12 +108,12 @@ public class IamPolicyEvaluator {
List<PolicyStatement> 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<String, List<String>> conditionCtx) {
Map<String, List<String>> ctx = normalizeConditionContext(conditionCtx);
+ String loweredAction = lowercase(action);
List<PolicyStatement> identityStmts = parseAll(caller.identityPolicies());
List<PolicyStatement> sessionStmts = caller.sessionPolicyDocument() == null
? null : parseAll(List.of(caller.sessionPolicyDocument()));
List<PolicyStatement> 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<String> 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<PolicyStatement> 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<PolicyStatement> statements, boolean failed) {
+ }
+
private List<PolicyStatement> 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<String> actions = nodeToList(stmt.get("Action"));
- List<String> 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<String> actions = lowercaseAll(nodeToList(stmt.get("Action")));
+ List<String> notActions = lowercaseAll(nodeToList(stmt.get("NotAction")));
List<String> resources = nodeToList(stmt.get("Resource"));
List<String> notResources= nodeToList(stmt.get("NotResource"));
Map<String, Map<String, List<String>>> conditions = parseConditions(stmt.get("Condition"));
@@ -727,6 +771,14 @@ public class IamPolicyEvaluator {
return result.isEmpty() ? null : result;
}
+ private static List<String> lowercaseAll(List<String> values) {
+ List<String> lowered = new ArrayList<>(values.size());
+ for (String value : values) {
+ lowered.add(value.toLowerCase());
+ }
+ return lowered;
+ }
+
private List<String> nodeToList(JsonNode node) {
List<String> 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\":\"*\"}]}";
+ }
}