fix(s3): take the bucket's default encryption when a multipart upload is created (#9212)

This commit is contained in:
glorfindel33 authored and GitHub committed 2026-11-23 07:17:02 -04:00
1 parent bf68a01fc2
commit c15e1e26ce
7 files changed
+209 -22

No files matched your search

@@ -949,10 +949,12 @@ public class S3Controller {
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5"));
Response.ResponseBuilder response = Response.ok().header("ETag", part.getETag());
// S3 echoes the part checksum only when the upload declared an algorithm.
ChecksumAlgorithm declared = s3Service.getMultipartUpload(bucket, key, uploadId).getChecksumAlgorithm();
MultipartUpload upload = s3Service.getMultipartUpload(bucket, key, uploadId);
ChecksumAlgorithm declared = upload.getChecksumAlgorithm();
if (declared != null) {
response.header(declared.headerName(), part.getChecksum().valueFor(declared));
}
appendServerSideEncryptionHeaders(response, upload);
appendSseCustomerHeaders(response,
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-algorithm"),
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5"));
@@ -1723,6 +1725,7 @@ public class S3Controller {
response.header("x-amz-checksum-algorithm", upload.getChecksumAlgorithm().name());
response.header("x-amz-checksum-type", upload.getChecksumType().name());
}
appendServerSideEncryptionHeaders(response, upload);
appendSseCustomerHeaders(response, upload);
return response.build();
}
@@ -3260,6 +3263,15 @@ public class S3Controller {
appendSseCustomerHeaders(resp, obj.getSseCustomerAlgorithm(), obj.getSseCustomerKeyMd5());
}
private static void appendServerSideEncryptionHeaders(Response.ResponseBuilder response, MultipartUpload upload) {
if (upload.effectiveServerSideEncryption() != null) {
response.header("x-amz-server-side-encryption", upload.effectiveServerSideEncryption());
}
if (upload.effectiveSseKmsKeyId() != null) {
response.header("x-amz-server-side-encryption-aws-kms-key-id", upload.effectiveSseKmsKeyId());
}
}
private void appendSseCustomerHeaders(Response.ResponseBuilder resp, MultipartUpload upload) {
appendSseCustomerHeaders(resp, upload.getSseCustomerAlgorithm(), upload.getSseCustomerKeyMd5());
}
@@ -3411,12 +3423,7 @@ public class S3Controller {
}
Response.ResponseBuilder response = Response.ok(xml.end("CopyPartResult").build())
.type(MediaType.APPLICATION_XML);
if (destinationUpload.getServerSideEncryption() != null) {
response.header("x-amz-server-side-encryption", destinationUpload.getServerSideEncryption());
}
if (destinationUpload.getSseKmsKeyId() != null) {
response.header("x-amz-server-side-encryption-aws-kms-key-id", destinationUpload.getSseKmsKeyId());
}
appendServerSideEncryptionHeaders(response, destinationUpload);
appendSseCustomerHeaders(response,
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-algorithm"),
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5"));
@@ -4841,7 +4841,16 @@ public class S3Service implements Resettable, ResourceProvider {
upload.setContentEncoding(objectOptions.getContentEncoding());
upload.setCacheControl(objectOptions.getCacheControl());
upload.setServerSideEncryption(normalizedServerSideEncryption);
upload.setSseKmsKeyId("aws:kms".equals(normalizedServerSideEncryption) ? sseKmsKeyId : null);
upload.setSseKmsKeyId("aws:kms".equals(normalizedServerSideEncryption)
|| "aws:kms:dsse".equals(normalizedServerSideEncryption) ? sseKmsKeyId : null);
if (normalizedServerSideEncryption == null && customerKey == null) {
ObjectEncryption encryption = defaultEncryption(destination);
upload.setDefaultServerSideEncryption(encryption.algorithm());
upload.setDefaultSseKmsKeyId("aws:kms".equals(encryption.algorithm())
|| "aws:kms:dsse".equals(encryption.algorithm())
? encryption.kmsKeyId()
: null);
}
if (customerKey != null) {
upload.setSseCustomerAlgorithm(customerKey.algorithm());
upload.setSseCustomerKeyMd5(customerKey.keyMd5());
@@ -5368,8 +5377,8 @@ public class S3Service implements Resettable, ResourceProvider {
.withContentDisposition(upload.getContentDisposition())
.withContentEncoding(upload.getContentEncoding())
.withCacheControl(upload.getCacheControl())
.withServerSideEncryption(upload.getServerSideEncryption())
.withSseKmsKeyId(upload.getSseKmsKeyId())
.withServerSideEncryption(upload.effectiveServerSideEncryption())
.withSseKmsKeyId(upload.effectiveSseKmsKeyId())
.withAcl(upload.getAcl())
.withGrantRead(upload.getGrantRead())
.withGrantWrite(upload.getGrantWrite())
@@ -5634,7 +5643,7 @@ public class S3Service implements Resettable, ResourceProvider {
/** The encryption condition keys of an upload's parts and completion: what it recorded when created. */
public Map<String, List<String>> multipartEncryptionConditions(String bucket, String key, String uploadId) {
MultipartUpload upload = getMultipartUpload(bucket, key, uploadId);
return S3ObjectActions.encryptionConditions(upload.getServerSideEncryption(), upload.getSseKmsKeyId());
return S3ObjectActions.encryptionConditions(upload.effectiveServerSideEncryption(), upload.effectiveSseKmsKeyId());
}
/** Checks a part or a completion against the bucket policy by the encryption its upload recorded. */
@@ -21,6 +21,11 @@ public class MultipartUpload {
private String cacheControl;
private String serverSideEncryption;
private String sseKmsKeyId;
// What the bucket's default encryption gave an upload whose request named none, when it was created. The two
// fields above hold only what the request sent: the CreateMultipartUpload request is judged by its own headers,
// its parts and completion by the effective encryption.
private String defaultServerSideEncryption;
private String defaultSseKmsKeyId;
private String sseCustomerAlgorithm;
private String sseCustomerKeyMd5;
private String acl;
@@ -87,6 +92,23 @@ public class MultipartUpload {
public String getSseKmsKeyId() { return sseKmsKeyId; }
public void setSseKmsKeyId(String sseKmsKeyId) { this.sseKmsKeyId = sseKmsKeyId; }
public String getDefaultServerSideEncryption() { return defaultServerSideEncryption; }
public void setDefaultServerSideEncryption(String defaultServerSideEncryption) {
this.defaultServerSideEncryption = defaultServerSideEncryption;
}
public String getDefaultSseKmsKeyId() { return defaultSseKmsKeyId; }
public void setDefaultSseKmsKeyId(String defaultSseKmsKeyId) { this.defaultSseKmsKeyId = defaultSseKmsKeyId; }
/** The encryption the upload's parts and object take: what the request sent, else the bucket default taken at creation. */
public String effectiveServerSideEncryption() {
return serverSideEncryption != null ? serverSideEncryption : defaultServerSideEncryption;
}
public String effectiveSseKmsKeyId() {
return serverSideEncryption != null ? sseKmsKeyId : defaultSseKmsKeyId;
}
public String getSseCustomerAlgorithm() { return sseCustomerAlgorithm; }
public void setSseCustomerAlgorithm(String sseCustomerAlgorithm) { this.sseCustomerAlgorithm = sseCustomerAlgorithm; }