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"
|
2020-11-03 10:02:39 +00:00
|
|
|
containerSDK "github.com/nspcc-dev/neofs-api-go/pkg/container"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/container"
|
2020-08-11 12:10:01 +00:00
|
|
|
containerEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/container"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Process new container from the user by checking container sanity
|
|
|
|
// and sending approve tx back to morph.
|
|
|
|
func (cp *Processor) processContainerPut(put *containerEvent.Put) {
|
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-05-19 14:55:43 +00:00
|
|
|
err := cp.checkPutContainer(put)
|
|
|
|
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-05-19 14:55:43 +00:00
|
|
|
cp.approvePutContainer(put)
|
|
|
|
}
|
2020-11-03 10:02:39 +00:00
|
|
|
|
2021-05-19 14:55:43 +00:00
|
|
|
func (cp *Processor) checkPutContainer(e *containerEvent.Put) error {
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-19 16:16:54 +00:00
|
|
|
return cp.checkKeyOwnership(cnr, key)
|
2021-05-19 14:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *Processor) approvePutContainer(e *containerEvent.Put) {
|
2021-05-19 15:41:33 +00:00
|
|
|
// FIXME: here we should bind key to owner if needed
|
2021-05-25 15:55:31 +00:00
|
|
|
err := cp.cnrClient.Put(e.Container(), e.PublicKey(), e.Signature(), e.SessionToken())
|
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-19 16:42:29 +00:00
|
|
|
cid := e.ContainerID()
|
|
|
|
|
|
|
|
// receive owner of the related container
|
|
|
|
cnr, err := cp.cnrClient.Get(cid)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not receive the container: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// receive all owner keys
|
|
|
|
ownerKeys, err := cp.idClient.AccountKeys(cnr.OwnerID())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not received owner keys %s: %w", cnr.OwnerID(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify signature
|
|
|
|
cidHash := sha256.Sum256(cid)
|
|
|
|
sig := e.Signature()
|
|
|
|
|
|
|
|
for _, ownerKey := range ownerKeys {
|
|
|
|
if ownerKey.Verify(sig, cidHash[:]) {
|
|
|
|
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-05-19 16:00:28 +00:00
|
|
|
// FIXME: here we should try notary invoke
|
|
|
|
err := cp.cnrClient.Delete(e.ContainerID(), e.Signature())
|
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
|
|
|
}
|
|
|
|
}
|