mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): populate PrincipalIsAWSService for signed requests (#4695)
* fix(iam): set PrincipalIsAWSService for signed IAM requests * fix(iam): apply global conditions to additional resources * fix(iam): accept JSON booleans in Bool conditions
This commit is contained in:
1 parent
43ed79d15b
commit
e6894119f4
11 files changed
+255
-15
No files matched your search
+1
@@ -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
|
||||
|
||||
@@ -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 = """
|
||||
|
||||
+41
@@ -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);
|
||||
}
|
||||
|
||||
+30
-1
@@ -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
|
||||
|
||||
+97
@@ -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);
|
||||
|
||||
@@ -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 =
|
||||
|
||||
Reference in new issue
Block a user