mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3): resolve ResourceAccount conditions from the bucket owner (#7372)
* fix(s3): resolve ResourceAccount conditions from the bucket owner * fix(s3): generate ResourceAccount action coverage Read s3:ResourceAccount support from the Service Reference action table and carry each evaluated action into shared signed-request and service-call context. Preserve resolved bucket ownership, existing request tag coverage and SDK session-policy regressions. Reuse AwsTestClients for IAM and STS fixtures. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> --------- Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
7bac1e356a
commit
16704ff32b
13 files changed
+721
-75
No files matched your search
+26
-5
@@ -27,6 +27,8 @@ import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -1132,6 +1134,7 @@ class IamConditionContextResolverTest {
|
||||
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"));
|
||||
assertFalse(conditions.containsKey("s3:ResourceAccount"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1145,16 +1148,19 @@ class IamConditionContextResolverTest {
|
||||
assertFalse(conditions.containsKey("aws:SecureTransport"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceAccountIsTheBucketOwnerWhenAnS3ArnCarriesNoAccount() {
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"arn:aws:s3:::partner-bucket", "arn:aws:s3:::partner-bucket/report.csv",
|
||||
"arn:aws-cn:s3:::partner-bucket/report.csv", "arn:aws-us-gov:s3:::partner-bucket"})
|
||||
void resourceAccountIsTheBucketOwnerWhenAnS3ArnCarriesNoAccount(String resourceArn) {
|
||||
// S3 bucket and object ARNs have an empty account segment by design, so the owner has to
|
||||
// come from service state. Falling back to the caller would make a cross-account bucket
|
||||
// look like the caller's own.
|
||||
Map<String, List<String>> conditions = IamConditionContextResolver.withGlobalContext(
|
||||
null, "arn:aws:s3:::partner-bucket/report.csv", "eu-west-2",
|
||||
"000000000000", "111111111111");
|
||||
null, resourceArn, "eu-west-2",
|
||||
"000000000000", "111111111111", "s3:GetObject");
|
||||
|
||||
assertEquals(List.of("111111111111"), conditions.get("aws:ResourceAccount"));
|
||||
assertEquals(List.of("111111111111"), conditions.get("s3:ResourceAccount"));
|
||||
assertEquals(List.of("000000000000"), conditions.get("aws:PrincipalAccount"));
|
||||
}
|
||||
|
||||
@@ -1165,6 +1171,7 @@ class IamConditionContextResolverTest {
|
||||
|
||||
assertFalse(conditions.containsKey("aws:ResourceAccount"),
|
||||
"guessing the owner would authorize cross-account access a Condition meant to refuse");
|
||||
assertFalse(conditions.containsKey("s3:ResourceAccount"));
|
||||
assertEquals(List.of("000000000000"), conditions.get("aws:PrincipalAccount"));
|
||||
}
|
||||
|
||||
@@ -1203,11 +1210,25 @@ class IamConditionContextResolverTest {
|
||||
@Test
|
||||
void aServiceCallWithNoSourceLeavesTheSourceKeysOut() {
|
||||
Map<String, List<String>> conditions = IamConditionContextResolver.serviceCallContext(
|
||||
"ses.amazonaws.com", "arn:aws:s3:::bucket", "111122223333", null, null);
|
||||
"ses.amazonaws.com", "arn:aws:s3:::bucket", "111122223333", null, null, "s3:PutObject");
|
||||
|
||||
assertFalse(conditions.containsKey("aws:SourceArn"));
|
||||
assertFalse(conditions.containsKey("aws:SourceAccount"));
|
||||
assertEquals(List.of("111122223333"), conditions.get("aws:ResourceAccount"));
|
||||
assertEquals(List.of("111122223333"), conditions.get("s3:ResourceAccount"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"s3:AllowVendedLogDeliveryForResource", "s3:PutAccessPointPublicAccessBlock", "s3:UnknownAction", "sns:Publish"})
|
||||
void resourceAccountKeyIsAbsentForActionsNotListingIt(String action) {
|
||||
Map<String, List<String>> signed = IamConditionContextResolver.withGlobalContext(
|
||||
null, "arn:aws:s3:::bucket", "us-east-1", "222233334444", "111122223333", action);
|
||||
Map<String, List<String>> service = IamConditionContextResolver.serviceCallContext(
|
||||
"ses.amazonaws.com", "arn:aws:s3:::bucket", "111122223333", null, null, action);
|
||||
for (Map<String, List<String>> context : List.of(signed, service)) {
|
||||
assertEquals(List.of("111122223333"), context.get("aws:ResourceAccount"));
|
||||
assertFalse(context.containsKey("s3:ResourceAccount"));
|
||||
}
|
||||
}
|
||||
|
||||
private Decision decisionForEveryTarget(String policy, String action, ContainerRequestContext request) {
|
||||
|
||||
@@ -2486,7 +2486,7 @@ class IamEnforcementFilterTest {
|
||||
.thenReturn(conditions);
|
||||
|
||||
Map<String, List<String>> expected = IamConditionContextResolver.withGlobalContext(
|
||||
conditions, "arn:aws:s3:::partner-bucket", "us-east-1", "222233334444", "111111111111");
|
||||
conditions, "arn:aws:s3:::partner-bucket", "us-east-1", "222233334444", "111111111111", "s3:ListBucket");
|
||||
assertEquals(List.of("111111111111"), expected.get("aws:ResourceAccount"));
|
||||
|
||||
when(evaluator.evaluateResourcePolicyFor(any(), any(), anyString(), anyString(), any()))
|
||||
|
||||
+15
-1
@@ -128,7 +128,7 @@ class ServiceCallAuthorizerTest {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"lambda.amazonaws.com"},"Action":"s3:GetObjectVersion","Resource":"%s",
|
||||
"Condition":{"StringEquals":{"s3:VersionId":"v2"}}}]}""".formatted(object);
|
||||
"Condition":{"StringEquals":{"s3:VersionId":"v2","s3:ResourceAccount":"%s"}}}]}""".formatted(object, OWNER);
|
||||
providers.add((scope, arn) -> object.equals(arn)
|
||||
? List.of(new ResourcePolicyProvider.ResourcePolicy(policy, OWNER)) : List.of());
|
||||
|
||||
@@ -139,6 +139,20 @@ class ServiceCallAuthorizerTest {
|
||||
assertEquals(Decision.DENY, authorizer.authorize("lambda", "s3:GetObjectVersion", object, null, OWNER));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aServiceCallOmitsResourceAccountWhenItsActionDoesNotListTheKey() {
|
||||
String resource = "arn:aws:s3:us-east-1:" + OWNER + ":accesspoint/reports";
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"lambda.amazonaws.com"},"Action":"s3:*","Resource":"*",
|
||||
"Condition":{"Null":{"s3:ResourceAccount":"true"}}}]}""";
|
||||
providers.add((scope, arn) -> List.of(new ResourcePolicyProvider.ResourcePolicy(policy, OWNER)));
|
||||
|
||||
assertEquals(Decision.ALLOW,
|
||||
authorizer.authorize("lambda", "s3:PutAccessPointPublicAccessBlock", resource, null, OWNER));
|
||||
assertEquals(Decision.DENY, authorizer.authorize("lambda", "s3:GetObject", resource, null, OWNER));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDenyInTheTopicPolicyWins() {
|
||||
topicPolicy("""
|
||||
|
||||
+12
@@ -8,7 +8,9 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class S3ControlTagConditionKeysTest {
|
||||
|
||||
@@ -17,6 +19,16 @@ class S3ControlTagConditionKeysTest {
|
||||
+ "<Tag><Key>Env</Key><Value>dev</Value></Tag>"
|
||||
+ "</Tags></TagResourceRequest>";
|
||||
|
||||
@Test
|
||||
void resourceAccountCoverageComesFromTheActionTable() {
|
||||
assertTrue(S3ControlTagConditionKeys.takes("s3:GetObject", "s3:ResourceAccount"));
|
||||
assertTrue(S3ControlTagConditionKeys.takes("s3:PutObject", "s3:ResourceAccount"));
|
||||
assertTrue(S3ControlTagConditionKeys.takes("s3:ListBucket", "s3:ResourceAccount"));
|
||||
assertFalse(S3ControlTagConditionKeys.takes("s3:PutAccessPointPublicAccessBlock", "s3:ResourceAccount"));
|
||||
assertFalse(S3ControlTagConditionKeys.takes("s3:AllowVendedLogDeliveryForResource", "s3:ResourceAccount"));
|
||||
assertFalse(S3ControlTagConditionKeys.takes(null, "s3:ResourceAccount"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagResourceTakesEachTagOfItsBodyAndTheirKeys() {
|
||||
assertEquals(Map.of("aws:RequestTag/Owner", List.of("team"), "aws:RequestTag/Env", List.of("dev"),
|
||||
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.testing.AwsTestClients;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures.UserCredentials;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.github.hectorvent.floci.testing.S3IamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testutil.S3RequestSigner;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.AwsCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.iam.IamClient;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.S3Exception;
|
||||
import software.amazon.awssdk.services.sts.StsClient;
|
||||
import software.amazon.awssdk.services.sts.model.Credentials;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(S3IamEnforcementProfile.class)
|
||||
class S3ResourceAccountConditionIntegrationTest {
|
||||
private static final String OWNER = "111122223333";
|
||||
private static final String CALLER = "222233334444";
|
||||
private static final String ALLOW_S3 = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:*","Resource":"*"}]}
|
||||
""";
|
||||
|
||||
@TestHTTPResource("/")
|
||||
URI endpoint;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"aws:ResourceAccount", "s3:ResourceAccount"})
|
||||
void sessionPolicyUsesBucketOwnerAndStillRestrictsPrefixAndHonorsDeny(String conditionKey) {
|
||||
String suffix = UUID.randomUUID().toString();
|
||||
String bucket = "resource-account-" + suffix;
|
||||
String user = "resource-account-" + suffix;
|
||||
String role = "resource-account-" + suffix;
|
||||
UserCredentials ownerKeys = EnforcementFixtures.createUser(
|
||||
cleanup, endpoint.getPort(), auth(OWNER, "iam"), user);
|
||||
EnforcementFixtures.putUserPolicy(endpoint.getPort(), auth(OWNER, "iam"), user, "s3", ALLOW_S3);
|
||||
cleanup.register(() -> EnforcementFixtures.removeRole(endpoint.getPort(), auth(CALLER, "iam"), role));
|
||||
cleanup.register(() -> EnforcementFixtures.removeBucket(endpoint.getPort(),
|
||||
S3RequestSigner.signedAs(ownerKeys.accessKeyId(), ownerKeys.secretAccessKey()), bucket));
|
||||
try (IamClient iam = AwsTestClients.create(IamClient.builder(), CALLER, Region.US_EAST_1);
|
||||
StsClient sts = AwsTestClients.create(StsClient.builder(), CALLER, Region.US_EAST_1);
|
||||
S3Client owner = client(AwsBasicCredentials.create(ownerKeys.accessKeyId(), ownerKeys.secretAccessKey()))) {
|
||||
String roleArn = iam.createRole(request -> request.roleName(role).assumeRolePolicyDocument("""
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"sts:AssumeRole",
|
||||
"Principal":{"AWS":"arn:aws:iam::%s:root"}}]}
|
||||
""".formatted(CALLER))).role().arn();
|
||||
iam.putRolePolicy(request -> request.roleName(role).policyName("s3").policyDocument(ALLOW_S3));
|
||||
owner.createBucket(request -> request.bucket(bucket));
|
||||
String grant = """
|
||||
{"Effect":"Allow","Principal":{"AWS":"%s"},"Action":"s3:*",
|
||||
"Resource":["arn:aws:s3:::%s","arn:aws:s3:::%s/*"]}
|
||||
""".formatted(roleArn, bucket, bucket);
|
||||
owner.putBucketPolicy(request -> request.bucket(bucket).policy(
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[" + grant + "]}"));
|
||||
try (S3Client matching = session(sts, roleArn, bucket, conditionKey, OWNER);
|
||||
S3Client wrongOwner = session(sts, roleArn, bucket, conditionKey, CALLER)) {
|
||||
matching.putObject(request -> request.bucket(bucket).key("allowed/index-0"),
|
||||
RequestBody.fromString("snapshot-data"));
|
||||
assertEquals("snapshot-data", matching.getObjectAsBytes(
|
||||
request -> request.bucket(bucket).key("allowed/index-0")).asUtf8String());
|
||||
assertEquals(1, matching.listObjectsV2(request -> request.bucket(bucket).prefix("allowed/")).keyCount());
|
||||
denied(() -> matching.listObjectsV2(request -> request.bucket(bucket).prefix("other/")));
|
||||
denied(() -> wrongOwner.listObjectsV2(request -> request.bucket(bucket).prefix("allowed/")));
|
||||
denied(() -> wrongOwner.getObjectAsBytes(request -> request.bucket(bucket).key("allowed/index-0")));
|
||||
|
||||
String deny = """
|
||||
{"Effect":"Deny","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::%s/*",
|
||||
"Condition":{"StringEquals":{"%s":"%s"}}}
|
||||
""".formatted(bucket, conditionKey, OWNER);
|
||||
owner.putBucketPolicy(request -> request.bucket(bucket).policy(
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[" + grant + "," + deny + "]}"));
|
||||
denied(() -> matching.getObjectAsBytes(request -> request.bucket(bucket).key("allowed/index-0")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private S3Client session(StsClient sts, String roleArn, String bucket, String conditionKey, String owner) {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:ListBucket","Resource":"arn:aws:s3:::%1$s",
|
||||
"Condition":{"StringLike":{"s3:prefix":"allowed/*","%2$s":"%3$s"}}},
|
||||
{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject"],"Resource":"arn:aws:s3:::%1$s/allowed/*",
|
||||
"Condition":{"StringEquals":{"%2$s":"%3$s"}}}]}
|
||||
""".formatted(bucket, conditionKey, owner);
|
||||
Credentials credentials = sts.assumeRole(request -> request.roleArn(roleArn)
|
||||
.roleSessionName("snapshot-" + owner).policy(policy)).credentials();
|
||||
return client(AwsSessionCredentials.create(
|
||||
credentials.accessKeyId(), credentials.secretAccessKey(), credentials.sessionToken()));
|
||||
}
|
||||
|
||||
private S3Client client(AwsCredentials credentials) {
|
||||
return S3Client.builder().endpointOverride(endpoint).region(Region.US_EAST_1).forcePathStyle(true)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(credentials)).build();
|
||||
}
|
||||
|
||||
private static void denied(Runnable operation) {
|
||||
S3Exception error = assertThrows(S3Exception.class, operation::run);
|
||||
assertEquals(403, error.statusCode());
|
||||
assertEquals("AccessDenied", error.awsErrorDetails().errorCode());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user