2020-09-21 09:51:15 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
2020-11-19 14:17:15 +00:00
|
|
|
"crypto/ecdsa"
|
2022-02-11 12:25:05 +00:00
|
|
|
"crypto/elliptic"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2020-10-03 07:39:02 +00:00
|
|
|
"fmt"
|
2020-09-21 09:51:15 +00:00
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/core/container"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/engine"
|
|
|
|
eaclV2 "github.com/TrueCloudLab/frostfs-node/pkg/services/object/acl/eacl/v2"
|
|
|
|
v2 "github.com/TrueCloudLab/frostfs-node/pkg/services/object/acl/v2"
|
|
|
|
bearerSDK "github.com/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/client"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/container/acl"
|
|
|
|
frostfsecdsa "github.com/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
|
|
|
eaclSDK "github.com/TrueCloudLab/frostfs-sdk-go/eacl"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/user"
|
2021-05-31 08:55:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-09-21 09:51:15 +00:00
|
|
|
)
|
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
// CheckerPrm groups parameters for Checker
|
|
|
|
// constructor.
|
|
|
|
type CheckerPrm struct {
|
2022-09-08 10:32:25 +00:00
|
|
|
eaclSrc container.EACLSource
|
2022-02-11 12:25:05 +00:00
|
|
|
validator *eaclSDK.Validator
|
2020-11-19 08:38:50 +00:00
|
|
|
localStorage *engine.StorageEngine
|
2022-02-11 12:25:05 +00:00
|
|
|
state netmap.State
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 10:32:25 +00:00
|
|
|
func (c *CheckerPrm) SetEACLSource(v container.EACLSource) *CheckerPrm {
|
2022-02-11 12:25:05 +00:00
|
|
|
c.eaclSrc = v
|
|
|
|
return c
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
func (c *CheckerPrm) SetValidator(v *eaclSDK.Validator) *CheckerPrm {
|
|
|
|
c.validator = v
|
|
|
|
return c
|
2020-09-22 16:18:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
func (c *CheckerPrm) SetLocalStorage(v *engine.StorageEngine) *CheckerPrm {
|
|
|
|
c.localStorage = v
|
|
|
|
return c
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
func (c *CheckerPrm) SetNetmapState(v netmap.State) *CheckerPrm {
|
|
|
|
c.state = v
|
|
|
|
return c
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
// Checker implements v2.ACLChecker interfaces and provides
|
|
|
|
// ACL/eACL validation functionality.
|
|
|
|
type Checker struct {
|
2022-09-08 10:32:25 +00:00
|
|
|
eaclSrc container.EACLSource
|
2022-02-11 12:25:05 +00:00
|
|
|
validator *eaclSDK.Validator
|
|
|
|
localStorage *engine.StorageEngine
|
|
|
|
state netmap.State
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 11:04:32 +00:00
|
|
|
// Various EACL check errors.
|
|
|
|
var (
|
2022-03-29 11:46:37 +00:00
|
|
|
errEACLDeniedByRule = errors.New("denied by rule")
|
|
|
|
errBearerExpired = errors.New("bearer token has expired")
|
|
|
|
errBearerInvalidSignature = errors.New("bearer token has invalid signature")
|
|
|
|
errBearerInvalidContainerID = errors.New("bearer token was created for another container")
|
|
|
|
errBearerNotSignedByOwner = errors.New("bearer token is not signed by the container owner")
|
|
|
|
errBearerInvalidOwner = errors.New("bearer token owner differs from the request sender")
|
2022-03-18 11:04:32 +00:00
|
|
|
)
|
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
// NewChecker creates Checker.
|
|
|
|
// Panics if at least one of the parameter is nil.
|
|
|
|
func NewChecker(prm *CheckerPrm) *Checker {
|
|
|
|
panicOnNil := func(fieldName string, field interface{}) {
|
|
|
|
if field == nil {
|
|
|
|
panic(fmt.Sprintf("incorrect field %s (%T): %v", fieldName, field, field))
|
2020-09-21 16:12:52 +00:00
|
|
|
}
|
2020-12-14 13:50:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
panicOnNil("EACLSource", prm.eaclSrc)
|
|
|
|
panicOnNil("EACLValidator", prm.validator)
|
|
|
|
panicOnNil("LocalStorageEngine", prm.localStorage)
|
|
|
|
panicOnNil("NetmapState", prm.state)
|
2020-12-14 13:50:45 +00:00
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
return &Checker{
|
|
|
|
eaclSrc: prm.eaclSrc,
|
|
|
|
validator: prm.validator,
|
|
|
|
localStorage: prm.localStorage,
|
|
|
|
state: prm.state,
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
// CheckBasicACL is a main check function for basic ACL.
|
|
|
|
func (c *Checker) CheckBasicACL(info v2.RequestInfo) bool {
|
2020-09-22 16:18:41 +00:00
|
|
|
// check basic ACL permissions
|
2022-06-17 13:40:51 +00:00
|
|
|
return info.BasicACL().IsOpAllowed(info.Operation(), info.RequestRole())
|
2020-09-22 16:18:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
// StickyBitCheck validates owner field in the request if sticky bit is enabled.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (c *Checker) StickyBitCheck(info v2.RequestInfo, owner user.ID) bool {
|
2021-09-10 11:35:29 +00:00
|
|
|
// According to NeoFS specification sticky bit has no effect on system nodes
|
|
|
|
// for correct intra-container work with objects (in particular, replication).
|
2022-06-17 13:40:51 +00:00
|
|
|
if info.RequestRole() == acl.RoleContainer {
|
2021-09-10 11:35:29 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-06-17 13:40:51 +00:00
|
|
|
if !info.BasicACL().Sticky() {
|
2020-09-22 16:18:41 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
if len(info.SenderKey()) == 0 {
|
2021-11-08 10:49:20 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
requestSenderKey := unmarshalPublicKey(info.SenderKey())
|
2020-11-19 14:26:04 +00:00
|
|
|
|
|
|
|
return isOwnerFromKey(owner, requestSenderKey)
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
2020-10-02 11:40:09 +00:00
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
// CheckEACL is a main check function for extended ACL.
|
2022-03-18 11:04:32 +00:00
|
|
|
func (c *Checker) CheckEACL(msg interface{}, reqInfo v2.RequestInfo) error {
|
2022-06-17 13:40:51 +00:00
|
|
|
basicACL := reqInfo.BasicACL()
|
|
|
|
if !basicACL.Extendable() {
|
2022-03-18 11:04:32 +00:00
|
|
|
return nil
|
2020-10-03 07:50:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 16:08:22 +00:00
|
|
|
// if bearer token is not allowed, then ignore it
|
2022-06-17 13:40:51 +00:00
|
|
|
if !basicACL.AllowedBearerRules(reqInfo.Operation()) {
|
2022-02-11 12:25:05 +00:00
|
|
|
reqInfo.CleanBearer()
|
2020-10-21 16:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 07:22:02 +00:00
|
|
|
var table eaclSDK.Table
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr := reqInfo.ContainerID()
|
2022-01-20 09:45:58 +00:00
|
|
|
|
2022-05-12 07:22:02 +00:00
|
|
|
bearerTok := reqInfo.Bearer()
|
|
|
|
if bearerTok == nil {
|
2022-06-22 10:55:31 +00:00
|
|
|
eaclInfo, err := c.eaclSrc.GetEACL(cnr)
|
2022-01-20 09:45:58 +00:00
|
|
|
if err != nil {
|
2022-08-01 15:28:40 +00:00
|
|
|
if client.IsErrEACLNotFound(err) {
|
2022-03-18 11:04:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2022-01-20 09:45:58 +00:00
|
|
|
}
|
2022-05-12 07:22:02 +00:00
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
table = *eaclInfo.Value
|
2022-01-20 09:45:58 +00:00
|
|
|
} else {
|
2022-05-12 07:22:02 +00:00
|
|
|
table = bearerTok.EACLTable()
|
2022-01-20 09:45:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 13:11:18 +00:00
|
|
|
// if bearer token is not present, isValidBearer returns true
|
2022-03-18 11:04:32 +00:00
|
|
|
if err := isValidBearer(reqInfo, c.state); err != nil {
|
|
|
|
return err
|
2020-10-21 13:11:18 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 09:06:04 +00:00
|
|
|
hdrSrcOpts := make([]eaclV2.Option, 0, 3)
|
2020-10-03 07:50:41 +00:00
|
|
|
|
2020-12-15 09:06:04 +00:00
|
|
|
hdrSrcOpts = append(hdrSrcOpts,
|
2022-02-11 12:25:05 +00:00
|
|
|
eaclV2.WithLocalObjectStorage(c.localStorage),
|
2022-05-31 17:00:41 +00:00
|
|
|
eaclV2.WithCID(cnr),
|
2022-05-24 18:47:21 +00:00
|
|
|
eaclV2.WithOID(reqInfo.ObjectID()),
|
2020-12-15 09:06:04 +00:00
|
|
|
)
|
2020-10-03 07:50:41 +00:00
|
|
|
|
|
|
|
if req, ok := msg.(eaclV2.Request); ok {
|
|
|
|
hdrSrcOpts = append(hdrSrcOpts, eaclV2.WithServiceRequest(req))
|
|
|
|
} else {
|
2020-12-28 15:59:42 +00:00
|
|
|
hdrSrcOpts = append(hdrSrcOpts,
|
|
|
|
eaclV2.WithServiceResponse(
|
|
|
|
msg.(eaclV2.Response),
|
2022-02-11 12:25:05 +00:00
|
|
|
reqInfo.Request().(eaclV2.Request),
|
2020-12-28 15:59:42 +00:00
|
|
|
),
|
|
|
|
)
|
2020-10-03 07:50:41 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
hdrSrc, err := eaclV2.NewMessageHeaderSource(hdrSrcOpts...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't parse headers: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-17 13:40:51 +00:00
|
|
|
var eaclRole eaclSDK.Role
|
|
|
|
switch op := reqInfo.RequestRole(); op {
|
|
|
|
default:
|
|
|
|
eaclRole = eaclSDK.Role(op)
|
|
|
|
case acl.RoleOwner:
|
|
|
|
eaclRole = eaclSDK.RoleUser
|
|
|
|
case acl.RoleInnerRing, acl.RoleContainer:
|
|
|
|
eaclRole = eaclSDK.RoleSystem
|
|
|
|
case acl.RoleOthers:
|
|
|
|
eaclRole = eaclSDK.RoleOthers
|
|
|
|
}
|
|
|
|
|
2022-06-09 11:26:50 +00:00
|
|
|
action, _ := c.validator.CalculateAction(new(eaclSDK.ValidationUnit).
|
2022-06-17 13:40:51 +00:00
|
|
|
WithRole(eaclRole).
|
|
|
|
WithOperation(eaclSDK.Operation(reqInfo.Operation())).
|
2022-05-31 17:00:41 +00:00
|
|
|
WithContainerID(&cnr).
|
2022-02-11 12:25:05 +00:00
|
|
|
WithSenderKey(reqInfo.SenderKey()).
|
2022-05-17 12:46:25 +00:00
|
|
|
WithHeaderSource(hdrSrc).
|
2022-05-12 07:22:02 +00:00
|
|
|
WithEACLTable(&table),
|
2020-10-03 07:50:41 +00:00
|
|
|
)
|
|
|
|
|
2022-03-18 11:04:32 +00:00
|
|
|
if action != eaclSDK.ActionAllow {
|
|
|
|
return errEACLDeniedByRule
|
|
|
|
}
|
|
|
|
return nil
|
2020-10-03 07:50:41 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 11:04:32 +00:00
|
|
|
// isValidBearer checks whether bearer token was correctly signed by authorized
|
2022-02-11 12:25:05 +00:00
|
|
|
// entity. This method might be defined on whole ACL service because it will
|
|
|
|
// require fetching current epoch to check lifetime.
|
2022-03-18 11:04:32 +00:00
|
|
|
func isValidBearer(reqInfo v2.RequestInfo, st netmap.State) error {
|
2022-05-17 13:59:46 +00:00
|
|
|
ownerCnr := reqInfo.ContainerOwner()
|
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
token := reqInfo.Bearer()
|
2020-10-21 13:11:18 +00:00
|
|
|
|
2022-05-12 07:22:02 +00:00
|
|
|
// 0. Check if bearer token is present in reqInfo.
|
|
|
|
if token == nil {
|
2022-03-18 11:04:32 +00:00
|
|
|
return nil
|
2020-10-21 13:11:18 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 12:45:28 +00:00
|
|
|
// 1. First check token lifetime. Simplest verification.
|
2022-06-03 14:12:55 +00:00
|
|
|
if token.InvalidAt(st.CurrentEpoch()) {
|
2022-03-18 11:04:32 +00:00
|
|
|
return errBearerExpired
|
2020-10-22 12:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Then check if bearer token is signed correctly.
|
2022-06-03 14:12:55 +00:00
|
|
|
if !token.VerifySignature() {
|
2022-03-18 11:04:32 +00:00
|
|
|
return errBearerInvalidSignature
|
2020-10-21 13:11:18 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 11:46:37 +00:00
|
|
|
// 3. Then check if container is either empty or equal to the container in the request.
|
|
|
|
cnr, isSet := token.EACLTable().CID()
|
|
|
|
if isSet && !cnr.Equals(reqInfo.ContainerID()) {
|
|
|
|
return errBearerInvalidContainerID
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. Then check if container owner signed this token.
|
2022-06-03 14:12:55 +00:00
|
|
|
if !bearerSDK.ResolveIssuer(*token).Equals(ownerCnr) {
|
2022-12-23 17:35:35 +00:00
|
|
|
// TODO: #767 in this case we can issue all owner keys from frostfs.id and check once again
|
2022-03-18 11:04:32 +00:00
|
|
|
return errBearerNotSignedByOwner
|
2020-10-21 13:11:18 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 11:46:37 +00:00
|
|
|
// 5. Then check if request sender has rights to use this token.
|
2022-12-23 17:35:35 +00:00
|
|
|
var keySender frostfsecdsa.PublicKey
|
2022-06-03 14:12:55 +00:00
|
|
|
|
|
|
|
err := keySender.Decode(reqInfo.SenderKey())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode sender public key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var usrSender user.ID
|
|
|
|
user.IDFromKey(&usrSender, ecdsa.PublicKey(keySender))
|
2022-05-12 07:22:02 +00:00
|
|
|
|
2022-06-03 14:12:55 +00:00
|
|
|
if !token.AssertUser(usrSender) {
|
2022-12-23 17:35:35 +00:00
|
|
|
// TODO: #767 in this case we can issue all owner keys from frostfs.id and check once again
|
2022-05-12 07:22:02 +00:00
|
|
|
return errBearerInvalidOwner
|
2020-10-21 13:11:18 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 11:04:32 +00:00
|
|
|
return nil
|
2020-10-21 13:11:18 +00:00
|
|
|
}
|
2020-10-22 12:37:59 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func isOwnerFromKey(id user.ID, key *keys.PublicKey) bool {
|
|
|
|
if key == nil {
|
2020-11-19 14:17:15 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
var id2 user.ID
|
|
|
|
user.IDFromKey(&id2, (ecdsa.PublicKey)(*key))
|
|
|
|
|
|
|
|
return id.Equals(id2)
|
2020-11-19 14:17:15 +00:00
|
|
|
}
|
2021-05-26 16:49:42 +00:00
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
func unmarshalPublicKey(bs []byte) *keys.PublicKey {
|
|
|
|
pub, err := keys.NewPublicKeyFromBytes(bs, elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2021-05-26 16:51:24 +00:00
|
|
|
}
|
2022-02-11 12:25:05 +00:00
|
|
|
return pub
|
2021-05-26 16:51:24 +00:00
|
|
|
}
|