2022-04-22 13:30:20 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
2022-09-09 09:09:38 +00:00
|
|
|
"bytes"
|
2024-05-20 13:34:31 +00:00
|
|
|
"context"
|
2022-04-22 13:30:20 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
core "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/acl"
|
|
|
|
cidSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto"
|
|
|
|
frostfsecdsa "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2022-04-22 13:30:20 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
)
|
|
|
|
|
2022-05-05 11:00:30 +00:00
|
|
|
type message interface {
|
|
|
|
SignedDataSize() int
|
|
|
|
ReadSignedData([]byte) ([]byte, error)
|
|
|
|
GetSignature() *Signature
|
|
|
|
SetSignature(*Signature)
|
|
|
|
}
|
|
|
|
|
2022-09-08 12:44:27 +00:00
|
|
|
func eACLErr(op eacl.Operation, err error) error {
|
|
|
|
return fmt.Errorf("access to operation %s is denied by extended ACL check: %w", op, err)
|
|
|
|
}
|
|
|
|
|
2023-10-31 11:56:55 +00:00
|
|
|
var (
|
|
|
|
errBearerWrongContainer = errors.New("bearer token is created for another container")
|
|
|
|
errBearerSignature = errors.New("invalid bearer token signature")
|
|
|
|
)
|
2022-09-12 11:40:06 +00:00
|
|
|
|
2022-09-08 12:44:27 +00:00
|
|
|
// verifyClient verifies if the request for a client operation
|
|
|
|
// was signed by a key allowed by (e)ACL rules.
|
|
|
|
// Operation must be one of:
|
|
|
|
// - 1. ObjectPut;
|
|
|
|
// - 2. ObjectGet.
|
2024-05-20 13:34:31 +00:00
|
|
|
func (s *Service) verifyClient(ctx context.Context, req message, cid cidSDK.ID, rawBearer []byte, op acl.Op) error {
|
2022-05-05 11:00:30 +00:00
|
|
|
err := verifyMessage(req)
|
2022-04-22 13:30:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-28 11:10:06 +00:00
|
|
|
|
|
|
|
isAuthorized, err := s.isAuthorized(req, op)
|
|
|
|
if isAuthorized || err != nil {
|
|
|
|
return err
|
2023-06-23 08:25:08 +00:00
|
|
|
}
|
2022-04-22 13:30:20 +00:00
|
|
|
|
|
|
|
cnr, err := s.cnrSource.Get(cid)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get container %s: %w", cid, err)
|
|
|
|
}
|
|
|
|
|
2022-10-25 09:52:41 +00:00
|
|
|
eaclOp := eACLOp(op)
|
|
|
|
|
2023-06-28 11:10:06 +00:00
|
|
|
bt, err := parseBearer(rawBearer, cid, eaclOp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-10-25 09:52:41 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 15:10:33 +00:00
|
|
|
role, pubKey, err := roleAndPubKeyFromReq(cnr, req, bt)
|
2022-09-08 12:44:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get request role: %w", err)
|
|
|
|
}
|
2022-04-22 13:30:20 +00:00
|
|
|
|
2024-10-09 07:50:30 +00:00
|
|
|
return s.checkAPE(ctx, bt, cnr, cid, op, role, pubKey)
|
2022-05-05 11:00:30 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 11:10:06 +00:00
|
|
|
// Returns true iff the operation is read-only and request was signed
|
|
|
|
// with one of the authorized keys.
|
|
|
|
func (s *Service) isAuthorized(req message, op acl.Op) (bool, error) {
|
|
|
|
if op != acl.OpObjectGet {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sign := req.GetSignature()
|
|
|
|
if sign == nil {
|
|
|
|
return false, errors.New("missing signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
key := sign.GetKey()
|
|
|
|
for i := range s.authorizedKeys {
|
|
|
|
if bytes.Equal(s.authorizedKeys[i], key) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseBearer(rawBearer []byte, cid cidSDK.ID, eaclOp eacl.Operation) (*bearer.Token, error) {
|
|
|
|
if len(rawBearer) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bt := new(bearer.Token)
|
|
|
|
if err := bt.Unmarshal(rawBearer); err != nil {
|
|
|
|
return nil, eACLErr(eaclOp, fmt.Errorf("invalid bearer token: %w", err))
|
|
|
|
}
|
|
|
|
if !bt.AssertContainer(cid) {
|
|
|
|
return nil, eACLErr(eaclOp, errBearerWrongContainer)
|
|
|
|
}
|
|
|
|
if !bt.VerifySignature() {
|
|
|
|
return nil, eACLErr(eaclOp, errBearerSignature)
|
|
|
|
}
|
|
|
|
return bt, nil
|
|
|
|
}
|
|
|
|
|
2022-05-05 11:00:30 +00:00
|
|
|
func verifyMessage(m message) error {
|
|
|
|
binBody, err := m.ReadSignedData(nil)
|
2022-04-22 13:30:20 +00:00
|
|
|
if err != nil {
|
2022-05-05 11:00:30 +00:00
|
|
|
return fmt.Errorf("marshal request body: %w", err)
|
2022-04-22 13:30:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 11:00:30 +00:00
|
|
|
sig := m.GetSignature()
|
|
|
|
|
2023-06-26 13:18:39 +00:00
|
|
|
// TODO(@cthulhu-rider): #468 use Signature message from FrostFS API to avoid conversion
|
2022-05-05 11:00:30 +00:00
|
|
|
var sigV2 refs.Signature
|
|
|
|
sigV2.SetKey(sig.GetKey())
|
|
|
|
sigV2.SetSign(sig.GetSign())
|
|
|
|
sigV2.SetScheme(refs.ECDSA_SHA512)
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
var sigSDK frostfscrypto.Signature
|
2022-07-22 14:04:37 +00:00
|
|
|
if err := sigSDK.ReadFromV2(sigV2); err != nil {
|
|
|
|
return fmt.Errorf("can't read signature: %w", err)
|
|
|
|
}
|
2022-04-22 13:30:20 +00:00
|
|
|
|
2022-05-05 11:00:30 +00:00
|
|
|
if !sigSDK.Verify(binBody) {
|
|
|
|
return errors.New("invalid signature")
|
2022-04-22 13:30:20 +00:00
|
|
|
}
|
2022-05-05 11:00:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-10 15:32:04 +00:00
|
|
|
// SignMessage uses the provided key and signs any protobuf
|
|
|
|
// message that was generated for the TreeService by the
|
2022-12-23 17:35:35 +00:00
|
|
|
// protoc-gen-go-frostfs generator. Returns any errors directly.
|
2022-10-10 15:32:04 +00:00
|
|
|
func SignMessage(m message, key *ecdsa.PrivateKey) error {
|
2022-05-05 11:00:30 +00:00
|
|
|
binBody, err := m.ReadSignedData(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
keySDK := frostfsecdsa.Signer(*key)
|
2022-05-05 11:00:30 +00:00
|
|
|
data, err := keySDK.Sign(binBody)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawPub := make([]byte, keySDK.Public().MaxEncodedSize())
|
|
|
|
rawPub = rawPub[:keySDK.Public().Encode(rawPub)]
|
|
|
|
m.SetSignature(&Signature{
|
|
|
|
Key: rawPub,
|
|
|
|
Sign: data,
|
|
|
|
})
|
2022-04-22 13:30:20 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-08 12:44:27 +00:00
|
|
|
|
2024-04-11 15:10:33 +00:00
|
|
|
func roleAndPubKeyFromReq(cnr *core.Container, req message, bt *bearer.Token) (acl.Role, *keys.PublicKey, error) {
|
2022-09-08 12:44:27 +00:00
|
|
|
role := acl.RoleOthers
|
|
|
|
owner := cnr.Value.Owner()
|
|
|
|
|
2022-10-25 09:52:41 +00:00
|
|
|
rawKey := req.GetSignature().GetKey()
|
|
|
|
if bt != nil && bt.Impersonate() {
|
|
|
|
rawKey = bt.SigningKeyBytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub, err := keys.NewPublicKeyFromBytes(rawKey, elliptic.P256())
|
2022-09-08 12:44:27 +00:00
|
|
|
if err != nil {
|
2024-04-11 15:10:33 +00:00
|
|
|
return role, nil, fmt.Errorf("invalid public key: %w", err)
|
2022-09-08 12:44:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var reqSigner user.ID
|
|
|
|
user.IDFromKey(&reqSigner, (ecdsa.PublicKey)(*pub))
|
|
|
|
|
|
|
|
if reqSigner.Equals(owner) {
|
|
|
|
role = acl.RoleOwner
|
|
|
|
}
|
|
|
|
|
2024-04-11 15:10:33 +00:00
|
|
|
return role, pub, nil
|
2022-09-08 12:44:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func eACLOp(op acl.Op) eacl.Operation {
|
|
|
|
switch op {
|
|
|
|
case acl.OpObjectGet:
|
|
|
|
return eacl.OperationGet
|
|
|
|
case acl.OpObjectPut:
|
|
|
|
return eacl.OperationPut
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected tree service ACL operation: %s", op))
|
|
|
|
}
|
|
|
|
}
|