2020-07-03 13:52:04 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
2023-10-02 08:52:07 +00:00
|
|
|
"bytes"
|
2020-07-03 13:52:04 +00:00
|
|
|
"context"
|
2023-10-02 08:52:07 +00:00
|
|
|
"crypto/md5"
|
2022-05-19 14:56:42 +00:00
|
|
|
"crypto/sha256"
|
2023-10-02 08:52:07 +00:00
|
|
|
"encoding/base64"
|
2022-05-12 16:58:11 +00:00
|
|
|
"encoding/hex"
|
2023-06-27 12:49:20 +00:00
|
|
|
"encoding/json"
|
2022-02-08 16:54:04 +00:00
|
|
|
"errors"
|
2022-03-01 15:07:15 +00:00
|
|
|
"fmt"
|
2020-07-03 13:52:04 +00:00
|
|
|
"io"
|
2022-06-29 10:51:05 +00:00
|
|
|
"mime"
|
2024-08-01 08:21:20 +00:00
|
|
|
"net/http"
|
2022-06-29 10:51:05 +00:00
|
|
|
"path/filepath"
|
2022-08-01 16:52:09 +00:00
|
|
|
"strconv"
|
2021-08-10 10:03:09 +00:00
|
|
|
"strings"
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
2023-11-13 08:01:47 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/auth"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
2024-09-27 09:18:41 +00:00
|
|
|
apierr "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
2024-09-27 08:14:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/frostfs"
|
2024-09-27 08:13:13 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/tree"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2024-08-01 08:21:20 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/pkg/detector"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-08-01 16:52:09 +00:00
|
|
|
"github.com/minio/sio"
|
2021-07-18 13:40:19 +00:00
|
|
|
"go.uber.org/zap"
|
2020-07-03 13:52:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2020-10-19 01:04:37 +00:00
|
|
|
getParams struct {
|
2022-02-08 16:54:04 +00:00
|
|
|
// payload range
|
|
|
|
off, ln uint64
|
|
|
|
|
2023-06-27 12:49:20 +00:00
|
|
|
objInfo *data.ObjectInfo
|
|
|
|
bktInfo *data.BucketInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
getFrostFSParams struct {
|
|
|
|
// payload range
|
|
|
|
off, ln uint64
|
|
|
|
|
2022-05-31 12:38:06 +00:00
|
|
|
oid oid.ID
|
2022-06-02 16:56:04 +00:00
|
|
|
bktInfo *data.BucketInfo
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
2021-07-18 13:40:19 +00:00
|
|
|
|
2023-10-17 11:21:39 +00:00
|
|
|
DeleteMarkerError struct {
|
2024-09-27 09:18:41 +00:00
|
|
|
ErrorCode apierr.ErrorCode
|
2023-10-17 11:21:39 +00:00
|
|
|
}
|
2020-07-03 13:52:04 +00:00
|
|
|
)
|
|
|
|
|
2023-10-17 11:21:39 +00:00
|
|
|
func (e DeleteMarkerError) Error() string {
|
|
|
|
return "object is delete marker"
|
|
|
|
}
|
|
|
|
|
2022-06-10 11:57:41 +00:00
|
|
|
const (
|
|
|
|
continuationToken = "<continuation-token>"
|
|
|
|
)
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
func newAddress(cnr cid.ID, obj oid.ID) oid.Address {
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnr)
|
|
|
|
addr.SetObject(obj)
|
2022-02-08 16:54:04 +00:00
|
|
|
return addr
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// objectHead returns all object's headers.
|
2024-06-25 12:57:55 +00:00
|
|
|
func (n *Layer) objectHead(ctx context.Context, bktInfo *data.BucketInfo, idObj oid.ID) (*object.Object, error) {
|
2024-09-27 08:14:45 +00:00
|
|
|
prm := frostfs.PrmObjectHead{
|
2024-07-15 12:47:19 +00:00
|
|
|
Container: bktInfo.CID,
|
|
|
|
Object: idObj,
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 17:35:20 +00:00
|
|
|
n.prepareAuthParameters(ctx, &prm.PrmAuth, bktInfo.Owner)
|
2022-02-08 16:54:04 +00:00
|
|
|
|
2024-07-15 12:47:19 +00:00
|
|
|
return n.frostFS.HeadObject(ctx, prm)
|
2020-08-05 13:47:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 12:57:55 +00:00
|
|
|
func (n *Layer) initObjectPayloadReader(ctx context.Context, p getParams) (io.Reader, error) {
|
2023-06-27 12:49:20 +00:00
|
|
|
if _, isCombined := p.objInfo.Headers[MultipartObjectSize]; !isCombined {
|
|
|
|
return n.initFrostFSObjectPayloadReader(ctx, getFrostFSParams{
|
|
|
|
off: p.off,
|
|
|
|
ln: p.ln,
|
|
|
|
oid: p.objInfo.ID,
|
|
|
|
bktInfo: p.bktInfo,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
combinedObj, err := n.objectGet(ctx, p.bktInfo, p.objInfo.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("get combined object '%s': %w", p.objInfo.ID.EncodeToString(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var parts []*data.PartInfo
|
2024-07-15 12:47:19 +00:00
|
|
|
if err = json.NewDecoder(combinedObj.Payload).Decode(&parts); err != nil {
|
2023-06-27 12:49:20 +00:00
|
|
|
return nil, fmt.Errorf("unmarshal combined object parts: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
isEncrypted := FormEncryptionInfo(p.objInfo.Headers).Enabled
|
|
|
|
objParts := make([]partObj, len(parts))
|
|
|
|
for i, part := range parts {
|
|
|
|
size := part.Size
|
|
|
|
if isEncrypted {
|
|
|
|
if size, err = sio.EncryptedSize(part.Size); err != nil {
|
|
|
|
return nil, fmt.Errorf("compute encrypted size: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
objParts[i] = partObj{
|
|
|
|
OID: part.OID,
|
|
|
|
Size: size,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 11:48:33 +00:00
|
|
|
return newMultiObjectReader(ctx, multiObjectReaderConfig{
|
|
|
|
layer: n,
|
|
|
|
off: p.off,
|
|
|
|
ln: p.ln,
|
|
|
|
parts: objParts,
|
|
|
|
bktInfo: p.bktInfo,
|
|
|
|
})
|
2023-06-27 12:49:20 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// initializes payload reader of the FrostFS object.
|
2022-02-08 16:54:04 +00:00
|
|
|
// Zero range corresponds to full payload (panics if only offset is set).
|
2024-06-25 12:57:55 +00:00
|
|
|
func (n *Layer) initFrostFSObjectPayloadReader(ctx context.Context, p getFrostFSParams) (io.Reader, error) {
|
2024-09-27 08:14:45 +00:00
|
|
|
var prmAuth frostfs.PrmAuth
|
2024-07-15 12:47:19 +00:00
|
|
|
n.prepareAuthParameters(ctx, &prmAuth, p.bktInfo.Owner)
|
|
|
|
|
|
|
|
if p.off+p.ln != 0 {
|
2024-09-27 08:14:45 +00:00
|
|
|
prm := frostfs.PrmObjectRange{
|
2024-07-15 12:47:19 +00:00
|
|
|
PrmAuth: prmAuth,
|
|
|
|
Container: p.bktInfo.CID,
|
|
|
|
Object: p.oid,
|
|
|
|
PayloadRange: [2]uint64{p.off, p.ln},
|
|
|
|
}
|
|
|
|
|
|
|
|
return n.frostFS.RangeObject(ctx, prm)
|
2022-02-08 16:54:04 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
prm := frostfs.PrmObjectGet{
|
2024-07-15 12:47:19 +00:00
|
|
|
PrmAuth: prmAuth,
|
|
|
|
Container: p.bktInfo.CID,
|
|
|
|
Object: p.oid,
|
|
|
|
}
|
2022-02-08 16:54:04 +00:00
|
|
|
|
2024-07-15 12:47:19 +00:00
|
|
|
res, err := n.frostFS.GetObject(ctx, prm)
|
2022-03-02 16:09:02 +00:00
|
|
|
if err != nil {
|
2022-09-13 10:35:30 +00:00
|
|
|
return nil, err
|
2022-02-08 16:54:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 16:09:02 +00:00
|
|
|
return res.Payload, nil
|
2021-10-04 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// objectGet returns an object with payload in the object.
|
2024-09-27 08:14:45 +00:00
|
|
|
func (n *Layer) objectGet(ctx context.Context, bktInfo *data.BucketInfo, objID oid.ID) (*frostfs.Object, error) {
|
|
|
|
return n.objectGetBase(ctx, bktInfo, objID, frostfs.PrmAuth{})
|
2024-07-15 15:35:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// objectGetWithAuth returns an object with payload in the object. Uses provided PrmAuth.
|
2024-09-27 08:14:45 +00:00
|
|
|
func (n *Layer) objectGetWithAuth(ctx context.Context, bktInfo *data.BucketInfo, objID oid.ID, auth frostfs.PrmAuth) (*frostfs.Object, error) {
|
2024-07-15 15:35:54 +00:00
|
|
|
return n.objectGetBase(ctx, bktInfo, objID, auth)
|
|
|
|
}
|
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
func (n *Layer) objectGetBase(ctx context.Context, bktInfo *data.BucketInfo, objID oid.ID, auth frostfs.PrmAuth) (*frostfs.Object, error) {
|
|
|
|
prm := frostfs.PrmObjectGet{
|
2024-07-15 12:47:19 +00:00
|
|
|
PrmAuth: auth,
|
|
|
|
Container: bktInfo.CID,
|
|
|
|
Object: objID,
|
2022-02-08 16:54:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 17:35:20 +00:00
|
|
|
n.prepareAuthParameters(ctx, &prm.PrmAuth, bktInfo.Owner)
|
2022-02-08 16:54:04 +00:00
|
|
|
|
2024-07-15 12:47:19 +00:00
|
|
|
return n.frostFS.GetObject(ctx, prm)
|
2021-06-24 11:10:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 06:59:24 +00:00
|
|
|
// MimeByFilePath detect mime type by file path extension.
|
|
|
|
func MimeByFilePath(path string) string {
|
|
|
|
ext := filepath.Ext(path)
|
2022-06-29 10:51:05 +00:00
|
|
|
if len(ext) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return mime.TypeByExtension(ext)
|
|
|
|
}
|
|
|
|
|
2022-08-01 16:52:09 +00:00
|
|
|
func encryptionReader(r io.Reader, size uint64, key []byte) (io.Reader, uint64, error) {
|
|
|
|
encSize, err := sio.EncryptedSize(size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, fmt.Errorf("failed to compute enc size: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err = sio.EncryptReader(r, sio.Config{MinVersion: sio.Version20, MaxVersion: sio.Version20, Key: key, CipherSuites: []byte{sio.AES_256_GCM}})
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, fmt.Errorf("couldn't create encrypter: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, encSize, nil
|
|
|
|
}
|
|
|
|
|
2022-08-11 08:48:58 +00:00
|
|
|
func ParseCompletedPartHeader(hdr string) (*Part, error) {
|
2022-08-01 16:52:09 +00:00
|
|
|
// partInfo[0] -- part number, partInfo[1] -- part size, partInfo[2] -- checksum
|
|
|
|
partInfo := strings.Split(hdr, "-")
|
|
|
|
if len(partInfo) != 3 {
|
|
|
|
return nil, fmt.Errorf("invalid completed part header")
|
|
|
|
}
|
|
|
|
num, err := strconv.Atoi(partInfo[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid completed part number '%s': %w", partInfo[0], err)
|
|
|
|
}
|
2023-06-01 13:45:28 +00:00
|
|
|
size, err := strconv.ParseUint(partInfo[1], 10, 64)
|
2022-08-01 16:52:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid completed part size '%s': %w", partInfo[1], err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Part{
|
|
|
|
ETag: partInfo[2],
|
|
|
|
PartNumber: num,
|
2023-06-01 13:45:28 +00:00
|
|
|
Size: size,
|
2022-08-01 16:52:09 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// PutObject stores object into FrostFS, took payload from io.Reader.
|
2024-06-25 12:57:55 +00:00
|
|
|
func (n *Layer) PutObject(ctx context.Context, p *PutObjectParams) (*data.ExtendedObjectInfo, error) {
|
2022-07-19 14:58:18 +00:00
|
|
|
bktSettings, err := n.GetBucketSettings(ctx, p.BktInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't get versioning settings object: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
r := p.Reader
|
2022-08-01 16:52:09 +00:00
|
|
|
if p.Encryption.Enabled() {
|
2024-09-12 15:48:00 +00:00
|
|
|
var size uint64
|
|
|
|
if p.Size != nil {
|
|
|
|
size = *p.Size
|
|
|
|
}
|
|
|
|
p.Header[AttributeDecryptedSize] = strconv.FormatUint(size, 10)
|
2022-08-01 16:52:09 +00:00
|
|
|
if err = addEncryptionHeaders(p.Header, p.Encryption); err != nil {
|
|
|
|
return nil, fmt.Errorf("add encryption header: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var encSize uint64
|
2024-09-12 15:48:00 +00:00
|
|
|
if r, encSize, err = encryptionReader(p.Reader, size, p.Encryption.Key()); err != nil {
|
2022-08-01 16:52:09 +00:00
|
|
|
return nil, fmt.Errorf("create encrypter: %w", err)
|
|
|
|
}
|
2024-09-12 15:48:00 +00:00
|
|
|
p.Size = &encSize
|
2022-08-01 16:52:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 20:35:50 +00:00
|
|
|
if r != nil {
|
|
|
|
if len(p.Header[api.ContentType]) == 0 {
|
2022-09-07 06:59:24 +00:00
|
|
|
if contentType := MimeByFilePath(p.Object); len(contentType) == 0 {
|
2024-08-01 08:21:20 +00:00
|
|
|
d := detector.NewDetector(r, http.DetectContentType)
|
2022-05-17 14:56:05 +00:00
|
|
|
if contentType, err := d.Detect(); err == nil {
|
2022-06-29 10:51:05 +00:00
|
|
|
p.Header[api.ContentType] = contentType
|
|
|
|
}
|
2024-08-01 08:21:20 +00:00
|
|
|
r = d.RestoredReader()
|
2022-06-29 10:51:05 +00:00
|
|
|
} else {
|
2021-11-13 20:35:50 +00:00
|
|
|
p.Header[api.ContentType] = contentType
|
|
|
|
}
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-10 08:19:09 +00:00
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
prm := frostfs.PrmObjectCreate{
|
2022-08-11 22:48:56 +00:00
|
|
|
Container: p.BktInfo.CID,
|
2022-09-07 06:59:24 +00:00
|
|
|
Filepath: p.Object,
|
2022-08-11 22:48:56 +00:00
|
|
|
Payload: r,
|
2022-11-08 09:12:55 +00:00
|
|
|
CreationTime: TimeNow(ctx),
|
2023-04-24 23:49:12 +00:00
|
|
|
CopiesNumber: p.CopiesNumbers,
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
2024-09-12 15:48:00 +00:00
|
|
|
if p.Size != nil {
|
|
|
|
prm.PayloadSize = *p.Size
|
|
|
|
}
|
2022-03-01 19:02:24 +00:00
|
|
|
|
|
|
|
prm.Attributes = make([][2]string, 0, len(p.Header))
|
|
|
|
|
|
|
|
for k, v := range p.Header {
|
|
|
|
prm.Attributes = append(prm.Attributes, [2]string{k, v})
|
|
|
|
}
|
|
|
|
|
2024-07-22 09:00:17 +00:00
|
|
|
createdObj, err := n.objectPutAndHash(ctx, prm, p.BktInfo)
|
2022-03-15 09:37:56 +00:00
|
|
|
if err != nil {
|
2022-04-12 06:52:44 +00:00
|
|
|
return nil, err
|
2022-03-15 09:37:56 +00:00
|
|
|
}
|
2024-09-12 15:28:44 +00:00
|
|
|
|
|
|
|
if !p.Encryption.Enabled() && len(p.ContentMD5) > 0 {
|
2023-10-02 08:52:07 +00:00
|
|
|
headerMd5Hash, err := base64.StdEncoding.DecodeString(p.ContentMD5)
|
|
|
|
if err != nil {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrInvalidDigest)
|
2023-10-02 08:52:07 +00:00
|
|
|
}
|
2024-07-22 09:00:17 +00:00
|
|
|
if !bytes.Equal(headerMd5Hash, createdObj.MD5Sum) {
|
|
|
|
err = n.objectDelete(ctx, p.BktInfo, createdObj.ID)
|
2023-10-02 08:52:07 +00:00
|
|
|
if err != nil {
|
2024-07-22 09:00:17 +00:00
|
|
|
n.reqLogger(ctx).Debug(logs.FailedToDeleteObject, zap.Stringer("cid", p.BktInfo.CID), zap.Stringer("oid", createdObj.ID))
|
2023-10-02 08:52:07 +00:00
|
|
|
}
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrInvalidDigest)
|
2023-10-02 08:52:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-15 09:37:56 +00:00
|
|
|
|
2023-11-13 08:01:47 +00:00
|
|
|
if !p.Encryption.Enabled() && len(p.ContentSHA256Hash) > 0 && !auth.IsStandardContentSHA256(p.ContentSHA256Hash) {
|
|
|
|
contentHashBytes, err := hex.DecodeString(p.ContentSHA256Hash)
|
|
|
|
if err != nil {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrContentSHA256Mismatch)
|
2023-11-13 08:01:47 +00:00
|
|
|
}
|
2024-07-22 09:00:17 +00:00
|
|
|
if !bytes.Equal(contentHashBytes, createdObj.HashSum) {
|
|
|
|
err = n.objectDelete(ctx, p.BktInfo, createdObj.ID)
|
2023-11-13 08:01:47 +00:00
|
|
|
if err != nil {
|
2024-07-22 09:00:17 +00:00
|
|
|
n.reqLogger(ctx).Debug(logs.FailedToDeleteObject, zap.Stringer("cid", p.BktInfo.CID), zap.Stringer("oid", createdObj.ID))
|
2023-11-13 08:01:47 +00:00
|
|
|
}
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrContentSHA256Mismatch)
|
2023-11-13 08:01:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-22 09:00:17 +00:00
|
|
|
n.reqLogger(ctx).Debug(logs.PutObject, zap.Stringer("cid", p.BktInfo.CID), zap.Stringer("oid", createdObj.ID))
|
2024-01-17 14:26:02 +00:00
|
|
|
now := TimeNow(ctx)
|
2023-06-01 13:45:28 +00:00
|
|
|
newVersion := &data.NodeVersion{
|
|
|
|
BaseNodeVersion: data.BaseNodeVersion{
|
2024-07-22 09:00:17 +00:00
|
|
|
OID: createdObj.ID,
|
|
|
|
ETag: hex.EncodeToString(createdObj.HashSum),
|
|
|
|
FilePath: p.Object,
|
|
|
|
Created: &now,
|
|
|
|
Owner: &n.gateOwner,
|
|
|
|
CreationEpoch: createdObj.CreationEpoch,
|
2023-06-01 13:45:28 +00:00
|
|
|
},
|
|
|
|
IsUnversioned: !bktSettings.VersioningEnabled(),
|
2023-06-27 12:49:20 +00:00
|
|
|
IsCombined: p.Header[MultipartObjectSize] != "",
|
2023-06-01 13:45:28 +00:00
|
|
|
}
|
2024-09-12 15:48:00 +00:00
|
|
|
|
2023-10-02 08:52:07 +00:00
|
|
|
if len(p.CompleteMD5Hash) > 0 {
|
|
|
|
newVersion.MD5 = p.CompleteMD5Hash
|
|
|
|
} else {
|
2024-07-22 09:00:17 +00:00
|
|
|
newVersion.MD5 = hex.EncodeToString(createdObj.MD5Sum)
|
2023-10-02 08:52:07 +00:00
|
|
|
}
|
2023-06-01 13:45:28 +00:00
|
|
|
|
2024-09-12 15:48:00 +00:00
|
|
|
if p.Size != nil {
|
|
|
|
newVersion.Size = *p.Size
|
|
|
|
} else {
|
|
|
|
newVersion.Size = createdObj.Size
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
if newVersion.ID, err = n.treeService.AddVersion(ctx, p.BktInfo, newVersion); err != nil {
|
2022-05-17 14:56:05 +00:00
|
|
|
return nil, fmt.Errorf("couldn't add new verion to tree service: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
if p.Lock != nil && (p.Lock.Retention != nil || p.Lock.LegalHold != nil) {
|
2022-08-29 11:30:38 +00:00
|
|
|
putLockInfoPrms := &PutLockInfoParams{
|
2024-04-10 06:41:07 +00:00
|
|
|
ObjVersion: &data.ObjectVersion{
|
2022-08-11 22:48:56 +00:00
|
|
|
BktInfo: p.BktInfo,
|
|
|
|
ObjectName: p.Object,
|
2024-07-22 09:00:17 +00:00
|
|
|
VersionID: createdObj.ID.EncodeToString(),
|
2022-08-11 22:48:56 +00:00
|
|
|
},
|
2023-04-24 23:49:12 +00:00
|
|
|
NewLock: p.Lock,
|
|
|
|
CopiesNumbers: p.CopiesNumbers,
|
|
|
|
NodeVersion: newVersion, // provide new version to make one less tree service call in PutLockInfo
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
2022-05-26 13:11:14 +00:00
|
|
|
|
2022-08-29 11:30:38 +00:00
|
|
|
if err = n.PutLockInfo(ctx, putLockInfoPrms); err != nil {
|
2022-05-26 13:11:14 +00:00
|
|
|
return nil, err
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
2022-02-28 10:22:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 14:33:49 +00:00
|
|
|
n.cache.CleanListCacheEntriesContainingObject(p.Object, p.BktInfo.CID)
|
2021-09-01 16:10:52 +00:00
|
|
|
|
2022-05-24 14:55:56 +00:00
|
|
|
objInfo := &data.ObjectInfo{
|
2024-07-22 09:00:17 +00:00
|
|
|
ID: createdObj.ID,
|
2022-08-11 08:48:58 +00:00
|
|
|
CID: p.BktInfo.CID,
|
2021-08-13 14:13:14 +00:00
|
|
|
|
2023-08-03 12:08:22 +00:00
|
|
|
Owner: n.gateOwner,
|
2022-06-03 14:33:47 +00:00
|
|
|
Bucket: p.BktInfo.Name,
|
|
|
|
Name: p.Object,
|
2024-07-22 09:00:17 +00:00
|
|
|
Size: createdObj.Size,
|
2022-11-08 09:12:55 +00:00
|
|
|
Created: prm.CreationTime,
|
2022-06-03 14:33:47 +00:00
|
|
|
Headers: p.Header,
|
|
|
|
ContentType: p.Header[api.ContentType],
|
2022-07-18 14:51:34 +00:00
|
|
|
HashSum: newVersion.ETag,
|
2023-10-02 08:52:07 +00:00
|
|
|
MD5Sum: newVersion.MD5,
|
2022-05-24 14:55:56 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 22:32:18 +00:00
|
|
|
extendedObjInfo := &data.ExtendedObjectInfo{
|
|
|
|
ObjectInfo: objInfo,
|
|
|
|
NodeVersion: newVersion,
|
|
|
|
}
|
|
|
|
|
2023-08-03 12:08:22 +00:00
|
|
|
n.cache.PutObjectWithName(n.BearerOwner(ctx), extendedObjInfo)
|
2022-05-24 14:55:56 +00:00
|
|
|
|
2022-10-14 14:36:43 +00:00
|
|
|
return extendedObjInfo, nil
|
2021-08-13 14:13:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 12:57:55 +00:00
|
|
|
func (n *Layer) headLastVersionIfNotDeleted(ctx context.Context, bkt *data.BucketInfo, objectName string) (*data.ExtendedObjectInfo, error) {
|
2023-08-03 12:08:22 +00:00
|
|
|
owner := n.BearerOwner(ctx)
|
2022-10-03 14:33:49 +00:00
|
|
|
if extObjInfo := n.cache.GetLastObject(owner, bkt.Name, objectName); extObjInfo != nil {
|
|
|
|
return extObjInfo, nil
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
node, err := n.treeService.GetLatestVersion(ctx, bkt, objectName)
|
2021-08-13 14:13:14 +00:00
|
|
|
if err != nil {
|
2024-09-27 08:13:13 +00:00
|
|
|
if errors.Is(err, tree.ErrNodeNotFound) {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", apierr.GetAPIError(apierr.ErrNoSuchKey), err.Error())
|
2022-05-18 07:48:30 +00:00
|
|
|
}
|
2021-08-13 14:13:14 +00:00
|
|
|
return nil, err
|
2021-08-10 08:19:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 14:26:02 +00:00
|
|
|
if node.IsDeleteMarker {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, DeleteMarkerError{ErrorCode: apierr.ErrNoSuchKey}
|
2021-08-13 14:13:14 +00:00
|
|
|
}
|
2021-08-18 13:48:58 +00:00
|
|
|
|
2022-05-18 06:51:12 +00:00
|
|
|
meta, err := n.objectHead(ctx, bkt, node.OID)
|
2022-05-17 14:56:05 +00:00
|
|
|
if err != nil {
|
2023-06-29 12:46:42 +00:00
|
|
|
if client.IsErrObjectNotFound(err) {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s; %s", apierr.GetAPIError(apierr.ErrNoSuchKey), err.Error(), node.OID.EncodeToString())
|
2023-06-29 12:46:42 +00:00
|
|
|
}
|
2022-05-17 14:56:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-22 16:54:11 +00:00
|
|
|
objInfo := objectInfoFromMeta(bkt, meta)
|
2023-10-02 08:52:07 +00:00
|
|
|
objInfo.MD5Sum = node.MD5
|
2022-08-08 22:32:18 +00:00
|
|
|
|
|
|
|
extObjInfo := &data.ExtendedObjectInfo{
|
|
|
|
ObjectInfo: objInfo,
|
|
|
|
NodeVersion: node,
|
|
|
|
}
|
|
|
|
|
2022-10-03 14:33:49 +00:00
|
|
|
n.cache.PutObjectWithName(owner, extObjInfo)
|
2021-08-18 13:48:58 +00:00
|
|
|
|
2022-08-08 22:32:18 +00:00
|
|
|
return extObjInfo, nil
|
2021-08-10 08:19:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 12:57:55 +00:00
|
|
|
func (n *Layer) headVersion(ctx context.Context, bkt *data.BucketInfo, p *HeadObjectParams) (*data.ExtendedObjectInfo, error) {
|
2022-05-31 08:12:53 +00:00
|
|
|
var err error
|
|
|
|
var foundVersion *data.NodeVersion
|
2022-08-08 22:35:26 +00:00
|
|
|
if p.VersionID == data.UnversionedObjectVersionID {
|
2022-09-13 09:44:18 +00:00
|
|
|
foundVersion, err = n.treeService.GetUnversioned(ctx, bkt, p.Object)
|
2022-01-18 09:40:41 +00:00
|
|
|
if err != nil {
|
2024-09-27 08:13:13 +00:00
|
|
|
if errors.Is(err, tree.ErrNodeNotFound) {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", apierr.GetAPIError(apierr.ErrNoSuchVersion), err.Error())
|
2022-05-31 08:12:53 +00:00
|
|
|
}
|
2022-01-18 09:40:41 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-31 08:12:53 +00:00
|
|
|
} else {
|
2022-09-13 09:44:18 +00:00
|
|
|
versions, err := n.treeService.GetVersions(ctx, bkt, p.Object)
|
2022-05-31 08:12:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't get versions: %w", err)
|
2022-01-18 09:40:41 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 08:12:53 +00:00
|
|
|
for _, version := range versions {
|
|
|
|
if version.OID.EncodeToString() == p.VersionID {
|
|
|
|
foundVersion = version
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if foundVersion == nil {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, fmt.Errorf("%w: there isn't tree node with requested version id", apierr.GetAPIError(apierr.ErrNoSuchVersion))
|
2022-05-18 07:48:30 +00:00
|
|
|
}
|
2021-08-10 10:03:09 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 12:08:22 +00:00
|
|
|
owner := n.BearerOwner(ctx)
|
2022-10-03 14:33:49 +00:00
|
|
|
if extObjInfo := n.cache.GetObject(owner, newAddress(bkt.CID, foundVersion.OID)); extObjInfo != nil {
|
2022-08-08 22:32:18 +00:00
|
|
|
return extObjInfo, nil
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 14:26:02 +00:00
|
|
|
if foundVersion.IsDeleteMarker {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, DeleteMarkerError{ErrorCode: apierr.ErrMethodNotAllowed}
|
2023-10-17 11:21:39 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 07:48:30 +00:00
|
|
|
meta, err := n.objectHead(ctx, bkt, foundVersion.OID)
|
2021-08-10 12:08:15 +00:00
|
|
|
if err != nil {
|
2022-04-21 08:49:56 +00:00
|
|
|
if client.IsErrObjectNotFound(err) {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", apierr.GetAPIError(apierr.ErrNoSuchVersion), err.Error())
|
2021-08-10 10:03:09 +00:00
|
|
|
}
|
2021-08-10 12:08:15 +00:00
|
|
|
return nil, err
|
2021-08-10 10:03:09 +00:00
|
|
|
}
|
2022-07-22 16:54:11 +00:00
|
|
|
objInfo := objectInfoFromMeta(bkt, meta)
|
2023-10-02 08:52:07 +00:00
|
|
|
objInfo.MD5Sum = foundVersion.MD5
|
2022-08-08 22:32:18 +00:00
|
|
|
|
|
|
|
extObjInfo := &data.ExtendedObjectInfo{
|
|
|
|
ObjectInfo: objInfo,
|
|
|
|
NodeVersion: foundVersion,
|
|
|
|
}
|
|
|
|
|
2022-10-03 14:33:49 +00:00
|
|
|
n.cache.PutObject(owner, extObjInfo)
|
2021-08-18 13:48:58 +00:00
|
|
|
|
2022-08-08 22:32:18 +00:00
|
|
|
return extObjInfo, nil
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// objectDelete puts tombstone object into frostfs.
|
2024-06-25 12:57:55 +00:00
|
|
|
func (n *Layer) objectDelete(ctx context.Context, bktInfo *data.BucketInfo, idObj oid.ID) error {
|
2024-09-27 08:14:45 +00:00
|
|
|
return n.objectDeleteBase(ctx, bktInfo, idObj, frostfs.PrmAuth{})
|
2024-07-15 15:35:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// objectDeleteWithAuth puts tombstone object into frostfs. Uses provided PrmAuth.
|
2024-09-27 08:14:45 +00:00
|
|
|
func (n *Layer) objectDeleteWithAuth(ctx context.Context, bktInfo *data.BucketInfo, idObj oid.ID, auth frostfs.PrmAuth) error {
|
2024-07-15 15:35:54 +00:00
|
|
|
return n.objectDeleteBase(ctx, bktInfo, idObj, auth)
|
|
|
|
}
|
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
func (n *Layer) objectDeleteBase(ctx context.Context, bktInfo *data.BucketInfo, idObj oid.ID, auth frostfs.PrmAuth) error {
|
|
|
|
prm := frostfs.PrmObjectDelete{
|
2024-07-15 15:35:54 +00:00
|
|
|
PrmAuth: auth,
|
2022-06-01 17:35:20 +00:00
|
|
|
Container: bktInfo.CID,
|
2022-05-25 17:25:43 +00:00
|
|
|
Object: idObj,
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 17:35:20 +00:00
|
|
|
n.prepareAuthParameters(ctx, &prm.PrmAuth, bktInfo.Owner)
|
2022-03-01 19:02:24 +00:00
|
|
|
|
2022-10-03 14:33:49 +00:00
|
|
|
n.cache.DeleteObject(newAddress(bktInfo.CID, idObj))
|
2022-03-01 19:02:24 +00:00
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
return n.frostFS.DeleteObject(ctx, prm)
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
2021-07-18 13:40:19 +00:00
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// objectPutAndHash prepare auth parameters and invoke frostfs.CreateObject.
|
2024-09-27 08:14:45 +00:00
|
|
|
func (n *Layer) objectPutAndHash(ctx context.Context, prm frostfs.PrmObjectCreate, bktInfo *data.BucketInfo) (*data.CreatedObjectInfo, error) {
|
2022-06-01 17:35:20 +00:00
|
|
|
n.prepareAuthParameters(ctx, &prm.PrmAuth, bktInfo.Owner)
|
2023-07-14 12:30:47 +00:00
|
|
|
prm.ClientCut = n.features.ClientCut()
|
2023-08-25 10:07:59 +00:00
|
|
|
prm.BufferMaxSize = n.features.BufferMaxSizeForPut()
|
2023-08-25 10:06:43 +00:00
|
|
|
prm.WithoutHomomorphicHash = bktInfo.HomomorphicHashDisabled
|
2023-06-01 13:45:28 +00:00
|
|
|
var size uint64
|
2022-05-19 14:56:42 +00:00
|
|
|
hash := sha256.New()
|
2023-10-02 08:52:07 +00:00
|
|
|
md5Hash := md5.New()
|
2022-05-19 14:56:42 +00:00
|
|
|
prm.Payload = wrapReader(prm.Payload, 64*1024, func(buf []byte) {
|
2023-06-01 13:45:28 +00:00
|
|
|
size += uint64(len(buf))
|
2022-05-19 14:56:42 +00:00
|
|
|
hash.Write(buf)
|
2023-10-02 08:52:07 +00:00
|
|
|
md5Hash.Write(buf)
|
2022-05-19 14:56:42 +00:00
|
|
|
})
|
2024-07-22 09:00:17 +00:00
|
|
|
res, err := n.frostFS.CreateObject(ctx, prm)
|
2022-11-16 10:32:27 +00:00
|
|
|
if err != nil {
|
2023-08-01 14:57:41 +00:00
|
|
|
if _, errDiscard := io.Copy(io.Discard, prm.Payload); errDiscard != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
n.reqLogger(ctx).Warn(logs.FailedToDiscardPutPayloadProbablyGoroutineLeaks, zap.Error(errDiscard))
|
2023-08-01 14:57:41 +00:00
|
|
|
}
|
|
|
|
|
2024-07-22 09:00:17 +00:00
|
|
|
return nil, err
|
2022-11-16 10:32:27 +00:00
|
|
|
}
|
2024-07-22 09:00:17 +00:00
|
|
|
return &data.CreatedObjectInfo{
|
|
|
|
ID: res.ObjectID,
|
|
|
|
Size: size,
|
|
|
|
HashSum: hash.Sum(nil),
|
|
|
|
MD5Sum: md5Hash.Sum(nil),
|
|
|
|
CreationEpoch: res.CreationEpoch,
|
|
|
|
}, nil
|
2022-05-19 14:56:42 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 09:32:58 +00:00
|
|
|
type logWrapper struct {
|
|
|
|
log *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logWrapper) Printf(format string, args ...interface{}) {
|
|
|
|
l.log.Info(fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
|
2022-01-20 14:36:21 +00:00
|
|
|
func IsSystemHeader(key string) bool {
|
2022-07-26 01:08:25 +00:00
|
|
|
_, ok := api.SystemMetadata[key]
|
2022-12-20 08:38:58 +00:00
|
|
|
return ok || strings.HasPrefix(key, api.FrostFSSystemMetadataPrefix)
|
2022-01-20 14:36:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 14:56:42 +00:00
|
|
|
func wrapReader(input io.Reader, bufSize int, f func(buf []byte)) io.Reader {
|
2022-05-25 15:59:36 +00:00
|
|
|
if input == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 14:56:42 +00:00
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
var buf = make([]byte, bufSize)
|
|
|
|
for {
|
|
|
|
n, err := input.Read(buf)
|
|
|
|
if n > 0 {
|
|
|
|
f(buf[:n])
|
|
|
|
_, _ = w.Write(buf[:n]) // ignore error, input is not ReadCloser
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
_ = w.CloseWithError(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return r
|
|
|
|
}
|