2021-05-19 12:18:07 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2021-05-19 12:26:41 +00:00
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/sha256"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-05-19 16:32:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
|
2021-05-19 12:18:07 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event/container"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/eacl"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2021-05-19 12:18:07 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (cp *Processor) processSetEACL(e container.SetEACL) {
|
|
|
|
if !cp.alphabetState.IsAlphabet() {
|
|
|
|
cp.log.Info("non alphabet mode, ignore set EACL")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := cp.checkSetEACL(e)
|
|
|
|
if err != nil {
|
|
|
|
cp.log.Error("set EACL check failed",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cp.approveSetEACL(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *Processor) checkSetEACL(e container.SetEACL) error {
|
2021-05-19 12:26:41 +00:00
|
|
|
// verify signature
|
|
|
|
key, err := keys.NewPublicKeyFromBytes(e.PublicKey(), elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid key: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-05-27 12:07:39 +00:00
|
|
|
binTable := e.Table()
|
|
|
|
tableHash := sha256.Sum256(binTable)
|
2021-05-19 12:26:41 +00:00
|
|
|
|
|
|
|
if !key.Verify(e.Signature(), tableHash[:]) {
|
|
|
|
return errors.New("invalid signature")
|
|
|
|
}
|
|
|
|
|
2021-05-19 12:28:10 +00:00
|
|
|
// verify the identity of the container owner
|
|
|
|
|
|
|
|
// unmarshal table
|
|
|
|
table := eacl.NewTable()
|
|
|
|
|
2021-05-27 12:07:39 +00:00
|
|
|
err = table.Unmarshal(binTable)
|
2021-05-19 12:28:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid binary table: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// receive owner of the related container
|
2021-05-19 16:32:25 +00:00
|
|
|
cnr, err := wrapper.Get(cp.cnrClient, table.CID())
|
2021-05-19 12:28:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not receive the container: %w", err)
|
|
|
|
}
|
|
|
|
|
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 = checkTokenContextWithCID(tok, table.CID(), func(c *session.ContainerContext) bool {
|
|
|
|
return c.IsForSetEACL()
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 12:07:39 +00:00
|
|
|
|
2021-05-19 12:28:10 +00:00
|
|
|
// check key ownership
|
2021-05-28 12:31:00 +00:00
|
|
|
return cp.checkKeyOwnership(cnr, key)
|
2021-05-19 12:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *Processor) approveSetEACL(e container.SetEACL) {
|
2021-09-08 08:20:11 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if nr := e.NotaryRequest(); nr != nil {
|
|
|
|
// setEACL event was received via Notary service
|
|
|
|
err = cp.cnrClient.Morph().NotarySignAndInvokeTX(nr.MainTransaction)
|
|
|
|
} else {
|
|
|
|
// setEACL event was received via notification service
|
|
|
|
err = cp.cnrClient.PutEACL(e.Table(), e.PublicKey(), e.Signature(), e.SessionToken())
|
|
|
|
}
|
2021-05-19 12:18:07 +00:00
|
|
|
if err != nil {
|
|
|
|
cp.log.Error("could not approve set EACL",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|