Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): give a role session's aws:PrincipalArn as its role's ARN in S3's bucket-policy checks (#4838)
Priamo Germosenb512d273e191
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.javaindex 5ab1b73884..8488b37d61 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@@ -17,6 +17,7 @@ import io.github.hectorvent.floci.core.storage.WriteProfile;import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourcePolicyDecision;import io.github.hectorvent.floci.services.iam.IamService;+import io.github.hectorvent.floci.services.iam.IamService.CallerArns;import io.github.hectorvent.floci.services.lambda.LambdaService;import io.github.hectorvent.floci.services.lambda.model.InvocationType;import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;@@ -1067,7 +1068,8 @@ public class S3Service implements Resettable, ResourceProvider {Bucket bucket = ownedBucketEntry.value();String bucketOwner = ownedBucketEntry.account();- String principalArn = resolvePrincipalArn(authorization.accessKeyId()).orElse(null);+ CallerArns caller = resolveCaller(authorization.accessKeyId()).orElse(null);+ String principalArn = caller == null ? null : caller.callerArn();boolean sameAccountAsOwner = isSameAccountAsBucketOwner(authorization.accessKeyId(), principalArn, bucketOwner);if (isBucketPolicyAction(action) && !sameAccountAsOwner) {@@ -1091,8 +1093,8 @@ public class S3Service implements Resettable, ResourceProvider {throw accessDeniedException(bucketName, key);}- Map<String, List<String>> conditionCtx = principalArn != null- ? Map.of("aws:PrincipalArn", List.of(principalArn),+ Map<String, List<String>> conditionCtx = caller != null+ ? Map.of("aws:PrincipalArn", List.of(caller.principalArn()),"aws:PrincipalIsAWSService", List.of("false")): Map.of("aws:PrincipalIsAWSService", List.of("false"));@@ -1115,18 +1117,25 @@ public class S3Service implements Resettable, ResourceProvider {throw accessDeniedException(bucketName, key);}- private Optional<String> resolvePrincipalArn(String accessKeyId) {+ /**+ * The signing caller's two ARNs: the caller ARN a bucket policy's {@code Principal} is matched+ * against, and the request's {@code aws:PrincipalArn}, which for a role session is the role's ARN,+ * path included, rather than the session's. A key IAM does not know is treated as its account's+ * root, which is both.+ */+ private Optional<CallerArns> resolveCaller(String accessKeyId) {if (accessKeyId == null || accessKeyId.isBlank()) {return Optional.empty();}if (iamService != null) {- Optional<String> callerArn = iamService.resolveCallerArn(accessKeyId);- if (callerArn.isPresent()) {- return callerArn;+ Optional<CallerArns> callerArns = iamService.resolveCallerArns(accessKeyId);+ if (callerArns.isPresent()) {+ return callerArns;}}String account = accessKeyId.matches("\\d{12}") ? accessKeyId : ownerId();- return Optional.of(regionResolver.buildGlobalArn("iam", account, "root"));+ String rootArn = regionResolver.buildGlobalArn("iam", account, "root");+ return Optional.of(new CallerArns(rootArn, rootArn));}private boolean isSameAccountAsBucketOwner(String accessKeyId, String principalArn, String bucketOwnerAccount) {@@ -1159,8 +1168,8 @@ public class S3Service implements Resettable, ResourceProvider {return new SignedPrincipalResourcePolicyEvaluation(ResourcePolicyDecision.NEUTRAL, null);}- Optional<String> principalArn = resolvePrincipalArn(authorization.accessKeyId());- if (principalArn.isEmpty()) {+ Optional<CallerArns> caller = resolveCaller(authorization.accessKeyId());+ if (caller.isEmpty()) {return new SignedPrincipalResourcePolicyEvaluation(ResourcePolicyDecision.NEUTRAL, null);}@@ -1172,13 +1181,13 @@ public class S3Service implements Resettable, ResourceProvider {objectMapper,ownedBucket.value().getPolicy(),"AWS",- principalArn.get(),+ caller.get().callerArn(),action,resourceArn,- Map.of("aws:PrincipalArn", principalArn.get(),+ Map.of("aws:PrincipalArn", caller.get().principalArn(),"aws:PrincipalIsAWSService", "false"));ResourcePolicyDecision decision = switch (policyEvaluation.decision()) {- case ALLOW -> policyEvaluation.directPrincipalAllow() && principalArn.get().contains(":user/")+ case ALLOW -> policyEvaluation.directPrincipalAllow() && caller.get().callerArn().contains(":user/")? ResourcePolicyDecision.ALLOW_DIRECT_IAM_USER: ResourcePolicyDecision.ALLOW;case DENY -> ResourcePolicyDecision.EXPLICIT_DENY;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.javaindex 563390b312..3b9be5f338 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@@ -192,6 +192,55 @@ class S3BucketPolicyEnforcementIntegrationTest {.body(equalTo("data"));}+ @Test+ void aRoleSessionsPrincipalArnInABucketPolicyIsItsRolesArn() {+ // S3 evaluates the bucket policy itself (s3.enforce-auth, and the CopyObject source check),+ // and there aws:PrincipalArn is the ARN of the role that was assumed, not the session's: a+ // Deny keyed on the role's ARN fires, one keyed on the session ARN does not.+ String suffix = UUID.randomUUID().toString().substring(0, 8);+ String bucket = "bp-role-session-" + suffix;+ String role = "bp-role-" + suffix;+ String roleArn = "arn:aws:iam::000000000000:role/team/" + role;+ createBucket(bucket);+ putObject(bucket, "denied/x.txt", "denied");+ putObject(bucket, "session/x.txt", "session");+ given().formParam("Action", "CreateRole").formParam("RoleName", role).formParam("Path", "/team/")+ .formParam("AssumeRolePolicyDocument", """+ {"Version":"2012-10-17","Statement":[{"Effect":"Allow",+ "Principal":{"AWS":"arn:aws:iam::000000000000:root"},"Action":"sts:AssumeRole"}]}""")+ .header("Authorization", auth("000000000000", "iam")).when().post("/").then().statusCode(200);+ given().formParam("Action", "PutRolePolicy").formParam("RoleName", role).formParam("PolicyName", "s3")+ .formParam("PolicyDocument", """+ {"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:*","Resource":"*"}]}""")+ .header("Authorization", auth("000000000000", "iam")).when().post("/").then().statusCode(200);+ ExtractableResponse<Response> assumed = given().formParam("Action", "AssumeRole")+ .formParam("RoleArn", roleArn).formParam("RoleSessionName", "s")+ .header("Authorization", auth("000000000000", "sts")).when().post("/")+ .then().statusCode(200).extract();+ String sessionArn = assumed.path("AssumeRoleResponse.AssumeRoleResult.AssumedRoleUser.Arn");+ S3RequestSigner session = S3RequestSigner.signedAs(+ assumed.path("AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId"),+ assumed.path("AssumeRoleResponse.AssumeRoleResult.Credentials.SecretAccessKey"),+ assumed.path("AssumeRoleResponse.AssumeRoleResult.Credentials.SessionToken"));+ putBucketPolicy(bucket, """+ {"Version":"2012-10-17","Statement":[+ {"Effect":"Deny","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::%1$s/denied/*",+ "Condition":{"ArnEquals":{"aws:PrincipalArn":"%2$s"}}},+ {"Effect":"Deny","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::%1$s/session/*",+ "Condition":{"ArnEquals":{"aws:PrincipalArn":"%3$s"}}}]}""".formatted(bucket, roleArn, sessionArn));++ given().filter(session).when().get("/" + bucket + "/session/x.txt")+ .then().statusCode(200).body(equalTo("session"));+ given().filter(session).when().get("/" + bucket + "/denied/x.txt")+ .then().statusCode(403);+ given().filter(session).header("x-amz-copy-source", "/" + bucket + "/session/x.txt")+ .when().put("/" + bucket + "/copied-session.txt")+ .then().statusCode(200);+ given().filter(session).header("x-amz-copy-source", "/" + bucket + "/denied/x.txt")+ .when().put("/" + bucket + "/copied-denied.txt")+ .then().statusCode(403);+ }+@Testvoid enforcesBucketPolicyForSignedCallers() {String suffix = UUID.randomUUID().toString().substring(0, 8);