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"
|
|
|
|
"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"
|
|
|
|
apiErrors "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
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 {
|
|
|
|
ErrorCode apiErrors.ErrorCode
|
|
|
|
}
|
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) {
|
2022-06-06 11:09:09 +00:00
|
|
|
prm := PrmObjectRead{
|
2022-06-01 17:35:20 +00:00
|
|
|
Container: bktInfo.CID,
|
2022-04-25 09:57:58 +00:00
|
|
|
Object: idObj,
|
2022-03-01 19:02:24 +00:00
|
|
|
WithHeader: true,
|
|
|
|
}
|
|
|
|
|
2022-06-01 17:35:20 +00:00
|
|
|
n.prepareAuthParameters(ctx, &prm.PrmAuth, bktInfo.Owner)
|
2022-02-08 16:54:04 +00:00
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
res, err := n.frostFS.ReadObject(ctx, prm)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
2022-09-13 10:35:30 +00:00
|
|
|
return nil, err
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
2022-02-08 16:54:04 +00:00
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
return res.Head, nil
|
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
|
|
|
|
if err = json.Unmarshal(combinedObj.Payload(), &parts); err != nil {
|
|
|
|
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) {
|
2022-06-06 11:09:09 +00:00
|
|
|
prm := PrmObjectRead{
|
2022-05-31 12:38:06 +00:00
|
|
|
Container: p.bktInfo.CID,
|
|
|
|
Object: p.oid,
|
2022-03-01 19:02:24 +00:00
|
|
|
WithPayload: true,
|
|
|
|
PayloadRange: [2]uint64{p.off, p.ln},
|
2022-02-08 16:54:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 16:56:04 +00:00
|
|
|
n.prepareAuthParameters(ctx, &prm.PrmAuth, p.bktInfo.Owner)
|
2022-02-08 16:54:04 +00:00
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
res, err := n.frostFS.ReadObject(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-06-25 12:57:55 +00:00
|
|
|
func (n *Layer) objectGet(ctx context.Context, bktInfo *data.BucketInfo, objID oid.ID) (*object.Object, error) {
|
2024-07-15 15:35:54 +00:00
|
|
|
return n.objectGetBase(ctx, bktInfo, objID, PrmAuth{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// objectGetWithAuth returns an object with payload in the object. Uses provided PrmAuth.
|
|
|
|
func (n *Layer) objectGetWithAuth(ctx context.Context, bktInfo *data.BucketInfo, objID oid.ID, auth PrmAuth) (*object.Object, error) {
|
|
|
|
return n.objectGetBase(ctx, bktInfo, objID, auth)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Layer) objectGetBase(ctx context.Context, bktInfo *data.BucketInfo, objID oid.ID, auth PrmAuth) (*object.Object, error) {
|
2022-06-06 11:09:09 +00:00
|
|
|
prm := PrmObjectRead{
|
2024-07-15 15:35:54 +00:00
|
|
|
PrmAuth: auth,
|
2022-06-01 17:35:20 +00:00
|
|
|
Container: bktInfo.CID,
|
|
|
|
Object: objID,
|
2022-03-01 19:02:24 +00:00
|
|
|
WithHeader: true,
|
|
|
|
WithPayload: true,
|
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
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
res, err := n.frostFS.ReadObject(ctx, prm)
|
2022-02-08 16:54:04 +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-01 19:02:24 +00:00
|
|
|
return res.Head, nil
|
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() {
|
2023-06-01 13:45:28 +00:00
|
|
|
p.Header[AttributeDecryptedSize] = strconv.FormatUint(p.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
|
2023-06-01 13:45:28 +00:00
|
|
|
if r, encSize, err = encryptionReader(p.Reader, p.Size, p.Encryption.Key()); err != nil {
|
2022-08-01 16:52:09 +00:00
|
|
|
return nil, fmt.Errorf("create encrypter: %w", err)
|
|
|
|
}
|
2023-06-01 13:45:28 +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 {
|
2022-06-29 10:51:05 +00:00
|
|
|
d := newDetector(r)
|
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
|
|
|
|
}
|
|
|
|
r = d.MultiReader()
|
|
|
|
} 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
|
|
|
|
2022-06-06 11:09:09 +00:00
|
|
|
prm := PrmObjectCreate{
|
2022-08-11 22:48:56 +00:00
|
|
|
Container: p.BktInfo.CID,
|
2023-06-01 13:45:28 +00:00
|
|
|
PayloadSize: p.Size,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
prm.Attributes = make([][2]string, 0, len(p.Header))
|
|
|
|
|
|
|
|
for k, v := range p.Header {
|
|
|
|
prm.Attributes = append(prm.Attributes, [2]string{k, v})
|
|
|
|
}
|
|
|
|
|
2023-10-02 08:52:07 +00:00
|
|
|
size, id, hash, md5Hash, 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
|
|
|
}
|
2023-10-02 08:52:07 +00:00
|
|
|
if len(p.ContentMD5) > 0 {
|
|
|
|
headerMd5Hash, err := base64.StdEncoding.DecodeString(p.ContentMD5)
|
|
|
|
if err != nil {
|
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrInvalidDigest)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(headerMd5Hash, md5Hash) {
|
|
|
|
err = n.objectDelete(ctx, p.BktInfo, id)
|
|
|
|
if err != nil {
|
|
|
|
n.reqLogger(ctx).Debug(logs.FailedToDeleteObject, zap.Stringer("cid", p.BktInfo.CID), zap.Stringer("oid", id))
|
|
|
|
}
|
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrInvalidDigest)
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrContentSHA256Mismatch)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(contentHashBytes, hash) {
|
|
|
|
err = n.objectDelete(ctx, p.BktInfo, id)
|
|
|
|
if err != nil {
|
|
|
|
n.reqLogger(ctx).Debug(logs.FailedToDeleteObject, zap.Stringer("cid", p.BktInfo.CID), zap.Stringer("oid", id))
|
|
|
|
}
|
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrContentSHA256Mismatch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 11:07:52 +00:00
|
|
|
n.reqLogger(ctx).Debug(logs.PutObject, zap.Stringer("cid", p.BktInfo.CID), zap.Stringer("oid", 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{
|
|
|
|
OID: id,
|
|
|
|
ETag: hex.EncodeToString(hash),
|
|
|
|
FilePath: p.Object,
|
2024-01-17 14:26:02 +00:00
|
|
|
Size: p.Size,
|
|
|
|
Created: &now,
|
|
|
|
Owner: &n.gateOwner,
|
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
|
|
|
}
|
2023-10-02 08:52:07 +00:00
|
|
|
if len(p.CompleteMD5Hash) > 0 {
|
|
|
|
newVersion.MD5 = p.CompleteMD5Hash
|
|
|
|
} else {
|
|
|
|
newVersion.MD5 = hex.EncodeToString(md5Hash)
|
|
|
|
}
|
2023-06-01 13:45:28 +00:00
|
|
|
|
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,
|
|
|
|
VersionID: id.EncodeToString(),
|
|
|
|
},
|
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{
|
2022-08-11 08:48:58 +00:00
|
|
|
ID: id,
|
|
|
|
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,
|
2023-06-01 13:45:28 +00:00
|
|
|
Size: 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 {
|
2022-05-18 07:48:30 +00:00
|
|
|
if errors.Is(err, ErrNodeNotFound) {
|
2023-06-30 09:03:55 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", apiErrors.GetAPIError(apiErrors.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 {
|
2023-10-17 11:21:39 +00:00
|
|
|
return nil, DeleteMarkerError{ErrorCode: apiErrors.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-04-01 09:51:05 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s; %s", apiErrors.GetAPIError(apiErrors.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 {
|
2022-05-31 08:12:53 +00:00
|
|
|
if errors.Is(err, ErrNodeNotFound) {
|
2023-06-30 09:03:55 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", apiErrors.GetAPIError(apiErrors.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 {
|
2023-06-30 09:03:55 +00:00
|
|
|
return nil, fmt.Errorf("%w: there isn't tree node with requested version id", apiErrors.GetAPIError(apiErrors.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 {
|
2023-10-17 11:21:39 +00:00
|
|
|
return nil, DeleteMarkerError{ErrorCode: apiErrors.ErrMethodNotAllowed}
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-06-30 09:03:55 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", apiErrors.GetAPIError(apiErrors.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-07-15 15:35:54 +00:00
|
|
|
return n.objectDeleteBase(ctx, bktInfo, idObj, PrmAuth{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// objectDeleteWithAuth puts tombstone object into frostfs. Uses provided PrmAuth.
|
|
|
|
func (n *Layer) objectDeleteWithAuth(ctx context.Context, bktInfo *data.BucketInfo, idObj oid.ID, auth PrmAuth) error {
|
|
|
|
return n.objectDeleteBase(ctx, bktInfo, idObj, auth)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Layer) objectDeleteBase(ctx context.Context, bktInfo *data.BucketInfo, idObj oid.ID, auth PrmAuth) error {
|
2022-06-06 11:09:09 +00:00
|
|
|
prm := 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.
|
2022-05-19 14:56:42 +00:00
|
|
|
// Returns object ID and payload sha256 hash.
|
2024-06-25 12:57:55 +00:00
|
|
|
func (n *Layer) objectPutAndHash(ctx context.Context, prm PrmObjectCreate, bktInfo *data.BucketInfo) (uint64, oid.ID, []byte, []byte, 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
|
|
|
})
|
2022-12-20 08:38:58 +00:00
|
|
|
id, 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
|
|
|
}
|
|
|
|
|
2023-10-02 08:52:07 +00:00
|
|
|
return 0, oid.ID{}, nil, nil, err
|
2022-11-16 10:32:27 +00:00
|
|
|
}
|
2023-10-02 08:52:07 +00:00
|
|
|
return size, id, hash.Sum(nil), md5Hash.Sum(nil), 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
|
|
|
|
}
|