2020-09-21 09:51:15 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
2020-09-22 16:18:41 +00:00
|
|
|
"bytes"
|
2020-09-21 09:51:15 +00:00
|
|
|
"context"
|
|
|
|
|
2020-09-21 16:30:43 +00:00
|
|
|
acl "github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl"
|
|
|
|
containerSDK "github.com/nspcc-dev/neofs-api-go/pkg/container"
|
2020-09-21 09:51:15 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
2020-09-21 13:33:49 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2020-09-21 16:30:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/container"
|
2020-09-21 13:33:49 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-09-21 09:51:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// BasicChecker checks basic ACL rules.
|
|
|
|
BasicChecker struct {
|
2020-09-21 16:30:43 +00:00
|
|
|
containers container.Source
|
2020-09-21 16:12:52 +00:00
|
|
|
sender SenderClassifier
|
|
|
|
next object.Service
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
putStreamBasicChecker struct {
|
2020-09-21 16:12:52 +00:00
|
|
|
source *BasicChecker
|
2020-09-21 13:33:49 +00:00
|
|
|
next object.PutObjectStreamer
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getStreamBasicChecker struct {
|
2020-09-22 16:18:41 +00:00
|
|
|
next object.GetObjectStreamer
|
|
|
|
info requestInfo
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
searchStreamBasicChecker struct {
|
2020-09-21 16:12:52 +00:00
|
|
|
object.SearchObjectStreamer
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getRangeStreamBasicChecker struct {
|
2020-09-21 16:12:52 +00:00
|
|
|
object.GetRangeObjectStreamer
|
|
|
|
}
|
|
|
|
|
|
|
|
requestInfo struct {
|
|
|
|
basicACL uint32
|
|
|
|
requestRole acl.Role
|
|
|
|
operation acl.Operation // put, get, head, etc.
|
2020-09-22 16:18:41 +00:00
|
|
|
owner *refs.OwnerID // container owner
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-09-21 13:33:49 +00:00
|
|
|
var (
|
2020-09-21 16:12:52 +00:00
|
|
|
ErrMalformedRequest = errors.New("malformed request")
|
|
|
|
ErrUnknownRole = errors.New("can't classify request sender")
|
|
|
|
ErrUnknownContainer = errors.New("can't fetch container info")
|
2020-09-22 16:18:41 +00:00
|
|
|
ErrBasicAccessDenied = errors.New("access denied by basic acl")
|
2020-09-21 13:33:49 +00:00
|
|
|
)
|
|
|
|
|
2020-09-21 09:51:15 +00:00
|
|
|
// NewBasicChecker is a constructor for basic ACL checker of object requests.
|
2020-09-21 16:12:52 +00:00
|
|
|
func NewBasicChecker(
|
|
|
|
c SenderClassifier,
|
2020-09-21 16:30:43 +00:00
|
|
|
cnr container.Source,
|
2020-09-21 16:12:52 +00:00
|
|
|
next object.Service) BasicChecker {
|
|
|
|
|
2020-09-21 09:51:15 +00:00
|
|
|
return BasicChecker{
|
2020-09-21 16:12:52 +00:00
|
|
|
containers: cnr,
|
|
|
|
sender: c,
|
|
|
|
next: next,
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b BasicChecker) Get(
|
|
|
|
ctx context.Context,
|
|
|
|
request *object.GetRequest) (object.GetObjectStreamer, error) {
|
|
|
|
|
2020-09-21 16:12:52 +00:00
|
|
|
cid, err := getContainerIDFromRequest(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 16:12:52 +00:00
|
|
|
reqInfo, err := b.findRequestInfo(request, cid, acl.OperationGet)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !basicACLCheck(reqInfo) {
|
|
|
|
return nil, ErrBasicAccessDenied
|
2020-09-21 13:33:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 09:51:15 +00:00
|
|
|
stream, err := b.next.Get(ctx, request)
|
2020-09-22 16:18:41 +00:00
|
|
|
|
|
|
|
return getStreamBasicChecker{
|
|
|
|
next: stream,
|
|
|
|
info: reqInfo,
|
|
|
|
}, err
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b BasicChecker) Put(ctx context.Context) (object.PutObjectStreamer, error) {
|
|
|
|
streamer, err := b.next.Put(ctx)
|
|
|
|
|
|
|
|
return putStreamBasicChecker{
|
2020-09-21 16:12:52 +00:00
|
|
|
source: &b,
|
2020-09-21 13:33:49 +00:00
|
|
|
next: streamer,
|
2020-09-21 09:51:15 +00:00
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b BasicChecker) Head(
|
|
|
|
ctx context.Context,
|
|
|
|
request *object.HeadRequest) (*object.HeadResponse, error) {
|
|
|
|
|
2020-09-21 16:12:52 +00:00
|
|
|
cid, err := getContainerIDFromRequest(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reqInfo, err := b.findRequestInfo(request, cid, acl.OperationHead)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !basicACLCheck(reqInfo) {
|
|
|
|
return nil, ErrBasicAccessDenied
|
|
|
|
}
|
|
|
|
|
2020-09-21 09:51:15 +00:00
|
|
|
return b.next.Head(ctx, request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b BasicChecker) Search(
|
|
|
|
ctx context.Context,
|
|
|
|
request *object.SearchRequest) (object.SearchObjectStreamer, error) {
|
|
|
|
|
2020-09-21 16:12:52 +00:00
|
|
|
var cid *refs.ContainerID
|
|
|
|
|
|
|
|
cid, err := getContainerIDFromRequest(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reqInfo, err := b.findRequestInfo(request, cid, acl.OperationSearch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !basicACLCheck(reqInfo) {
|
|
|
|
return nil, ErrBasicAccessDenied
|
|
|
|
}
|
|
|
|
|
2020-09-21 09:51:15 +00:00
|
|
|
stream, err := b.next.Search(ctx, request)
|
2020-09-21 16:12:52 +00:00
|
|
|
return searchStreamBasicChecker{stream}, err
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b BasicChecker) Delete(
|
|
|
|
ctx context.Context,
|
|
|
|
request *object.DeleteRequest) (*object.DeleteResponse, error) {
|
|
|
|
|
2020-09-21 16:12:52 +00:00
|
|
|
cid, err := getContainerIDFromRequest(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reqInfo, err := b.findRequestInfo(request, cid, acl.OperationDelete)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !basicACLCheck(reqInfo) {
|
|
|
|
return nil, ErrBasicAccessDenied
|
|
|
|
}
|
|
|
|
|
2020-09-21 09:51:15 +00:00
|
|
|
return b.next.Delete(ctx, request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b BasicChecker) GetRange(
|
|
|
|
ctx context.Context,
|
|
|
|
request *object.GetRangeRequest) (object.GetRangeObjectStreamer, error) {
|
|
|
|
|
2020-09-21 16:12:52 +00:00
|
|
|
cid, err := getContainerIDFromRequest(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reqInfo, err := b.findRequestInfo(request, cid, acl.OperationRange)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !basicACLCheck(reqInfo) {
|
|
|
|
return nil, ErrBasicAccessDenied
|
|
|
|
}
|
|
|
|
|
2020-09-21 09:51:15 +00:00
|
|
|
stream, err := b.next.GetRange(ctx, request)
|
2020-09-21 16:12:52 +00:00
|
|
|
return getRangeStreamBasicChecker{stream}, err
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b BasicChecker) GetRangeHash(
|
|
|
|
ctx context.Context,
|
|
|
|
request *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) {
|
|
|
|
|
2020-09-21 16:12:52 +00:00
|
|
|
cid, err := getContainerIDFromRequest(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reqInfo, err := b.findRequestInfo(request, cid, acl.OperationRangeHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !basicACLCheck(reqInfo) {
|
|
|
|
return nil, ErrBasicAccessDenied
|
|
|
|
}
|
|
|
|
|
2020-09-21 09:51:15 +00:00
|
|
|
return b.next.GetRangeHash(ctx, request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p putStreamBasicChecker) Send(request *object.PutRequest) error {
|
2020-09-21 16:12:52 +00:00
|
|
|
body := request.GetBody()
|
|
|
|
if body == nil {
|
|
|
|
return ErrMalformedRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
part := body.GetObjectPart()
|
|
|
|
if _, ok := part.(*object.PutObjectPartInit); ok {
|
|
|
|
cid, err := getContainerIDFromRequest(request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-22 16:18:41 +00:00
|
|
|
owner, err := getObjectOwnerFromMessage(request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-21 16:12:52 +00:00
|
|
|
reqInfo, err := p.source.findRequestInfo(request, cid, acl.OperationPut)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-22 16:18:41 +00:00
|
|
|
if !basicACLCheck(reqInfo) || !stickyBitCheck(reqInfo, owner) {
|
2020-09-21 16:12:52 +00:00
|
|
|
return ErrBasicAccessDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 09:51:15 +00:00
|
|
|
return p.next.Send(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p putStreamBasicChecker) CloseAndRecv() (*object.PutResponse, error) {
|
|
|
|
return p.next.CloseAndRecv()
|
|
|
|
}
|
|
|
|
|
2020-09-22 16:18:41 +00:00
|
|
|
func (g getStreamBasicChecker) Recv() (*object.GetResponse, error) {
|
|
|
|
resp, err := g.next.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
body := resp.GetBody()
|
|
|
|
if body == nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
part := body.GetObjectPart()
|
|
|
|
if _, ok := part.(*object.GetObjectPartInit); ok {
|
|
|
|
owner, err := getObjectOwnerFromMessage(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !stickyBitCheck(g.info, owner) {
|
|
|
|
return nil, ErrBasicAccessDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2020-09-21 16:12:52 +00:00
|
|
|
func (b BasicChecker) findRequestInfo(
|
|
|
|
req RequestV2,
|
|
|
|
cid *refs.ContainerID,
|
|
|
|
op acl.Operation) (info requestInfo, err error) {
|
|
|
|
|
2020-09-21 16:30:43 +00:00
|
|
|
// container.Source interface implemented with SDK's definitions,
|
|
|
|
// so we have to convert id there.
|
|
|
|
containerID := containerSDK.NewIDFromV2(cid)
|
|
|
|
|
2020-09-21 16:12:52 +00:00
|
|
|
// fetch actual container
|
2020-09-21 16:30:43 +00:00
|
|
|
cnr, err := b.containers.Get(containerID)
|
2020-09-21 16:12:52 +00:00
|
|
|
if err != nil || cnr.GetOwnerID() == nil {
|
|
|
|
return info, ErrUnknownContainer
|
|
|
|
}
|
|
|
|
|
|
|
|
// find request role
|
|
|
|
role := b.sender.Classify(req, cid, cnr)
|
|
|
|
if role == acl.RoleUnknown {
|
|
|
|
return info, ErrUnknownRole
|
|
|
|
}
|
|
|
|
|
|
|
|
info.basicACL = cnr.GetBasicACL()
|
|
|
|
info.requestRole = role
|
|
|
|
info.operation = op
|
2020-09-22 16:18:41 +00:00
|
|
|
info.owner = cnr.GetOwnerID()
|
2020-09-21 16:12:52 +00:00
|
|
|
|
|
|
|
return info, nil
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 16:12:52 +00:00
|
|
|
func getContainerIDFromRequest(req interface{}) (id *refs.ContainerID, err error) {
|
|
|
|
defer func() {
|
|
|
|
// if there is a NPE on get body and get address
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = ErrMalformedRequest
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
switch v := req.(type) {
|
|
|
|
case *object.GetRequest:
|
|
|
|
return v.GetBody().GetAddress().GetContainerID(), nil
|
|
|
|
case *object.PutRequest:
|
|
|
|
objPart := v.GetBody().GetObjectPart()
|
|
|
|
if part, ok := objPart.(*object.PutObjectPartInit); ok {
|
|
|
|
return part.GetHeader().GetContainerID(), nil
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("can't get cid in chunk")
|
|
|
|
}
|
|
|
|
case *object.HeadRequest:
|
|
|
|
return v.GetBody().GetAddress().GetContainerID(), nil
|
|
|
|
case *object.SearchRequest:
|
|
|
|
return v.GetBody().GetContainerID(), nil
|
|
|
|
case *object.DeleteRequest:
|
|
|
|
return v.GetBody().GetAddress().GetContainerID(), nil
|
|
|
|
case *object.GetRangeRequest:
|
|
|
|
return v.GetBody().GetAddress().GetContainerID(), nil
|
|
|
|
case *object.GetRangeHashRequest:
|
|
|
|
return v.GetBody().GetAddress().GetContainerID(), nil
|
|
|
|
default:
|
|
|
|
return nil, errors.New("unknown request type")
|
|
|
|
}
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 16:18:41 +00:00
|
|
|
func getObjectOwnerFromMessage(req interface{}) (id *refs.OwnerID, err error) {
|
|
|
|
defer func() {
|
|
|
|
// if there is a NPE on get body and get address
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = ErrMalformedRequest
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
switch v := req.(type) {
|
|
|
|
case *object.PutRequest:
|
|
|
|
objPart := v.GetBody().GetObjectPart()
|
|
|
|
if part, ok := objPart.(*object.PutObjectPartInit); ok {
|
|
|
|
return part.GetHeader().GetOwnerID(), nil
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("can't get cid in chunk")
|
|
|
|
}
|
|
|
|
case *object.GetResponse:
|
|
|
|
objPart := v.GetBody().GetObjectPart()
|
|
|
|
if part, ok := objPart.(*object.GetObjectPartInit); ok {
|
|
|
|
return part.GetHeader().GetOwnerID(), nil
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("can't get cid in chunk")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, errors.New("unsupported request type")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// main check function for basic ACL
|
2020-09-21 16:12:52 +00:00
|
|
|
func basicACLCheck(info requestInfo) bool {
|
2020-09-22 16:18:41 +00:00
|
|
|
rule := basicACLHelper(info.basicACL)
|
|
|
|
|
|
|
|
// check basic ACL permissions
|
|
|
|
var checkFn func(acl.Operation) bool
|
|
|
|
|
|
|
|
switch info.requestRole {
|
|
|
|
case acl.RoleUser:
|
|
|
|
checkFn = rule.UserAllowed
|
|
|
|
case acl.RoleSystem:
|
|
|
|
checkFn = rule.SystemAllowed
|
|
|
|
case acl.RoleOthers:
|
|
|
|
checkFn = rule.OthersAllowed
|
|
|
|
default:
|
|
|
|
// log there
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return checkFn(info.operation)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stickyBitCheck(info requestInfo, owner *refs.OwnerID) bool {
|
|
|
|
if owner == nil || info.owner == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := basicACLHelper(info.basicACL)
|
|
|
|
if !rule.Sticky() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes.Equal(owner.GetValue(), info.owner.GetValue())
|
2020-09-21 09:51:15 +00:00
|
|
|
}
|