mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(s3): give a role session's aws:PrincipalArn as its role's ARN in S3's bucket-policy checks (#4838)
Follow-up from the review of #4810. S3 evaluates bucket policies itself in two places: the signed-request check (authorizeSignedBucketPolicy, behind s3.enforce-auth) and the CopyObject and UploadPartCopy source check (signedPrincipalResourcePolicyDecision). Both took the caller from S3Service.resolvePrincipalArn, which read IamService .resolveCallerArn, so for a role session the same value, the assumed-role session ARN, served both as the principal a policy's Principal is matched against and as aws:PrincipalArn. AWS puts the ARN of the role that was assumed, path included, in aws:PrincipalArn, as the enforcement filter does since #4810. A bucket-policy Deny keyed on the session ARN therefore fired, and refused the read or the copy, where AWS would not apply it. S3 now reads both through IamService.resolveCallerArns, added in #4810: the caller ARN still drives Principal matching and the same-account test, and the principal ARN fills aws:PrincipalArn. A key IAM does not know keeps S3's account-root fallback, which is both. The private helper is renamed resolveCaller, so it is not mistaken for IamService.resolvePrincipalArn. S3BucketPolicyEnforcementIntegrationTest gets a role under /team/ whose session reads and copies from a bucket with two Denies: one keyed on the role's ARN, which refuses it, and one keyed on the session ARN, which does not. It fails on main, where the read the session-ARN Deny should not touch gets 403.
This commit is contained in:
1 parent
cd89be4a80
commit
b512d273e1
2 files changed
+71
-13
No files matched your search
@@ -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;
|
||||
|
||||
+49
@@ -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);
|
||||
}
|
||||
|
||||
@Test
|
||||
void enforcesBucketPolicyForSignedCallers() {
|
||||
String suffix = UUID.randomUUID().toString().substring(0, 8);
|
||||
|
||||
Reference in new issue
Block a user