2022-04-22 13:30:20 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
2022-09-09 09:09:38 +00:00
|
|
|
"bytes"
|
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/client"
|
|
|
|
"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-09-12 11:28:37 +00:00
|
|
|
"go.uber.org/zap"
|
2022-04-22 13:30:20 +00:00
|
|
|
)
|
|
|
|
|
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 basicACLErr(op acl.Op) error {
|
|
|
|
return fmt.Errorf("access to operation %s is denied by basic ACL check", op)
|
|
|
|
}
|
|
|
|
|
|
|
|
func eACLErr(op eacl.Operation, err error) error {
|
|
|
|
return fmt.Errorf("access to operation %s is denied by extended ACL check: %w", op, err)
|
|
|
|
}
|
|
|
|
|
2022-09-12 11:40:06 +00:00
|
|
|
var errBearerWrongOwner = errors.New("bearer token must be signed by the container owner")
|
|
|
|
var errBearerWrongContainer = errors.New("bearer token is created for another container")
|
|
|
|
var errBearerSignature = errors.New("invalid bearer token signature")
|
|
|
|
|
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.
|
|
|
|
func (s *Service) verifyClient(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
|
|
|
|
}
|
|
|
|
|
|
|
|
cnr, err := s.cnrSource.Get(cid)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get container %s: %w", cid, err)
|
|
|
|
}
|
|
|
|
|
2022-09-08 12:44:27 +00:00
|
|
|
role, err := roleFromReq(cnr, req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get request role: %w", err)
|
|
|
|
}
|
2022-04-22 13:30:20 +00:00
|
|
|
|
2022-09-08 12:44:27 +00:00
|
|
|
basicACL := cnr.Value.BasicACL()
|
2022-05-05 11:00:30 +00:00
|
|
|
|
2022-09-08 12:44:27 +00:00
|
|
|
if !basicACL.IsOpAllowed(op, role) {
|
|
|
|
return basicACLErr(op)
|
|
|
|
}
|
2022-05-05 11:00:30 +00:00
|
|
|
|
2022-09-08 12:44:27 +00:00
|
|
|
if !basicACL.Extendable() {
|
2022-05-05 11:00:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-08 12:44:27 +00:00
|
|
|
eaclOp := eACLOp(op)
|
|
|
|
|
2022-09-12 11:28:37 +00:00
|
|
|
var tableFromBearer bool
|
|
|
|
if len(rawBearer) != 0 {
|
|
|
|
if !basicACL.AllowedBearerRules(op) {
|
|
|
|
s.log.Debug("bearer presented but not allowed by ACL",
|
|
|
|
zap.String("cid", cid.EncodeToString()),
|
|
|
|
zap.String("op", op.String()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
tableFromBearer = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-08 12:44:27 +00:00
|
|
|
var tb eacl.Table
|
2022-09-12 11:28:37 +00:00
|
|
|
if tableFromBearer {
|
2022-09-08 12:44:27 +00:00
|
|
|
var bt bearer.Token
|
|
|
|
if err = bt.Unmarshal(rawBearer); err != nil {
|
|
|
|
return eACLErr(eaclOp, fmt.Errorf("invalid bearer token: %w", err))
|
|
|
|
}
|
|
|
|
if !bearer.ResolveIssuer(bt).Equals(cnr.Value.Owner()) {
|
2022-09-12 11:40:06 +00:00
|
|
|
return eACLErr(eaclOp, errBearerWrongOwner)
|
2022-09-08 12:44:27 +00:00
|
|
|
}
|
|
|
|
if !bt.AssertContainer(cid) {
|
2022-09-12 11:40:06 +00:00
|
|
|
return eACLErr(eaclOp, errBearerWrongContainer)
|
2022-09-08 12:44:27 +00:00
|
|
|
}
|
|
|
|
if !bt.VerifySignature() {
|
2022-09-12 11:40:06 +00:00
|
|
|
return eACLErr(eaclOp, errBearerSignature)
|
2022-09-08 12:44:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tb = bt.EACLTable()
|
|
|
|
} else {
|
|
|
|
tbCore, err := s.eaclSource.GetEACL(cid)
|
|
|
|
if err != nil {
|
|
|
|
if client.IsErrEACLNotFound(err) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-05 11:00:30 +00:00
|
|
|
|
2022-09-08 12:44:27 +00:00
|
|
|
return fmt.Errorf("get eACL table: %w", err)
|
|
|
|
}
|
2022-05-05 11:00:30 +00:00
|
|
|
|
2022-09-08 12:44:27 +00:00
|
|
|
tb = *tbCore.Value
|
|
|
|
}
|
|
|
|
|
2022-09-09 09:09:38 +00:00
|
|
|
return checkEACL(tb, req.GetSignature().GetKey(), eACLRole(role), eaclOp)
|
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()
|
|
|
|
|
|
|
|
// TODO(@cthulhu-rider): #1387 use Signature message from NeoFS API to avoid conversion
|
|
|
|
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
|
|
|
|
|
|
|
func roleFromReq(cnr *core.Container, req message) (acl.Role, error) {
|
|
|
|
role := acl.RoleOthers
|
|
|
|
owner := cnr.Value.Owner()
|
|
|
|
|
|
|
|
pub, err := keys.NewPublicKeyFromBytes(req.GetSignature().GetKey(), elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return role, fmt.Errorf("invalid public key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var reqSigner user.ID
|
|
|
|
user.IDFromKey(&reqSigner, (ecdsa.PublicKey)(*pub))
|
|
|
|
|
|
|
|
if reqSigner.Equals(owner) {
|
|
|
|
role = acl.RoleOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
return role, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func eACLRole(role acl.Role) eacl.Role {
|
|
|
|
switch role {
|
|
|
|
case acl.RoleOwner:
|
|
|
|
return eacl.RoleUser
|
|
|
|
case acl.RoleOthers:
|
|
|
|
return eacl.RoleOthers
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected tree service ACL role: %s", role))
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 09:09:38 +00:00
|
|
|
|
2022-09-12 11:40:06 +00:00
|
|
|
var errDENY = errors.New("DENY eACL rule")
|
|
|
|
var errNoAllowRules = errors.New("not found allowing rules for the request")
|
|
|
|
|
2022-09-09 09:09:38 +00:00
|
|
|
// checkEACL searches for the eACL rules that could be applied to the request
|
2023-02-05 15:59:38 +00:00
|
|
|
// (a tuple of a signer key, his FrostFS role and a request operation).
|
2022-09-09 09:09:38 +00:00
|
|
|
// It does not filter the request by the filters of the eACL table since tree
|
|
|
|
// requests do not contain any "object" information that could be filtered and,
|
|
|
|
// therefore, filtering leads to unexpected results.
|
|
|
|
// The code was copied with the minor updates from the SDK repo:
|
2022-12-23 17:35:35 +00:00
|
|
|
// https://github.com/nspcc-dev/frostfs-sdk-go/blob/43a57d42dd50dc60465bfd3482f7f12bcfcf3411/eacl/validator.go#L28.
|
2022-09-09 09:09:38 +00:00
|
|
|
func checkEACL(tb eacl.Table, signer []byte, role eacl.Role, op eacl.Operation) error {
|
|
|
|
for _, record := range tb.Records() {
|
|
|
|
// check type of operation
|
|
|
|
if record.Operation() != op {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check target
|
|
|
|
if !targetMatches(record, role, signer) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch a := record.Action(); a {
|
|
|
|
case eacl.ActionAllow:
|
|
|
|
return nil
|
|
|
|
case eacl.ActionDeny:
|
2022-09-12 11:40:06 +00:00
|
|
|
return eACLErr(op, errDENY)
|
2022-09-09 09:09:38 +00:00
|
|
|
default:
|
|
|
|
return eACLErr(op, fmt.Errorf("unexpected action: %s", a))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-12 11:40:06 +00:00
|
|
|
return eACLErr(op, errNoAllowRules)
|
2022-09-09 09:09:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func targetMatches(rec eacl.Record, role eacl.Role, signer []byte) bool {
|
|
|
|
for _, target := range rec.Targets() {
|
|
|
|
// check public key match
|
|
|
|
if pubs := target.BinaryKeys(); len(pubs) != 0 {
|
|
|
|
for _, key := range pubs {
|
|
|
|
if bytes.Equal(key, signer) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check target group match
|
|
|
|
if role == target.Role() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|