diff --git a/docs/services/s3.md b/docs/services/s3.md index 95bc906f99..ad83d3bd47 100644 --- a/docs/services/s3.md +++ b/docs/services/s3.md @@ -246,6 +246,8 @@ Bucket policy statements can specify principals using `Principal` or `NotPrincip 5. Unsigned (anonymous) requests and ACL-based public access fall back to standard S3 ACL and anonymous authorization paths. 6. Unknown access key IDs return HTTP 403 with `InvalidAccessKeyId`. +With `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED` also on, the bucket policy and the caller's identity policies are combined the way AWS combines them. For same-account access, an explicit `Allow` in either policy grants the request, so a principal that only the bucket policy names needs no identity policy at all. For cross-account access, both the bucket policy and the caller's identity policy must allow the action. An explicit `Deny` in either policy denies the request in both cases. + ### Wire Response Shape When a request is denied by bucket policy enforcement, Floci returns HTTP 403 with an S3 XML error body including the `` element: diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java index f0fc7df390..3896d87702 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java @@ -399,10 +399,15 @@ public class IamEnforcementFilter implements ContainerRequestFilter { * for the duration of this call so that {@link IamService#resolveCallerContext} and * {@link IamService#resolveCallerArn}, which both key their per-account lookups off the * ambient account, resolve the credential's actual owner instead of the default account. + * + *

The caller supplies no resource-policy decision here, so this overload resolves the + * applicable resource policies itself through {@link ResourcePolicyProvider}, exactly as + * {@link #filter} does for the request's primary resource. Skipping that resolution would + * make the resource policy invisible on this path and leave a same-account principal that + * only a resource policy names without any grant at all. */ public void authorizeAdditionalResource(String authorizationHeader, String action, String resource) { - authorizeAdditionalResource( - authorizationHeader, action, resource, ResourcePolicyDecision.NEUTRAL, null); + authorizeAdditionalResource(authorizationHeader, action, resource, null, null); } /** @@ -422,6 +427,19 @@ public class IamEnforcementFilter implements ContainerRequestFilter { /** * Authorizes a secondary resource whose owning account is known. Resource-policy grants * crossing an account boundary require a matching identity-policy grant as well. + * + *

A {@code null} {@code resourcePolicyDecision} means the caller has no decision of its + * own and the registered {@link ResourcePolicyProvider}s are consulted here instead, under + * the credential's own account context. + * + *

The condition context built here never includes per-action, header-derived keys (such as + * {@code s3:RequestObjectTag/*} from an {@code x-amz-tagging} header) the way {@link #filter} + * builds for the request's primary resource. A presigned POST's outer HTTP headers are not + * covered by its signed policy document and do not correspond to what {@code S3Controller} + * actually applies to the uploaded object, so trusting one here would let an attacker satisfy a + * tag-scoped grant condition with a header the object is never actually tagged with. Only the + * caller-identity {@code aws:PrincipalArn} and the server-derived global keys (via {@link + * IamConditionContextResolver#withGlobalContext}) are safe to add. */ public void authorizeAdditionalResource( String authorizationHeader, @@ -439,7 +457,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter { if (akid == null || "test".equals(akid)) { return; } - if (extractCredentialScope(authorizationHeader) == null) { + String credentialScope = extractCredentialScope(authorizationHeader); + if (credentialScope == null) { return; } @@ -476,13 +495,34 @@ public class IamEnforcementFilter implements ContainerRequestFilter { conditionContext.put("aws:PrincipalArn", List.of(principalArn.get())); } - ResourceAccountRelationship accountRelationship = resourceOwnerAccountId == null - || accountId.equals(resourceOwnerAccountId) + Map> effectiveContext = conditionContext; + ResourcePolicyDecision effectiveDecision = resourcePolicyDecision; + String effectiveOwnerAccountId = resourceOwnerAccountId; + if (effectiveDecision == null) { + List resourcePolicies = resolveResourcePolicies( + credentialScope, resource); + effectiveOwnerAccountId = resourcePolicies.isEmpty() + ? null : resourcePolicies.getFirst().ownerAccountId(); + List policyDocs = resourcePolicies.stream() + .map(ResourcePolicyProvider.ResourcePolicy::policyDocument) + .filter(doc -> doc != null && !doc.isBlank()) + .toList(); + String region = requestContext.getRegion() == null + ? config.defaultRegion() : requestContext.getRegion(); + effectiveContext = IamConditionContextResolver.withGlobalContext( + conditionContext, resource, region, accountId, effectiveOwnerAccountId); + effectiveDecision = evaluator.evaluateResourcePolicy( + policyDocs.isEmpty() ? null : policyDocs, + caller.principalArn(), action, resource, effectiveContext); + } + + ResourceAccountRelationship accountRelationship = effectiveOwnerAccountId == null + || accountId.equals(effectiveOwnerAccountId) ? ResourceAccountRelationship.SAME_ACCOUNT : ResourceAccountRelationship.CROSS_ACCOUNT; Decision decision = evaluator.evaluateResolvedResourcePolicy( - caller, resourcePolicyDecision, accountRelationship, - action, resource, conditionContext); + caller, effectiveDecision, accountRelationship, + action, resource, effectiveContext); if (decision != Decision.DENY) { return; } 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.java index 98f3dd177b..7b166359fd 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 @@ -404,5 +404,114 @@ class S3BucketPolicyEnforcementIntegrationTest { .statusCode(200) .body(equalTo(content)); } -} + @Test + void sameAccountPrimaryRequestNeedsOnlyOneAllowAcrossBothPolicies() { + String suffix = UUID.randomUUID().toString().substring(0, 8); + String bucket = "bp-either-" + suffix; + + createBucket(bucket); + + String developerName = "dev-" + suffix; + Credentials developer = createUserWithCredentials(developerName); + String writerName = "writer-" + suffix; + Credentials writer = createUserWithCredentials(writerName); + putUserPolicy(writerName, "WriteBucket", """ + { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": "s3:PutObject", + "Resource": "arn:aws:s3:::%s/*" + } + ] + }""".formatted(bucket), "000000000000"); + + putBucketPolicy(bucket, """ + { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Principal": {"AWS": "%s"}, + "Action": ["s3:GetObject", "s3:PutObject"], + "Resource": "arn:aws:s3:::%s/*" + } + ] + }""".formatted(developer.userArn(), bucket)); + + // 1. Bucket policy alone grants the developer, who has no identity policy at all + given() + .filter(developer.signer()) + .contentType("text/plain") + .body("bucket policy only") + .when() + .put("/" + bucket + "/from-bucket-policy.txt") + .then() + .statusCode(200); + + // 2. Identity policy alone grants the writer, whom the bucket policy never names + given() + .filter(writer.signer()) + .contentType("text/plain") + .body("identity policy only") + .when() + .put("/" + bucket + "/from-identity-policy.txt") + .then() + .statusCode(200); + + // 3. An explicit deny in the bucket policy overrides the writer's identity allow + putBucketPolicy(bucket, """ + { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Principal": {"AWS": "%s"}, + "Action": ["s3:GetObject", "s3:PutObject"], + "Resource": "arn:aws:s3:::%s/*" + }, + { + "Effect": "Deny", + "Principal": {"AWS": "%s"}, + "Action": "s3:PutObject", + "Resource": "arn:aws:s3:::%s/*" + } + ] + }""".formatted(developer.userArn(), bucket, writer.userArn(), bucket)); + + given() + .filter(writer.signer()) + .contentType("text/plain") + .body("denied by bucket policy") + .when() + .put("/" + bucket + "/denied-by-bucket-policy.txt") + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + + // 4. An explicit deny in the identity policy overrides the developer's bucket-policy allow + putUserPolicy(developerName, "DenyWrite", """ + { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Deny", + "Action": "s3:PutObject", + "Resource": "arn:aws:s3:::%s/*" + } + ] + }""".formatted(bucket), "000000000000"); + + given() + .filter(developer.signer()) + .contentType("text/plain") + .body("denied by identity policy") + .when() + .put("/" + bucket + "/denied-by-identity-policy.txt") + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostAuthEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostAuthEnforcementIntegrationTest.java index 86337b4cb4..1db64ec18c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostAuthEnforcementIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostAuthEnforcementIntegrationTest.java @@ -19,6 +19,7 @@ import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; /** @@ -399,6 +400,158 @@ class S3PresignedPostAuthEnforcementIntegrationTest { .header("ETag", notNullValue()); } + @Test + @Order(32) + void acceptsGenuineSignatureWhenOnlyBucketPolicyAllowsPutObject() { + String bucket = "presigned-post-bucket-policy-bucket"; + String key = "uploads/bucket-policy-only.txt"; + String fileContent = "uploaded under a bucket-policy-only grant"; + String userName = "presigned-post-bucket-policy-user"; + String[] credentials = createUserAndAccessKey(userName); + String accessKeyId = credentials[0]; + String secretKey = credentials[1]; + + createBucket(bucket); + putBucketPolicy(bucket, """ + {"Version":"2012-10-17","Statement":[ + {"Effect":"Allow", + "Principal":{"AWS":"arn:aws:iam::000000000000:user/%1$s"}, + "Action":"s3:PutObject", + "Resource":"arn:aws:s3:::%2$s/*"} + ]}""".formatted(userName, bucket)); + + String policyBase64 = buildPolicyBase64(bucket, key); + String credential = accessKeyId + "/20260101/us-east-1/s3/aws4_request"; + String signature = signPolicy(policyBase64, credential, secretKey); + + given() + .multiPart("key", key) + .multiPart("policy", policyBase64) + .multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256") + .multiPart("x-amz-credential", credential) + .multiPart("x-amz-date", AMZ_DATE) + .multiPart("x-amz-signature", signature) + .multiPart("file", "bucket-policy-only.txt", + fileContent.getBytes(StandardCharsets.UTF_8), "text/plain") + .when() + .post("/" + bucket) + .then() + .statusCode(204) + .header("ETag", notNullValue()); + } + + @Test + @Order(33) + void rejectsGenuineSignatureWhenBucketPolicyDeniesPutObject() { + String bucket = "presigned-post-bucket-policy-deny-bucket"; + String key = "uploads/bucket-policy-denied.txt"; + String userName = "presigned-post-bucket-policy-denied-user"; + String[] credentials = createUserAndAccessKey(userName); + String accessKeyId = credentials[0]; + String secretKey = credentials[1]; + putUserPolicy(userName, "AllowPutObject", """ + {"Version":"2012-10-17","Statement":[ + {"Effect":"Allow","Action":"s3:PutObject","Resource":"*"} + ]}"""); + + createBucket(bucket); + putBucketPolicy(bucket, """ + {"Version":"2012-10-17","Statement":[ + {"Effect":"Deny", + "Principal":{"AWS":"arn:aws:iam::000000000000:user/%1$s"}, + "Action":"s3:PutObject", + "Resource":"arn:aws:s3:::%2$s/*"} + ]}""".formatted(userName, bucket)); + + String policyBase64 = buildPolicyBase64(bucket, key); + String credential = accessKeyId + "/20260101/us-east-1/s3/aws4_request"; + String signature = signPolicy(policyBase64, credential, secretKey); + + given() + .multiPart("key", key) + .multiPart("policy", policyBase64) + .multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256") + .multiPart("x-amz-credential", credential) + .multiPart("x-amz-date", AMZ_DATE) + .multiPart("x-amz-signature", signature) + .multiPart("file", "bucket-policy-denied.txt", + "should not be stored".getBytes(StandardCharsets.UTF_8), "text/plain") + .when() + .post("/" + bucket) + .then() + .statusCode(403) + .body("Error.Code", equalTo("AccessDenied")); + } + + /** + * Regression for a tag-condition spoofing gap: a presigned POST's signed policy document + * covers the multipart fields, never the request's outer HTTP headers, and {@code + * S3Controller} does not apply any tag to the uploaded object on this path at all. A + * bucket-policy grant conditioned on {@code s3:RequestObjectTag/*} must not be satisfiable by + * setting an unsigned {@code x-amz-tagging} header, since a caller could then obtain a grant + * meant only for tagged uploads without the object ever carrying that tag. + */ + @Test + @Order(34) + void rejectsGenuineSignatureWhenBucketPolicyGrantRequiresATagTheHeaderCannotSupply() { + String bucket = "presigned-post-tag-condition-bucket"; + String key = "uploads/tag-condition.txt"; + String userName = "presigned-post-tag-condition-user"; + String[] credentials = createUserAndAccessKey(userName); + String accessKeyId = credentials[0]; + String secretKey = credentials[1]; + + createBucket(bucket); + putBucketPolicy(bucket, """ + {"Version":"2012-10-17","Statement":[ + {"Effect":"Allow", + "Principal":{"AWS":"arn:aws:iam::000000000000:user/%1$s"}, + "Action":"s3:PutObject", + "Resource":"arn:aws:s3:::%2$s/*", + "Condition":{"StringEquals":{"s3:RequestObjectTag/team":"eng"}}} + ]}""".formatted(userName, bucket)); + + String policyBase64 = buildPolicyBase64(bucket, key); + String credential = accessKeyId + "/20260101/us-east-1/s3/aws4_request"; + String signature = signPolicy(policyBase64, credential, secretKey); + + given() + .header("x-amz-tagging", "team=eng") + .multiPart("key", key) + .multiPart("policy", policyBase64) + .multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256") + .multiPart("x-amz-credential", credential) + .multiPart("x-amz-date", AMZ_DATE) + .multiPart("x-amz-signature", signature) + .multiPart("file", "tag-condition.txt", + "should not be stored".getBytes(StandardCharsets.UTF_8), "text/plain") + .when() + .post("/" + bucket) + .then() + .statusCode(403) + .body("Error.Code", equalTo("AccessDenied")); + } + + private static void createBucket(String bucket) { + given() + .filter(S3RequestSigner.signedAs(LEGACY_ACCESS_KEY_ID, LEGACY_SECRET_KEY)) + .when() + .put("/" + bucket) + .then() + .statusCode(200); + } + + private static void putBucketPolicy(String bucket, String policyDocument) { + given() + .filter(S3RequestSigner.signedAs(LEGACY_ACCESS_KEY_ID, LEGACY_SECRET_KEY)) + .contentType("application/json") + .body(policyDocument) + .when() + .put("/" + bucket + "?policy") + .then() + .statusCode(200); + } + private static final String IAM_ROOT_AUTH = "AWS4-HMAC-SHA256 Credential=" + LEGACY_ACCESS_KEY_ID + "/20260101/us-east-1/iam/aws4_request" + ", SignedHeaders=host, Signature=abc"; diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostBucketPolicyAccountContextIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostBucketPolicyAccountContextIntegrationTest.java new file mode 100644 index 0000000000..9c5cd5927e --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostBucketPolicyAccountContextIntegrationTest.java @@ -0,0 +1,135 @@ +package io.github.hectorvent.floci.services.s3; + +import io.github.hectorvent.floci.core.common.IamEnforcementFilter; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.QuarkusTestProfile; +import io.quarkus.test.junit.TestProfile; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.util.Map; +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +/** + * Regression coverage for a presigned POST bucket-policy-only grant where the bucket and the + * signer both live in a non-default account, distinct from + * {@code S3PresignedPostIamOnlyEnforcementIntegrationTest}'s identity-policy coverage of the + * same account-context gap. {@link IamEnforcementFilter#authorizeAdditionalResource(String, + * String, String)} must resolve the applicable bucket policy under the signing credential's own + * account context (pushed onto {@code RequestContext} for the duration of that call), or a bucket + * owned by a non-default account is invisible to it and the grant is silently dropped. + * + *

{@code global-bucket-namespace} is enabled so bucket resolution (both here and in the + * eventual object write) does not itself depend on the ambient account, isolating the assertion + * to the account used for the identity/resource-policy relationship rather than to bucket lookup + * scoping. + */ +@QuarkusTest +@TestProfile(S3PresignedPostBucketPolicyAccountContextIntegrationTest.GlobalNamespaceIamOnlyProfile.class) +class S3PresignedPostBucketPolicyAccountContextIntegrationTest { + + private static final String REGION = "us-east-1"; + /** A non-default account, distinct from the configured {@code 000000000000} default. */ + private static final String NON_DEFAULT_ACCOUNT = "222233334444"; + private static final DateTimeFormatter AMZ_DATE_FMT = + DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC); + + public static final class GlobalNamespaceIamOnlyProfile implements QuarkusTestProfile { + @Override + public Map getConfigOverrides() { + return Map.of( + "floci.services.iam.enforcement-enabled", "true", + "floci.services.s3.enforce-auth", "false", + "floci.services.s3.global-bucket-namespace", "true"); + } + } + + @Test + void presignedPostSucceedsForNonDefaultAccountBucketAndSignerWhenOnlyBucketPolicyAllows() { + String suffix = UUID.randomUUID().toString().substring(0, 8); + String bucket = "presigned-post-bp-xacct-" + suffix; + String userName = "presigned-post-bp-xacct-user-" + suffix; + + createBucketAsRoot(bucket, NON_DEFAULT_ACCOUNT); + String accessKeyId = createUser(userName, NON_DEFAULT_ACCOUNT); + putBucketPolicy(bucket, """ + {"Version":"2012-10-17","Statement":[ + {"Effect":"Allow", + "Principal":{"AWS":"arn:aws:iam::%1$s:user/%2$s"}, + "Action":"s3:PutObject", + "Resource":"arn:aws:s3:::%3$s/*"} + ]}""".formatted(NON_DEFAULT_ACCOUNT, userName, bucket), NON_DEFAULT_ACCOUNT); + + given() + .multiPart("key", "bp-allowed-xacct.txt") + .multiPart("x-amz-credential", presignedCredential(accessKeyId)) + .multiPart("file", "bp-allowed-xacct.txt", + "uploaded via presigned POST".getBytes(StandardCharsets.UTF_8), "text/plain") + .when() + .post("/" + bucket) + .then() + .statusCode(204); + } + + private static String presignedCredential(String accessKeyId) { + String amzDate = AMZ_DATE_FMT.format(Instant.now()); + String dateStamp = amzDate.substring(0, 8); + return accessKeyId + "/" + dateStamp + "/" + REGION + "/s3/aws4_request"; + } + + private static void createBucketAsRoot(String bucket, String accountId) { + given() + .header("Authorization", auth(accountId, "s3")) + .when() + .put("/" + bucket) + .then() + .statusCode(200); + } + + private static void putBucketPolicy(String bucket, String policyDocument, String accountId) { + given() + .header("Authorization", auth(accountId, "s3")) + .contentType("application/json") + .body(policyDocument) + .when() + .put("/" + bucket + "?policy") + .then() + .statusCode(200); + } + + /** Creates the user (and its access key) as root in {@code accountId}. */ + private static String createUser(String userName, String accountId) { + String authHeader = auth(accountId, "iam"); + given() + .formParam("Action", "CreateUser") + .formParam("UserName", userName) + .header("Authorization", authHeader) + .when() + .post("/") + .then() + .statusCode(200); + + return given() + .formParam("Action", "CreateAccessKey") + .formParam("UserName", userName) + .header("Authorization", authHeader) + .when() + .post("/") + .then() + .statusCode(200) + .extract() + .path("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId"); + } + + private static String auth(String accessKeyId, String service) { + String amzDate = AMZ_DATE_FMT.format(Instant.now()).substring(0, 8); + return "AWS4-HMAC-SHA256 Credential=" + accessKeyId + "/" + amzDate + "/" + REGION + "/" + service + + "/aws4_request, SignedHeaders=host, Signature=abc"; + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIamOnlyEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIamOnlyEnforcementIntegrationTest.java index 8cd88daba5..bb6a593a92 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIamOnlyEnforcementIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIamOnlyEnforcementIntegrationTest.java @@ -142,6 +142,61 @@ class S3PresignedPostIamOnlyEnforcementIntegrationTest { .statusCode(204); } + /** + * Regression for {@code IamEnforcementFilter#authorizeAdditionalResource} resolving the + * bucket policy without the global condition keys ({@code aws:PrincipalAccount} in + * particular) that {@code filter()} always supplies for the header-signed and presigned-URL + * paths. A bucket-policy {@code Deny} conditioned on the account key being absent must not + * fire for a genuine signed principal, whose account this filter always knows. Exercised in + * this IAM-only profile (rather than {@code S3PresignedPostAuthEnforcementIntegrationTest}) + * so only {@code authorizeAdditionalResource} gates the request: with + * {@code s3.enforce-auth} on, {@code S3Service#authorizeSignedBucketPolicy} independently + * re-evaluates the same bucket policy through its own, narrower condition context. + */ + @Test + void presignedPostSucceedsWhenBucketPolicyDeniesOnlyForMissingPrincipalAccount() { + String suffix = UUID.randomUUID().toString().substring(0, 8); + String bucket = "presigned-post-null-condition-" + suffix; + String userName = "presigned-post-null-condition-user-" + suffix; + + createBucketAsRoot(bucket); + String accessKeyId = createUser(userName); + putUserPolicy(userName, "AllowPutObject", """ + {"Version":"2012-10-17","Statement":[ + {"Effect":"Allow","Action":"s3:PutObject","Resource":"*"} + ]}"""); + putBucketPolicy(bucket, """ + {"Version":"2012-10-17","Statement":[ + {"Effect":"Deny", + "Principal":{"AWS":"arn:aws:iam::000000000000:user/%1$s"}, + "Action":"s3:PutObject", + "Resource":"arn:aws:s3:::%2$s/*", + "Condition":{"Null":{"aws:PrincipalAccount":"true"}}} + ]}""".formatted(userName, bucket)); + + given() + .multiPart("key", "principal-account-present.txt") + .multiPart("x-amz-credential", presignedCredential(accessKeyId)) + .multiPart("file", "principal-account-present.txt", + "uploaded with aws:PrincipalAccount populated".getBytes(StandardCharsets.UTF_8), + "text/plain") + .when() + .post("/" + bucket) + .then() + .statusCode(204); + } + + private static void putBucketPolicy(String bucket, String policyDocument) { + given() + .header("Authorization", auth("test", "s3")) + .contentType("application/json") + .body(policyDocument) + .when() + .put("/" + bucket + "?policy") + .then() + .statusCode(200); + } + private static String presignedCredential(String accessKeyId) { String amzDate = AMZ_DATE_FMT.format(Instant.now()); String dateStamp = amzDate.substring(0, 8);