2020-10-02 12:23:52 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
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
|
|
|
|
|
|
|
|
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 {
|
2022-05-31 17:00:41 +00:00
|
|
|
Head(oid.Address) (*object.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
|
|
|
}
|
|
|
|
|
2022-12-30 11:19:42 +00:00
|
|
|
func (c *cfg) initDefault() {
|
2022-12-30 11:29:29 +00:00
|
|
|
c.storage = (*localStorage)(nil)
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
func NewMessageHeaderSource(opts ...Option) (eaclSDK.TypedHeaderSource, error) {
|
2022-12-30 11:19:42 +00:00
|
|
|
var c cfg
|
|
|
|
c.initDefault()
|
2020-10-02 12:23:52 +00:00
|
|
|
|
|
|
|
for i := range opts {
|
2022-12-30 11:19:42 +00:00
|
|
|
opts[i](&c)
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 11:19:42 +00:00
|
|
|
if c.msg == nil {
|
2022-05-17 13:32:03 +00:00
|
|
|
return nil, errors.New("message is not provided")
|
|
|
|
}
|
|
|
|
|
2022-06-02 14:37:45 +00:00
|
|
|
var res headerSource
|
|
|
|
|
2022-12-30 11:19:42 +00:00
|
|
|
err := c.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
|
|
|
|
2022-12-30 11:19:42 +00:00
|
|
|
res.requestHeaders = requestHeaders(c.msg)
|
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-01-20 09:45:58 +00:00
|
|
|
func requestHeaders(msg xHeaderSource) []eaclSDK.Header {
|
2022-05-17 13:40:25 +00:00
|
|
|
return msg.GetXHeaders()
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 18:47:21 +00:00
|
|
|
var errMissingOID = errors.New("object ID is missing")
|
|
|
|
|
2023-03-20 07:01:27 +00:00
|
|
|
// nolint: funlen
|
2023-01-24 10:41:05 +00:00
|
|
|
func (c *cfg) readObjectHeaders(dst *headerSource) error {
|
|
|
|
switch m := c.msg.(type) {
|
2020-10-02 12:23:52 +00:00
|
|
|
default:
|
2023-01-24 10:41:05 +00:00
|
|
|
panic(fmt.Sprintf("unexpected message type %T", c.msg))
|
2022-05-17 13:32:03 +00:00
|
|
|
case requestXHeaderSource:
|
2020-10-02 12:23:52 +00:00
|
|
|
switch req := m.req.(type) {
|
2022-05-24 18:47:21 +00:00
|
|
|
case
|
|
|
|
*objectV2.GetRequest,
|
|
|
|
*objectV2.HeadRequest:
|
2023-01-24 10:41:05 +00:00
|
|
|
if c.obj == nil {
|
2022-06-02 14:37:45 +00:00
|
|
|
return errMissingOID
|
2022-05-24 18:47:21 +00:00
|
|
|
}
|
|
|
|
|
2023-01-24 10:41:05 +00:00
|
|
|
objHeaders, completed := c.localObjectHeaders(c.cnr, c.obj)
|
2022-07-21 13:47:42 +00:00
|
|
|
|
|
|
|
dst.objectHeaders = objHeaders
|
|
|
|
dst.incompleteObjectHeaders = !completed
|
2020-12-18 12:51:29 +00:00
|
|
|
case
|
|
|
|
*objectV2.GetRangeRequest,
|
|
|
|
*objectV2.GetRangeHashRequest,
|
|
|
|
*objectV2.DeleteRequest:
|
2023-01-24 10:41:05 +00:00
|
|
|
if c.obj == nil {
|
2022-06-02 14:37:45 +00:00
|
|
|
return errMissingOID
|
2022-05-24 18:47:21 +00:00
|
|
|
}
|
|
|
|
|
2023-01-24 10:41:05 +00:00
|
|
|
dst.objectHeaders = addressHeaders(c.cnr, c.obj)
|
2020-10-02 12:23:52 +00:00
|
|
|
case *objectV2.PutRequest:
|
|
|
|
if v, ok := req.GetBody().GetObjectPart().(*objectV2.PutObjectPartInit); ok {
|
|
|
|
oV2 := new(objectV2.Object)
|
|
|
|
oV2.SetObjectID(v.GetObjectID())
|
|
|
|
oV2.SetHeader(v.GetHeader())
|
|
|
|
|
2023-01-24 10:41:05 +00:00
|
|
|
dst.objectHeaders = headersFromObject(object.NewFromV2(oV2), c.cnr, c.obj)
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
case *objectV2.SearchRequest:
|
2022-05-12 16:37:46 +00:00
|
|
|
cnrV2 := req.GetBody().GetContainerID()
|
2022-05-31 17:00:41 +00:00
|
|
|
var cnr cid.ID
|
2022-05-12 16:37:46 +00:00
|
|
|
|
|
|
|
if cnrV2 != nil {
|
2022-05-17 12:46:25 +00:00
|
|
|
if err := cnr.ReadFromV2(*cnrV2); err != nil {
|
2022-06-02 14:37:45 +00:00
|
|
|
return fmt.Errorf("can't parse container ID: %w", err)
|
2022-05-17 12:46:25 +00:00
|
|
|
}
|
2022-05-12 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 14:37:45 +00:00
|
|
|
dst.objectHeaders = []eaclSDK.Header{cidHeader(cnr)}
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
2022-05-17 13:32:03 +00:00
|
|
|
case responseXHeaderSource:
|
2020-10-02 12:23:52 +00:00
|
|
|
switch resp := m.resp.(type) {
|
2022-07-21 13:47:42 +00:00
|
|
|
default:
|
2023-01-24 10:41:05 +00:00
|
|
|
objectHeaders, completed := c.localObjectHeaders(c.cnr, c.obj)
|
2022-07-21 13:47:42 +00:00
|
|
|
|
|
|
|
dst.objectHeaders = objectHeaders
|
|
|
|
dst.incompleteObjectHeaders = !completed
|
2020-10-02 12:23:52 +00:00
|
|
|
case *objectV2.GetResponse:
|
|
|
|
if v, ok := resp.GetBody().GetObjectPart().(*objectV2.GetObjectPartInit); ok {
|
|
|
|
oV2 := new(objectV2.Object)
|
|
|
|
oV2.SetObjectID(v.GetObjectID())
|
|
|
|
oV2.SetHeader(v.GetHeader())
|
|
|
|
|
2023-01-24 10:41:05 +00:00
|
|
|
dst.objectHeaders = headersFromObject(object.NewFromV2(oV2), c.cnr, c.obj)
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
case *objectV2.HeadResponse:
|
|
|
|
oV2 := new(objectV2.Object)
|
|
|
|
|
|
|
|
var hdr *objectV2.Header
|
|
|
|
|
|
|
|
switch v := resp.GetBody().GetHeaderPart().(type) {
|
2020-12-07 17:49:47 +00:00
|
|
|
case *objectV2.ShortHeader:
|
2020-10-02 12:23:52 +00:00
|
|
|
hdr = new(objectV2.Header)
|
2020-12-07 17:49:47 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
var idV2 refsV2.ContainerID
|
2023-01-24 10:41:05 +00:00
|
|
|
c.cnr.WriteToV2(&idV2)
|
2022-05-12 16:37:46 +00:00
|
|
|
|
|
|
|
hdr.SetContainerID(&idV2)
|
2020-12-07 17:49:47 +00:00
|
|
|
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()
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
oV2.SetHeader(hdr)
|
|
|
|
|
2023-01-24 10:41:05 +00:00
|
|
|
dst.objectHeaders = headersFromObject(object.NewFromV2(oV2), c.cnr, c.obj)
|
2022-05-24 18:47:21 +00:00
|
|
|
}
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
2022-05-24 18:47:21 +00:00
|
|
|
|
2022-06-02 14:37:45 +00:00
|
|
|
return nil
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
|
2023-01-24 10:41:05 +00:00
|
|
|
func (c *cfg) localObjectHeaders(cnr cid.ID, idObj *oid.ID) ([]eaclSDK.Header, bool) {
|
2022-07-21 13:47:42 +00:00
|
|
|
if idObj != nil {
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnr)
|
|
|
|
addr.SetObject(*idObj)
|
|
|
|
|
2023-01-24 10:41:05 +00:00
|
|
|
obj, err := c.storage.Head(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
|
|
|
}
|