2023-08-07 06:54:47 +00:00
|
|
|
package object
|
2022-02-11 12:25:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-10-21 07:22:54 +00:00
|
|
|
"context"
|
2022-05-12 16:37:46 +00:00
|
|
|
"crypto/sha256"
|
2022-02-11 12:25:05 +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
|
|
|
core "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/acl"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2023-08-07 06:54:47 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-02-11 12:25:05 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2023-08-07 06:54:47 +00:00
|
|
|
type InnerRing interface {
|
|
|
|
InnerRingKeys() ([][]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type SenderClassifier struct {
|
2022-09-28 07:41:01 +00:00
|
|
|
log *logger.Logger
|
2023-08-07 06:54:47 +00:00
|
|
|
innerRing InnerRing
|
2022-02-11 12:25:05 +00:00
|
|
|
netmap core.Source
|
|
|
|
}
|
|
|
|
|
2023-08-07 06:54:47 +00:00
|
|
|
func NewSenderClassifier(innerRing InnerRing, netmap core.Source, log *logger.Logger) SenderClassifier {
|
|
|
|
return SenderClassifier{
|
|
|
|
log: log,
|
|
|
|
innerRing: innerRing,
|
|
|
|
netmap: netmap,
|
|
|
|
}
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 06:54:47 +00:00
|
|
|
type ClassifyResult struct {
|
|
|
|
Role acl.Role
|
|
|
|
Key []byte
|
|
|
|
}
|
2022-02-11 12:25:05 +00:00
|
|
|
|
2023-08-07 06:54:47 +00:00
|
|
|
func (c SenderClassifier) Classify(
|
2024-10-21 13:27:28 +00:00
|
|
|
ctx context.Context,
|
2023-08-07 06:54:47 +00:00
|
|
|
ownerID *user.ID,
|
|
|
|
ownerKey *keys.PublicKey,
|
|
|
|
idCnr cid.ID,
|
2023-10-31 11:56:55 +00:00
|
|
|
cnr container.Container,
|
|
|
|
) (res *ClassifyResult, err error) {
|
2022-02-11 12:25:05 +00:00
|
|
|
ownerKeyInBytes := ownerKey.Bytes()
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
// TODO: #767 get owner from frostfs.id if present
|
2022-02-11 12:25:05 +00:00
|
|
|
|
|
|
|
// if request owner is the same as container owner, return RoleUser
|
2022-06-28 07:01:05 +00:00
|
|
|
if ownerID.Equals(cnr.Owner()) {
|
2023-08-07 06:54:47 +00:00
|
|
|
return &ClassifyResult{
|
|
|
|
Role: acl.RoleOwner,
|
|
|
|
Key: ownerKeyInBytes,
|
2022-02-11 12:25:05 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
return c.IsInnerRingOrContainerNode(ctx, ownerKeyInBytes, idCnr, cnr)
|
2023-08-07 06:54:47 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
func (c SenderClassifier) IsInnerRingOrContainerNode(ctx context.Context, ownerKeyInBytes []byte, idCnr cid.ID, cnr container.Container) (*ClassifyResult, error) {
|
2022-02-11 12:25:05 +00:00
|
|
|
isInnerRingNode, err := c.isInnerRingKey(ownerKeyInBytes)
|
|
|
|
if err != nil {
|
|
|
|
// do not throw error, try best case matching
|
2024-10-21 13:27:28 +00:00
|
|
|
c.log.Debug(ctx, logs.V2CantCheckIfRequestFromInnerRing,
|
2022-02-11 12:25:05 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
} else if isInnerRingNode {
|
2023-08-07 06:54:47 +00:00
|
|
|
return &ClassifyResult{
|
|
|
|
Role: acl.RoleInnerRing,
|
|
|
|
Key: ownerKeyInBytes,
|
2022-02-11 12:25:05 +00:00
|
|
|
}, 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
|
2024-10-21 13:27:28 +00:00
|
|
|
c.log.Debug(ctx, logs.V2CantCheckIfRequestFromContainerNode,
|
2022-02-11 12:25:05 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
} else if isContainerNode {
|
2023-08-07 06:54:47 +00:00
|
|
|
return &ClassifyResult{
|
|
|
|
Role: acl.RoleContainer,
|
|
|
|
Key: ownerKeyInBytes,
|
2022-02-11 12:25:05 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if none of above, return RoleOthers
|
2023-08-07 06:54:47 +00:00
|
|
|
return &ClassifyResult{
|
|
|
|
Role: acl.RoleOthers,
|
|
|
|
Key: ownerKeyInBytes,
|
2022-02-11 12:25:05 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-08-07 06:54:47 +00:00
|
|
|
func (c SenderClassifier) isInnerRingKey(owner []byte) (bool, error) {
|
2022-02-11 12:25:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-08-07 06:54:47 +00:00
|
|
|
func (c SenderClassifier) isContainerKey(
|
2022-02-11 12:25:05 +00:00
|
|
|
owner, idCnr []byte,
|
2023-10-31 11:56:55 +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
|
|
|
|
}
|
|
|
|
|
2024-05-08 07:02:54 +00:00
|
|
|
in, err := LookupKeyInContainer(nm, owner, idCnr, cnr)
|
2022-02-11 12:25:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-08 07:02:54 +00:00
|
|
|
return LookupKeyInContainer(nm, owner, idCnr, cnr)
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-08 07:02:54 +00:00
|
|
|
func LookupKeyInContainer(
|
2022-06-08 23:18:26 +00:00
|
|
|
nm *netmap.NetMap,
|
2024-05-08 07:02:54 +00:00
|
|
|
pkey, idCnr []byte,
|
2023-10-31 11:56:55 +00:00
|
|
|
cnr container.Container,
|
|
|
|
) (bool, error) {
|
2022-06-28 07:01:05 +00:00
|
|
|
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] {
|
2024-05-08 07:02:54 +00:00
|
|
|
if bytes.Equal(cnrVectors[i][j].PublicKey(), pkey) {
|
2022-06-08 23:18:26 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|