2020-08-11 12:10:01 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2021-05-19 14:55:43 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-10-12 14:56:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2022-01-31 13:34:01 +00:00
|
|
|
cntClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/container"
|
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
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
d containerSDK.Domain
|
2021-10-12 14:56:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Process a new container from the user by checking the container sanity
|
|
|
|
// and sending approve tx back to the 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 {
|
2022-05-18 15:20:08 +00:00
|
|
|
binCnr := ctx.e.Container()
|
2022-06-28 07:01:05 +00:00
|
|
|
var cnr containerSDK.Container
|
2021-10-12 14:56:17 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
err := cnr.Unmarshal(binCnr)
|
2021-05-19 16:08:07 +00:00
|
|
|
if err != nil {
|
2022-05-18 15:20:08 +00:00
|
|
|
return fmt.Errorf("invalid binary container: %w", err)
|
2021-05-19 16:08:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
err = cp.verifySignature(signatureVerificationData{
|
2022-06-28 07:01:05 +00:00
|
|
|
ownerContainer: cnr.Owner(),
|
2022-05-18 15:20:08 +00:00
|
|
|
verb: session.VerbContainerPut,
|
|
|
|
binTokenSession: ctx.e.SessionToken(),
|
|
|
|
binPublicKey: ctx.e.PublicKey(),
|
|
|
|
signature: ctx.e.Signature(),
|
|
|
|
signedData: binCnr,
|
|
|
|
})
|
2021-05-19 14:55:43 +00:00
|
|
|
if err != nil {
|
2022-05-18 15:20:08 +00:00
|
|
|
return fmt.Errorf("auth container creation: %w", err)
|
2021-05-19 14:55:43 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
return nil
|
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())
|
2022-06-28 07:01:05 +00:00
|
|
|
prm.SetName(ctx.d.Name())
|
|
|
|
prm.SetZone(ctx.d.Zone())
|
2021-11-12 15:19:05 +00:00
|
|
|
|
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 {
|
2022-05-31 17:00:41 +00:00
|
|
|
binCnr := e.ContainerID()
|
2021-05-19 16:42:29 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
var idCnr cid.ID
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
err := idCnr.Decode(binCnr)
|
2021-05-19 16:42:29 +00:00
|
|
|
if err != nil {
|
2022-05-18 15:20:08 +00:00
|
|
|
return fmt.Errorf("invalid container ID: %w", err)
|
2021-05-19 16:42:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
// receive owner of the related container
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr, err := cp.cnrClient.Get(binCnr)
|
2021-05-19 16:42:29 +00:00
|
|
|
if err != nil {
|
2022-05-18 15:20:08 +00:00
|
|
|
return fmt.Errorf("could not receive the container: %w", err)
|
2021-05-27 12:07:39 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
err = cp.verifySignature(signatureVerificationData{
|
2022-06-28 07:01:05 +00:00
|
|
|
ownerContainer: cnr.Value.Owner(),
|
2022-05-18 15:20:08 +00:00
|
|
|
verb: session.VerbContainerDelete,
|
|
|
|
idContainerSet: true,
|
|
|
|
idContainer: idCnr,
|
|
|
|
binTokenSession: e.SessionToken(),
|
|
|
|
signature: e.Signature(),
|
2022-05-31 17:00:41 +00:00
|
|
|
signedData: binCnr,
|
2022-05-18 15:20:08 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-05-25 15:17:48 +00:00
|
|
|
return fmt.Errorf("auth container removal: %w", err)
|
2021-05-19 16:42:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
return nil
|
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
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
func checkNNS(ctx *putContainerContext, cnr containerSDK.Container) error {
|
|
|
|
// fetch domain info
|
|
|
|
ctx.d = containerSDK.ReadDomain(cnr)
|
2021-10-12 14:56:17 +00:00
|
|
|
|
|
|
|
// if PutNamed event => check if values in container correspond to args
|
|
|
|
if named, ok := ctx.e.(interface {
|
|
|
|
Name() string
|
|
|
|
Zone() string
|
|
|
|
}); ok {
|
2022-06-28 07:01:05 +00:00
|
|
|
if name := named.Name(); name != ctx.d.Name() {
|
|
|
|
return fmt.Errorf("names differ %s/%s", name, ctx.d.Name())
|
2021-10-12 14:56:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
if zone := named.Zone(); zone != ctx.d.Zone() {
|
|
|
|
return fmt.Errorf("zones differ %s/%s", zone, ctx.d.Zone())
|
2021-10-12 14:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-01 12:45:35 +00:00
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
func checkSubnet(subCli *morphsubnet.Client, cnr containerSDK.Container) error {
|
2021-12-01 12:45:35 +00:00
|
|
|
prm := morphsubnet.UserAllowedPrm{}
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
subID := cnr.PlacementPolicy().Subnet()
|
2022-06-08 23:18:26 +00:00
|
|
|
if subnetid.IsZero(subID) {
|
2021-12-01 12:45:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-15 08:08:10 +00:00
|
|
|
prm.SetID(subID.Marshal())
|
2022-06-28 07:01:05 +00:00
|
|
|
prm.SetClient(cnr.Owner().WalletBytes())
|
2021-12-01 12:45:35 +00:00
|
|
|
|
|
|
|
res, err := subCli.UserAllowed(prm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not check user in contract: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !res.Allowed() {
|
2022-06-08 23:18:26 +00:00
|
|
|
return fmt.Errorf("user is not allowed to create containers in %v subnetwork", subID)
|
2021-12-01 12:45:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|