2022-08-01 16:52:09 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
2022-11-09 10:07:18 +00:00
|
|
|
"crypto/tls"
|
2022-08-01 16:52:09 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2023-06-21 14:16:40 +00:00
|
|
|
"net/http/httptest"
|
2022-08-01 16:52:09 +00:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
2024-03-19 13:56:32 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2024-09-27 08:14:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/frostfs"
|
2022-08-01 16:52:09 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
aes256Key = "MTIzNDU2Nzg5MHF3ZXJ0eXVpb3Bhc2RmZ2hqa2x6eGM="
|
|
|
|
aes256KeyMD5 = "NtkH/y2maPit+yUkhq4Q7A=="
|
|
|
|
partNumberQuery = "partNumber"
|
|
|
|
uploadIDQuery = "uploadId"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSimpleGetEncrypted(t *testing.T) {
|
|
|
|
tc := prepareHandlerContext(t)
|
|
|
|
|
|
|
|
bktName, objName := "bucket-for-sse-c", "object-to-encrypt"
|
2022-10-04 08:31:09 +00:00
|
|
|
bktInfo := createTestBucket(tc, bktName)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
content := "content"
|
|
|
|
putEncryptedObject(t, tc, bktName, objName, content)
|
|
|
|
|
|
|
|
objInfo, err := tc.Layer().GetObjectInfo(tc.Context(), &layer.HeadObjectParams{BktInfo: bktInfo, Object: objName})
|
|
|
|
require.NoError(t, err)
|
2024-09-27 08:14:45 +00:00
|
|
|
obj, err := tc.MockedPool().GetObject(tc.Context(), frostfs.PrmObjectGet{Container: bktInfo.CID, Object: objInfo.ID})
|
2022-08-01 16:52:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
encryptedContent, err := io.ReadAll(obj.Payload)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, content, string(encryptedContent))
|
|
|
|
|
2023-06-27 12:49:20 +00:00
|
|
|
response, _ := getEncryptedObject(tc, bktName, objName)
|
2022-08-01 16:52:09 +00:00
|
|
|
require.Equal(t, content, string(response))
|
2024-11-15 10:20:18 +00:00
|
|
|
|
|
|
|
result := listVersions(t, tc, bktName)
|
|
|
|
require.Len(t, result.Version, 1)
|
|
|
|
require.Equal(t, uint64(len(content)), result.Version[0].Size)
|
2022-08-01 16:52:09 +00:00
|
|
|
}
|
|
|
|
|
2024-11-11 11:22:58 +00:00
|
|
|
func TestMD5HeaderBadOrEmpty(t *testing.T) {
|
|
|
|
tc := prepareHandlerContext(t)
|
|
|
|
|
|
|
|
bktName, objName := "bucket-for-sse-c", "object-to-encrypt"
|
|
|
|
createTestBucket(tc, bktName)
|
|
|
|
content := "content"
|
|
|
|
|
|
|
|
headers := map[string]string{
|
|
|
|
api.ContentMD5: "",
|
|
|
|
}
|
|
|
|
putEncryptedObjectWithHeadersErr(t, tc, bktName, objName, content, headers, errors.ErrInvalidDigest)
|
|
|
|
|
|
|
|
headers = map[string]string{
|
|
|
|
api.ContentMD5: "YWJjMTIzIT8kKiYoKSctPUB+",
|
|
|
|
}
|
|
|
|
|
|
|
|
putEncryptedObjectWithHeadersErr(t, tc, bktName, objName, content, headers, errors.ErrBadDigest)
|
|
|
|
}
|
|
|
|
|
2022-08-01 16:52:09 +00:00
|
|
|
func TestGetEncryptedRange(t *testing.T) {
|
|
|
|
tc := prepareHandlerContext(t)
|
|
|
|
|
|
|
|
bktName, objName := "bucket-for-sse-c", "object-to-encrypt"
|
2022-10-04 08:31:09 +00:00
|
|
|
createTestBucket(tc, bktName)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
var sb strings.Builder
|
|
|
|
for i := 0; i < 1<<16+11; i++ {
|
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
sb.Write([]byte("b"))
|
|
|
|
case 1<<16 - 2:
|
|
|
|
sb.Write([]byte("c"))
|
|
|
|
case 1<<16 - 1:
|
|
|
|
sb.Write([]byte("d"))
|
|
|
|
case 1 << 16:
|
|
|
|
sb.Write([]byte("e"))
|
|
|
|
case 1<<16 + 1:
|
|
|
|
sb.Write([]byte("f"))
|
|
|
|
case 1<<16 + 10:
|
|
|
|
sb.Write([]byte("g"))
|
|
|
|
default:
|
|
|
|
sb.Write([]byte("a"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
content := sb.String()
|
|
|
|
putEncryptedObject(t, tc, bktName, objName, content)
|
|
|
|
|
|
|
|
full := getEncryptedObjectRange(t, tc, bktName, objName, 0, sb.Len()-1)
|
|
|
|
require.Equalf(t, content, string(full), "expected len: %d, actual len: %d", len(content), len(full))
|
|
|
|
|
|
|
|
beginning := getEncryptedObjectRange(t, tc, bktName, objName, 0, 3)
|
|
|
|
require.Equal(t, content[:4], string(beginning))
|
|
|
|
|
|
|
|
middle := getEncryptedObjectRange(t, tc, bktName, objName, 1<<16-3, 1<<16+2)
|
|
|
|
require.Equal(t, "acdefa", string(middle))
|
|
|
|
|
|
|
|
end := getEncryptedObjectRange(t, tc, bktName, objName, 1<<16+2, len(content)-1)
|
|
|
|
require.Equal(t, "aaaaaaaag", string(end))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestS3EncryptionSSECMultipartUpload(t *testing.T) {
|
|
|
|
tc := prepareHandlerContext(t)
|
|
|
|
bktName, objName := "bucket-for-sse-c-multipart-s3-tests", "multipart_enc"
|
2022-10-04 08:31:09 +00:00
|
|
|
createTestBucket(tc, bktName)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
objLen := 30 * 1024 * 1024
|
|
|
|
partSize := objLen / 6
|
|
|
|
headerMetaKey := api.MetadataPrefix + "foo"
|
|
|
|
headers := map[string]string{
|
|
|
|
headerMetaKey: "bar",
|
|
|
|
api.ContentType: "text/plain",
|
|
|
|
}
|
|
|
|
|
2022-10-25 10:16:08 +00:00
|
|
|
data := multipartUploadEncrypted(tc, bktName, objName, headers, objLen, partSize)
|
2022-08-01 16:52:09 +00:00
|
|
|
require.Equal(t, objLen, len(data))
|
|
|
|
|
2023-06-27 12:49:20 +00:00
|
|
|
resData, resHeader := getEncryptedObject(tc, bktName, objName)
|
2022-08-01 16:52:09 +00:00
|
|
|
equalDataSlices(t, data, resData)
|
|
|
|
require.Equal(t, headers[api.ContentType], resHeader.Get(api.ContentType))
|
|
|
|
require.Equal(t, headers[headerMetaKey], resHeader[headerMetaKey][0])
|
|
|
|
require.Equal(t, strconv.Itoa(objLen), resHeader.Get(api.ContentLength))
|
|
|
|
|
2023-06-27 12:49:20 +00:00
|
|
|
checkContentUsingRangeEnc(tc, bktName, objName, data, 1000000)
|
|
|
|
checkContentUsingRangeEnc(tc, bktName, objName, data, 10000000)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipartUploadGetRange(t *testing.T) {
|
|
|
|
hc := prepareHandlerContext(t)
|
|
|
|
bktName, objName := "bucket-for-multipart-s3-tests", "multipart_obj"
|
|
|
|
createTestBucket(hc, bktName)
|
|
|
|
|
|
|
|
objLen := 30 * 1024 * 1024
|
|
|
|
partSize := objLen / 6
|
|
|
|
headerMetaKey := api.MetadataPrefix + "foo"
|
|
|
|
headers := map[string]string{
|
|
|
|
headerMetaKey: "bar",
|
|
|
|
api.ContentType: "text/plain",
|
|
|
|
}
|
|
|
|
|
|
|
|
data := multipartUpload(hc, bktName, objName, headers, objLen, partSize)
|
|
|
|
require.Equal(t, objLen, len(data))
|
|
|
|
|
|
|
|
resData, resHeader := getObject(hc, bktName, objName)
|
|
|
|
equalDataSlices(t, data, resData)
|
|
|
|
require.Equal(t, headers[api.ContentType], resHeader.Get(api.ContentType))
|
|
|
|
require.Equal(t, headers[headerMetaKey], resHeader[headerMetaKey][0])
|
|
|
|
require.Equal(t, strconv.Itoa(objLen), resHeader.Get(api.ContentLength))
|
|
|
|
|
|
|
|
checkContentUsingRange(hc, bktName, objName, data, 1000000)
|
|
|
|
checkContentUsingRange(hc, bktName, objName, data, 10000000)
|
2022-08-01 16:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func equalDataSlices(t *testing.T, expected, actual []byte) {
|
|
|
|
require.Equal(t, len(expected), len(actual), "sizes don't match")
|
|
|
|
|
|
|
|
if bytes.Equal(expected, actual) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(expected); i++ {
|
|
|
|
if expected[i] != actual[i] {
|
|
|
|
require.Equalf(t, expected[i], actual[i], "differ start with '%d' position, length: %d", i, len(expected))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-27 12:49:20 +00:00
|
|
|
func checkContentUsingRangeEnc(hc *handlerContext, bktName, objName string, data []byte, step int) {
|
|
|
|
checkContentUsingRangeBase(hc, bktName, objName, data, step, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkContentUsingRange(hc *handlerContext, bktName, objName string, data []byte, step int) {
|
|
|
|
checkContentUsingRangeBase(hc, bktName, objName, data, step, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkContentUsingRangeBase(hc *handlerContext, bktName, objName string, data []byte, step int, encrypted bool) {
|
2022-08-01 16:52:09 +00:00
|
|
|
var off, toRead, end int
|
|
|
|
|
|
|
|
for off < len(data) {
|
|
|
|
toRead = len(data) - off
|
|
|
|
if toRead > step {
|
|
|
|
toRead = step
|
|
|
|
}
|
|
|
|
end = off + toRead - 1
|
|
|
|
|
2023-06-27 12:49:20 +00:00
|
|
|
var rangeData []byte
|
|
|
|
if encrypted {
|
|
|
|
rangeData = getEncryptedObjectRange(hc.t, hc, bktName, objName, off, end)
|
|
|
|
} else {
|
|
|
|
rangeData = getObjectRange(hc.t, hc, bktName, objName, off, end)
|
|
|
|
}
|
|
|
|
|
|
|
|
equalDataSlices(hc.t, data[off:end+1], rangeData)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
off += step
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 10:16:08 +00:00
|
|
|
func multipartUploadEncrypted(hc *handlerContext, bktName, objName string, headers map[string]string, objLen, partsSize int) (objData []byte) {
|
|
|
|
multipartInfo := createMultipartUploadEncrypted(hc, bktName, objName, headers)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
var sum, currentPart int
|
|
|
|
var etags []string
|
|
|
|
adjustedSize := partsSize
|
|
|
|
|
|
|
|
for sum < objLen {
|
|
|
|
currentPart++
|
|
|
|
|
|
|
|
sum += partsSize
|
|
|
|
if sum > objLen {
|
|
|
|
adjustedSize = objLen - sum
|
|
|
|
}
|
|
|
|
|
2022-10-25 10:16:08 +00:00
|
|
|
etag, data := uploadPartEncrypted(hc, bktName, objName, multipartInfo.UploadID, currentPart, adjustedSize)
|
2022-08-01 16:52:09 +00:00
|
|
|
etags = append(etags, etag)
|
|
|
|
objData = append(objData, data...)
|
|
|
|
}
|
|
|
|
|
2022-10-25 10:16:08 +00:00
|
|
|
completeMultipartUpload(hc, bktName, objName, multipartInfo.UploadID, etags)
|
2022-08-01 16:52:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-27 12:49:20 +00:00
|
|
|
func multipartUpload(hc *handlerContext, bktName, objName string, headers map[string]string, objLen, partsSize int) (objData []byte) {
|
|
|
|
multipartInfo := createMultipartUpload(hc, bktName, objName, headers)
|
|
|
|
|
|
|
|
var sum, currentPart int
|
|
|
|
var etags []string
|
|
|
|
adjustedSize := partsSize
|
|
|
|
|
|
|
|
for sum < objLen {
|
|
|
|
currentPart++
|
|
|
|
|
|
|
|
sum += partsSize
|
|
|
|
if sum > objLen {
|
|
|
|
adjustedSize = objLen - sum
|
|
|
|
}
|
|
|
|
|
|
|
|
etag, data := uploadPart(hc, bktName, objName, multipartInfo.UploadID, currentPart, adjustedSize)
|
|
|
|
etags = append(etags, etag)
|
|
|
|
objData = append(objData, data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
completeMultipartUpload(hc, bktName, objName, multipartInfo.UploadID, etags)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-25 10:16:08 +00:00
|
|
|
func createMultipartUploadEncrypted(hc *handlerContext, bktName, objName string, headers map[string]string) *InitiateMultipartUploadResponse {
|
2024-03-19 13:56:32 +00:00
|
|
|
return createMultipartUploadOkBase(hc, bktName, objName, true, headers)
|
2022-10-25 10:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createMultipartUpload(hc *handlerContext, bktName, objName string, headers map[string]string) *InitiateMultipartUploadResponse {
|
2024-03-19 13:56:32 +00:00
|
|
|
return createMultipartUploadOkBase(hc, bktName, objName, false, headers)
|
2022-10-25 10:16:08 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 13:56:32 +00:00
|
|
|
func createMultipartUploadOkBase(hc *handlerContext, bktName, objName string, encrypted bool, headers map[string]string) *InitiateMultipartUploadResponse {
|
|
|
|
w := createMultipartUploadBase(hc, bktName, objName, encrypted, headers)
|
|
|
|
multipartInitInfo := &InitiateMultipartUploadResponse{}
|
|
|
|
readResponse(hc.t, w, http.StatusOK, multipartInitInfo)
|
|
|
|
return multipartInitInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func createMultipartUploadAssertS3Error(hc *handlerContext, bktName, objName string, headers map[string]string, code errors.ErrorCode) {
|
|
|
|
w := createMultipartUploadBase(hc, bktName, objName, false, headers)
|
|
|
|
assertS3Error(hc.t, w, errors.GetAPIError(code))
|
|
|
|
}
|
|
|
|
|
|
|
|
func createMultipartUploadBase(hc *handlerContext, bktName, objName string, encrypted bool, headers map[string]string) *httptest.ResponseRecorder {
|
2022-10-25 10:16:08 +00:00
|
|
|
w, r := prepareTestRequest(hc, bktName, objName, nil)
|
|
|
|
if encrypted {
|
|
|
|
setEncryptHeaders(r)
|
|
|
|
}
|
2022-08-01 16:52:09 +00:00
|
|
|
setHeaders(r, headers)
|
2022-10-25 10:16:08 +00:00
|
|
|
hc.Handler().CreateMultipartUploadHandler(w, r)
|
2024-03-19 13:56:32 +00:00
|
|
|
return w
|
2022-08-01 16:52:09 +00:00
|
|
|
}
|
2022-10-25 10:16:08 +00:00
|
|
|
|
|
|
|
func completeMultipartUpload(hc *handlerContext, bktName, objName, uploadID string, partsETags []string) {
|
2023-06-21 14:16:40 +00:00
|
|
|
w := completeMultipartUploadBase(hc, bktName, objName, uploadID, partsETags)
|
|
|
|
assertStatus(hc.t, w, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func completeMultipartUploadBase(hc *handlerContext, bktName, objName, uploadID string, partsETags []string) *httptest.ResponseRecorder {
|
2022-08-01 16:52:09 +00:00
|
|
|
query := make(url.Values)
|
|
|
|
query.Set(uploadIDQuery, uploadID)
|
|
|
|
complete := &CompleteMultipartUpload{
|
|
|
|
Parts: []*layer.CompletedPart{},
|
|
|
|
}
|
|
|
|
for i, tag := range partsETags {
|
|
|
|
complete.Parts = append(complete.Parts, &layer.CompletedPart{
|
|
|
|
ETag: tag,
|
|
|
|
PartNumber: i + 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-25 10:16:08 +00:00
|
|
|
w, r := prepareTestFullRequest(hc, bktName, objName, query, complete)
|
|
|
|
hc.Handler().CompleteMultipartUploadHandler(w, r)
|
2023-06-21 14:16:40 +00:00
|
|
|
|
|
|
|
return w
|
2022-08-01 16:52:09 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 13:40:55 +00:00
|
|
|
func abortMultipartUpload(hc *handlerContext, bktName, objName, uploadID string) {
|
|
|
|
w := abortMultipartUploadBase(hc, bktName, objName, uploadID)
|
|
|
|
assertStatus(hc.t, w, http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func abortMultipartUploadBase(hc *handlerContext, bktName, objName, uploadID string) *httptest.ResponseRecorder {
|
|
|
|
query := make(url.Values)
|
|
|
|
query.Set(uploadIDQuery, uploadID)
|
|
|
|
|
|
|
|
w, r := prepareTestFullRequest(hc, bktName, objName, query, nil)
|
|
|
|
hc.Handler().AbortMultipartUploadHandler(w, r)
|
|
|
|
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2022-10-25 10:16:08 +00:00
|
|
|
func uploadPartEncrypted(hc *handlerContext, bktName, objName, uploadID string, num, size int) (string, []byte) {
|
|
|
|
return uploadPartBase(hc, bktName, objName, true, uploadID, num, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func uploadPart(hc *handlerContext, bktName, objName, uploadID string, num, size int) (string, []byte) {
|
|
|
|
return uploadPartBase(hc, bktName, objName, false, uploadID, num, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func uploadPartBase(hc *handlerContext, bktName, objName string, encrypted bool, uploadID string, num, size int) (string, []byte) {
|
2022-08-01 16:52:09 +00:00
|
|
|
partBody := make([]byte, size)
|
|
|
|
_, err := rand.Read(partBody)
|
2022-10-25 10:16:08 +00:00
|
|
|
require.NoError(hc.t, err)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
query := make(url.Values)
|
|
|
|
query.Set(uploadIDQuery, uploadID)
|
|
|
|
query.Set(partNumberQuery, strconv.Itoa(num))
|
|
|
|
|
2022-10-25 10:16:08 +00:00
|
|
|
w, r := prepareTestRequestWithQuery(hc, bktName, objName, query, partBody)
|
|
|
|
if encrypted {
|
|
|
|
setEncryptHeaders(r)
|
|
|
|
}
|
|
|
|
hc.Handler().UploadPartHandler(w, r)
|
|
|
|
assertStatus(hc.t, w, http.StatusOK)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
return w.Header().Get(api.ETag), partBody
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipartEncrypted(t *testing.T) {
|
|
|
|
partSize := 5*1048576 + 1<<16 - 5 // 5MB (min part size) + 64kb (cipher block size) - 5 (to check corner range)
|
|
|
|
|
2022-10-25 10:16:08 +00:00
|
|
|
hc := prepareHandlerContext(t)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
bktName, objName := "bucket-for-sse-c-multipart", "object-to-encrypt-multipart"
|
2022-10-25 10:16:08 +00:00
|
|
|
createTestBucket(hc, bktName)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
2022-10-25 10:16:08 +00:00
|
|
|
multipartInitInfo := createMultipartUploadEncrypted(hc, bktName, objName, map[string]string{})
|
|
|
|
part1ETag, part1 := uploadPartEncrypted(hc, bktName, objName, multipartInitInfo.UploadID, 1, partSize)
|
|
|
|
part2ETag, part2 := uploadPartEncrypted(hc, bktName, objName, multipartInitInfo.UploadID, 2, 5)
|
|
|
|
completeMultipartUpload(hc, bktName, objName, multipartInitInfo.UploadID, []string{part1ETag, part2ETag})
|
2022-08-01 16:52:09 +00:00
|
|
|
|
2023-06-27 12:49:20 +00:00
|
|
|
res, _ := getEncryptedObject(hc, bktName, objName)
|
2022-08-01 16:52:09 +00:00
|
|
|
require.Equal(t, len(part1)+len(part2), len(res))
|
|
|
|
require.Equal(t, append(part1, part2...), res)
|
|
|
|
|
2022-10-25 10:16:08 +00:00
|
|
|
part2Range := getEncryptedObjectRange(t, hc, bktName, objName, len(part1), len(part1)+len(part2)-1)
|
2022-08-01 16:52:09 +00:00
|
|
|
require.Equal(t, part2[0:], part2Range)
|
2024-11-15 10:20:18 +00:00
|
|
|
|
|
|
|
result := listVersions(t, hc, bktName)
|
|
|
|
require.Len(t, result.Version, 1)
|
|
|
|
require.EqualValues(t, uint64(partSize+5), result.Version[0].Size)
|
2022-08-01 16:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func putEncryptedObject(t *testing.T, tc *handlerContext, bktName, objName, content string) {
|
|
|
|
body := bytes.NewReader([]byte(content))
|
2022-10-04 08:31:09 +00:00
|
|
|
w, r := prepareTestPayloadRequest(tc, bktName, objName, body)
|
2022-08-01 16:52:09 +00:00
|
|
|
setEncryptHeaders(r)
|
|
|
|
tc.Handler().PutObjectHandler(w, r)
|
|
|
|
assertStatus(t, w, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2024-11-11 11:22:58 +00:00
|
|
|
func putEncryptedObjectWithHeadersErr(t *testing.T, tc *handlerContext, bktName, objName, content string, headers map[string]string, code errors.ErrorCode) {
|
|
|
|
body := bytes.NewReader([]byte(content))
|
|
|
|
w, r := prepareTestPayloadRequest(tc, bktName, objName, body)
|
|
|
|
setHeaders(r, headers)
|
|
|
|
|
|
|
|
tc.Handler().PutObjectHandler(w, r)
|
|
|
|
assertS3Error(t, w, errors.GetAPIError(code))
|
|
|
|
}
|
|
|
|
|
2023-06-27 12:49:20 +00:00
|
|
|
func getEncryptedObject(hc *handlerContext, bktName, objName string) ([]byte, http.Header) {
|
|
|
|
w, r := prepareTestRequest(hc, bktName, objName, nil)
|
2022-08-01 16:52:09 +00:00
|
|
|
setEncryptHeaders(r)
|
2023-06-27 12:49:20 +00:00
|
|
|
return getObjectBase(hc, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getObject(hc *handlerContext, bktName, objName string) ([]byte, http.Header) {
|
|
|
|
w, r := prepareTestRequest(hc, bktName, objName, nil)
|
|
|
|
return getObjectBase(hc, w, r)
|
|
|
|
}
|
|
|
|
|
2024-11-11 13:23:17 +00:00
|
|
|
func getObjectWithHeaders(hc *handlerContext, bktName, objName string, headers map[string]string) *httptest.ResponseRecorder {
|
|
|
|
w, r := prepareTestRequest(hc, bktName, objName, nil)
|
|
|
|
for k, v := range headers {
|
|
|
|
r.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
hc.Handler().GetObjectHandler(w, r)
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2023-06-27 12:49:20 +00:00
|
|
|
func getObjectBase(hc *handlerContext, w *httptest.ResponseRecorder, r *http.Request) ([]byte, http.Header) {
|
|
|
|
hc.Handler().GetObjectHandler(w, r)
|
|
|
|
assertStatus(hc.t, w, http.StatusOK)
|
2022-08-01 16:52:09 +00:00
|
|
|
content, err := io.ReadAll(w.Result().Body)
|
2023-06-27 12:49:20 +00:00
|
|
|
require.NoError(hc.t, err)
|
2022-08-01 16:52:09 +00:00
|
|
|
return content, w.Header()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEncryptedObjectRange(t *testing.T, tc *handlerContext, bktName, objName string, start, end int) []byte {
|
2022-10-04 08:31:09 +00:00
|
|
|
w, r := prepareTestRequest(tc, bktName, objName, nil)
|
2022-08-01 16:52:09 +00:00
|
|
|
setEncryptHeaders(r)
|
|
|
|
r.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end))
|
|
|
|
tc.Handler().GetObjectHandler(w, r)
|
|
|
|
assertStatus(t, w, http.StatusPartialContent)
|
|
|
|
content, err := io.ReadAll(w.Result().Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
|
|
|
func setEncryptHeaders(r *http.Request) {
|
2022-11-09 10:07:18 +00:00
|
|
|
r.TLS = &tls.ConnectionState{}
|
2022-08-01 16:52:09 +00:00
|
|
|
r.Header.Set(api.AmzServerSideEncryptionCustomerAlgorithm, layer.AESEncryptionAlgorithm)
|
|
|
|
r.Header.Set(api.AmzServerSideEncryptionCustomerKey, aes256Key)
|
|
|
|
r.Header.Set(api.AmzServerSideEncryptionCustomerKeyMD5, aes256KeyMD5)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setHeaders(r *http.Request, header map[string]string) {
|
|
|
|
for key, val := range header {
|
|
|
|
r.Header.Set(key, val)
|
|
|
|
}
|
|
|
|
}
|