2021-10-04 14:30:38 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-13 18:50:02 +00:00
|
|
|
"encoding/xml"
|
2021-10-04 14:30:38 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
2021-11-15 12:56:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-02-08 16:54:04 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object/address"
|
2021-10-04 14:30:38 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
func (n *layer) putSystemObject(ctx context.Context, p *PutSystemObjectParams) (*data.ObjectInfo, error) {
|
|
|
|
objInfo, err := n.putSystemObjectIntoNeoFS(ctx, p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = n.systemCache.PutObject(systemObjectKey(p.BktInfo, p.ObjName), objInfo); err != nil {
|
|
|
|
n.log.Error("couldn't cache system object", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return objInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) headSystemObject(ctx context.Context, bkt *data.BucketInfo, objName string) (*data.ObjectInfo, error) {
|
|
|
|
if objInfo := n.systemCache.GetObject(systemObjectKey(bkt, objName)); objInfo != nil {
|
|
|
|
return objInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
versions, err := n.headSystemVersions(ctx, bkt, objName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = n.systemCache.PutObject(systemObjectKey(bkt, objName), versions.getLast()); err != nil {
|
|
|
|
n.log.Error("couldn't cache system object", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return versions.getLast(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) deleteSystemObject(ctx context.Context, bktInfo *data.BucketInfo, name string) error {
|
2021-11-13 20:35:50 +00:00
|
|
|
f := &findParams{
|
2022-03-01 19:02:24 +00:00
|
|
|
attr: [2]string{objectSystemAttributeName, name},
|
|
|
|
cid: bktInfo.CID,
|
2021-11-13 20:35:50 +00:00
|
|
|
}
|
|
|
|
ids, err := n.objectSearch(ctx, f)
|
2021-10-13 18:50:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:54:04 +00:00
|
|
|
for i := range ids {
|
|
|
|
if err = n.objectDelete(ctx, bktInfo.CID, &ids[i]); err != nil {
|
2021-10-13 18:50:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n.systemCache.Delete(systemObjectKey(bktInfo, name))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) putSystemObjectIntoNeoFS(ctx context.Context, p *PutSystemObjectParams) (*data.ObjectInfo, error) {
|
2021-10-04 14:30:38 +00:00
|
|
|
versions, err := n.headSystemVersions(ctx, p.BktInfo, p.ObjName)
|
|
|
|
if err != nil && !errors.IsS3Error(err, errors.ErrNoSuchKey) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
prm := PrmObjectCreate{
|
|
|
|
Container: *p.BktInfo.CID,
|
|
|
|
Creator: *p.BktInfo.Owner,
|
|
|
|
Attributes: make([][2]string, 2, 2+len(p.Metadata)),
|
|
|
|
Payload: p.Reader,
|
|
|
|
}
|
2021-10-04 14:30:38 +00:00
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
prm.Attributes[0][0], prm.Attributes[0][1] = objectSystemAttributeName, p.ObjName
|
|
|
|
prm.Attributes[1][0], prm.Attributes[1][1] = attrVersionsIgnore, "true"
|
2021-10-04 14:30:38 +00:00
|
|
|
|
|
|
|
for k, v := range p.Metadata {
|
2022-01-20 14:36:21 +00:00
|
|
|
if !IsSystemHeader(k) {
|
|
|
|
k = p.Prefix + k
|
|
|
|
}
|
2022-03-01 19:02:24 +00:00
|
|
|
|
|
|
|
if v == "" && p.Prefix == tagPrefix {
|
2021-10-04 14:30:38 +00:00
|
|
|
v = tagEmptyMark
|
|
|
|
}
|
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
prm.Attributes = append(prm.Attributes, [2]string{k, v})
|
|
|
|
}
|
2021-10-04 14:30:38 +00:00
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
id, err := n.neoFS.CreateObject(ctx, prm)
|
2021-10-04 14:30:38 +00:00
|
|
|
if err != nil {
|
2022-01-21 12:06:39 +00:00
|
|
|
return nil, n.transformNeofsError(ctx, err)
|
2021-10-04 14:30:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
meta, err := n.objectHead(ctx, p.BktInfo.CID, id)
|
2021-10-04 14:30:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
idsToDeleteArr := updateCRDT2PSetHeaders(p.Metadata, versions, false) // false means "last write wins"
|
|
|
|
|
2021-10-04 14:30:38 +00:00
|
|
|
for _, id := range idsToDeleteArr {
|
|
|
|
if err = n.objectDelete(ctx, p.BktInfo.CID, id); err != nil {
|
|
|
|
n.log.Warn("couldn't delete system object",
|
|
|
|
zap.Stringer("version id", id),
|
|
|
|
zap.String("name", p.ObjName),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
return objInfoFromMeta(p.BktInfo, meta), nil
|
2021-10-04 14:30:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
func (n *layer) getSystemObjectFromNeoFS(ctx context.Context, bkt *data.BucketInfo, objName string) (*object.Object, error) {
|
|
|
|
versions, err := n.headSystemVersions(ctx, bkt, objName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-10-04 14:30:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
objInfo := versions.getLast()
|
|
|
|
|
2022-02-08 16:54:04 +00:00
|
|
|
var addr address.Address
|
|
|
|
|
|
|
|
addr.SetContainerID(bkt.CID)
|
|
|
|
addr.SetObjectID(objInfo.ID)
|
|
|
|
|
|
|
|
obj, err := n.objectGet(ctx, &addr)
|
2021-10-04 14:30:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
if len(obj.Payload()) == 0 {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInternalError)
|
|
|
|
}
|
|
|
|
return obj, nil
|
2021-10-04 14:30:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
func (n *layer) getCORS(ctx context.Context, bkt *data.BucketInfo, sysName string) (*data.CORSConfiguration, error) {
|
|
|
|
if cors := n.systemCache.GetCORS(systemObjectKey(bkt, sysName)); cors != nil {
|
|
|
|
return cors, nil
|
2021-10-04 14:30:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
obj, err := n.getSystemObjectFromNeoFS(ctx, bkt, sysName)
|
2021-10-04 14:30:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
cors := &data.CORSConfiguration{}
|
2021-10-04 14:30:38 +00:00
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
if err = xml.Unmarshal(obj.Payload(), &cors); err != nil {
|
2021-10-04 14:30:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
if err = n.systemCache.PutCORS(systemObjectKey(bkt, sysName), cors); err != nil {
|
2021-10-04 14:32:35 +00:00
|
|
|
n.log.Warn("couldn't put system meta to objects cache",
|
|
|
|
zap.Stringer("object id", obj.ID()),
|
2021-10-13 18:50:02 +00:00
|
|
|
zap.Stringer("bucket id", bkt.CID),
|
2021-10-04 14:32:35 +00:00
|
|
|
zap.Error(err))
|
|
|
|
}
|
2021-10-04 14:30:38 +00:00
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
return cors, nil
|
2021-10-04 14:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) headSystemVersions(ctx context.Context, bkt *data.BucketInfo, sysName string) (*objectVersions, error) {
|
2021-11-13 20:35:50 +00:00
|
|
|
f := &findParams{
|
2022-03-01 19:02:24 +00:00
|
|
|
attr: [2]string{objectSystemAttributeName, sysName},
|
|
|
|
cid: bkt.CID,
|
2021-11-13 20:35:50 +00:00
|
|
|
}
|
|
|
|
ids, err := n.objectSearch(ctx, f)
|
2021-10-04 14:30:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
versions := newObjectVersions(sysName)
|
2022-02-08 16:54:04 +00:00
|
|
|
for i := range ids {
|
|
|
|
meta, err := n.objectHead(ctx, bkt.CID, &ids[i])
|
2021-10-04 14:30:38 +00:00
|
|
|
if err != nil {
|
|
|
|
n.log.Warn("couldn't head object",
|
2022-02-08 16:54:04 +00:00
|
|
|
zap.Stringer("object id", &ids[i]),
|
2021-10-04 14:30:38 +00:00
|
|
|
zap.Stringer("bucket id", bkt.CID),
|
|
|
|
zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if oi := objInfoFromMeta(bkt, meta); oi != nil {
|
|
|
|
if !isSystem(oi) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
versions.appendVersion(oi)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastVersion := versions.getLast()
|
|
|
|
if lastVersion == nil {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrNoSuchKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
return versions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// systemObjectKey is a key to use in SystemCache.
|
|
|
|
func systemObjectKey(bktInfo *data.BucketInfo, obj string) string {
|
|
|
|
return bktInfo.Name + obj
|
|
|
|
}
|