mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(s3): expose bucket policy status (#6397)
* fix(s3): expose bucket policy status * fix(s3): align bucket policy status response * test(s3): verify policy status through SDK and IAM * refactor(s3): reuse bucket policy lookup for status
This commit is contained in:
1 parent
8ef9e1ddbf
commit
be7b0ed62b
10 files changed
+283
-3
No files matched your search
+62
@@ -0,0 +1,62 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
|
||||
import software.amazon.awssdk.services.s3.model.DeleteBucketRequest;
|
||||
import software.amazon.awssdk.services.s3.model.GetBucketPolicyStatusRequest;
|
||||
import software.amazon.awssdk.services.s3.model.GetBucketPolicyStatusResponse;
|
||||
import software.amazon.awssdk.services.s3.model.PutBucketPolicyRequest;
|
||||
import software.amazon.awssdk.services.s3.model.S3Exception;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
/** SDK-level coverage for the S3 GetBucketPolicyStatus response shape and errors. */
|
||||
class S3PolicyStatusTest {
|
||||
|
||||
@Test
|
||||
void decodesPolicyStatusRootElement() {
|
||||
String bucket = TestFixtures.uniqueName("sdk-policy-status");
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::%s/*"}]}
|
||||
""".formatted(bucket);
|
||||
|
||||
try (S3Client s3 = TestFixtures.s3Client()) {
|
||||
s3.createBucket(CreateBucketRequest.builder().bucket(bucket).build());
|
||||
s3.putBucketPolicy(PutBucketPolicyRequest.builder().bucket(bucket).policy(policy).build());
|
||||
|
||||
GetBucketPolicyStatusResponse response = s3.getBucketPolicyStatus(
|
||||
GetBucketPolicyStatusRequest.builder().bucket(bucket).build());
|
||||
|
||||
assertThat(response.policyStatus()).isNotNull();
|
||||
assertThat(response.policyStatus().isPublic()).isTrue();
|
||||
} finally {
|
||||
try (S3Client cleanup = TestFixtures.s3Client()) {
|
||||
cleanup.deleteBucket(DeleteBucketRequest.builder().bucket(bucket).build());
|
||||
} catch (Exception ignored) {
|
||||
// The compatibility runner may retain state after a failed assertion.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportsMissingBucketPolicy() {
|
||||
String bucket = TestFixtures.uniqueName("sdk-policy-status");
|
||||
try (S3Client s3 = TestFixtures.s3Client()) {
|
||||
s3.createBucket(CreateBucketRequest.builder().bucket(bucket).build());
|
||||
|
||||
assertThatThrownBy(() -> s3.getBucketPolicyStatus(
|
||||
GetBucketPolicyStatusRequest.builder().bucket(bucket).build()))
|
||||
.isInstanceOfSatisfying(S3Exception.class, exception ->
|
||||
assertThat(exception.awsErrorDetails().errorCode()).isEqualTo("NoSuchBucketPolicy"));
|
||||
} finally {
|
||||
try (S3Client cleanup = TestFixtures.s3Client()) {
|
||||
cleanup.deleteBucket(DeleteBucketRequest.builder().bucket(bucket).build());
|
||||
} catch (Exception ignored) {
|
||||
// The compatibility runner may retain state after a failed assertion.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -14,7 +14,7 @@
|
||||
| **Versioning** | PutBucketVersioning, GetBucketVersioning |
|
||||
| **Tagging** | PutBucketTagging, GetBucketTagging, PutObjectTagging, GetObjectTagging, DeleteObjectTagging |
|
||||
| **Annotations** | PutObjectAnnotation, GetObjectAnnotation, ListObjectAnnotations, DeleteObjectAnnotation |
|
||||
| **Policy** | PutBucketPolicy, GetBucketPolicy, DeleteBucketPolicy |
|
||||
| **Policy** | PutBucketPolicy, GetBucketPolicy, GetBucketPolicyStatus, DeleteBucketPolicy |
|
||||
| **CORS** | PutBucketCors, GetBucketCors, DeleteBucketCors |
|
||||
| **Lifecycle** | PutBucketLifecycle, GetBucketLifecycle, DeleteBucketLifecycle |
|
||||
| **ACL** | PutBucketAcl, GetBucketAcl, PutObjectAcl, GetObjectAcl |
|
||||
@@ -477,7 +477,6 @@ A single public statement makes the whole policy public. As on AWS, `RestrictPub
|
||||
- AWS's organization-level Block Public Access policies are not modelled; only the bucket and account levels combine.
|
||||
- `CreateBucket` applies compatible ACL headers but rejects public ACLs with HTTP 400, including on `ObjectWriter` buckets, because new buckets start with Block Public Access enabled. After creation, an `ObjectWriter` bucket without an explicit `PutPublicAccessBlock` still accepts a public `PutBucketAcl`; default bucket-level enforcement remains a gap.
|
||||
- `GetBucketAcl` returns the stored ACL rather than the effective one, so it does not reflect an `IgnorePublicAcls` that is suppressing a grant.
|
||||
- `GetBucketPolicyStatus` has no handler.
|
||||
- `RestrictPublicBuckets` exempts AWS service principals on AWS. Floci reaches the signed path only for IAM principals, so there is nothing to exempt.
|
||||
|
||||
### Account-level operations
|
||||
|
||||
@@ -688,6 +688,7 @@ public class IamActionRegistry {
|
||||
sub("website", "s3:GetBucketWebsite"),
|
||||
sub("logging", "s3:GetBucketLogging"),
|
||||
sub("policy", "s3:GetBucketPolicy"),
|
||||
sub("policyStatus", "s3:GetBucketPolicyStatus"),
|
||||
sub("cors", "s3:GetBucketCORS"),
|
||||
sub("lifecycle", "s3:GetLifecycleConfiguration"),
|
||||
sub("acl", "s3:GetBucketAcl"),
|
||||
|
||||
@@ -644,6 +644,15 @@ public class S3Controller {
|
||||
.type("application/xml")
|
||||
.build();
|
||||
}
|
||||
if (hasQueryParam(uriInfo, "policyStatus")) {
|
||||
s3Service.authorizeBucketRead(bucket, "s3:GetBucketPolicyStatus", authorization);
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
|
||||
.start("PolicyStatus")
|
||||
.elem("IsPublic", s3Service.isBucketPolicyPublic(bucket))
|
||||
.end("PolicyStatus");
|
||||
return Response.ok(xml.build()).type(MediaType.APPLICATION_XML).build();
|
||||
}
|
||||
if (hasQueryParam(uriInfo, "policy")) {
|
||||
s3Service.authorizeBucketRead(bucket, "s3:GetBucketPolicy", authorization);
|
||||
return Response.ok(s3Service.getBucketPolicy(bucket)).build();
|
||||
|
||||
@@ -175,7 +175,8 @@ final class S3PublicAccessEvaluator {
|
||||
return hasPublicPrincipal(statement.path("Principal"));
|
||||
}
|
||||
// An Allow on NotPrincipal grants to everyone the statement does not name.
|
||||
return statement.hasNonNull("NotPrincipal");
|
||||
return statement.hasNonNull("NotPrincipal")
|
||||
&& !hasPublicPrincipal(statement.path("NotPrincipal"));
|
||||
}
|
||||
|
||||
private static boolean conditionPinsToFixedValues(JsonNode conditions) {
|
||||
|
||||
@@ -5451,6 +5451,11 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
return bucket.getPolicy();
|
||||
}
|
||||
|
||||
/** Returns whether the bucket policy grants public access according to S3 policy-status rules. */
|
||||
public boolean isBucketPolicyPublic(String bucketName) {
|
||||
return S3PublicAccessEvaluator.policyIsPublic(objectMapper, getBucketPolicy(bucketName));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code BlockPublicPolicy} rejects a bucket policy that grants public access. As on AWS the
|
||||
* check runs whoever the caller is, so it does not sit behind {@code enforce-auth}, and it
|
||||
|
||||
@@ -722,6 +722,7 @@ class IamActionRegistryTest {
|
||||
assertEquals("s3:ListBucketVersions", bucketAction("GET", "versions"));
|
||||
assertEquals("s3:ListBucketMultipartUploads", bucketAction("GET", "uploads"));
|
||||
assertEquals("s3:GetBucketPolicy", bucketAction("GET", "policy"));
|
||||
assertEquals("s3:GetBucketPolicyStatus", bucketAction("GET", "policyStatus"));
|
||||
assertEquals("s3:GetLifecycleConfiguration", bucketAction("GET", "lifecycle"));
|
||||
assertEquals("s3:GetEncryptionConfiguration", bucketAction("GET", "encryption"));
|
||||
}
|
||||
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AccountResolver;
|
||||
import io.github.hectorvent.floci.core.common.IamEnforcementFilter;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.services.cloudtrail.CloudTrailService;
|
||||
import io.github.hectorvent.floci.services.floci.ui.UiPages;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.sns.SnsQueryHandler;
|
||||
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Clock;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Pins the REST route and XML shape for S3 GetBucketPolicyStatus. */
|
||||
class S3PolicyStatusControllerTest {
|
||||
|
||||
@Test
|
||||
void policyStatusSubresourceReturnsPolicyStatusXml() {
|
||||
String bucket = "policy-status-bucket";
|
||||
S3Service s3Service = mock(S3Service.class);
|
||||
when(s3Service.isBucketPolicyPublic(bucket)).thenReturn(true);
|
||||
when(s3Service.listObjectsWithPrefixes(anyString(), any(), any(), anyInt(), any(), any()))
|
||||
.thenReturn(new S3Service.ListObjectsResult(List.of(), List.of(), false, null));
|
||||
|
||||
CurrentVertxRequest currentVertxRequest = mock(CurrentVertxRequest.class, RETURNS_DEEP_STUBS);
|
||||
when(currentVertxRequest.getCurrent().request().path()).thenReturn("/" + bucket);
|
||||
|
||||
MultivaluedHashMap<String, String> query = new MultivaluedHashMap<>();
|
||||
query.putSingle("policyStatus", "");
|
||||
UriInfo uriInfo = mock(UriInfo.class);
|
||||
when(uriInfo.getQueryParameters()).thenReturn(query);
|
||||
when(uriInfo.getRequestUri()).thenReturn(URI.create("http://localhost/" + bucket + "?policyStatus"));
|
||||
|
||||
HttpHeaders headers = mock(HttpHeaders.class);
|
||||
when(headers.getHeaderString(anyString())).thenReturn(null);
|
||||
|
||||
S3Controller controller = new S3Controller(
|
||||
s3Service,
|
||||
mock(S3SelectService.class),
|
||||
mock(RegionResolver.class),
|
||||
mock(SnsQueryHandler.class),
|
||||
currentVertxRequest,
|
||||
mock(UiPages.class),
|
||||
mock(CloudTrailService.class),
|
||||
mock(AccountResolver.class),
|
||||
mock(RequestContext.class),
|
||||
mock(IamService.class),
|
||||
mock(IamEnforcementFilter.class),
|
||||
mock(EmulatorConfig.class),
|
||||
Clock.systemUTC());
|
||||
|
||||
Response response = controller.listObjects(bucket, null, null, null, null, null, null, null,
|
||||
null, null, null, uriInfo, headers);
|
||||
|
||||
String entity = response.getEntity().toString();
|
||||
assertTrue(entity.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?><PolicyStatus"));
|
||||
assertTrue(entity.contains("<IsPublic>true</IsPublic></PolicyStatus>"));
|
||||
}
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures.UserCredentials;
|
||||
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.GetBucketPolicyStatusResponse;
|
||||
import software.amazon.awssdk.services.s3.model.S3Exception;
|
||||
|
||||
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.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/** SDK coverage for the policy-status payload, missing-policy errors and IAM action routing. */
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class S3PolicyStatusIntegrationTest {
|
||||
|
||||
@TestHTTPResource("/")
|
||||
URI baseUri;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@Test
|
||||
void reportsNoSuchBucketPolicyBeforeCreationAndAfterDeletion() {
|
||||
try (S3Client admin = client("test", "test")) {
|
||||
String bucket = createBucket(admin);
|
||||
assertMissingPolicy(admin, bucket);
|
||||
admin.putBucketPolicy(request -> request.bucket(bucket).policy(policy(bucket, "*")));
|
||||
admin.deleteBucketPolicy(request -> request.bucket(bucket));
|
||||
assertMissingPolicy(admin, bucket);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void decodesPublicAndPrivatePolicyStatus() {
|
||||
try (S3Client admin = client("test", "test")) {
|
||||
String bucket = createBucket(admin);
|
||||
admin.putBucketPolicy(request -> request.bucket(bucket).policy(policy(bucket, "*")));
|
||||
assertPolicyStatus(admin, bucket, true);
|
||||
admin.putBucketPolicy(request -> request.bucket(bucket)
|
||||
.policy(policy(bucket, "arn:aws:iam::123456789012:root")));
|
||||
assertPolicyStatus(admin, bucket, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void statusOnlyPermissionDoesNotRequireListBucket() {
|
||||
String user = "policy-status-" + UUID.randomUUID();
|
||||
UserCredentials credentials = EnforcementFixtures.createUser(
|
||||
cleanup, baseUri.getPort(), auth("test", "iam"), user);
|
||||
try (S3Client admin = client("test", "test");
|
||||
S3Client reader = client(credentials.accessKeyId(), credentials.secretAccessKey())) {
|
||||
String bucket = createBucket(admin);
|
||||
admin.putBucketPolicy(request -> request.bucket(bucket).policy(policy(bucket, "*")));
|
||||
EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth("test", "iam"), user, "status-only", """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Action":"s3:GetBucketPolicyStatus","Resource":"arn:aws:s3:::%s"}]}
|
||||
""".formatted(bucket));
|
||||
|
||||
assertPolicyStatus(reader, bucket, true);
|
||||
S3Exception denied = assertThrows(S3Exception.class,
|
||||
() -> reader.listObjectsV2(request -> request.bucket(bucket)));
|
||||
assertEquals(403, denied.statusCode());
|
||||
assertEquals("AccessDenied", denied.awsErrorDetails().errorCode());
|
||||
}
|
||||
}
|
||||
|
||||
private String createBucket(S3Client admin) {
|
||||
String bucket = "policy-status-" + UUID.randomUUID();
|
||||
cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(), auth("test", "s3"), bucket));
|
||||
admin.createBucket(request -> request.bucket(bucket));
|
||||
return bucket;
|
||||
}
|
||||
|
||||
private S3Client client(String accessKey, String secretKey) {
|
||||
return S3Client.builder()
|
||||
.endpointOverride(baseUri)
|
||||
.region(Region.US_EAST_1)
|
||||
.forcePathStyle(true)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey)))
|
||||
.build();
|
||||
}
|
||||
|
||||
private static String policy(String bucket, String principal) {
|
||||
return """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"AWS":"%s"},"Action":"s3:GetObject","Resource":"arn:aws:s3:::%s/*"}]}
|
||||
""".formatted(principal, bucket);
|
||||
}
|
||||
|
||||
private static void assertPolicyStatus(S3Client client, String bucket, boolean expected) {
|
||||
GetBucketPolicyStatusResponse response = client.getBucketPolicyStatus(request -> request.bucket(bucket));
|
||||
assertNotNull(response.policyStatus());
|
||||
assertEquals(expected, response.policyStatus().isPublic());
|
||||
}
|
||||
|
||||
private static void assertMissingPolicy(S3Client client, String bucket) {
|
||||
S3Exception missing = assertThrows(S3Exception.class,
|
||||
() -> client.getBucketPolicyStatus(request -> request.bucket(bucket)));
|
||||
assertEquals(404, missing.statusCode());
|
||||
assertEquals("NoSuchBucketPolicy", missing.awsErrorDetails().errorCode());
|
||||
}
|
||||
}
|
||||
@@ -203,6 +203,14 @@ class S3PublicPolicyStatusTest {
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notPrincipalWildcardIsNotPublic() {
|
||||
assertFalse(isPublic("""
|
||||
{"Statement":[{"NotPrincipal":"*",
|
||||
"Resource":"*","Action":"s3:GetObject","Effect":"Allow"}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSingleStatementObjectIsEvaluated() {
|
||||
assertTrue(isPublic("""
|
||||
|
||||
Reference in new issue
Block a user