mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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.
This commit is contained in:
1 parent
ca18ad22f0
commit
43a0d737e9
3 files changed
+170
-20
No files matched your search
@@ -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\":\"*\"}]}";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user