2020-09-29 12:37:19 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-05-31 08:55:40 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
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-05-31 08:55:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-02-15 08:28:42 +00:00
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
|
|
|
"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"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
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/owner"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/storagegroup"
|
2020-09-29 12:37:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FormatValidator represents object format validator.
|
2020-10-03 10:14:09 +00:00
|
|
|
type FormatValidator struct {
|
|
|
|
*cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// FormatValidatorOption represents FormatValidator constructor option.
|
|
|
|
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-02-16 15:49:19 +00:00
|
|
|
// DeleteObjects objects places objects to removal queue.
|
|
|
|
//
|
|
|
|
// Returns apistatus.IrregularObjectLock if at least one object
|
|
|
|
// is locked.
|
|
|
|
DeleteObjects(*addressSDK.Address, ...*addressSDK.Address) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Locker is an object lock storage interface.
|
|
|
|
type Locker interface {
|
|
|
|
// Lock list of objects as locked by locker in the specified container.
|
|
|
|
//
|
|
|
|
// Returns apistatus.IrregularObjectLock if at least object in locked
|
|
|
|
// 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")
|
|
|
|
|
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.
|
|
|
|
//
|
2020-09-29 12:37:19 +00:00
|
|
|
// Returns nil error if object has valid structure.
|
2022-03-03 14:19:05 +00:00
|
|
|
func (v *FormatValidator) Validate(obj *object.Object) error {
|
2020-09-29 12:37:19 +00:00
|
|
|
if obj == nil {
|
|
|
|
return errNilObject
|
2020-11-16 09:43:52 +00:00
|
|
|
} else if obj.ID() == nil {
|
2020-09-29 12:37:19 +00:00
|
|
|
return errNilID
|
2020-11-16 09:43:52 +00:00
|
|
|
} else if obj.ContainerID() == nil {
|
2020-09-29 12:37:19 +00:00
|
|
|
return errNilCID
|
|
|
|
}
|
|
|
|
|
2021-10-14 17:25:16 +00:00
|
|
|
if err := v.checkOwner(obj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-23 13:30:14 +00:00
|
|
|
|
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
|
|
|
|
2021-10-14 17:25:16 +00:00
|
|
|
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
|
|
|
|
2021-10-14 17:25:16 +00:00
|
|
|
if err := v.checkExpiration(obj); err != nil {
|
|
|
|
return fmt.Errorf("object did not pass expiration check: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
if err := object.CheckHeaderVerificationFields(obj); err != nil {
|
2021-10-14 17:25:16 +00:00
|
|
|
return fmt.Errorf("(%T) could not validate header fields: %w", v, err)
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
if obj = obj.Parent(); obj != nil {
|
2021-10-14 17:25:16 +00:00
|
|
|
return v.Validate(obj)
|
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 {
|
2020-11-16 09:43:52 +00:00
|
|
|
token := obj.SessionToken()
|
|
|
|
key := obj.Signature().Key()
|
2020-09-29 12:37:19 +00:00
|
|
|
|
|
|
|
if token == nil || !bytes.Equal(token.SessionKey(), key) {
|
2020-11-16 09:43:52 +00:00
|
|
|
return v.checkOwnerKey(obj.OwnerID(), obj.Signature().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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *FormatValidator) checkOwnerKey(id *owner.ID, key []byte) error {
|
2021-05-31 08:55:40 +00:00
|
|
|
pub, err := keys.NewPublicKeyFromBytes(key, elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-21 12:15:10 +00:00
|
|
|
id2 := owner.NewIDFromPublicKey((*ecdsa.PublicKey)(pub))
|
2020-09-29 12:37:19 +00:00
|
|
|
|
2021-05-31 10:30:59 +00:00
|
|
|
if !id.Equal(id2) {
|
|
|
|
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
|
|
|
|
2020-09-30 17:53:12 +00:00
|
|
|
// ValidateContent validates payload content according to 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
|
|
|
}
|
|
|
|
|
2021-02-19 09:29:42 +00:00
|
|
|
// check if tombstone has the same expiration in body and header
|
|
|
|
exp, err := expirationEpochAttribute(o)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exp != tombstone.ExpirationEpoch() {
|
|
|
|
return errTombstoneExpiration
|
|
|
|
}
|
|
|
|
|
|
|
|
// mark all objects from tombstone body as removed in storage engine
|
2020-12-11 08:03:27 +00:00
|
|
|
cid := o.ContainerID()
|
|
|
|
idList := tombstone.Members()
|
2022-01-26 12:11:13 +00:00
|
|
|
addrList := make([]*addressSDK.Address, 0, len(idList))
|
2020-10-03 10:14:09 +00:00
|
|
|
|
2020-12-11 08:03:27 +00:00
|
|
|
for _, id := range idList {
|
|
|
|
if id == nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) empty member in tombstone", v)
|
2020-10-01 17:14:10 +00:00
|
|
|
}
|
2020-10-03 10:14:09 +00:00
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
a := addressSDK.NewAddress()
|
2020-12-11 08:03:27 +00:00
|
|
|
a.SetContainerID(cid)
|
|
|
|
a.SetObjectID(id)
|
|
|
|
|
|
|
|
addrList = append(addrList, a)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
sg := storagegroup.New()
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range sg.Members() {
|
|
|
|
if id == nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) empty member in SG", v)
|
2020-12-17 16:54:38 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-16 15:49:19 +00:00
|
|
|
case object.TypeLock:
|
|
|
|
if len(o.Payload()) == 0 {
|
|
|
|
return errors.New("empty payload in lock")
|
|
|
|
}
|
|
|
|
|
|
|
|
var lock object.Lock
|
|
|
|
|
|
|
|
err := lock.Unmarshal(o.Payload())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode lock payload: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.locker != nil {
|
|
|
|
// mark all objects from lock list as locked in storage engine
|
|
|
|
locklist := make([]oid.ID, lock.NumberOfMembers())
|
|
|
|
lock.ReadMembers(locklist)
|
|
|
|
|
|
|
|
err = v.locker.Lock(*o.ContainerID(), *o.ID(), locklist)
|
|
|
|
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 {
|
2021-10-14 17:25:16 +00:00
|
|
|
// TODO: use appropriate functionality after neofs-api-go#352
|
|
|
|
if len(obj.OwnerID().ToV2().GetValue()) != owner.NEO3WalletSize {
|
|
|
|
return errIncorrectOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-15 08:28:42 +00:00
|
|
|
// WithNetState returns options to set network state interface.
|
|
|
|
func WithNetState(netState netmap.State) FormatValidatorOption {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.netState = netState
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 10:14:09 +00:00
|
|
|
// WithDeleteHandler returns option to set delete queue processor.
|
|
|
|
func WithDeleteHandler(v DeleteHandler) FormatValidatorOption {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.deleteHandler = v
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 15:49:19 +00:00
|
|
|
|
|
|
|
// WithLocker returns option to set object lock storage.
|
|
|
|
func WithLocker(v Locker) FormatValidatorOption {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.locker = v
|
|
|
|
}
|
|
|
|
}
|