2020-08-11 12:10:01 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2021-05-19 16:08:07 +00:00
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/sha256"
|
|
|
|
"errors"
|
2021-05-19 14:55:43 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-05-19 16:08:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-10-12 14:56:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2021-05-28 12:39:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2020-11-03 10:02:39 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/container"
|
2022-01-31 13:34:01 +00:00
|
|
|
cntClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/container"
|
2022-01-31 11:04:59 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid"
|
2021-12-01 12:45:35 +00:00
|
|
|
morphsubnet "github.com/nspcc-dev/neofs-node/pkg/morph/client/subnet"
|
2021-10-12 14:56:17 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
2020-08-11 12:10:01 +00:00
|
|
|
containerEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/container"
|
2021-11-10 07:08:33 +00:00
|
|
|
containerSDK "github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2021-12-01 12:45:35 +00:00
|
|
|
subnetid "github.com/nspcc-dev/neofs-sdk-go/subnet/id"
|
2020-08-11 12:10:01 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-10-12 14:56:17 +00:00
|
|
|
// putEvent is a common interface of Put and PutNamed event.
|
|
|
|
type putEvent interface {
|
|
|
|
event.Event
|
|
|
|
Container() []byte
|
|
|
|
PublicKey() []byte
|
|
|
|
Signature() []byte
|
|
|
|
SessionToken() []byte
|
|
|
|
NotaryRequest() *payload.P2PNotaryRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type putContainerContext struct {
|
|
|
|
e putEvent
|
|
|
|
|
|
|
|
name, zone string // from container structure
|
|
|
|
}
|
|
|
|
|
2020-08-11 12:10:01 +00:00
|
|
|
// Process new container from the user by checking container sanity
|
|
|
|
// and sending approve tx back to morph.
|
2021-10-12 14:56:17 +00:00
|
|
|
func (cp *Processor) processContainerPut(put putEvent) {
|
2021-03-23 15:20:44 +00:00
|
|
|
if !cp.alphabetState.IsAlphabet() {
|
|
|
|
cp.log.Info("non alphabet mode, ignore container put")
|
2020-08-11 12:10:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:56:17 +00:00
|
|
|
ctx := &putContainerContext{
|
|
|
|
e: put,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := cp.checkPutContainer(ctx)
|
2021-05-19 14:55:43 +00:00
|
|
|
if err != nil {
|
|
|
|
cp.log.Error("put container check failed",
|
2020-11-03 10:02:39 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:56:17 +00:00
|
|
|
cp.approvePutContainer(ctx)
|
2021-05-19 14:55:43 +00:00
|
|
|
}
|
2020-11-03 10:02:39 +00:00
|
|
|
|
2021-10-12 14:56:17 +00:00
|
|
|
func (cp *Processor) checkPutContainer(ctx *putContainerContext) error {
|
|
|
|
e := ctx.e
|
|
|
|
|
2021-05-19 16:08:07 +00:00
|
|
|
// verify signature
|
|
|
|
key, err := keys.NewPublicKeyFromBytes(e.PublicKey(), elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
binCnr := e.Container()
|
|
|
|
tableHash := sha256.Sum256(binCnr)
|
|
|
|
|
|
|
|
if !key.Verify(e.Signature(), tableHash[:]) {
|
|
|
|
return errors.New("invalid signature")
|
|
|
|
}
|
|
|
|
|
2021-05-19 14:55:43 +00:00
|
|
|
// unmarshal container structure
|
|
|
|
cnr := containerSDK.New()
|
|
|
|
|
2021-05-19 16:08:07 +00:00
|
|
|
err = cnr.Unmarshal(binCnr)
|
2021-05-19 14:55:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid binary container: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-12-01 12:45:35 +00:00
|
|
|
// check owner allowance in the subnetwork
|
|
|
|
err = checkSubnet(cp.subnetClient, cnr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("incorrect subnetwork: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:56:17 +00:00
|
|
|
// check native name and zone
|
|
|
|
err = checkNNS(ctx, cnr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("NNS: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-05-19 14:55:43 +00:00
|
|
|
// perform format check
|
|
|
|
err = container.CheckFormat(cnr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("incorrect container format: %w", err)
|
2020-11-03 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 12:07:39 +00:00
|
|
|
// unmarshal session token if presented
|
|
|
|
tok, err := tokenFromEvent(e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-28 12:39:27 +00:00
|
|
|
if tok != nil {
|
|
|
|
// check token context
|
|
|
|
err = checkTokenContext(tok, func(c *session.ContainerContext) bool {
|
|
|
|
return c.IsForPut()
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 12:07:39 +00:00
|
|
|
|
|
|
|
cnr.SetSessionToken(tok)
|
|
|
|
|
2021-05-19 16:16:54 +00:00
|
|
|
return cp.checkKeyOwnership(cnr, key)
|
2021-05-19 14:55:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 14:56:17 +00:00
|
|
|
func (cp *Processor) approvePutContainer(ctx *putContainerContext) {
|
|
|
|
e := ctx.e
|
|
|
|
|
2021-08-25 11:35:17 +00:00
|
|
|
var err error
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
prm := cntClient.PutPrm{}
|
2021-11-12 15:19:05 +00:00
|
|
|
|
|
|
|
prm.SetContainer(e.Container())
|
|
|
|
prm.SetKey(e.PublicKey())
|
|
|
|
prm.SetSignature(e.Signature())
|
|
|
|
prm.SetToken(e.SessionToken())
|
|
|
|
prm.SetName(ctx.name)
|
|
|
|
prm.SetZone(ctx.zone)
|
|
|
|
|
2021-08-25 11:35:17 +00:00
|
|
|
if nr := e.NotaryRequest(); nr != nil {
|
|
|
|
// put event was received via Notary service
|
|
|
|
err = cp.cnrClient.Morph().NotarySignAndInvokeTX(nr.MainTransaction)
|
|
|
|
} else {
|
|
|
|
// put event was received via notification service
|
2021-11-12 15:19:05 +00:00
|
|
|
err = cp.cnrClient.Put(prm)
|
2021-08-25 11:35:17 +00:00
|
|
|
}
|
2020-08-11 12:10:01 +00:00
|
|
|
if err != nil {
|
2021-05-19 14:55:43 +00:00
|
|
|
cp.log.Error("could not approve put container",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2020-08-11 12:10:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-02 16:04:29 +00:00
|
|
|
|
|
|
|
// Process delete container operation from the user by checking container sanity
|
|
|
|
// and sending approve tx back to morph.
|
|
|
|
func (cp *Processor) processContainerDelete(delete *containerEvent.Delete) {
|
2021-03-23 15:20:44 +00:00
|
|
|
if !cp.alphabetState.IsAlphabet() {
|
|
|
|
cp.log.Info("non alphabet mode, ignore container delete")
|
2020-09-02 16:04:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-19 14:55:43 +00:00
|
|
|
err := cp.checkDeleteContainer(delete)
|
|
|
|
if err != nil {
|
|
|
|
cp.log.Error("delete container check failed",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cp.approveDeleteContainer(delete)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *Processor) checkDeleteContainer(e *containerEvent.Delete) error {
|
2021-05-28 12:39:27 +00:00
|
|
|
binCID := e.ContainerID()
|
2021-05-19 16:42:29 +00:00
|
|
|
|
|
|
|
// receive owner of the related container
|
2021-05-28 12:39:27 +00:00
|
|
|
cnr, err := cp.cnrClient.Get(binCID)
|
2021-05-19 16:42:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not receive the container: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-05-27 12:07:39 +00:00
|
|
|
token, err := tokenFromEvent(e)
|
2021-05-19 16:42:29 +00:00
|
|
|
if err != nil {
|
2021-05-27 12:07:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var checkKeys keys.PublicKeys
|
|
|
|
|
|
|
|
if token != nil {
|
2021-05-28 12:39:27 +00:00
|
|
|
// check token context
|
2022-02-07 13:34:02 +00:00
|
|
|
// TODO: #1147 think how to avoid version casts
|
2021-05-28 12:39:27 +00:00
|
|
|
idV2 := new(refs.ContainerID)
|
|
|
|
idV2.SetValue(binCID)
|
|
|
|
|
|
|
|
id := cid.NewFromV2(idV2)
|
|
|
|
|
|
|
|
err = checkTokenContextWithCID(token, id, func(c *session.ContainerContext) bool {
|
|
|
|
return c.IsForDelete()
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-27 12:07:39 +00:00
|
|
|
key, err := keys.NewPublicKeyFromBytes(token.SessionKey(), elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid session key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check token ownership
|
|
|
|
err = cp.checkKeyOwnershipWithToken(cnr, key, token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
checkKeys = keys.PublicKeys{key}
|
|
|
|
} else {
|
2021-11-11 08:59:14 +00:00
|
|
|
prm := neofsid.AccountKeysPrm{}
|
|
|
|
prm.SetID(cnr.OwnerID())
|
|
|
|
|
2021-05-27 12:07:39 +00:00
|
|
|
// receive all owner keys from NeoFS ID contract
|
2021-11-11 08:59:14 +00:00
|
|
|
checkKeys, err = cp.idClient.AccountKeys(prm)
|
2021-05-27 12:07:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not received owner keys %s: %w", cnr.OwnerID(), err)
|
|
|
|
}
|
2021-05-19 16:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// verify signature
|
2021-05-28 12:39:27 +00:00
|
|
|
cidHash := sha256.Sum256(binCID)
|
2021-05-19 16:42:29 +00:00
|
|
|
sig := e.Signature()
|
|
|
|
|
2021-05-27 12:07:39 +00:00
|
|
|
for _, key := range checkKeys {
|
|
|
|
if key.Verify(sig, cidHash[:]) {
|
2021-05-19 16:42:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("signature verification failed on all owner keys ")
|
2021-05-19 14:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *Processor) approveDeleteContainer(e *containerEvent.Delete) {
|
2021-09-08 08:17:19 +00:00
|
|
|
var err error
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
prm := cntClient.DeletePrm{}
|
2021-11-12 15:19:05 +00:00
|
|
|
|
|
|
|
prm.SetCID(e.ContainerID())
|
|
|
|
prm.SetSignature(e.Signature())
|
|
|
|
prm.SetToken(e.SessionToken())
|
|
|
|
|
2021-09-08 08:17:19 +00:00
|
|
|
if nr := e.NotaryRequest(); nr != nil {
|
|
|
|
// delete event was received via Notary service
|
|
|
|
err = cp.cnrClient.Morph().NotarySignAndInvokeTX(nr.MainTransaction)
|
|
|
|
} else {
|
|
|
|
// delete event was received via notification service
|
2021-11-12 15:19:05 +00:00
|
|
|
err = cp.cnrClient.Delete(prm)
|
2021-09-08 08:17:19 +00:00
|
|
|
}
|
2020-09-02 16:04:29 +00:00
|
|
|
if err != nil {
|
2021-05-19 14:55:43 +00:00
|
|
|
cp.log.Error("could not approve delete container",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2020-09-02 16:04:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-12 14:56:17 +00:00
|
|
|
|
|
|
|
func checkNNS(ctx *putContainerContext, cnr *containerSDK.Container) error {
|
|
|
|
// fetch native name and zone
|
|
|
|
ctx.name, ctx.zone = containerSDK.GetNativeNameWithZone(cnr)
|
|
|
|
|
|
|
|
// if PutNamed event => check if values in container correspond to args
|
|
|
|
if named, ok := ctx.e.(interface {
|
|
|
|
Name() string
|
|
|
|
Zone() string
|
|
|
|
}); ok {
|
|
|
|
if name := named.Name(); name != ctx.name {
|
|
|
|
return fmt.Errorf("names differ %s/%s", name, ctx.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if zone := named.Zone(); zone != ctx.zone {
|
|
|
|
return fmt.Errorf("zones differ %s/%s", zone, ctx.zone)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-01 12:45:35 +00:00
|
|
|
|
|
|
|
func checkSubnet(subCli *morphsubnet.Client, cnr *containerSDK.Container) error {
|
|
|
|
prm := morphsubnet.UserAllowedPrm{}
|
|
|
|
|
|
|
|
subID := cnr.PlacementPolicy().SubnetID()
|
|
|
|
if subID == nil || subnetid.IsZero(*subID) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rawSubID, err := subID.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not marshal container subnetwork: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ownerID, err := cnr.OwnerID().Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not marshal container ownerID: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
prm.SetID(rawSubID)
|
|
|
|
prm.SetClient(ownerID)
|
|
|
|
|
|
|
|
res, err := subCli.UserAllowed(prm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not check user in contract: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !res.Allowed() {
|
|
|
|
return fmt.Errorf("user is not allowed to create containers in %s subnetwork", subID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|