2020-10-02 12:23:52 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-11-16 09:43:52 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
2020-10-02 12:23:52 +00:00
|
|
|
eaclSDK "github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl"
|
2021-05-31 11:03:17 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2020-10-02 12:23:52 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
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"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/acl/eacl"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
|
|
|
type cfg struct {
|
|
|
|
storage ObjectStorage
|
|
|
|
|
|
|
|
msg xHeaderSource
|
2020-12-15 09:06:04 +00:00
|
|
|
|
|
|
|
addr *refs.Address
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectStorage interface {
|
|
|
|
Head(*objectSDK.Address) (*object.Object, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Request interface {
|
|
|
|
GetMetaHeader() *session.RequestMetaHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response interface {
|
|
|
|
GetMetaHeader() *session.ResponseMetaHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
type headerSource struct {
|
|
|
|
*cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultCfg() *cfg {
|
|
|
|
return &cfg{
|
|
|
|
storage: new(localStorage),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMessageHeaderSource(opts ...Option) eacl.TypedHeaderSource {
|
|
|
|
cfg := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &headerSource{
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *headerSource) HeadersOfType(typ eaclSDK.FilterHeaderType) ([]eacl.Header, bool) {
|
|
|
|
switch typ {
|
|
|
|
default:
|
|
|
|
return nil, true
|
|
|
|
case eaclSDK.HeaderFromRequest:
|
|
|
|
return requestHeaders(h.msg), true
|
|
|
|
case eaclSDK.HeaderFromObject:
|
2020-12-14 13:50:45 +00:00
|
|
|
return h.objectHeaders()
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func requestHeaders(msg xHeaderSource) []eacl.Header {
|
|
|
|
xHdrs := msg.GetXHeaders()
|
|
|
|
|
|
|
|
res := make([]eacl.Header, 0, len(xHdrs))
|
|
|
|
|
|
|
|
for i := range xHdrs {
|
2020-11-16 09:43:52 +00:00
|
|
|
res = append(res, pkg.NewXHeaderFromV2(xHdrs[i]))
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-12-14 13:50:45 +00:00
|
|
|
func (h *headerSource) objectHeaders() ([]eacl.Header, bool) {
|
2020-12-15 09:06:04 +00:00
|
|
|
var addr *objectSDK.Address
|
|
|
|
if h.addr != nil {
|
|
|
|
addr = objectSDK.NewAddressFromV2(h.addr)
|
|
|
|
}
|
|
|
|
|
2020-10-02 12:23:52 +00:00
|
|
|
switch m := h.msg.(type) {
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected message type %T", h.msg))
|
|
|
|
case *requestXHeaderSource:
|
|
|
|
switch req := m.req.(type) {
|
|
|
|
case *objectV2.GetRequest:
|
2020-12-15 09:06:04 +00:00
|
|
|
return h.localObjectHeaders(h.addr)
|
2020-10-02 12:23:52 +00:00
|
|
|
case *objectV2.HeadRequest:
|
2020-12-15 09:06:04 +00:00
|
|
|
return h.localObjectHeaders(h.addr)
|
2020-12-18 12:51:29 +00:00
|
|
|
case
|
|
|
|
*objectV2.GetRangeRequest,
|
|
|
|
*objectV2.GetRangeHashRequest,
|
|
|
|
*objectV2.DeleteRequest:
|
2020-12-15 09:06:04 +00:00
|
|
|
return addressHeaders(objectSDK.NewAddressFromV2(h.addr)), true
|
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())
|
|
|
|
|
2020-12-15 09:06:04 +00:00
|
|
|
if addr == nil {
|
|
|
|
addr = objectSDK.NewAddress()
|
2021-05-31 11:03:17 +00:00
|
|
|
addr.SetContainerID(cid.NewFromV2(v.GetHeader().GetContainerID()))
|
2020-12-15 09:06:04 +00:00
|
|
|
addr.SetObjectID(objectSDK.NewIDFromV2(v.GetObjectID()))
|
2020-12-14 13:50:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 09:06:04 +00:00
|
|
|
hs := headersFromObject(object.NewFromV2(oV2), addr)
|
|
|
|
|
2020-12-14 13:50:45 +00:00
|
|
|
return hs, true
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
case *objectV2.SearchRequest:
|
|
|
|
return []eacl.Header{cidHeader(
|
2021-05-31 11:03:17 +00:00
|
|
|
cid.NewFromV2(
|
2020-12-14 12:07:20 +00:00
|
|
|
req.GetBody().GetContainerID()),
|
2020-12-14 13:50:45 +00:00
|
|
|
)}, true
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
case *responseXHeaderSource:
|
|
|
|
switch resp := m.resp.(type) {
|
2020-12-14 12:07:20 +00:00
|
|
|
default:
|
2020-12-15 09:06:04 +00:00
|
|
|
hs, _ := h.localObjectHeaders(h.addr)
|
2020-12-14 13:50:45 +00:00
|
|
|
return hs, true
|
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())
|
|
|
|
|
2020-12-15 09:06:04 +00:00
|
|
|
return headersFromObject(object.NewFromV2(oV2), addr), true
|
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
|
|
|
|
2020-12-15 09:06:04 +00:00
|
|
|
hdr.SetContainerID(h.addr.GetContainerID())
|
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)
|
|
|
|
|
2020-12-15 09:06:04 +00:00
|
|
|
return headersFromObject(object.NewFromV2(oV2), addr), true
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 13:50:45 +00:00
|
|
|
return nil, true
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 13:50:45 +00:00
|
|
|
func (h *headerSource) localObjectHeaders(addrV2 *refs.Address) ([]eacl.Header, bool) {
|
2020-12-14 12:07:20 +00:00
|
|
|
addr := objectSDK.NewAddressFromV2(addrV2)
|
|
|
|
|
|
|
|
obj, err := h.storage.Head(addr)
|
2020-10-02 12:23:52 +00:00
|
|
|
if err == nil {
|
2020-12-15 09:06:04 +00:00
|
|
|
return headersFromObject(obj, addr), true
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 13:50:45 +00:00
|
|
|
return addressHeaders(addr), false
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func cidHeader(cid *cid.ID) eacl.Header {
|
2020-12-14 12:07:20 +00:00
|
|
|
return &sysObjHdr{
|
|
|
|
k: acl.FilterObjectContainerID,
|
|
|
|
v: cidValue(cid),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func oidHeader(oid *objectSDK.ID) eacl.Header {
|
|
|
|
return &sysObjHdr{
|
|
|
|
k: acl.FilterObjectID,
|
|
|
|
v: idValue(oid),
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addressHeaders(addr *objectSDK.Address) []eacl.Header {
|
|
|
|
res := make([]eacl.Header, 1, 2)
|
|
|
|
res[0] = cidHeader(addr.ContainerID())
|
2020-10-02 12:23:52 +00:00
|
|
|
|
2020-12-14 12:07:20 +00:00
|
|
|
if oid := addr.ObjectID(); oid != nil {
|
|
|
|
res = append(res, oidHeader(oid))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|