mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
s3api: return SSE headers from CompleteMultipartUpload (#13389)
* s3api: return the object's SSE headers from CompleteMultipartUpload AWS answers CompleteMultipartUpload for an encrypted object with x-amz-server-side-encryption, the KMS key id and bucket-key-enabled. Read them from the committed entry the way HEAD does, on both the first answer and a retry answered from the committed object. SSE-C customer headers are not echoed, as AWS does not send them on Complete. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: test CompleteMultipartUpload SSE response headers Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: answer CompleteMultipartUpload SSE headers from the entry only setSSEResponseHeaders falls back to the request's KMS key id and bucket-key headers, so a Complete request carrying them could report a bucket key the object does not use. Complete's own headers say nothing about the object; pass none. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: test that Complete does not echo request SSE-KMS headers Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
da62513b3e
commit
c3e284a45c
4 files changed
+191
-4
No files matched your search
@@ -204,6 +204,8 @@ type CompleteMultipartUploadResult struct {
|
||||
|
||||
// Store the VersionId internally for setting HTTP header, but don't marshal to XML
|
||||
VersionId *string `xml:"-"`
|
||||
|
||||
SSE SSEResponseMetadata `xml:"-"`
|
||||
}
|
||||
|
||||
// copySSEHeadersFromFirstPart copies all SSE-related headers from the first part to the destination entry
|
||||
@@ -277,12 +279,13 @@ type multipartCompletionState struct {
|
||||
objectDir, objectName string // where this completion writes its object (the version file when versioned)
|
||||
}
|
||||
|
||||
func completeMultipartResult(r *http.Request, input *s3.CompleteMultipartUploadInput, etag string, entry *filer_pb.Entry) *CompleteMultipartUploadResult {
|
||||
func (s3a *S3ApiServer) completeMultipartResult(r *http.Request, input *s3.CompleteMultipartUploadInput, etag string, entry *filer_pb.Entry) *CompleteMultipartUploadResult {
|
||||
result := &CompleteMultipartUploadResult{
|
||||
Location: aws.String(fmt.Sprintf("%s://%s/%s/%s", getRequestScheme(r), r.Host, url.PathEscape(*input.Bucket), urlPathEscape(*input.Key))),
|
||||
Bucket: input.Bucket,
|
||||
ETag: aws.String(etag),
|
||||
Key: objectKey(input.Key),
|
||||
SSE: s3a.completedObjectSSE(entry),
|
||||
}
|
||||
if entry != nil && entry.Extended != nil {
|
||||
if versionIdBytes, ok := entry.Extended[s3_constants.ExtVersionIdKey]; ok {
|
||||
@@ -297,6 +300,22 @@ func completeMultipartResult(r *http.Request, input *s3.CompleteMultipartUploadI
|
||||
return result
|
||||
}
|
||||
|
||||
// completedObjectSSE is the encryption CompleteMultipartUpload reports, read
|
||||
// from the object entry the way HEAD reads it; AWS sends no SSE-C headers here.
|
||||
func (s3a *S3ApiServer) completedObjectSSE(entry *filer_pb.Entry) SSEResponseMetadata {
|
||||
switch s3a.detectPrimarySSEType(entry) {
|
||||
case s3_constants.SSETypeS3:
|
||||
return SSEResponseMetadata{SSEType: s3_constants.SSETypeS3}
|
||||
case s3_constants.SSETypeKMS:
|
||||
sse := SSEResponseMetadata{SSEType: s3_constants.SSETypeKMS}
|
||||
if key, err := DeserializeSSEKMSMetadata(entry.Extended[s3_constants.SeaweedFSSSEKMSKey]); err == nil {
|
||||
sse.KMSKeyID, sse.BucketKeyEnabled = key.KeyID, key.BucketKeyEnabled
|
||||
}
|
||||
return sse
|
||||
}
|
||||
return SSEResponseMetadata{}
|
||||
}
|
||||
|
||||
func extractMultipartSSES3Info(entry *filer_pb.Entry) (*multipartSSES3Info, error) {
|
||||
if entry == nil || entry.Extended == nil {
|
||||
return nil, nil
|
||||
@@ -786,6 +805,7 @@ func (s3a *S3ApiServer) completeMultipartUpload(r *http.Request, input *s3.Compl
|
||||
completionState.objectDir, completionState.objectName = dirName, entryName
|
||||
|
||||
etagQuote := "\"" + completionState.multipartETag + "\""
|
||||
var completedEntry *filer_pb.Entry
|
||||
// Check if versioning is configured for this bucket BEFORE creating any files.
|
||||
versioningState, vErr := s3a.getVersioningState(*input.Bucket)
|
||||
if vErr != nil {
|
||||
@@ -857,6 +877,7 @@ func (s3a *S3ApiServer) completeMultipartUpload(r *http.Request, input *s3.Compl
|
||||
}
|
||||
versionEntry.Attributes.FileSize = uint64(completionState.offset)
|
||||
versionEntry.Attributes.Mtime = versionMtime
|
||||
completedEntry = versionEntry
|
||||
}
|
||||
|
||||
// Construct entry with metadata for caching in .versions directory
|
||||
@@ -931,6 +952,7 @@ func (s3a *S3ApiServer) completeMultipartUpload(r *http.Request, input *s3.Compl
|
||||
Key: objectKey(input.Key),
|
||||
VersionId: aws.String(versionId),
|
||||
ChecksumType: completionState.checksumType,
|
||||
SSE: s3a.completedObjectSSE(completedEntry),
|
||||
}
|
||||
output.SetChecksum(completionState.checksumHeaderName, completionState.checksumValue)
|
||||
return s3err.ErrNone
|
||||
@@ -994,6 +1016,7 @@ func (s3a *S3ApiServer) completeMultipartUpload(r *http.Request, input *s3.Compl
|
||||
entry.Attributes.Mime = completionState.mime
|
||||
}
|
||||
entry.Attributes.FileSize = uint64(completionState.offset)
|
||||
completedEntry = entry
|
||||
}, removal, cond, versionsDir)
|
||||
if err != nil {
|
||||
if errors.Is(err, errUploadRemoved) {
|
||||
@@ -1032,6 +1055,7 @@ func (s3a *S3ApiServer) completeMultipartUpload(r *http.Request, input *s3.Compl
|
||||
ETag: aws.String(etagQuote),
|
||||
Key: objectKey(input.Key),
|
||||
ChecksumType: completionState.checksumType,
|
||||
SSE: s3a.completedObjectSSE(completedEntry),
|
||||
// VersionId field intentionally omitted for suspended versioning
|
||||
}
|
||||
output.SetChecksum(completionState.checksumHeaderName, completionState.checksumValue)
|
||||
@@ -1095,6 +1119,7 @@ func (s3a *S3ApiServer) completeMultipartUpload(r *http.Request, input *s3.Compl
|
||||
if completionState.entityWithTtl {
|
||||
entry.Extended[s3_constants.SeaweedFSExpiresS3] = []byte("true")
|
||||
}
|
||||
completedEntry = entry
|
||||
}, removal, cond, "")
|
||||
if err != nil {
|
||||
if errors.Is(err, errUploadRemoved) {
|
||||
@@ -1124,6 +1149,7 @@ func (s3a *S3ApiServer) completeMultipartUpload(r *http.Request, input *s3.Compl
|
||||
ETag: aws.String(etagQuote),
|
||||
Key: objectKey(input.Key),
|
||||
ChecksumType: completionState.checksumType,
|
||||
SSE: s3a.completedObjectSSE(completedEntry),
|
||||
}
|
||||
output.SetChecksum(completionState.checksumHeaderName, completionState.checksumValue)
|
||||
return s3err.ErrNone
|
||||
@@ -1319,7 +1345,7 @@ func (s3a *S3ApiServer) committedCompletion(r *http.Request, input *s3.CompleteM
|
||||
if uploadId, ok := entry.Extended[s3_constants.SeaweedFSUploadId]; !ok || string(uploadId) != *input.UploadId {
|
||||
return nil, nil
|
||||
}
|
||||
return completeMultipartResult(r, input, getEtagFromEntry(entry), entry), entry
|
||||
return s3a.completeMultipartResult(r, input, getEtagFromEntry(entry), entry), entry
|
||||
}
|
||||
|
||||
// resumeCommittedObject finds an entry this upload already committed at the
|
||||
@@ -1350,7 +1376,7 @@ func (s3a *S3ApiServer) resumeCommittedObject(r *http.Request, input *s3.Complet
|
||||
glog.Errorf("completeMultipartUpload: failed to retire the null delete marker for %s/%s: %v", *input.Bucket, *input.Key, err)
|
||||
return nil, s3err.ErrInternalError
|
||||
}
|
||||
return completeMultipartResult(r, input, getEtagFromEntry(entry), entry), s3err.ErrNone
|
||||
return s3a.completeMultipartResult(r, input, getEtagFromEntry(entry), entry), s3err.ErrNone
|
||||
}
|
||||
|
||||
// Metadata-only: the version file's chunks are the still-registered parts'
|
||||
|
||||
@@ -184,7 +184,7 @@ func TestCompleteMultipartResultChecksumFromEntry(t *testing.T) {
|
||||
s3_constants.ExtChecksumType: []byte(s3_constants.ChecksumTypeComposite),
|
||||
}}
|
||||
|
||||
result := completeMultipartResult(httptest.NewRequest(http.MethodPost, "/bucket/key", nil), input, "\"etag-1\"", entry)
|
||||
result := (&S3ApiServer{}).completeMultipartResult(httptest.NewRequest(http.MethodPost, "/bucket/key", nil), input, "\"etag-1\"", entry)
|
||||
if result.ChecksumCRC32C != "fx4FQw==-1" {
|
||||
t.Fatalf("ChecksumCRC32C = %q, want %q", result.ChecksumCRC32C, "fx4FQw==-1")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
package s3api
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
"github.com/seaweedfs/seaweedfs/weed/cluster"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/s3_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
||||
util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCompleteMultipartUploadReturnsSSEHeaders(t *testing.T) {
|
||||
util_http.InitGlobalHttpClient()
|
||||
km := GetSSES3KeyManager()
|
||||
oldSuperKey := km.superKey
|
||||
km.superKey = make([]byte, 32)
|
||||
for i := range km.superKey {
|
||||
km.superKey[i] = byte(i + 1)
|
||||
}
|
||||
t.Cleanup(func() { km.superKey = oldSuperKey })
|
||||
kmsKey := SetupTestKMS(t)
|
||||
t.Cleanup(kmsKey.Cleanup)
|
||||
|
||||
customerKey := make([]byte, 32)
|
||||
for i := range customerKey {
|
||||
customerKey[i] = byte(0x40 + i)
|
||||
}
|
||||
keySum := md5.Sum(customerKey)
|
||||
ssec := map[string]string{
|
||||
s3_constants.AmzServerSideEncryptionCustomerAlgorithm: "AES256",
|
||||
s3_constants.AmzServerSideEncryptionCustomerKey: base64.StdEncoding.EncodeToString(customerKey),
|
||||
s3_constants.AmzServerSideEncryptionCustomerKeyMD5: base64.StdEncoding.EncodeToString(keySum[:]),
|
||||
}
|
||||
|
||||
sseHeaders := []string{
|
||||
s3_constants.AmzServerSideEncryption,
|
||||
s3_constants.AmzServerSideEncryptionAwsKmsKeyId,
|
||||
s3_constants.AmzServerSideEncryptionContext,
|
||||
s3_constants.AmzServerSideEncryptionBucketKeyEnabled,
|
||||
s3_constants.AmzServerSideEncryptionCustomerAlgorithm,
|
||||
s3_constants.AmzServerSideEncryptionCustomerKeyMD5,
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
bucketEncryption *s3_pb.EncryptionConfiguration
|
||||
initiate map[string]string
|
||||
perRequest map[string]string // sent on every request, as SSE-C needs
|
||||
want map[string]string
|
||||
}{
|
||||
{
|
||||
name: "no encryption",
|
||||
},
|
||||
{
|
||||
name: "SSE-S3",
|
||||
initiate: map[string]string{s3_constants.AmzServerSideEncryption: "AES256"},
|
||||
want: map[string]string{s3_constants.AmzServerSideEncryption: "AES256"},
|
||||
},
|
||||
{
|
||||
name: "bucket default SSE-S3",
|
||||
bucketEncryption: &s3_pb.EncryptionConfiguration{SseAlgorithm: "AES256"},
|
||||
want: map[string]string{s3_constants.AmzServerSideEncryption: "AES256"},
|
||||
},
|
||||
{
|
||||
name: "SSE-KMS with bucket key",
|
||||
initiate: map[string]string{
|
||||
s3_constants.AmzServerSideEncryption: "aws:kms",
|
||||
s3_constants.AmzServerSideEncryptionAwsKmsKeyId: kmsKey.KeyID,
|
||||
s3_constants.AmzServerSideEncryptionBucketKeyEnabled: "true",
|
||||
},
|
||||
want: map[string]string{
|
||||
s3_constants.AmzServerSideEncryption: "aws:kms",
|
||||
s3_constants.AmzServerSideEncryptionAwsKmsKeyId: kmsKey.KeyID,
|
||||
s3_constants.AmzServerSideEncryptionBucketKeyEnabled: "true",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bucket default SSE-KMS",
|
||||
bucketEncryption: &s3_pb.EncryptionConfiguration{SseAlgorithm: "aws:kms", KmsKeyId: kmsKey.KeyID},
|
||||
want: map[string]string{
|
||||
s3_constants.AmzServerSideEncryption: "aws:kms",
|
||||
s3_constants.AmzServerSideEncryptionAwsKmsKeyId: kmsKey.KeyID,
|
||||
},
|
||||
},
|
||||
{
|
||||
// AWS answers Complete for an SSE-C object without the customer headers.
|
||||
name: "SSE-C",
|
||||
perRequest: ssec,
|
||||
},
|
||||
}
|
||||
|
||||
for _, versioning := range []string{"", s3_constants.VersioningEnabled, s3_constants.VersioningSuspended} {
|
||||
for _, routed := range []bool{true, false} {
|
||||
for _, tc := range tests {
|
||||
t.Run("versioning="+versioning+"/routed="+map[bool]string{true: "yes", false: "no"}[routed]+"/"+tc.name, func(t *testing.T) {
|
||||
filerImpl := newStampingFiler(t)
|
||||
addr := startFakeFiler(t, filerImpl)
|
||||
s3a := newPutTestServer(t, addr)
|
||||
s3a.iam = &IdentityAccessManagement{}
|
||||
if routed {
|
||||
s3a.objectWriteLockClient = cluster.NewLockClient(s3a.option.GrpcDialOption, addr)
|
||||
s3a.objectWriteLockClient.SetRing([]pb.ServerAddress{addr}, 1)
|
||||
}
|
||||
s3a.bucketConfigCache.Set("b", &BucketConfig{Name: "b", Versioning: versioning, Ownership: s3_constants.OwnershipObjectWriter, Encryption: tc.bucketEncryption})
|
||||
|
||||
withHeaders := func(r *http.Request, headers ...map[string]string) *http.Request {
|
||||
for _, h := range headers {
|
||||
for k, v := range h {
|
||||
r.Header.Set(k, v)
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
out, code := s3a.createMultipartUpload(withHeaders(objectRequest(http.MethodPost, "mp", "uploads", ""), tc.initiate, tc.perRequest),
|
||||
&s3.CreateMultipartUploadInput{Bucket: aws.String("b"), Key: aws.String("mp")})
|
||||
require.Equal(t, s3err.ErrNone, code)
|
||||
w := httptest.NewRecorder()
|
||||
s3a.PutObjectPartHandler(w, withHeaders(objectRequest(http.MethodPut, "mp", "partNumber=1&uploadId="+*out.UploadId, "multipart body"), tc.perRequest))
|
||||
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
|
||||
require.Len(t, w.Header()["ETag"], 1)
|
||||
|
||||
// SSE-KMS headers on Complete say nothing about the object and are not echoed.
|
||||
stray := map[string]string{
|
||||
s3_constants.AmzServerSideEncryptionAwsKmsKeyId: "not-the-object-key",
|
||||
s3_constants.AmzServerSideEncryptionBucketKeyEnabled: "true",
|
||||
}
|
||||
body := `<CompleteMultipartUpload><Part><PartNumber>1</PartNumber><ETag>` + w.Header()["ETag"][0] + `</ETag></Part></CompleteMultipartUpload>`
|
||||
complete := func() *httptest.ResponseRecorder {
|
||||
w := httptest.NewRecorder()
|
||||
s3a.CompleteMultipartUploadHandler(w, withHeaders(objectRequest(http.MethodPost, "mp", "uploadId="+*out.UploadId, body), tc.perRequest, stray))
|
||||
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
|
||||
return w
|
||||
}
|
||||
first := complete()
|
||||
for _, header := range sseHeaders {
|
||||
require.Equal(t, tc.want[header], first.Header().Get(header), header)
|
||||
}
|
||||
require.NotContains(t, first.Body.String(), "ServerSideEncryption")
|
||||
require.NotContains(t, first.Body.String(), "SSEKMS")
|
||||
|
||||
// A retry is answered from the committed object, with the same headers.
|
||||
retry := complete()
|
||||
for _, header := range sseHeaders {
|
||||
require.Equal(t, tc.want[header], retry.Header().Get(header), "retry "+header)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -227,6 +227,8 @@ func (s3a *S3ApiServer) CompleteMultipartUploadHandler(w http.ResponseWriter, r
|
||||
if response.VersionId != nil {
|
||||
w.Header().Set("x-amz-version-id", *response.VersionId)
|
||||
}
|
||||
// The object's encryption comes from its entry, never from this request's headers.
|
||||
s3a.setSSEResponseHeaders(w, &http.Request{Header: http.Header{}}, response.SSE)
|
||||
// A retry answered from the completion that already committed has its
|
||||
// event behind it, as AWS sends one per completion that created the object.
|
||||
if commit.committed() {
|
||||
|
||||
Reference in new issue
Block a user