2022-02-11 12:25:05 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-05-12 16:37:46 +00:00
|
|
|
"crypto/sha256"
|
2022-02-11 12:25:05 +00:00
|
|
|
|
|
|
|
core "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
2022-06-17 13:40:51 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container/acl"
|
2022-05-31 17:00:41 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-02-11 12:25:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type senderClassifier struct {
|
|
|
|
log *zap.Logger
|
|
|
|
innerRing InnerRingFetcher
|
|
|
|
netmap core.Source
|
|
|
|
}
|
|
|
|
|
|
|
|
type classifyResult struct {
|
2022-06-17 13:40:51 +00:00
|
|
|
role acl.Role
|
2022-02-11 12:25:05 +00:00
|
|
|
key []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c senderClassifier) classify(
|
|
|
|
req MetaWithToken,
|
2022-05-31 17:00:41 +00:00
|
|
|
idCnr cid.ID,
|
2022-06-28 07:01:05 +00:00
|
|
|
cnr container.Container) (res *classifyResult, err error) {
|
2022-02-11 12:25:05 +00:00
|
|
|
ownerID, ownerKey, err := req.RequestOwner()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ownerKeyInBytes := ownerKey.Bytes()
|
|
|
|
|
|
|
|
// TODO: #767 get owner from neofs.id if present
|
|
|
|
|
|
|
|
// if request owner is the same as container owner, return RoleUser
|
2022-06-28 07:01:05 +00:00
|
|
|
if ownerID.Equals(cnr.Owner()) {
|
2022-02-11 12:25:05 +00:00
|
|
|
return &classifyResult{
|
2022-06-17 13:40:51 +00:00
|
|
|
role: acl.RoleOwner,
|
2022-02-11 12:25:05 +00:00
|
|
|
key: ownerKeyInBytes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
isInnerRingNode, err := c.isInnerRingKey(ownerKeyInBytes)
|
|
|
|
if err != nil {
|
|
|
|
// do not throw error, try best case matching
|
|
|
|
c.log.Debug("can't check if request from inner ring",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
} else if isInnerRingNode {
|
|
|
|
return &classifyResult{
|
2022-06-17 13:40:51 +00:00
|
|
|
role: acl.RoleInnerRing,
|
2022-02-11 12:25:05 +00:00
|
|
|
key: ownerKeyInBytes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
binCnr := make([]byte, sha256.Size)
|
|
|
|
idCnr.Encode(binCnr)
|
|
|
|
|
|
|
|
isContainerNode, err := c.isContainerKey(ownerKeyInBytes, binCnr, cnr)
|
2022-02-11 12:25:05 +00:00
|
|
|
if err != nil {
|
|
|
|
// error might happen if request has `RoleOther` key and placement
|
|
|
|
// is not possible for previous epoch, so
|
|
|
|
// do not throw error, try best case matching
|
|
|
|
c.log.Debug("can't check if request from container node",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
} else if isContainerNode {
|
|
|
|
return &classifyResult{
|
2022-06-17 13:40:51 +00:00
|
|
|
role: acl.RoleContainer,
|
2022-02-11 12:25:05 +00:00
|
|
|
key: ownerKeyInBytes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if none of above, return RoleOthers
|
|
|
|
return &classifyResult{
|
2022-06-17 13:40:51 +00:00
|
|
|
role: acl.RoleOthers,
|
2022-02-11 12:25:05 +00:00
|
|
|
key: ownerKeyInBytes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c senderClassifier) isInnerRingKey(owner []byte) (bool, error) {
|
|
|
|
innerRingKeys, err := c.innerRing.InnerRingKeys()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if request owner key in the inner ring list, return RoleSystem
|
|
|
|
for i := range innerRingKeys {
|
|
|
|
if bytes.Equal(innerRingKeys[i], owner) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c senderClassifier) isContainerKey(
|
|
|
|
owner, idCnr []byte,
|
2022-06-28 07:01:05 +00:00
|
|
|
cnr container.Container) (bool, error) {
|
2022-02-11 12:25:05 +00:00
|
|
|
nm, err := core.GetLatestNetworkMap(c.netmap) // first check current netmap
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
in, err := lookupKeyInContainer(nm, owner, idCnr, cnr)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
} else if in {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// then check previous netmap, this can happen in-between epoch change
|
|
|
|
// when node migrates data from last epoch container
|
|
|
|
nm, err = core.GetPreviousNetworkMap(c.netmap)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return lookupKeyInContainer(nm, owner, idCnr, cnr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupKeyInContainer(
|
2022-06-08 23:18:26 +00:00
|
|
|
nm *netmap.NetMap,
|
2022-02-11 12:25:05 +00:00
|
|
|
owner, idCnr []byte,
|
2022-06-28 07:01:05 +00:00
|
|
|
cnr container.Container) (bool, error) {
|
|
|
|
cnrVectors, err := nm.ContainerNodes(cnr.PlacementPolicy(), idCnr)
|
2022-02-11 12:25:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
for i := range cnrVectors {
|
|
|
|
for j := range cnrVectors[i] {
|
|
|
|
if bytes.Equal(cnrVectors[i][j].PublicKey(), owner) {
|
|
|
|
return true, nil
|
|
|
|
}
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|