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"
|
|
|
|
|
2020-12-14 12:07:20 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/acl"
|
2020-10-02 12:23:52 +00:00
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2022-05-12 16:37:46 +00:00
|
|
|
refsV2 "github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2020-10-02 12:23:52 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
2022-05-31 17:00:41 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2021-11-10 07:08:33 +00:00
|
|
|
eaclSDK "github.com/nspcc-dev/neofs-sdk-go/eacl"
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-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
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultCfg() *cfg {
|
|
|
|
return &cfg{
|
|
|
|
storage: new(localStorage),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
func NewMessageHeaderSource(opts ...Option) (eaclSDK.TypedHeaderSource, error) {
|
2020-10-02 12:23:52 +00:00
|
|
|
cfg := defaultCfg()
|
|
|
|
|
|
|
|
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-05-17 12:46:25 +00:00
|
|
|
objHdrs, err := cfg.objectHeaders()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
2022-05-17 12:46:25 +00:00
|
|
|
|
|
|
|
return headerSource{
|
|
|
|
objectHeaders: objHdrs,
|
|
|
|
requestHeaders: requestHeaders(cfg.msg),
|
|
|
|
}, 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-05-17 12:46:25 +00:00
|
|
|
return h.objectHeaders, true
|
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")
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
func (h *cfg) objectHeaders() ([]eaclSDK.Header, 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:
|
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:
|
2022-05-31 17:00:41 +00:00
|
|
|
if h.obj == nil {
|
2022-05-24 18:47:21 +00:00
|
|
|
return nil, errMissingOID
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
return h.localObjectHeaders(h.cnr, h.obj)
|
2020-12-18 12:51:29 +00:00
|
|
|
case
|
|
|
|
*objectV2.GetRangeRequest,
|
|
|
|
*objectV2.GetRangeHashRequest,
|
|
|
|
*objectV2.DeleteRequest:
|
2022-05-31 17:00:41 +00:00
|
|
|
if h.obj == nil {
|
2022-05-24 18:47:21 +00:00
|
|
|
return nil, errMissingOID
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
return addressHeaders(h.cnr, h.obj), nil
|
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())
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
return headersFromObject(object.NewFromV2(oV2), h.cnr, h.obj), nil
|
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 {
|
|
|
|
return nil, fmt.Errorf("can't parse container ID: %w", err)
|
|
|
|
}
|
2022-05-12 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:32:03 +00:00
|
|
|
return []eaclSDK.Header{cidHeader(cnr)}, nil
|
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) {
|
|
|
|
case *objectV2.GetResponse:
|
|
|
|
if v, ok := resp.GetBody().GetObjectPart().(*objectV2.GetObjectPartInit); ok {
|
|
|
|
oV2 := new(objectV2.Object)
|
|
|
|
oV2.SetObjectID(v.GetObjectID())
|
|
|
|
oV2.SetHeader(v.GetHeader())
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
return headersFromObject(object.NewFromV2(oV2), h.cnr, h.obj), nil
|
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
|
2022-05-31 17:00:41 +00:00
|
|
|
h.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)
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
return headersFromObject(object.NewFromV2(oV2), h.cnr, h.obj), nil
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
return nil, nil
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (h *cfg) localObjectHeaders(cnr cid.ID, idObj *oid.ID) ([]eaclSDK.Header, error) {
|
|
|
|
if idObj != nil {
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnr)
|
|
|
|
addr.SetObject(*idObj)
|
2022-05-24 18:47:21 +00:00
|
|
|
|
2022-06-02 14:11:26 +00:00
|
|
|
obj, err := h.storage.Head(addr)
|
2022-05-24 18:47:21 +00:00
|
|
|
if err == nil {
|
2022-05-31 17:00:41 +00:00
|
|
|
return headersFromObject(obj, cnr, idObj), nil
|
2022-05-24 18:47:21 +00:00
|
|
|
}
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
2022-05-24 18:47:21 +00:00
|
|
|
|
|
|
|
// Still parse addressHeaders, because the errors is ignored in some places.
|
2022-06-02 14:11:26 +00:00
|
|
|
return addressHeaders(cnr, idObj), nil
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|