2020-09-21 13:33:49 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
2020-09-28 10:54:24 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
2020-09-21 16:30:43 +00:00
|
|
|
acl "github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
2020-09-21 13:33:49 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2020-09-21 16:30:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2020-10-21 17:30:54 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/util/signature"
|
2020-09-21 13:33:49 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
2020-10-21 17:30:54 +00:00
|
|
|
v2signature "github.com/nspcc-dev/neofs-api-go/v2/signature"
|
2020-09-21 13:33:49 +00:00
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
2020-09-21 16:30:43 +00:00
|
|
|
core "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2020-09-21 13:33:49 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
InnerRingFetcher interface {
|
|
|
|
InnerRingKeys() ([][]byte, error)
|
|
|
|
}
|
|
|
|
|
2020-10-02 11:39:32 +00:00
|
|
|
metaWithToken struct {
|
|
|
|
vheader *session.RequestVerificationHeader
|
|
|
|
token *session.SessionToken
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SenderClassifier struct {
|
2020-09-21 16:12:52 +00:00
|
|
|
innerRing InnerRingFetcher
|
2020-09-21 16:30:43 +00:00
|
|
|
netmap core.Source
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-09-28 10:54:24 +00:00
|
|
|
// fixme: update classifier constructor
|
2020-09-21 16:30:43 +00:00
|
|
|
func NewSenderClassifier(ir InnerRingFetcher, nm core.Source) SenderClassifier {
|
2020-09-21 16:12:52 +00:00
|
|
|
return SenderClassifier{
|
|
|
|
innerRing: ir,
|
|
|
|
netmap: nm,
|
|
|
|
}
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 16:12:52 +00:00
|
|
|
func (c SenderClassifier) Classify(
|
2020-10-02 11:39:32 +00:00
|
|
|
req metaWithToken,
|
2020-09-28 10:54:24 +00:00
|
|
|
cid *container.ID,
|
2020-10-21 17:39:14 +00:00
|
|
|
cnr *container.Container) (acl.Role, []byte, error) {
|
2020-10-02 11:39:32 +00:00
|
|
|
|
|
|
|
if cid == nil {
|
2020-10-21 17:39:14 +00:00
|
|
|
return 0, nil, errors.Wrap(ErrMalformedRequest, "container id is not set")
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ownerID, ownerKey, err := requestOwner(req)
|
2020-10-21 17:39:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 14:13:23 +00:00
|
|
|
ownerKeyInBytes := crypto.MarshalPublicKey(ownerKey)
|
|
|
|
|
2020-09-21 13:33:49 +00:00
|
|
|
// todo: get owner from neofs.id if present
|
|
|
|
|
|
|
|
// if request owner is the same as container owner, return RoleUser
|
2020-09-28 10:54:24 +00:00
|
|
|
if bytes.Equal(cnr.GetOwnerID().GetValue(), ownerID.ToV2().GetValue()) {
|
2020-10-21 17:39:14 +00:00
|
|
|
return acl.RoleUser, ownerKeyInBytes, nil
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isInnerRingNode, err := c.isInnerRingKey(ownerKeyInBytes)
|
|
|
|
if err != nil {
|
2020-10-21 17:39:14 +00:00
|
|
|
return 0, nil, errors.Wrap(err, "can't check if request from inner ring")
|
2020-09-21 13:33:49 +00:00
|
|
|
} else if isInnerRingNode {
|
2020-10-21 17:39:14 +00:00
|
|
|
return acl.RoleSystem, ownerKeyInBytes, nil
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 10:54:24 +00:00
|
|
|
isContainerNode, err := c.isContainerKey(ownerKeyInBytes, cid.ToV2().GetValue(), cnr)
|
2020-09-21 13:33:49 +00:00
|
|
|
if err != nil {
|
2020-10-21 17:39:14 +00:00
|
|
|
return 0, nil, errors.Wrap(err, "can't check if request from container node")
|
2020-09-21 13:33:49 +00:00
|
|
|
} else if isContainerNode {
|
2020-10-21 17:39:14 +00:00
|
|
|
return acl.RoleSystem, ownerKeyInBytes, nil
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if none of above, return RoleOthers
|
2020-10-21 17:39:14 +00:00
|
|
|
return acl.RoleOthers, ownerKeyInBytes, nil
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 11:39:32 +00:00
|
|
|
func requestOwner(req metaWithToken) (*owner.ID, *ecdsa.PublicKey, error) {
|
|
|
|
if req.vheader == nil {
|
|
|
|
return nil, nil, errors.Wrap(ErrMalformedRequest, "nil verification header")
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if session token is presented, use it as truth source
|
2020-10-21 17:30:54 +00:00
|
|
|
if req.token.GetBody() != nil {
|
|
|
|
// verify signature of session token
|
|
|
|
return ownerFromToken(req.token)
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise get original body signature
|
2020-10-02 11:39:32 +00:00
|
|
|
bodySignature := originalBodySignature(req.vheader)
|
2020-09-21 13:33:49 +00:00
|
|
|
if bodySignature == nil {
|
|
|
|
return nil, nil, errors.Wrap(ErrMalformedRequest, "nil at body signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
key := crypto.UnmarshalPublicKey(bodySignature.GetKey())
|
2020-09-21 16:30:43 +00:00
|
|
|
neo3wallet, err := owner.NEO3WalletFromPublicKey(key)
|
2020-09-21 13:33:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "can't create neo3 wallet")
|
|
|
|
}
|
|
|
|
|
2020-09-21 16:30:43 +00:00
|
|
|
// form user from public key
|
2020-09-28 10:54:24 +00:00
|
|
|
user := new(owner.ID)
|
|
|
|
user.SetNeo3Wallet(neo3wallet)
|
2020-09-21 13:33:49 +00:00
|
|
|
|
2020-09-21 16:30:43 +00:00
|
|
|
return user, key, nil
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 10:54:24 +00:00
|
|
|
func originalBodySignature(v *session.RequestVerificationHeader) *pkg.Signature {
|
2020-09-21 13:33:49 +00:00
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for v.GetOrigin() != nil {
|
|
|
|
v = v.GetOrigin()
|
|
|
|
}
|
|
|
|
|
2020-09-28 10:54:24 +00:00
|
|
|
return pkg.NewSignatureFromV2(v.GetBodySignature())
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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, cid []byte,
|
|
|
|
cnr *container.Container) (bool, error) {
|
|
|
|
|
|
|
|
// first check current netmap
|
2020-09-21 16:30:43 +00:00
|
|
|
nm, err := core.GetLatestNetworkMap(c.netmap)
|
2020-09-21 13:33:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
in, err := lookupKeyInContainer(nm, owner, cid, 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
|
2020-09-21 16:30:43 +00:00
|
|
|
nm, err = core.GetPreviousNetworkMap(c.netmap)
|
2020-09-21 13:33:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return lookupKeyInContainer(nm, owner, cid, cnr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupKeyInContainer(
|
2020-09-21 16:30:43 +00:00
|
|
|
nm *netmap.Netmap,
|
2020-09-21 13:33:49 +00:00
|
|
|
owner, cid []byte,
|
|
|
|
cnr *container.Container) (bool, error) {
|
|
|
|
|
|
|
|
cnrNodes, err := nm.GetContainerNodes(cnr.GetPlacementPolicy(), cid)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
flatCnrNodes := cnrNodes.Flatten() // we need single array to iterate on
|
|
|
|
for i := range flatCnrNodes {
|
|
|
|
if bytes.Equal(flatCnrNodes[i].InfoV2.GetPublicKey(), owner) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-10-21 17:30:54 +00:00
|
|
|
|
|
|
|
func ownerFromToken(token *session.SessionToken) (*owner.ID, *ecdsa.PublicKey, error) {
|
|
|
|
// 1. First check signature of session token.
|
|
|
|
signWrapper := v2signature.StableMarshalerWrapper{SM: token.GetBody()}
|
|
|
|
if err := signature.VerifyDataWithSource(signWrapper, func() (key, sig []byte) {
|
|
|
|
tokenSignature := token.GetSignature()
|
|
|
|
return tokenSignature.GetKey(), tokenSignature.GetSign()
|
|
|
|
}); err != nil {
|
|
|
|
return nil, nil, errors.Wrap(ErrMalformedRequest, "invalid session token signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Then check if session token owner issued the session token
|
|
|
|
tokenIssuerKey := crypto.UnmarshalPublicKey(token.GetSignature().GetKey())
|
|
|
|
tokenIssuerWallet, err := owner.NEO3WalletFromPublicKey(tokenIssuerKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(ErrMalformedRequest, "invalid token issuer key")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(token.GetBody().GetOwnerID().GetValue(), tokenIssuerWallet.Bytes()) {
|
|
|
|
// todo: in this case we can issue all owner keys from neofs.id and check once again
|
|
|
|
return nil, nil, errors.Wrap(ErrMalformedRequest, "invalid session token owner")
|
|
|
|
}
|
|
|
|
|
|
|
|
return owner.NewIDFromV2(token.GetBody().GetOwnerID()), tokenIssuerKey, nil
|
|
|
|
}
|