Skip to content

feature(s3/manager): add option to control default checksums #3151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions feature/s3/manager/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,21 @@ type Uploader struct {
// Defines the buffer strategy used when uploading a part
BufferProvider ReadSeekerWriteToProvider

// RequestChecksumCalculation determines when request checksum calculation is performed
// for multipart uploads.
//
// There are two possible values for this setting:
//
// 1. RequestChecksumCalculationWhenSupported (default): The checksum is always calculated
// if the operation supports it, regardless of whether the user sets an algorithm in the request.
//
// 2. RequestChecksumCalculationWhenRequired: The checksum is only calculated if the user
// explicitly sets a checksum algorithm in the request. This preserves backwards compatibility
// for applications that don't want automatic checksum calculation.
//
// Note: S3 Express buckets always require CRC32 checksums regardless of this setting.
RequestChecksumCalculation aws.RequestChecksumCalculation

// partPool allows for the re-usage of streaming payload part buffers between upload calls
partPool byteSlicePool
}
Expand Down Expand Up @@ -274,12 +289,13 @@ type Uploader struct {
// })
func NewUploader(client UploadAPIClient, options ...func(*Uploader)) *Uploader {
u := &Uploader{
S3: client,
PartSize: DefaultUploadPartSize,
Concurrency: DefaultUploadConcurrency,
LeavePartsOnError: false,
MaxUploadParts: MaxUploadParts,
BufferProvider: defaultUploadBufferProvider(),
S3: client,
PartSize: DefaultUploadPartSize,
Concurrency: DefaultUploadConcurrency,
LeavePartsOnError: false,
MaxUploadParts: MaxUploadParts,
RequestChecksumCalculation: aws.RequestChecksumCalculationWhenSupported,
BufferProvider: defaultUploadBufferProvider(),
}

for _, option := range options {
Expand Down Expand Up @@ -776,7 +792,9 @@ func (u *multiuploader) initChecksumAlgorithm() {
case u.in.ChecksumSHA256 != nil:
u.in.ChecksumAlgorithm = types.ChecksumAlgorithmSha256
default:
u.in.ChecksumAlgorithm = types.ChecksumAlgorithmCrc32
if u.cfg.RequestChecksumCalculation != aws.RequestChecksumCalculationWhenRequired {
u.in.ChecksumAlgorithm = types.ChecksumAlgorithmCrc32
}
}
}

Expand Down
88 changes: 88 additions & 0 deletions feature/s3/manager/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,94 @@ func TestUploaderValidARN(t *testing.T) {
}
}

// TestUploadRequestChecksumCalculation tests that checksum behavior respects
// the RequestChecksumCalculation setting for backwards compatibility.
func TestUploadRequestChecksumCalculation(t *testing.T) {
testCases := []struct {
name string
requestChecksumCalculation aws.RequestChecksumCalculation
inputChecksumAlgorithm types.ChecksumAlgorithm
expectedChecksumAlgorithm types.ChecksumAlgorithm
description string
}{
{
name: "WhenRequired_NoDefault",
requestChecksumCalculation: aws.RequestChecksumCalculationWhenRequired,
inputChecksumAlgorithm: "",
expectedChecksumAlgorithm: "",
description: "When RequestChecksumCalculationWhenRequired is set, no default checksum should be applied (backwards compatibility)",
},
{
name: "WhenSupported_HasDefault",
requestChecksumCalculation: aws.RequestChecksumCalculationWhenSupported,
inputChecksumAlgorithm: "",
expectedChecksumAlgorithm: types.ChecksumAlgorithmCrc32,
description: "When RequestChecksumCalculationWhenSupported is set, default CRC32 checksum should be applied",
},
{
name: "WhenRequired_ExplicitPreserved",
requestChecksumCalculation: aws.RequestChecksumCalculationWhenRequired,
inputChecksumAlgorithm: types.ChecksumAlgorithmSha256,
expectedChecksumAlgorithm: types.ChecksumAlgorithmSha256,
description: "Explicit checksums should always be preserved regardless of RequestChecksumCalculation setting",
},
{
name: "WhenSupported_ExplicitPreserved",
requestChecksumCalculation: aws.RequestChecksumCalculationWhenSupported,
inputChecksumAlgorithm: types.ChecksumAlgorithmSha1,
expectedChecksumAlgorithm: types.ChecksumAlgorithmSha1,
description: "Explicit checksums should override defaults even with WhenSupported",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
c, invocations, args := s3testing.NewUploadLoggingClient(nil)

mgr := manager.NewUploader(c, func(u *manager.Uploader) {
u.RequestChecksumCalculation = tc.requestChecksumCalculation
})

input := &s3.PutObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: bytes.NewReader(buf12MB),
}
if tc.inputChecksumAlgorithm != "" {
input.ChecksumAlgorithm = tc.inputChecksumAlgorithm
}

resp, err := mgr.Upload(context.Background(), input)
if err != nil {
t.Errorf("Expected no error but received %v", err)
}

expectedOps := []string{"CreateMultipartUpload", "UploadPart", "UploadPart", "UploadPart", "CompleteMultipartUpload"}
if diff := cmpDiff(expectedOps, *invocations); len(diff) > 0 {
t.Error(diff)
}

if resp.UploadID != "UPLOAD-ID" {
t.Errorf("expect %q, got %q", "UPLOAD-ID", resp.UploadID)
}

cmu := (*args)[0].(*s3.CreateMultipartUploadInput)
if cmu.ChecksumAlgorithm != tc.expectedChecksumAlgorithm {
t.Errorf("%s: Expected checksum algorithm %v in CreateMultipartUpload, but got %v",
tc.description, tc.expectedChecksumAlgorithm, cmu.ChecksumAlgorithm)
}

for i := 1; i <= 3; i++ {
uploadPart := (*args)[i].(*s3.UploadPartInput)
if uploadPart.ChecksumAlgorithm != tc.expectedChecksumAlgorithm {
t.Errorf("%s: Expected checksum algorithm %v in UploadPart %d, but got %v",
tc.description, tc.expectedChecksumAlgorithm, i, uploadPart.ChecksumAlgorithm)
}
}
})
}
}

type mockS3UploadServer struct {
*http.ServeMux

Expand Down
Loading