2020-08-11 12:10:01 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2024-03-11 14:55:50 +00:00
|
|
|
"errors"
|
2021-05-19 14:55:43 +00:00
|
|
|
"fmt"
|
2023-11-20 14:03:19 +00:00
|
|
|
"strings"
|
2021-05-19 14:55:43 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
|
|
|
|
containerEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event/container"
|
|
|
|
containerSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
2021-10-12 14:56:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2023-11-20 14:03:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
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
|
|
|
}
|
|
|
|
|
2024-03-11 14:55:50 +00:00
|
|
|
var errContainerAndOwnerNamespaceDontMatch = errors.New("container and owner namespaces do not match")
|
|
|
|
|
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.
|
2023-05-26 10:24:41 +00:00
|
|
|
func (cp *Processor) processContainerPut(put putEvent) bool {
|
2021-03-23 15:20:44 +00:00
|
|
|
if !cp.alphabetState.IsAlphabet() {
|
2023-04-12 14:35:10 +00:00
|
|
|
cp.log.Info(logs.ContainerNonAlphabetModeIgnoreContainerPut)
|
2023-05-26 10:24:41 +00:00
|
|
|
return true
|
2020-08-11 12:10:01 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-04-12 14:35:10 +00:00
|
|
|
cp.log.Error(logs.ContainerPutContainerCheckFailed,
|
2020-11-03 10:02:39 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
return false
|
2020-11-03 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
if err := cp.morphClient.NotarySignAndInvokeTX(ctx.e.NotaryRequest().MainTransaction); err != nil {
|
|
|
|
cp.log.Error(logs.ContainerCouldNotApprovePutContainer,
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
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
|
|
|
}
|
|
|
|
|
2022-04-29 17:48:11 +00:00
|
|
|
// check homomorphic hashing setting
|
|
|
|
err = checkHomomorphicHashing(cp.netState, cnr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("incorrect homomorphic hashing setting: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:56:17 +00:00
|
|
|
// check native name and zone
|
2023-11-20 14:03:19 +00:00
|
|
|
err = cp.checkNNS(ctx, cnr)
|
2021-10-12 14:56:17 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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.
|
2023-05-26 10:24:41 +00:00
|
|
|
func (cp *Processor) processContainerDelete(e containerEvent.Delete) bool {
|
2021-03-23 15:20:44 +00:00
|
|
|
if !cp.alphabetState.IsAlphabet() {
|
2023-04-12 14:35:10 +00:00
|
|
|
cp.log.Info(logs.ContainerNonAlphabetModeIgnoreContainerDelete)
|
2023-05-26 10:24:41 +00:00
|
|
|
return true
|
2020-09-02 16:04:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 12:37:01 +00:00
|
|
|
err := cp.checkDeleteContainer(e)
|
2021-05-19 14:55:43 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
cp.log.Error(logs.ContainerDeleteContainerCheckFailed,
|
2021-05-19 14:55:43 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
return false
|
2021-05-19 14:55:43 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
if err := cp.morphClient.NotarySignAndInvokeTX(e.NotaryRequest().MainTransaction); err != nil {
|
|
|
|
cp.log.Error(logs.ContainerCouldNotApproveDeleteContainer,
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2021-05-19 14:55:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 09:05:33 +00:00
|
|
|
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,
|
2023-06-01 08:55:06 +00:00
|
|
|
binPublicKey: e.PublicKeyValue,
|
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
|
|
|
}
|
|
|
|
|
2023-11-20 14:03:19 +00:00
|
|
|
func (cp *Processor) checkNNS(ctx *putContainerContext, cnr containerSDK.Container) error {
|
2022-06-28 07:01:05 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 14:03:19 +00:00
|
|
|
addr, err := util.Uint160DecodeBytesBE(cnr.Owner().WalletBytes()[1 : 1+util.Uint160Size])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get container owner address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
subject, err := cp.frostFSIDClient.GetSubject(addr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get subject from FrostfsID contract: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-07-29 08:31:05 +00:00
|
|
|
namespace, hasNamespace := strings.CutSuffix(ctx.d.Zone(), ".ns")
|
|
|
|
if !hasNamespace {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-20 14:03:19 +00:00
|
|
|
if subject.Namespace != namespace {
|
2024-03-11 14:55:50 +00:00
|
|
|
return errContainerAndOwnerNamespaceDontMatch
|
2023-11-20 14:03:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 14:56:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-12-01 12:45:35 +00:00
|
|
|
|
2022-04-29 17:48:11 +00:00
|
|
|
func checkHomomorphicHashing(ns NetworkState, cnr containerSDK.Container) error {
|
|
|
|
netSetting, err := ns.HomomorphicHashDisabled()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get setting in contract: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cnrSetting := containerSDK.IsHomomorphicHashingDisabled(cnr); netSetting != cnrSetting {
|
|
|
|
return fmt.Errorf("network setting: %t, container setting: %t", netSetting, cnrSetting)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|