Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(iam): populate PrincipalIsAWSService for signed requests (#4695)

XuJiane6894119f484
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java b/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java
index f468af4554..4f670dabd0 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java
@@ -101,7 +101,9 @@ public class IamConditionContextResolver {
putIfPresent(conditions, "aws:ResourceAccount", resourceAccount);
putIfPresent(conditions, "aws:PrincipalAccount", accountId);
putIfPresent(conditions, "aws:RequestedRegion", region);
- return conditions.isEmpty() ? null : conditions;
+ // Enforcement evaluates signed IAM credentials, not direct AWS service principals.
+ conditions.put("aws:PrincipalIsAWSService", List.of("false"));
+ return conditions;
}
private static String accountFromArn(String arn) {
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
index b3f3ea7f82..b351f9d247 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
@@ -727,22 +727,24 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
conditionContext.put("aws:PrincipalArn", List.of(principalArn.get()));
}
- Map<String, List<String>> effectiveContext = conditionContext;
ResourcePolicyDecision effectiveDecision = resourcePolicyDecision;
String effectiveOwnerAccountId = resourceOwnerAccountId;
+ List<String> policyDocs = null;
if (effectiveDecision == null) {
List<ResourcePolicyProvider.ResourcePolicy> resourcePolicies = resolveResourcePolicies(
credentialScope, resource);
effectiveOwnerAccountId = resourcePolicies.isEmpty()
? null : resourcePolicies.getFirst().ownerAccountId();
- List<String> policyDocs = resourcePolicies.stream()
+ policyDocs = resourcePolicies.stream()
.map(ResourcePolicyProvider.ResourcePolicy::policyDocument)
.filter(doc -> doc != null && !doc.isBlank())
.toList();
- String region = requestContext.getRegion() == null
- ? config.defaultRegion() : requestContext.getRegion();
- effectiveContext = IamConditionContextResolver.withGlobalContext(
- conditionContext, resource, region, accountId, effectiveOwnerAccountId);
+ }
+ String region = requestContext.getRegion() == null
+ ? config.defaultRegion() : requestContext.getRegion();
+ Map<String, List<String>> effectiveContext = IamConditionContextResolver.withGlobalContext(
+ conditionContext, resource, region, accountId, effectiveOwnerAccountId);
+ if (effectiveDecision == null) {
effectiveDecision = evaluator.evaluateResourcePolicy(
policyDocs.isEmpty() ? null : policyDocs,
caller.principalArn(), action, resource, effectiveContext);
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 34ed6722f7..89cb289dc0 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
@@ -1230,8 +1230,12 @@ public class IamPolicyEvaluator {
Map<String, Map<String, List<String>>> result = new LinkedHashMap<>();
condNode.fields().forEachRemaining(opEntry -> {
Map<String, List<String>> kvMap = new LinkedHashMap<>();
- opEntry.getValue().fields().forEachRemaining(kvEntry ->
- kvMap.put(kvEntry.getKey(), nodeToList(kvEntry.getValue())));
+ boolean boolOperator = "Bool".equals(parseOperator(opEntry.getKey()).baseOp());
+ opEntry.getValue().fields().forEachRemaining(kvEntry -> {
+ JsonNode value = kvEntry.getValue();
+ kvMap.put(kvEntry.getKey(), boolOperator && value.isBoolean()
+ ? List.of(value.asText()) : nodeToList(value));
+ });
result.put(opEntry.getKey(), kvMap);
});
return result.isEmpty() ? null : result;
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3PublicAccessEvaluator.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3PublicAccessEvaluator.java
index 2a08fe5858..17ec6ca451 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3PublicAccessEvaluator.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3PublicAccessEvaluator.java
@@ -481,14 +481,17 @@ final class S3PublicAccessEvaluator {
|| "ArnLike".equalsIgnoreCase(operator.getKey());
boolean exact = "StringEquals".equalsIgnoreCase(operator.getKey())
|| "ArnEquals".equalsIgnoreCase(operator.getKey());
- if ((!glob && !exact) || !operator.getValue().isObject()) {
+ boolean bool = "Bool".equalsIgnoreCase(operator.getKey());
+ if ((!glob && !exact && !bool) || !operator.getValue().isObject()) {
return false;
}
Iterator<Map.Entry<String, JsonNode>> entries = operator.getValue().fields();
while (entries.hasNext()) {
Map.Entry<String, JsonNode> entry = entries.next();
String actual = contextValue(context, entry.getKey());
- if (actual == null || !conditionValueMatches(entry.getValue(), actual, glob)) {
+ if (actual == null || !(bool
+ ? booleanConditionMatches(entry.getValue(), actual)
+ : conditionValueMatches(entry.getValue(), actual, glob))) {
return false;
}
}
@@ -508,6 +511,25 @@ final class S3PublicAccessEvaluator {
return null;
}
+ private static boolean booleanConditionMatches(JsonNode expected, String actual) {
+ if (!"true".equalsIgnoreCase(actual) && !"false".equalsIgnoreCase(actual)) {
+ return false;
+ }
+ if (expected.isBoolean() || expected.isTextual()) {
+ String value = expected.asText();
+ return ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value))
+ && value.equalsIgnoreCase(actual);
+ }
+ if (expected.isArray()) {
+ for (JsonNode item : expected) {
+ if (booleanConditionMatches(item, actual)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
private static boolean conditionValueMatches(JsonNode expected, String actual, boolean glob) {
if (expected == null || expected.isNull()) {
return false;
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java
index 4c158baa8e..ee5c6c16a4 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java
@@ -1082,8 +1082,9 @@ public class S3Service implements Resettable, ResourceProvider {
}
Map<String, List<String>> conditionCtx = principalArn != null
- ? Map.of("aws:PrincipalArn", List.of(principalArn))
- : Map.of();
+ ? Map.of("aws:PrincipalArn", List.of(principalArn),
+ "aws:PrincipalIsAWSService", List.of("false"))
+ : Map.of("aws:PrincipalIsAWSService", List.of("false"));
ResourcePolicyDecision decision = policyEvaluator.evaluateResourcePolicy(
List.of(policy),
@@ -1164,7 +1165,8 @@ public class S3Service implements Resettable, ResourceProvider {
principalArn.get(),
action,
resourceArn,
- Map.of("aws:PrincipalArn", principalArn.get()));
+ Map.of("aws:PrincipalArn", principalArn.get(),
+ "aws:PrincipalIsAWSService", "false"));
ResourcePolicyDecision decision = switch (policyEvaluation.decision()) {
case ALLOW -> policyEvaluation.directPrincipalAllow() && principalArn.get().contains(":user/")
? ResourcePolicyDecision.ALLOW_DIRECT_IAM_USER
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/IamConditionContextResolverTest.java b/src/test/java/io/github/hectorvent/floci/core/common/IamConditionContextResolverTest.java
index 59b55eabeb..f3b5a9981c 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/IamConditionContextResolverTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/IamConditionContextResolverTest.java
@@ -507,6 +507,7 @@ class IamConditionContextResolverTest {
assertEquals(List.of("000000000000"), conditions.get("aws:ResourceAccount"));
assertEquals(List.of("000000000000"), conditions.get("aws:PrincipalAccount"));
assertEquals(List.of("eu-west-2"), conditions.get("aws:RequestedRegion"));
+ assertEquals(List.of("false"), conditions.get("aws:PrincipalIsAWSService"));
}
@Test
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 66f6cb0ac6..db7d0ffff9 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
@@ -649,6 +649,24 @@ class IamPolicyEvaluatorTest {
caller, null, "s3:GetObject", "arn:aws:s3:::b/k", null));
}
+ @Test
+ void booleanResourcePolicyConditionAcceptsJsonBooleanValue() {
+ String denySignedPrincipal = """
+ {"Version":"2012-10-17","Statement":[{
+ "Effect":"Deny","Principal":"*","Action":"s3:GetObject","Resource":"*",
+ "Condition":{"Bool":{"aws:PrincipalIsAWSService":false}}
+ }]}""";
+ String principal = "arn:aws:iam::123456789012:user/alice";
+ String resource = "arn:aws:s3:::bucket/key";
+
+ assertEquals(ResourcePolicyDecision.EXPLICIT_DENY, evaluator.evaluateResourcePolicy(
+ List.of(denySignedPrincipal), principal, "s3:GetObject", resource,
+ Map.of("aws:PrincipalIsAWSService", List.of("false"))));
+ assertEquals(ResourcePolicyDecision.NEUTRAL, evaluator.evaluateResourcePolicy(
+ List.of(denySignedPrincipal), principal, "s3:GetObject", resource,
+ Map.of("aws:PrincipalIsAWSService", List.of("true"))));
+ }
+
@Test
void crossAccountEvaluationRequiresBothIdentityAndResourcePolicies() {
String bucketPolicy = """
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java
index 388ab90a60..c142738d9c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java
@@ -2356,6 +2356,47 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(204);
}
+ @Test
+ @Order(57)
+ void signedIamPrincipalMatchesPrincipalIsAwsServiceFalseInBucketPolicy() {
+ String bucket = "auth-principal-is-service-bucket";
+ given().filter(LOCAL_SIGNER).when().put("/" + bucket).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).body("private data")
+ .when().put("/" + bucket + "/data.txt")
+ .then().statusCode(200);
+
+ String denyForIamPrincipals = """
+ {
+ "Version": "2012-10-17",
+ "Statement": {
+ "Effect": "Deny",
+ "Principal": "*",
+ "Action": "s3:GetObject",
+ "Resource": "arn:aws:s3:::%s/*",
+ "Condition": {"Bool": {"aws:PrincipalIsAWSService": "false"}}
+ }
+ }""".formatted(bucket);
+ given().filter(LOCAL_SIGNER).contentType("application/json").body(denyForIamPrincipals)
+ .when().put("/" + bucket + "?policy")
+ .then().statusCode(200);
+
+ given().filter(LOCAL_SIGNER)
+ .when().get("/" + bucket + "/data.txt")
+ .then().statusCode(403)
+ .body(containsString("AccessDenied"));
+
+ String denyForServicePrincipals = denyForIamPrincipals.replace(
+ "\"aws:PrincipalIsAWSService\": \"false\"",
+ "\"aws:PrincipalIsAWSService\": \"true\"");
+ given().filter(LOCAL_SIGNER).contentType("application/json").body(denyForServicePrincipals)
+ .when().put("/" + bucket + "?policy")
+ .then().statusCode(200);
+ given().filter(LOCAL_SIGNER)
+ .when().get("/" + bucket + "/data.txt")
+ .then().statusCode(200)
+ .body(equalTo("private data"));
+ }
+
private static String publicObjectActionPolicy(String bucket, String action) {
return publicObjectActionPolicy(bucket, "*", action);
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3BucketPolicyEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3BucketPolicyEnforcementIntegrationTest.java
index 7b166359fd..563390b312 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3BucketPolicyEnforcementIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3BucketPolicyEnforcementIntegrationTest.java
@@ -160,7 +160,36 @@ class S3BucketPolicyEnforcementIntegrationTest {
.when()
.put("/" + bucketName + "?policy")
.then()
- .statusCode(200);
+ .statusCode(200);
+ }
+
+ @Test
+ void signedIamUserMatchesPrincipalIsAwsServiceFalseInIdentityPolicy() {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ String bucket = "principal-service-" + suffix;
+ String userName = "principal-service-" + suffix;
+ createBucket(bucket);
+ putObject(bucket, "data.txt", "data");
+
+ Credentials user = createUserWithCredentials(userName);
+ putUserPolicy(userName, "ReadWhenIamPrincipal", """
+ {
+ "Version": "2012-10-17",
+ "Statement": {
+ "Effect": "Allow",
+ "Action": "s3:GetObject",
+ "Resource": "arn:aws:s3:::%s/data.txt",
+ "Condition": {"Bool": {"aws:PrincipalIsAWSService": "false"}}
+ }
+ }""".formatted(bucket), "000000000000");
+
+ given()
+ .filter(user.signer())
+ .when()
+ .get("/" + bucket + "/data.txt")
+ .then()
+ .statusCode(200)
+ .body(equalTo("data"));
}
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3CopyObjectSourcePermissionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3CopyObjectSourcePermissionIntegrationTest.java
index 86a1160f31..1af2ec2d0a 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3CopyObjectSourcePermissionIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3CopyObjectSourcePermissionIntegrationTest.java
@@ -94,6 +94,103 @@ class S3CopyObjectSourcePermissionIntegrationTest {
.statusCode(200);
}
+ @Test
+ void copySourceReadHonorsSignedPrincipalGlobalCondition() {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ String sourceBucket = "copy-global-source-" + suffix;
+ String destBucket = "copy-global-dest-" + suffix;
+ String userName = "copy-global-user-" + suffix;
+
+ createBucketAsRoot(sourceBucket);
+ createBucketAsRoot(destBucket);
+ putObjectAsRoot(sourceBucket, "source.txt", "copy source");
+
+ UserCredentials caller = createUser(userName);
+ putUserPolicy(userName, "ConditionalSourceRead", """
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:GetObject","Resource":"arn:aws:s3:::%1$s/*",
+ "Condition":{"Bool":{"aws:PrincipalIsAWSService":"false"}}},
+ {"Effect":"Allow","Action":"s3:PutObject","Resource":"arn:aws:s3:::%2$s/*"}
+ ]} """.formatted(sourceBucket, destBucket));
+
+ given()
+ .filter(caller.signer())
+ .when()
+ .get("/" + sourceBucket + "/source.txt")
+ .then()
+ .statusCode(200);
+
+ given()
+ .filter(caller.signer())
+ .header("x-amz-copy-source", "/" + sourceBucket + "/source.txt")
+ .when()
+ .put("/" + destBucket + "/copied.txt")
+ .then()
+ .statusCode(200);
+
+ String uploadId = initiateMultipartUploadAsRoot(destBucket, "multipart.txt");
+ given()
+ .filter(caller.signer())
+ .header("x-amz-copy-source", "/" + sourceBucket + "/source.txt")
+ .queryParam("uploadId", uploadId)
+ .queryParam("partNumber", 1)
+ .when()
+ .put("/" + destBucket + "/multipart.txt")
+ .then()
+ .statusCode(200);
+ }
+
+ @Test
+ void jsonBooleanSourcePolicyDenyAppliesToDirectAndCopiedReads() {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ String sourceBucket = "copy-bool-source-" + suffix;
+ String destBucket = "copy-bool-dest-" + suffix;
+ String userName = "copy-bool-user-" + suffix;
+
+ createBucketAsRoot(sourceBucket);
+ createBucketAsRoot(destBucket);
+ putObjectAsRoot(sourceBucket, "source.txt", "protected source");
+ putBucketPolicyAsRoot(sourceBucket, """
+ {"Version":"2012-10-17","Statement":[{
+ "Effect":"Deny","Principal":"*","Action":"s3:GetObject",
+ "Resource":"arn:aws:s3:::%s/*",
+ "Condition":{"Bool":{"aws:PrincipalIsAWSService":false}}
+ }]}""".formatted(sourceBucket));
+
+ UserCredentials caller = createUser(userName);
+ putUserPolicy(userName, "SourceReadDestWrite", """
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:GetObject","Resource":"arn:aws:s3:::%1$s/*"},
+ {"Effect":"Allow","Action":"s3:PutObject","Resource":"arn:aws:s3:::%2$s/*"}
+ ]}""".formatted(sourceBucket, destBucket));
+
+ given()
+ .filter(caller.signer())
+ .when()
+ .get("/" + sourceBucket + "/source.txt")
+ .then()
+ .statusCode(403);
+
+ given()
+ .filter(caller.signer())
+ .header("x-amz-copy-source", "/" + sourceBucket + "/source.txt")
+ .when()
+ .put("/" + destBucket + "/copied.txt")
+ .then()
+ .statusCode(403);
+
+ String uploadId = initiateMultipartUploadAsRoot(destBucket, "multipart.txt");
+ given()
+ .filter(caller.signer())
+ .header("x-amz-copy-source", "/" + sourceBucket + "/source.txt")
+ .queryParam("uploadId", uploadId)
+ .queryParam("partNumber", 1)
+ .when()
+ .put("/" + destBucket + "/multipart.txt")
+ .then()
+ .statusCode(403);
+ }
+
@Test
void copyObjectSucceedsWhenSourceBucketPolicyAllowsCaller() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3PublicAccessEvaluatorTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3PublicAccessEvaluatorTest.java
index 7683fd9fe6..0448c12e83 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3PublicAccessEvaluatorTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3PublicAccessEvaluatorTest.java
@@ -128,6 +128,28 @@ class S3PublicAccessEvaluatorTest {
"arn:aws:cloudfront::000000000000:distribution/EOTHER")));
}
+ @Test
+ void principalIsAwsServiceBoolRequiresTheMatchingContextValue() {
+ String userArn = "arn:aws:iam::123456789012:user/alice";
+ String policy = """
+ {"Version":"2012-10-17","Statement":{
+ "Effect":"Allow",
+ "Principal":{"AWS":"%s"},
+ "Action":"s3:GetObject",
+ "Resource":"arn:aws:s3:::public-bucket/*",
+ "Condition":{"Bool":{"aws:PrincipalIsAWSService":"false"}}
+ }}""".formatted(userArn);
+
+ assertEquals(ALLOW, S3PublicAccessEvaluator.principalPolicyDecision(
+ OBJECT_MAPPER, policy, "AWS", userArn, "s3:GetObject", OBJECT_ARN,
+ Map.of("aws:PrincipalIsAWSService", "false")));
+ assertEquals(NEUTRAL, S3PublicAccessEvaluator.principalPolicyDecision(
+ OBJECT_MAPPER, policy, "AWS", userArn, "s3:GetObject", OBJECT_ARN,
+ Map.of("aws:PrincipalIsAWSService", "true")));
+ assertEquals(NEUTRAL, S3PublicAccessEvaluator.principalPolicyDecision(
+ OBJECT_MAPPER, policy, "AWS", userArn, "s3:GetObject", OBJECT_ARN, Map.of()));
+ }
+
@Test
void cloudFrontOaiPrincipalMatchesAwsPrincipal() {
String oaiArn =