From 27c8a321c7e932834db8034475858ec6190b666b Mon Sep 17 00:00:00 2001 From: Barak Amar Date: Wed, 6 Sep 2023 17:27:50 +0300 Subject: [PATCH] move sse header extract function to a different file --- pkg/block/s3/adapter.go | 71 ------------------------------- pkg/block/s3/extract_sse.go | 84 +++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 71 deletions(-) create mode 100644 pkg/block/s3/extract_sse.go diff --git a/pkg/block/s3/adapter.go b/pkg/block/s3/adapter.go index f5a0e746528..c26a8c4ed75 100644 --- a/pkg/block/s3/adapter.go +++ b/pkg/block/s3/adapter.go @@ -526,77 +526,6 @@ func (a *Adapter) copyPart(ctx context.Context, sourceObj, destinationObj block. }, nil } -// extractAmzServerSideHeaders extracts the x-amz-server-side-* headers from the response -// TODO(barak): verify that this is the correct way to extract the headers -func extractSSHeaderUploadPartCopy(resp *s3.UploadPartCopyOutput) http.Header { - // x-amz-server-side-* headers - headers := make(http.Header) - if resp.SSECustomerAlgorithm != nil { - headers.Set("X-Amz-Server-Side-Encryption-Customer-Algorithm", *resp.SSECustomerAlgorithm) - } - if resp.SSECustomerKeyMD5 != nil { - headers.Set("X-Amz-Server-Side-Encryption-Customer-Key-Md5", *resp.SSECustomerKeyMD5) - } - if resp.SSEKMSKeyId != nil { - headers.Set("X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id", *resp.SSEKMSKeyId) - } - if resp.ServerSideEncryption != "" { - headers.Set("X-Amz-Server-Side-Encryption", string(resp.ServerSideEncryption)) - } - return headers -} - -func extractSSHeaderCreateMultipartUpload(resp *s3.CreateMultipartUploadOutput) http.Header { - // x-amz-server-side-* headers - headers := make(http.Header) - if resp.SSECustomerAlgorithm != nil { - headers.Set("X-Amz-Server-Side-Encryption-Customer-Algorithm", *resp.SSECustomerAlgorithm) - } - if resp.SSECustomerKeyMD5 != nil { - headers.Set("X-Amz-Server-Side-Encryption-Customer-Key-Md5", *resp.SSECustomerKeyMD5) - } - if resp.SSEKMSKeyId != nil { - headers.Set("X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id", *resp.SSEKMSKeyId) - } - if resp.ServerSideEncryption != "" { - headers.Set("X-Amz-Server-Side-Encryption", string(resp.ServerSideEncryption)) - } - if resp.SSEKMSEncryptionContext != nil { - headers.Set("X-Amz-Server-Side-Encryption-Context", *resp.SSEKMSEncryptionContext) - } - return headers -} - -func extractSSHeaderCompleteMultipartUpload(resp *s3.CompleteMultipartUploadOutput) http.Header { - // x-amz-server-side-* headers - headers := make(http.Header) - if resp.SSEKMSKeyId != nil { - headers.Set("X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id", *resp.SSEKMSKeyId) - } - if resp.ServerSideEncryption != "" { - headers.Set("X-Amz-Server-Side-Encryption", string(resp.ServerSideEncryption)) - } - return headers -} - -func extractSSHeaderUploadPart(resp *s3.UploadPartOutput) http.Header { - // x-amz-server-side-* headers - headers := make(http.Header) - if resp.SSECustomerAlgorithm != nil { - headers.Set("X-Amz-Server-Side-Encryption-Customer-Algorithm", *resp.SSECustomerAlgorithm) - } - if resp.SSECustomerKeyMD5 != nil { - headers.Set("X-Amz-Server-Side-Encryption-Customer-Key-Md5", *resp.SSECustomerKeyMD5) - } - if resp.SSEKMSKeyId != nil { - headers.Set("X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id", *resp.SSEKMSKeyId) - } - if resp.ServerSideEncryption != "" { - headers.Set("X-Amz-Server-Side-Encryption", string(resp.ServerSideEncryption)) - } - return headers -} - func (a *Adapter) UploadCopyPart(ctx context.Context, sourceObj, destinationObj block.ObjectPointer, uploadID string, partNumber int) (*block.UploadPartResponse, error) { var err error defer reportMetrics("UploadCopyPart", time.Now(), nil, &err) diff --git a/pkg/block/s3/extract_sse.go b/pkg/block/s3/extract_sse.go new file mode 100644 index 00000000000..dc0169dc504 --- /dev/null +++ b/pkg/block/s3/extract_sse.go @@ -0,0 +1,84 @@ +package s3 + +import ( + "net/http" + + "github.com/aws/aws-sdk-go-v2/service/s3" +) + +// extractSSHeaderUploadPart extracts the x-amz-server-side-* headers from the given +// UploadPartOutput response. +func extractSSHeaderUploadPart(resp *s3.UploadPartOutput) http.Header { + // x-amz-server-side-* headers + headers := make(http.Header) + if resp.SSECustomerAlgorithm != nil { + headers.Set("X-Amz-Server-Side-Encryption-Customer-Algorithm", *resp.SSECustomerAlgorithm) + } + if resp.SSECustomerKeyMD5 != nil { + headers.Set("X-Amz-Server-Side-Encryption-Customer-Key-Md5", *resp.SSECustomerKeyMD5) + } + if resp.SSEKMSKeyId != nil { + headers.Set("X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id", *resp.SSEKMSKeyId) + } + if resp.ServerSideEncryption != "" { + headers.Set("X-Amz-Server-Side-Encryption", string(resp.ServerSideEncryption)) + } + return headers +} + +// extractSSHeaderUploadPartCopy extracts the x-amz-server-side-* headers from the given +// UploadPartCopyOutput response. +func extractSSHeaderUploadPartCopy(resp *s3.UploadPartCopyOutput) http.Header { + // x-amz-server-side-* headers + headers := make(http.Header) + if resp.SSECustomerAlgorithm != nil { + headers.Set("X-Amz-Server-Side-Encryption-Customer-Algorithm", *resp.SSECustomerAlgorithm) + } + if resp.SSECustomerKeyMD5 != nil { + headers.Set("X-Amz-Server-Side-Encryption-Customer-Key-Md5", *resp.SSECustomerKeyMD5) + } + if resp.SSEKMSKeyId != nil { + headers.Set("X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id", *resp.SSEKMSKeyId) + } + if resp.ServerSideEncryption != "" { + headers.Set("X-Amz-Server-Side-Encryption", string(resp.ServerSideEncryption)) + } + return headers +} + +// extractSSHeaderCreateMultipartUpload extracts the x-amz-server-side-* headers from the given +// CreateMultipartUploadOutput response. +func extractSSHeaderCreateMultipartUpload(resp *s3.CreateMultipartUploadOutput) http.Header { + // x-amz-server-side-* headers + headers := make(http.Header) + if resp.SSECustomerAlgorithm != nil { + headers.Set("X-Amz-Server-Side-Encryption-Customer-Algorithm", *resp.SSECustomerAlgorithm) + } + if resp.SSECustomerKeyMD5 != nil { + headers.Set("X-Amz-Server-Side-Encryption-Customer-Key-Md5", *resp.SSECustomerKeyMD5) + } + if resp.SSEKMSKeyId != nil { + headers.Set("X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id", *resp.SSEKMSKeyId) + } + if resp.ServerSideEncryption != "" { + headers.Set("X-Amz-Server-Side-Encryption", string(resp.ServerSideEncryption)) + } + if resp.SSEKMSEncryptionContext != nil { + headers.Set("X-Amz-Server-Side-Encryption-Context", *resp.SSEKMSEncryptionContext) + } + return headers +} + +// extractSSHeaderCompleteMultipartUpload extracts the x-amz-server-side-* headers from the given +// CompleteMultipartUploadOutput response. +func extractSSHeaderCompleteMultipartUpload(resp *s3.CompleteMultipartUploadOutput) http.Header { + // x-amz-server-side-* headers + headers := make(http.Header) + if resp.SSEKMSKeyId != nil { + headers.Set("X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id", *resp.SSEKMSKeyId) + } + if resp.ServerSideEncryption != "" { + headers.Set("X-Amz-Server-Side-Encryption", string(resp.ServerSideEncryption)) + } + return headers +}