mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): distinguish cross-account resource grants (#3590)
* fix(iam): distinguish cross-account resource grants * fix(iam): honor exact principal ARN resource grants * fix(s3): persist cross-account object metadata to owner --------- Co-authored-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com>
This commit is contained in:
1 parent
494bab4b30
commit
2f76640aa8
9 files changed
+790
-138
No files matched your search
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourceAccountRelationship;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourcePolicyDecision;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -83,6 +84,99 @@ class IamPolicyEvaluatorTest {
|
||||
null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void crossAccountIdentityAllowRequiresResourceAllow() {
|
||||
CallerContext caller = CallerContext.of(List.of(ALLOW_ALL));
|
||||
|
||||
assertEquals(Decision.DENY, evaluator.evaluateResolvedResourcePolicy(
|
||||
caller,
|
||||
ResourcePolicyDecision.NEUTRAL,
|
||||
ResourceAccountRelationship.CROSS_ACCOUNT,
|
||||
"s3:GetObject",
|
||||
"arn:aws:s3:::bucket/key",
|
||||
null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void crossAccountResourceAllowRequiresIdentityAllow() {
|
||||
CallerContext caller = CallerContext.of(List.of());
|
||||
|
||||
assertEquals(Decision.DENY, evaluator.evaluateResolvedResourcePolicy(
|
||||
caller,
|
||||
ResourcePolicyDecision.ALLOW,
|
||||
ResourceAccountRelationship.CROSS_ACCOUNT,
|
||||
"s3:GetObject",
|
||||
"arn:aws:s3:::bucket/key",
|
||||
null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void crossAccountAccessAllowsWhenIdentityAndResourcePoliciesAllow() {
|
||||
CallerContext caller = CallerContext.of(List.of(ALLOW_ALL));
|
||||
|
||||
assertEquals(Decision.ALLOW, evaluator.evaluateResolvedResourcePolicy(
|
||||
caller,
|
||||
ResourcePolicyDecision.ALLOW,
|
||||
ResourceAccountRelationship.CROSS_ACCOUNT,
|
||||
"s3:GetObject",
|
||||
"arn:aws:s3:::bucket/key",
|
||||
null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sameAccountDirectUserGrantBypassesBoundaryImplicitDeny() {
|
||||
CallerContext caller = new CallerContext(
|
||||
List.of(),
|
||||
null,
|
||||
"""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"dynamodb:*","Resource":"*"}
|
||||
]}""");
|
||||
|
||||
assertEquals(Decision.ALLOW, evaluator.evaluateResolvedResourcePolicy(
|
||||
caller,
|
||||
ResourcePolicyDecision.ALLOW_DIRECT_IAM_USER,
|
||||
ResourceAccountRelationship.SAME_ACCOUNT,
|
||||
"s3:GetObject",
|
||||
"arn:aws:s3:::bucket/key",
|
||||
null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sameAccountWildcardGrantRemainsLimitedByBoundary() {
|
||||
CallerContext caller = new CallerContext(
|
||||
List.of(),
|
||||
null,
|
||||
"""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"dynamodb:*","Resource":"*"}
|
||||
]}""");
|
||||
|
||||
assertEquals(Decision.DENY, evaluator.evaluateResolvedResourcePolicy(
|
||||
caller,
|
||||
ResourcePolicyDecision.ALLOW,
|
||||
ResourceAccountRelationship.SAME_ACCOUNT,
|
||||
"s3:GetObject",
|
||||
"arn:aws:s3:::bucket/key",
|
||||
null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sameAccountDirectUserGrantDoesNotBypassBoundaryExplicitDeny() {
|
||||
CallerContext caller = new CallerContext(
|
||||
List.of(),
|
||||
null,
|
||||
DENY_S3);
|
||||
|
||||
assertEquals(Decision.DENY, evaluator.evaluateResolvedResourcePolicy(
|
||||
caller,
|
||||
ResourcePolicyDecision.ALLOW_DIRECT_IAM_USER,
|
||||
ResourceAccountRelationship.SAME_ACCOUNT,
|
||||
"s3:GetObject",
|
||||
"arn:aws:s3:::bucket/key",
|
||||
null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void scpDenyWinsOverIdentityAllow() {
|
||||
CallerContext caller = adminWithScps(List.of(List.of(DENY_S3)));
|
||||
|
||||
+97
-10
@@ -38,6 +38,8 @@ class S3CopyObjectSourcePermissionIntegrationTest {
|
||||
}
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT_A = "111122223333";
|
||||
private static final String ACCOUNT_B = "222233334444";
|
||||
|
||||
@Test
|
||||
void copyObjectIsDeniedWhenCallerCannotReadTheSource() {
|
||||
@@ -121,6 +123,53 @@ class S3CopyObjectSourcePermissionIntegrationTest {
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void crossAccountCopyRequiresIdentityAndSourceBucketPolicyAllows() {
|
||||
String suffix = UUID.randomUUID().toString().substring(0, 8);
|
||||
String sourceBucket = "copy-cross-source-" + suffix;
|
||||
String destBucket = "copy-cross-dest-" + suffix;
|
||||
String userName = "copy-cross-user-" + suffix;
|
||||
|
||||
UserCredentials accountAAdmin = createAccountAdmin("copy-source-admin-" + suffix, ACCOUNT_A);
|
||||
UserCredentials accountBAdmin = createAccountAdmin("copy-dest-admin-" + suffix, ACCOUNT_B);
|
||||
createBucketAsRoot(sourceBucket, accountAAdmin.signer());
|
||||
putObjectAsRoot(sourceBucket, "shared.txt", "cross-account source", accountAAdmin.signer());
|
||||
createBucketAsRoot(destBucket, accountBAdmin.signer());
|
||||
|
||||
UserCredentials caller = createUser(userName, ACCOUNT_B);
|
||||
putUserPolicy(userName, "CopyAccess", """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:PutObject","Resource":"arn:aws:s3:::%s/*"}
|
||||
]}""".formatted(destBucket), ACCOUNT_B);
|
||||
putBucketPolicyAsRoot(
|
||||
sourceBucket,
|
||||
allowUserReadPolicy(sourceBucket, ACCOUNT_B, userName, "s3:GetObject"),
|
||||
accountAAdmin.signer());
|
||||
|
||||
given()
|
||||
.filter(caller.signer())
|
||||
.header("x-amz-copy-source", "/" + sourceBucket + "/shared.txt")
|
||||
.when()
|
||||
.put("/" + destBucket + "/denied.txt")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("<Code>AccessDenied</Code>"));
|
||||
|
||||
putUserPolicy(userName, "CopyAccess", """
|
||||
{"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), ACCOUNT_B);
|
||||
|
||||
given()
|
||||
.filter(caller.signer())
|
||||
.header("x-amz-copy-source", "/" + sourceBucket + "/shared.txt")
|
||||
.when()
|
||||
.put("/" + destBucket + "/copied.txt")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void versionedCopyUsesGetObjectVersionPermission() {
|
||||
String suffix = UUID.randomUUID().toString().substring(0, 8);
|
||||
@@ -291,8 +340,12 @@ class S3CopyObjectSourcePermissionIntegrationTest {
|
||||
}
|
||||
|
||||
private static void createBucketAsRoot(String bucket) {
|
||||
createBucketAsRoot(bucket, ROOT_SIGNER);
|
||||
}
|
||||
|
||||
private static void createBucketAsRoot(String bucket, S3RequestSigner signer) {
|
||||
given()
|
||||
.filter(ROOT_SIGNER)
|
||||
.filter(signer)
|
||||
.when()
|
||||
.put("/" + bucket)
|
||||
.then()
|
||||
@@ -300,8 +353,13 @@ class S3CopyObjectSourcePermissionIntegrationTest {
|
||||
}
|
||||
|
||||
private static void putObjectAsRoot(String bucket, String key, String body) {
|
||||
putObjectAsRoot(bucket, key, body, ROOT_SIGNER);
|
||||
}
|
||||
|
||||
private static void putObjectAsRoot(
|
||||
String bucket, String key, String body, S3RequestSigner signer) {
|
||||
given()
|
||||
.filter(ROOT_SIGNER)
|
||||
.filter(signer)
|
||||
.contentType("text/plain")
|
||||
.body(body)
|
||||
.when()
|
||||
@@ -347,8 +405,13 @@ class S3CopyObjectSourcePermissionIntegrationTest {
|
||||
}
|
||||
|
||||
private static void putBucketPolicyAsRoot(String bucket, String policy) {
|
||||
putBucketPolicyAsRoot(bucket, policy, ROOT_SIGNER);
|
||||
}
|
||||
|
||||
private static void putBucketPolicyAsRoot(
|
||||
String bucket, String policy, S3RequestSigner signer) {
|
||||
given()
|
||||
.filter(ROOT_SIGNER)
|
||||
.filter(signer)
|
||||
.contentType("application/json")
|
||||
.body(policy)
|
||||
.when()
|
||||
@@ -365,11 +428,16 @@ class S3CopyObjectSourcePermissionIntegrationTest {
|
||||
}
|
||||
|
||||
private static String allowUserReadPolicy(String bucket, String userName, String action) {
|
||||
return allowUserReadPolicy(bucket, "000000000000", userName, action);
|
||||
}
|
||||
|
||||
private static String allowUserReadPolicy(
|
||||
String bucket, String accountId, String userName, String action) {
|
||||
return """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Principal":{"AWS":"arn:aws:iam::000000000000:user/%1$s"},
|
||||
"Action":"%2$s","Resource":"arn:aws:s3:::%3$s/*"}
|
||||
]}""".formatted(userName, action, bucket);
|
||||
{"Effect":"Allow","Principal":{"AWS":"arn:aws:iam::%1$s:user/%2$s"},
|
||||
"Action":"%3$s","Resource":"arn:aws:s3:::%4$s/*"}
|
||||
]}""".formatted(accountId, userName, action, bucket);
|
||||
}
|
||||
|
||||
private static String allowPublicPutObjectPolicy(String bucket) {
|
||||
@@ -394,10 +462,14 @@ class S3CopyObjectSourcePermissionIntegrationTest {
|
||||
}
|
||||
|
||||
private static UserCredentials createUser(String userName) {
|
||||
return createUser(userName, "test");
|
||||
}
|
||||
|
||||
private static UserCredentials createUser(String userName, String accountId) {
|
||||
given()
|
||||
.formParam("Action", "CreateUser")
|
||||
.formParam("UserName", userName)
|
||||
.header("Authorization", auth("test", "iam"))
|
||||
.header("Authorization", auth(accountId, "iam"))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
@@ -406,7 +478,7 @@ class S3CopyObjectSourcePermissionIntegrationTest {
|
||||
io.restassured.path.xml.XmlPath key = given()
|
||||
.formParam("Action", "CreateAccessKey")
|
||||
.formParam("UserName", userName)
|
||||
.header("Authorization", auth("test", "iam"))
|
||||
.header("Authorization", auth(accountId, "iam"))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
@@ -418,13 +490,27 @@ class S3CopyObjectSourcePermissionIntegrationTest {
|
||||
key.getString("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.SecretAccessKey"));
|
||||
}
|
||||
|
||||
private static UserCredentials createAccountAdmin(String userName, String accountId) {
|
||||
UserCredentials credentials = createUser(userName, accountId);
|
||||
putUserPolicy(userName, "S3Admin", """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:*","Resource":"*"}
|
||||
]}""", accountId);
|
||||
return credentials;
|
||||
}
|
||||
|
||||
private static void putUserPolicy(String userName, String policyName, String policyDocument) {
|
||||
putUserPolicy(userName, policyName, policyDocument, "test");
|
||||
}
|
||||
|
||||
private static void putUserPolicy(
|
||||
String userName, String policyName, String policyDocument, String accountId) {
|
||||
given()
|
||||
.formParam("Action", "PutUserPolicy")
|
||||
.formParam("UserName", userName)
|
||||
.formParam("PolicyName", policyName)
|
||||
.formParam("PolicyDocument", policyDocument)
|
||||
.header("Authorization", auth("test", "iam"))
|
||||
.header("Authorization", auth(accountId, "iam"))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
@@ -441,7 +527,8 @@ class S3CopyObjectSourcePermissionIntegrationTest {
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of(
|
||||
"floci.services.iam.enforcement-enabled", "true",
|
||||
"floci.services.s3.enforce-auth", "true");
|
||||
"floci.services.s3.enforce-auth", "true",
|
||||
"floci.services.s3.global-bucket-namespace", "true");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import jakarta.enterprise.inject.Instance;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
@@ -17,6 +18,7 @@ import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -85,6 +87,12 @@ class S3GlobalBucketNamespaceTest {
|
||||
assertDoesNotThrow(() ->
|
||||
globalNs.putObject("shared-assets-bucket", "member-asset.json", memberAsset,
|
||||
"application/json", Map.of()));
|
||||
assertTrue(objects.getForAccount(
|
||||
ACCOUNT_A, "shared-assets-bucket/member-asset.json").isPresent(),
|
||||
"a cross-account write must persist in the bucket owner's partition");
|
||||
assertTrue(objects.getForAccount(
|
||||
ACCOUNT_B, "shared-assets-bucket/member-asset.json").isEmpty(),
|
||||
"a cross-account write must not create a shadow object in the caller's partition");
|
||||
assertArrayEquals(memberAsset,
|
||||
globalNs.getObject("shared-assets-bucket", "member-asset.json").getData());
|
||||
|
||||
@@ -204,4 +212,67 @@ class S3GlobalBucketNamespaceTest {
|
||||
() -> globalNs.getBucketPolicy("central-logs-bucket"));
|
||||
assertEquals("NoSuchBucketPolicy", notFound.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void objectMetadataOperationsResolveAndPersistCrossAccount() {
|
||||
Instance<RequestContext> ctx = mutableContext();
|
||||
AccountAwareStorageBackend<Bucket> buckets =
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), ctx, DEFAULT_ACCT);
|
||||
AccountAwareStorageBackend<S3Object> objects =
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), ctx, DEFAULT_ACCT);
|
||||
S3Service globalNs = new S3Service(
|
||||
buckets, objects, Path.of("s3-gns-object-metadata-test"), true, true);
|
||||
|
||||
caller.set(ACCOUNT_A);
|
||||
globalNs.createBucket("shared-metadata-bucket", "us-east-1");
|
||||
globalNs.putBucketVersioning("shared-metadata-bucket", "Enabled");
|
||||
S3Object created = globalNs.putObject(
|
||||
"shared-metadata-bucket", "object.txt", "payload".getBytes(UTF_8),
|
||||
"text/plain", Map.of());
|
||||
String versionId = created.getVersionId();
|
||||
|
||||
caller.set(ACCOUNT_B);
|
||||
globalNs.putObjectTagging(
|
||||
"shared-metadata-bucket", "object.txt", Map.of("environment", "shared"));
|
||||
assertEquals(Map.of("environment", "shared"),
|
||||
globalNs.getObjectTagging("shared-metadata-bucket", "object.txt"));
|
||||
|
||||
Instant retainUntil = Instant.parse("2035-01-01T00:00:00Z");
|
||||
globalNs.putObjectRetention(
|
||||
"shared-metadata-bucket", "object.txt", versionId,
|
||||
"GOVERNANCE", retainUntil, true);
|
||||
assertEquals(retainUntil, globalNs.getObjectRetention(
|
||||
"shared-metadata-bucket", "object.txt", versionId).getRetainUntilDate());
|
||||
|
||||
globalNs.putObjectLegalHold("shared-metadata-bucket", "object.txt", null, "ON");
|
||||
assertEquals("ON", globalNs.getObjectLegalHold(
|
||||
"shared-metadata-bucket", "object.txt", null).getLegalHoldStatus());
|
||||
|
||||
String defaultAcl = globalNs.getObjectAcl(
|
||||
"shared-metadata-bucket", "object.txt", versionId);
|
||||
assertTrue(defaultAcl.contains(ACCOUNT_A));
|
||||
assertFalse(defaultAcl.contains(ACCOUNT_B));
|
||||
globalNs.putObjectAcl(
|
||||
"shared-metadata-bucket", "object.txt", versionId,
|
||||
"", "public-read", null, null, null, null, null);
|
||||
String storedAcl = globalNs.getObjectAcl(
|
||||
"shared-metadata-bucket", "object.txt", versionId);
|
||||
assertTrue(storedAcl.contains(ACCOUNT_A));
|
||||
assertFalse(storedAcl.contains(ACCOUNT_B));
|
||||
|
||||
globalNs.deleteObjectTagging("shared-metadata-bucket", "object.txt");
|
||||
assertTrue(globalNs.getObjectTagging("shared-metadata-bucket", "object.txt").isEmpty());
|
||||
|
||||
String currentKey = "shared-metadata-bucket/object.txt";
|
||||
String versionKey = currentKey + "#v#" + versionId;
|
||||
S3Object ownerVersion = objects.getForAccount(ACCOUNT_A, versionKey)
|
||||
.orElseThrow(() -> new AssertionError("version vanished from owner partition"));
|
||||
assertEquals(retainUntil, ownerVersion.getRetainUntilDate());
|
||||
assertEquals(storedAcl, ownerVersion.getAcl());
|
||||
assertTrue(objects.getForAccount(ACCOUNT_A, currentKey).isPresent());
|
||||
assertTrue(objects.getForAccount(ACCOUNT_B, currentKey).isEmpty(),
|
||||
"cross-account metadata writes must not create a caller shadow object");
|
||||
assertTrue(objects.getForAccount(ACCOUNT_B, versionKey).isEmpty(),
|
||||
"cross-account version metadata writes must stay in the owner partition");
|
||||
}
|
||||
}
|
||||
+131
@@ -1,8 +1,11 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.github.hectorvent.floci.services.s3.S3PublicAccessEvaluator.PublicAccessDecision.ALLOW;
|
||||
@@ -142,6 +145,134 @@ class S3PublicAccessEvaluatorTest {
|
||||
"s3:GetObject", OBJECT_ARN, Map.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void exactAwsPrincipalIsReportedAsADirectGrant() {
|
||||
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/*"
|
||||
}}""".formatted(userArn);
|
||||
|
||||
S3PublicAccessEvaluator.PrincipalPolicyEvaluation evaluation =
|
||||
S3PublicAccessEvaluator.principalPolicyEvaluation(
|
||||
OBJECT_MAPPER, policy, "AWS", userArn,
|
||||
"s3:GetObject", OBJECT_ARN, Map.of());
|
||||
|
||||
assertEquals(ALLOW, evaluation.decision());
|
||||
assertTrue(evaluation.directPrincipalAllow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void exactPrincipalArnConditionIsReportedAsADirectGrant() {
|
||||
String userArn = "arn:aws:iam::123456789012:user/alice";
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*",
|
||||
"Condition":{"ArnEquals":{"aws:PrincipalArn":"%s"}}
|
||||
}}""".formatted(userArn);
|
||||
|
||||
S3PublicAccessEvaluator.PrincipalPolicyEvaluation evaluation =
|
||||
S3PublicAccessEvaluator.principalPolicyEvaluation(
|
||||
OBJECT_MAPPER, policy, "AWS", userArn,
|
||||
"s3:GetObject", OBJECT_ARN, Map.of("aws:PrincipalArn", userArn));
|
||||
|
||||
assertEquals(ALLOW, evaluation.decision());
|
||||
assertTrue(evaluation.directPrincipalAllow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void exactPrincipalArnConditionBypassesBoundaryImplicitDeny() {
|
||||
String userArn = "arn:aws:iam::123456789012:user/alice";
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*",
|
||||
"Condition":{"ArnEquals":{"aws:PrincipalArn":"%s"}}
|
||||
}}""".formatted(userArn);
|
||||
S3PublicAccessEvaluator.PrincipalPolicyEvaluation evaluation =
|
||||
S3PublicAccessEvaluator.principalPolicyEvaluation(
|
||||
OBJECT_MAPPER, policy, "AWS", userArn,
|
||||
"s3:GetObject", OBJECT_ARN, Map.of("aws:PrincipalArn", userArn));
|
||||
IamPolicyEvaluator.ResourcePolicyDecision resourceDecision =
|
||||
evaluation.directPrincipalAllow()
|
||||
? IamPolicyEvaluator.ResourcePolicyDecision.ALLOW_DIRECT_IAM_USER
|
||||
: IamPolicyEvaluator.ResourcePolicyDecision.ALLOW;
|
||||
CallerContext caller = new CallerContext(
|
||||
List.of(), null,
|
||||
"""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"dynamodb:*","Resource":"*"}
|
||||
]}""");
|
||||
|
||||
assertEquals(IamPolicyEvaluator.Decision.ALLOW,
|
||||
new IamPolicyEvaluator(OBJECT_MAPPER).evaluateResolvedResourcePolicy(
|
||||
caller,
|
||||
resourceDecision,
|
||||
IamPolicyEvaluator.ResourceAccountRelationship.SAME_ACCOUNT,
|
||||
"s3:GetObject",
|
||||
OBJECT_ARN,
|
||||
Map.of("aws:PrincipalArn", List.of(userArn))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardPrincipalArnConditionIsNotReportedAsADirectGrant() {
|
||||
String userArn = "arn:aws:iam::123456789012:user/alice";
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":{"AWS":"*"},
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*",
|
||||
"Condition":{"ArnLike":{"aws:PrincipalArn":"arn:aws:iam::123456789012:user/*"}}
|
||||
}}""";
|
||||
|
||||
S3PublicAccessEvaluator.PrincipalPolicyEvaluation evaluation =
|
||||
S3PublicAccessEvaluator.principalPolicyEvaluation(
|
||||
OBJECT_MAPPER, policy, "AWS", userArn,
|
||||
"s3:GetObject", OBJECT_ARN, Map.of("aws:PrincipalArn", userArn));
|
||||
|
||||
assertEquals(ALLOW, evaluation.decision());
|
||||
assertFalse(evaluation.directPrincipalAllow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchingDenyOverridesExactPrincipalArnCondition() {
|
||||
String userArn = "arn:aws:iam::123456789012:user/alice";
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*",
|
||||
"Condition":{"StringEquals":{"aws:PrincipalArn":"%s"}}
|
||||
},
|
||||
{
|
||||
"Effect":"Deny",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*",
|
||||
"Condition":{"ArnEquals":{"aws:PrincipalArn":"%s"}}
|
||||
}
|
||||
]}""".formatted(userArn, userArn);
|
||||
|
||||
S3PublicAccessEvaluator.PrincipalPolicyEvaluation evaluation =
|
||||
S3PublicAccessEvaluator.principalPolicyEvaluation(
|
||||
OBJECT_MAPPER, policy, "AWS", userArn,
|
||||
"s3:GetObject", OBJECT_ARN, Map.of("aws:PrincipalArn", userArn));
|
||||
|
||||
assertEquals(DENY, evaluation.decision());
|
||||
assertFalse(evaluation.directPrincipalAllow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void principalConditionalDenyOnlyAppliesWhenConditionMatches() {
|
||||
String policy = """
|
||||
|
||||
Reference in new issue
Block a user