2020-09-29 12:37:19 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2021-05-31 08:55:40 +00:00
|
|
|
"crypto/ecdsa"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-02-15 08:28:42 +00:00
|
|
|
"strconv"
|
2020-09-29 12:37:19 +00:00
|
|
|
|
2021-02-15 08:28:42 +00:00
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2022-05-16 13:15:31 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-02-15 08:28:42 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2022-02-16 15:49:19 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-05-18 15:20:08 +00:00
|
|
|
neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-02-16 15:49:19 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/storagegroup"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2020-09-29 12:37:19 +00:00
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// FormatValidator represents an object format validator.
|
2020-10-03 10:14:09 +00:00
|
|
|
type FormatValidator struct {
|
|
|
|
*cfg
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// FormatValidatorOption represents a FormatValidator constructor option.
|
2020-10-03 10:14:09 +00:00
|
|
|
type FormatValidatorOption func(*cfg)
|
|
|
|
|
|
|
|
type cfg struct {
|
|
|
|
deleteHandler DeleteHandler
|
2021-02-15 08:28:42 +00:00
|
|
|
|
|
|
|
netState netmap.State
|
2022-02-16 15:49:19 +00:00
|
|
|
|
|
|
|
locker Locker
|
2020-10-03 10:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteHandler is an interface of delete queue processor.
|
|
|
|
type DeleteHandler interface {
|
2022-04-21 11:28:05 +00:00
|
|
|
// DeleteObjects places objects to a removal queue.
|
2022-02-16 15:49:19 +00:00
|
|
|
//
|
2022-03-05 08:46:02 +00:00
|
|
|
// Returns apistatus.LockNonRegularObject if at least one object
|
2022-02-16 15:49:19 +00:00
|
|
|
// is locked.
|
2022-05-31 17:00:41 +00:00
|
|
|
DeleteObjects(oid.Address, ...oid.Address) error
|
2022-02-16 15:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Locker is an object lock storage interface.
|
|
|
|
type Locker interface {
|
|
|
|
// Lock list of objects as locked by locker in the specified container.
|
|
|
|
//
|
2022-03-05 08:46:02 +00:00
|
|
|
// Returns apistatus.LockNonRegularObject if at least object in locked
|
2022-02-16 15:49:19 +00:00
|
|
|
// list is irregular (not type of REGULAR).
|
|
|
|
Lock(idCnr cid.ID, locker oid.ID, locked []oid.ID) error
|
2020-10-03 10:14:09 +00:00
|
|
|
}
|
2020-09-29 12:37:19 +00:00
|
|
|
|
|
|
|
var errNilObject = errors.New("object is nil")
|
|
|
|
|
|
|
|
var errNilID = errors.New("missing identifier")
|
|
|
|
|
|
|
|
var errNilCID = errors.New("missing container identifier")
|
|
|
|
|
2021-02-19 09:29:42 +00:00
|
|
|
var errNoExpirationEpoch = errors.New("missing expiration epoch attribute")
|
|
|
|
|
|
|
|
var errTombstoneExpiration = errors.New("tombstone body and header contain different expiration values")
|
|
|
|
|
2022-06-07 17:49:38 +00:00
|
|
|
var errEmptySGMembers = errors.New("storage group with empty members list")
|
|
|
|
|
2020-10-03 10:14:09 +00:00
|
|
|
func defaultCfg() *cfg {
|
|
|
|
return new(cfg)
|
|
|
|
}
|
|
|
|
|
2020-09-29 12:37:19 +00:00
|
|
|
// NewFormatValidator creates, initializes and returns FormatValidator instance.
|
2020-10-03 10:14:09 +00:00
|
|
|
func NewFormatValidator(opts ...FormatValidatorOption) *FormatValidator {
|
|
|
|
cfg := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &FormatValidator{
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
2020-09-29 12:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates object format.
|
|
|
|
//
|
2020-09-30 17:53:12 +00:00
|
|
|
// Does not validate payload checksum and content.
|
2022-04-04 13:04:18 +00:00
|
|
|
// If unprepared is true, only fields set by user are validated.
|
2020-09-30 17:53:12 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns nil error if the object has valid structure.
|
2022-04-04 13:04:18 +00:00
|
|
|
func (v *FormatValidator) Validate(obj *object.Object, unprepared bool) error {
|
2020-09-29 12:37:19 +00:00
|
|
|
if obj == nil {
|
|
|
|
return errNilObject
|
2022-05-12 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, idSet := obj.ID()
|
|
|
|
if !unprepared && !idSet {
|
2020-09-29 12:37:19 +00:00
|
|
|
return errNilID
|
2022-05-12 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, cnrSet := obj.ContainerID()
|
|
|
|
if !cnrSet {
|
2020-09-29 12:37:19 +00:00
|
|
|
return errNilCID
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
if err := v.checkOwner(obj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-14 17:25:16 +00:00
|
|
|
if err := v.checkAttributes(obj); err != nil {
|
|
|
|
return fmt.Errorf("invalid attributes: %w", err)
|
|
|
|
}
|
2020-10-01 11:42:17 +00:00
|
|
|
|
2022-04-04 13:04:18 +00:00
|
|
|
if !unprepared {
|
|
|
|
if err := v.validateSignatureKey(obj); err != nil {
|
|
|
|
return fmt.Errorf("(%T) could not validate signature key: %w", v, err)
|
|
|
|
}
|
2021-02-15 08:28:42 +00:00
|
|
|
|
2022-04-04 13:04:18 +00:00
|
|
|
if err := v.checkExpiration(obj); err != nil {
|
|
|
|
return fmt.Errorf("object did not pass expiration check: %w", err)
|
|
|
|
}
|
2021-10-14 17:25:16 +00:00
|
|
|
|
2022-04-04 13:04:18 +00:00
|
|
|
if err := object.CheckHeaderVerificationFields(obj); err != nil {
|
|
|
|
return fmt.Errorf("(%T) could not validate header fields: %w", v, err)
|
|
|
|
}
|
2021-10-14 17:25:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
if obj = obj.Parent(); obj != nil {
|
2022-04-04 13:04:18 +00:00
|
|
|
// Parent object already exists.
|
|
|
|
return v.Validate(obj, false)
|
2020-09-29 12:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func (v *FormatValidator) validateSignatureKey(obj *object.Object) error {
|
2022-05-16 13:15:31 +00:00
|
|
|
// FIXME(@cthulhu-rider): temp solution, see neofs-sdk-go#233
|
|
|
|
sig := obj.Signature()
|
|
|
|
if sig == nil {
|
|
|
|
// TODO(@cthulhu-rider): #1387 use "const" error
|
|
|
|
return errors.New("missing signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
var sigV2 refs.Signature
|
|
|
|
sig.WriteToV2(&sigV2)
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
binKey := sigV2.GetKey()
|
|
|
|
|
|
|
|
var key neofsecdsa.PublicKey
|
|
|
|
|
|
|
|
err := key.Decode(binKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode public key: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-16 09:43:52 +00:00
|
|
|
token := obj.SessionToken()
|
2020-09-29 12:37:19 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
if token == nil || !token.AssertAuthKey(&key) {
|
2022-05-31 17:00:41 +00:00
|
|
|
return v.checkOwnerKey(*obj.OwnerID(), key)
|
2020-09-29 12:37:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 13:34:02 +00:00
|
|
|
// FIXME: #1159 perform token verification
|
2020-09-29 12:37:19 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (v *FormatValidator) checkOwnerKey(id user.ID, key neofsecdsa.PublicKey) error {
|
2022-05-17 13:59:46 +00:00
|
|
|
var id2 user.ID
|
2022-05-18 15:20:08 +00:00
|
|
|
user.IDFromKey(&id2, (ecdsa.PublicKey)(key))
|
2020-09-29 12:37:19 +00:00
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
if !id.Equals(id2) {
|
2021-05-31 10:30:59 +00:00
|
|
|
return fmt.Errorf("(%T) different owner identifiers %s/%s", v, id, id2)
|
2020-09-29 12:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-30 11:07:28 +00:00
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ValidateContent validates payload content according to the object type.
|
2022-03-03 14:19:05 +00:00
|
|
|
func (v *FormatValidator) ValidateContent(o *object.Object) error {
|
2020-12-01 11:23:28 +00:00
|
|
|
switch o.Type() {
|
2022-02-16 15:49:19 +00:00
|
|
|
case object.TypeRegular:
|
|
|
|
// ignore regular objects, they do not need payload formatting
|
2020-09-30 11:07:28 +00:00
|
|
|
case object.TypeTombstone:
|
2020-12-01 11:23:28 +00:00
|
|
|
if len(o.Payload()) == 0 {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) empty payload in tombstone", v)
|
2020-09-30 11:07:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 08:03:27 +00:00
|
|
|
tombstone := object.NewTombstone()
|
|
|
|
|
|
|
|
if err := tombstone.Unmarshal(o.Payload()); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not unmarshal tombstone content: %w", v, err)
|
2020-09-30 11:07:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// check if the tombstone has the same expiration in the body and the header
|
2021-02-19 09:29:42 +00:00
|
|
|
exp, err := expirationEpochAttribute(o)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exp != tombstone.ExpirationEpoch() {
|
|
|
|
return errTombstoneExpiration
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// mark all objects from the tombstone body as removed in the storage engine
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr, ok := o.ContainerID()
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing container ID")
|
|
|
|
}
|
|
|
|
|
2020-12-11 08:03:27 +00:00
|
|
|
idList := tombstone.Members()
|
2022-05-31 17:00:41 +00:00
|
|
|
addrList := make([]oid.Address, len(idList))
|
2020-10-03 10:14:09 +00:00
|
|
|
|
2022-03-15 11:16:46 +00:00
|
|
|
for i := range idList {
|
2022-05-31 17:00:41 +00:00
|
|
|
addrList[i].SetContainer(cnr)
|
|
|
|
addrList[i].SetObject(idList[i])
|
2020-12-11 08:03:27 +00:00
|
|
|
}
|
2020-12-01 11:23:28 +00:00
|
|
|
|
2020-10-03 10:14:09 +00:00
|
|
|
if v.deleteHandler != nil {
|
2022-02-16 15:49:19 +00:00
|
|
|
err = v.deleteHandler.DeleteObjects(AddressOf(o), addrList...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete objects from %s object content: %w", o.Type(), err)
|
|
|
|
}
|
2020-10-03 10:14:09 +00:00
|
|
|
}
|
2020-12-17 16:54:38 +00:00
|
|
|
case object.TypeStorageGroup:
|
|
|
|
if len(o.Payload()) == 0 {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) empty payload in SG", v)
|
2020-12-17 16:54:38 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 09:25:13 +00:00
|
|
|
var sg storagegroup.StorageGroup
|
2020-12-17 16:54:38 +00:00
|
|
|
|
|
|
|
if err := sg.Unmarshal(o.Payload()); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not unmarshal SG content: %w", v, err)
|
2020-12-17 16:54:38 +00:00
|
|
|
}
|
2022-06-07 17:49:38 +00:00
|
|
|
|
|
|
|
mm := sg.Members()
|
|
|
|
lenMM := len(mm)
|
|
|
|
if lenMM == 0 {
|
|
|
|
return errEmptySGMembers
|
|
|
|
}
|
|
|
|
|
|
|
|
uniqueFilter := make(map[oid.ID]struct{}, lenMM)
|
|
|
|
|
|
|
|
for i := 0; i < lenMM; i++ {
|
|
|
|
if _, alreadySeen := uniqueFilter[mm[i]]; alreadySeen {
|
|
|
|
return fmt.Errorf("storage group contains non-unique member: %s", mm[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
uniqueFilter[mm[i]] = struct{}{}
|
|
|
|
}
|
2022-02-16 15:49:19 +00:00
|
|
|
case object.TypeLock:
|
|
|
|
if len(o.Payload()) == 0 {
|
|
|
|
return errors.New("empty payload in lock")
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr, ok := o.ContainerID()
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing container")
|
|
|
|
}
|
|
|
|
|
|
|
|
id, ok := o.ID()
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing ID")
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:10:04 +00:00
|
|
|
// check that LOCK object has correct expiration epoch
|
|
|
|
lockExp, err := expirationEpochAttribute(o)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("lock object expiration epoch: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if currEpoch := v.netState.CurrentEpoch(); lockExp <= currEpoch {
|
|
|
|
return fmt.Errorf("lock object expiration: %d; current: %d", lockExp, currEpoch)
|
|
|
|
}
|
|
|
|
|
2022-02-16 15:49:19 +00:00
|
|
|
var lock object.Lock
|
|
|
|
|
2022-05-31 17:10:04 +00:00
|
|
|
err = lock.Unmarshal(o.Payload())
|
2022-02-16 15:49:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode lock payload: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.locker != nil {
|
2022-03-11 07:24:44 +00:00
|
|
|
num := lock.NumberOfMembers()
|
|
|
|
if num == 0 {
|
|
|
|
return errors.New("missing locked members")
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// mark all objects from lock list as locked in the storage engine
|
2022-03-11 07:24:44 +00:00
|
|
|
locklist := make([]oid.ID, num)
|
2022-02-16 15:49:19 +00:00
|
|
|
lock.ReadMembers(locklist)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
err = v.locker.Lock(cnr, id, locklist)
|
2022-02-16 15:49:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("lock objects from %s object content: %w", o.Type(), err)
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 15:15:16 +00:00
|
|
|
default:
|
2021-01-15 12:45:42 +00:00
|
|
|
// ignore all other object types, they do not need payload formatting
|
2020-09-30 11:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-03 10:14:09 +00:00
|
|
|
|
2021-02-15 08:28:42 +00:00
|
|
|
var errExpired = errors.New("object has expired")
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func (v *FormatValidator) checkExpiration(obj *object.Object) error {
|
2021-02-19 09:29:42 +00:00
|
|
|
exp, err := expirationEpochAttribute(obj)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errNoExpirationEpoch) {
|
|
|
|
return nil // objects without expiration attribute are valid
|
2021-02-15 08:28:42 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 09:29:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-02-15 08:28:42 +00:00
|
|
|
|
2021-02-19 09:29:42 +00:00
|
|
|
if exp < v.netState.CurrentEpoch() {
|
|
|
|
return errExpired
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func expirationEpochAttribute(obj *object.Object) (uint64, error) {
|
2021-02-19 09:29:42 +00:00
|
|
|
for _, a := range obj.Attributes() {
|
|
|
|
if a.Key() != objectV2.SysAttributeExpEpoch {
|
|
|
|
continue
|
2021-02-15 08:28:42 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 09:29:42 +00:00
|
|
|
return strconv.ParseUint(a.Value(), 10, 64)
|
2021-02-15 08:28:42 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 09:29:42 +00:00
|
|
|
return 0, errNoExpirationEpoch
|
2021-02-15 08:28:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 14:20:13 +00:00
|
|
|
var (
|
|
|
|
errDuplAttr = errors.New("duplication of attributes detected")
|
|
|
|
errEmptyAttrVal = errors.New("empty attribute value")
|
|
|
|
)
|
2021-06-23 13:30:14 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func (v *FormatValidator) checkAttributes(obj *object.Object) error {
|
2021-06-23 13:30:14 +00:00
|
|
|
as := obj.Attributes()
|
|
|
|
|
|
|
|
mUnique := make(map[string]struct{}, len(as))
|
|
|
|
|
|
|
|
for _, a := range as {
|
|
|
|
key := a.Key()
|
|
|
|
|
|
|
|
if _, was := mUnique[key]; was {
|
|
|
|
return errDuplAttr
|
|
|
|
}
|
|
|
|
|
2021-06-23 14:20:13 +00:00
|
|
|
if a.Value() == "" {
|
|
|
|
return errEmptyAttrVal
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:30:14 +00:00
|
|
|
mUnique[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-14 17:25:16 +00:00
|
|
|
var errIncorrectOwner = errors.New("incorrect object owner")
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func (v *FormatValidator) checkOwner(obj *object.Object) error {
|
2022-05-17 13:59:46 +00:00
|
|
|
if idOwner := obj.OwnerID(); idOwner == nil || len(idOwner.WalletBytes()) == 0 {
|
2021-10-14 17:25:16 +00:00
|
|
|
return errIncorrectOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// WithNetState returns options to set the network state interface.
|
2021-02-15 08:28:42 +00:00
|
|
|
func WithNetState(netState netmap.State) FormatValidatorOption {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.netState = netState
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// WithDeleteHandler returns an option to set delete queue processor.
|
2020-10-03 10:14:09 +00:00
|
|
|
func WithDeleteHandler(v DeleteHandler) FormatValidatorOption {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.deleteHandler = v
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 15:49:19 +00:00
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// WithLocker returns an option to set object lock storage.
|
2022-02-16 15:49:19 +00:00
|
|
|
func WithLocker(v Locker) FormatValidatorOption {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.locker = v
|
|
|
|
}
|
|
|
|
}
|