2020-10-02 12:23:52 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2022-05-17 13:32:03 +00:00
|
|
|
"errors"
|
2020-10-02 12:23:52 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/acl"
|
|
|
|
objectV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
refsV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
eaclSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2020-10-02 12:23:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
|
|
|
type cfg struct {
|
|
|
|
storage ObjectStorage
|
|
|
|
|
2023-07-06 08:28:36 +00:00
|
|
|
msg XHeaderSource
|
2020-12-15 09:06:04 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr cid.ID
|
|
|
|
obj *oid.ID
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectStorage interface {
|
2023-07-06 12:36:41 +00:00
|
|
|
Head(context.Context, oid.Address) (*objectSDK.Object, error)
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Request interface {
|
|
|
|
GetMetaHeader() *session.RequestMetaHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response interface {
|
|
|
|
GetMetaHeader() *session.ResponseMetaHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
type headerSource struct {
|
2022-05-17 12:46:25 +00:00
|
|
|
requestHeaders []eaclSDK.Header
|
|
|
|
objectHeaders []eaclSDK.Header
|
2022-06-02 14:37:45 +00:00
|
|
|
|
|
|
|
incompleteObjectHeaders bool
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 08:28:36 +00:00
|
|
|
func NewMessageHeaderSource(os ObjectStorage, xhs XHeaderSource, cnrID cid.ID, opts ...Option) (eaclSDK.TypedHeaderSource, error) {
|
|
|
|
cfg := &cfg{
|
|
|
|
storage: os,
|
|
|
|
cnr: cnrID,
|
|
|
|
msg: xhs,
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](cfg)
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:32:03 +00:00
|
|
|
if cfg.msg == nil {
|
|
|
|
return nil, errors.New("message is not provided")
|
|
|
|
}
|
|
|
|
|
2022-06-02 14:37:45 +00:00
|
|
|
var res headerSource
|
|
|
|
|
|
|
|
err := cfg.readObjectHeaders(&res)
|
2022-05-17 12:46:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
2022-05-17 12:46:25 +00:00
|
|
|
|
2023-07-06 08:28:36 +00:00
|
|
|
res.requestHeaders = cfg.msg.GetXHeaders()
|
2022-06-02 14:37:45 +00:00
|
|
|
|
|
|
|
return res, nil
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
func (h headerSource) HeadersOfType(typ eaclSDK.FilterHeaderType) ([]eaclSDK.Header, bool) {
|
2020-10-02 12:23:52 +00:00
|
|
|
switch typ {
|
|
|
|
default:
|
|
|
|
return nil, true
|
|
|
|
case eaclSDK.HeaderFromRequest:
|
2022-05-17 12:46:25 +00:00
|
|
|
return h.requestHeaders, true
|
2020-10-02 12:23:52 +00:00
|
|
|
case eaclSDK.HeaderFromObject:
|
2022-06-02 14:37:45 +00:00
|
|
|
return h.objectHeaders, !h.incompleteObjectHeaders
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
type xHeader session.XHeader
|
|
|
|
|
|
|
|
func (x xHeader) Key() string {
|
|
|
|
return (*session.XHeader)(&x).GetKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x xHeader) Value() string {
|
|
|
|
return (*session.XHeader)(&x).GetValue()
|
|
|
|
}
|
|
|
|
|
2022-05-24 18:47:21 +00:00
|
|
|
var errMissingOID = errors.New("object ID is missing")
|
|
|
|
|
2022-06-02 14:37:45 +00:00
|
|
|
func (h *cfg) readObjectHeaders(dst *headerSource) error {
|
2020-10-02 12:23:52 +00:00
|
|
|
switch m := h.msg.(type) {
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected message type %T", h.msg))
|
2022-05-17 13:32:03 +00:00
|
|
|
case requestXHeaderSource:
|
2023-04-04 09:03:19 +00:00
|
|
|
return h.readObjectHeadersFromRequestXHeaderSource(m, dst)
|
|
|
|
case responseXHeaderSource:
|
|
|
|
return h.readObjectHeadersResponseXHeaderSource(m, dst)
|
|
|
|
}
|
|
|
|
}
|
2022-05-24 18:47:21 +00:00
|
|
|
|
2023-04-04 09:03:19 +00:00
|
|
|
func (h *cfg) readObjectHeadersFromRequestXHeaderSource(m requestXHeaderSource, dst *headerSource) error {
|
|
|
|
switch req := m.req.(type) {
|
|
|
|
case
|
|
|
|
*objectV2.GetRequest,
|
|
|
|
*objectV2.HeadRequest:
|
|
|
|
if h.obj == nil {
|
|
|
|
return errMissingOID
|
|
|
|
}
|
2020-10-02 12:23:52 +00:00
|
|
|
|
2023-04-04 09:03:19 +00:00
|
|
|
objHeaders, completed := h.localObjectHeaders(h.cnr, h.obj)
|
2022-05-12 16:37:46 +00:00
|
|
|
|
2023-04-04 09:03:19 +00:00
|
|
|
dst.objectHeaders = objHeaders
|
|
|
|
dst.incompleteObjectHeaders = !completed
|
|
|
|
case
|
|
|
|
*objectV2.GetRangeRequest,
|
|
|
|
*objectV2.GetRangeHashRequest,
|
|
|
|
*objectV2.DeleteRequest:
|
|
|
|
if h.obj == nil {
|
|
|
|
return errMissingOID
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
2023-04-04 09:03:19 +00:00
|
|
|
|
|
|
|
dst.objectHeaders = addressHeaders(h.cnr, h.obj)
|
|
|
|
case *objectV2.PutRequest:
|
|
|
|
if v, ok := req.GetBody().GetObjectPart().(*objectV2.PutObjectPartInit); ok {
|
2020-10-02 12:23:52 +00:00
|
|
|
oV2 := new(objectV2.Object)
|
2023-04-04 09:03:19 +00:00
|
|
|
oV2.SetObjectID(v.GetObjectID())
|
|
|
|
oV2.SetHeader(v.GetHeader())
|
2020-10-02 12:23:52 +00:00
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
dst.objectHeaders = headersFromObject(objectSDK.NewFromV2(oV2), h.cnr, h.obj)
|
2023-04-04 09:03:19 +00:00
|
|
|
}
|
2023-09-06 14:06:54 +00:00
|
|
|
case *objectV2.PutSingleRequest:
|
|
|
|
dst.objectHeaders = headersFromObject(objectSDK.NewFromV2(req.GetBody().GetObject()), h.cnr, h.obj)
|
2023-04-04 09:03:19 +00:00
|
|
|
case *objectV2.SearchRequest:
|
|
|
|
cnrV2 := req.GetBody().GetContainerID()
|
|
|
|
var cnr cid.ID
|
2020-10-02 12:23:52 +00:00
|
|
|
|
2023-04-04 09:03:19 +00:00
|
|
|
if cnrV2 != nil {
|
|
|
|
if err := cnr.ReadFromV2(*cnrV2); err != nil {
|
|
|
|
return fmt.Errorf("can't parse container ID: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2020-12-07 17:49:47 +00:00
|
|
|
|
2023-04-04 09:03:19 +00:00
|
|
|
dst.objectHeaders = []eaclSDK.Header{cidHeader(cnr)}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-12 16:37:46 +00:00
|
|
|
|
2023-04-04 09:03:19 +00:00
|
|
|
func (h *cfg) readObjectHeadersResponseXHeaderSource(m responseXHeaderSource, dst *headerSource) error {
|
|
|
|
switch resp := m.resp.(type) {
|
|
|
|
default:
|
|
|
|
objectHeaders, completed := h.localObjectHeaders(h.cnr, h.obj)
|
2020-10-02 12:23:52 +00:00
|
|
|
|
2023-04-04 09:03:19 +00:00
|
|
|
dst.objectHeaders = objectHeaders
|
|
|
|
dst.incompleteObjectHeaders = !completed
|
|
|
|
case *objectV2.GetResponse:
|
|
|
|
if v, ok := resp.GetBody().GetObjectPart().(*objectV2.GetObjectPartInit); ok {
|
|
|
|
oV2 := new(objectV2.Object)
|
|
|
|
oV2.SetObjectID(v.GetObjectID())
|
|
|
|
oV2.SetHeader(v.GetHeader())
|
2020-10-02 12:23:52 +00:00
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
dst.objectHeaders = headersFromObject(objectSDK.NewFromV2(oV2), h.cnr, h.obj)
|
2022-05-24 18:47:21 +00:00
|
|
|
}
|
2023-04-04 09:03:19 +00:00
|
|
|
case *objectV2.HeadResponse:
|
|
|
|
oV2 := new(objectV2.Object)
|
|
|
|
|
|
|
|
var hdr *objectV2.Header
|
|
|
|
|
|
|
|
switch v := resp.GetBody().GetHeaderPart().(type) {
|
|
|
|
case *objectV2.ShortHeader:
|
|
|
|
hdr = new(objectV2.Header)
|
|
|
|
|
|
|
|
var idV2 refsV2.ContainerID
|
|
|
|
h.cnr.WriteToV2(&idV2)
|
|
|
|
|
|
|
|
hdr.SetContainerID(&idV2)
|
|
|
|
hdr.SetVersion(v.GetVersion())
|
|
|
|
hdr.SetCreationEpoch(v.GetCreationEpoch())
|
|
|
|
hdr.SetOwnerID(v.GetOwnerID())
|
|
|
|
hdr.SetObjectType(v.GetObjectType())
|
|
|
|
hdr.SetPayloadLength(v.GetPayloadLength())
|
|
|
|
case *objectV2.HeaderWithSignature:
|
|
|
|
hdr = v.GetHeader()
|
|
|
|
}
|
|
|
|
|
|
|
|
oV2.SetHeader(hdr)
|
2022-05-24 18:47:21 +00:00
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
dst.objectHeaders = headersFromObject(objectSDK.NewFromV2(oV2), h.cnr, h.obj)
|
2023-04-04 09:03:19 +00:00
|
|
|
}
|
2022-06-02 14:37:45 +00:00
|
|
|
return nil
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 13:47:42 +00:00
|
|
|
func (h *cfg) localObjectHeaders(cnr cid.ID, idObj *oid.ID) ([]eaclSDK.Header, bool) {
|
|
|
|
if idObj != nil {
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnr)
|
|
|
|
addr.SetObject(*idObj)
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
obj, err := h.storage.Head(context.TODO(), addr)
|
2022-07-21 13:47:42 +00:00
|
|
|
if err == nil {
|
|
|
|
return headersFromObject(obj, cnr, idObj), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addressHeaders(cnr, idObj), false
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func cidHeader(idCnr cid.ID) sysObjHdr {
|
2022-05-17 13:32:03 +00:00
|
|
|
return sysObjHdr{
|
2020-12-14 12:07:20 +00:00
|
|
|
k: acl.FilterObjectContainerID,
|
2022-05-20 08:10:51 +00:00
|
|
|
v: idCnr.EncodeToString(),
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func oidHeader(obj oid.ID) sysObjHdr {
|
2022-05-17 13:32:03 +00:00
|
|
|
return sysObjHdr{
|
2020-12-14 12:07:20 +00:00
|
|
|
k: acl.FilterObjectID,
|
2022-05-31 17:00:41 +00:00
|
|
|
v: obj.EncodeToString(),
|
2022-05-17 13:32:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
func ownerIDHeader(ownerID user.ID) sysObjHdr {
|
2022-05-17 13:32:03 +00:00
|
|
|
return sysObjHdr{
|
|
|
|
k: acl.FilterObjectOwnerID,
|
2022-05-31 17:00:41 +00:00
|
|
|
v: ownerID.EncodeToString(),
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func addressHeaders(cnr cid.ID, oid *oid.ID) []eaclSDK.Header {
|
2022-05-24 18:47:21 +00:00
|
|
|
hh := make([]eaclSDK.Header, 0, 2)
|
2022-05-31 17:00:41 +00:00
|
|
|
hh = append(hh, cidHeader(cnr))
|
2020-10-02 12:23:52 +00:00
|
|
|
|
2022-05-24 18:47:21 +00:00
|
|
|
if oid != nil {
|
|
|
|
hh = append(hh, oidHeader(*oid))
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 18:47:21 +00:00
|
|
|
return hh
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|