[#XX] tree: Make tree service use Bearer token's APE overrides
Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
This commit is contained in:
parent
f89c89178e
commit
21426a77e4
4 changed files with 119 additions and 13 deletions
|
@ -66,6 +66,7 @@ func initTreeService(c *cfg) {
|
||||||
tree.WithAuthorizedKeys(treeConfig.AuthorizedKeys()),
|
tree.WithAuthorizedKeys(treeConfig.AuthorizedKeys()),
|
||||||
tree.WithMetrics(c.metricsCollector.TreeService()),
|
tree.WithMetrics(c.metricsCollector.TreeService()),
|
||||||
tree.WithAPERouter(c.cfgObject.cfgAccessPolicyEngine.accessPolicyEngine),
|
tree.WithAPERouter(c.cfgObject.cfgAccessPolicyEngine.accessPolicyEngine),
|
||||||
|
tree.WithNetmapState(c.cfgNetmap.state),
|
||||||
)
|
)
|
||||||
|
|
||||||
c.cfgGRPC.performAndSave(func(_ string, _ net.Listener, s *grpc.Server) {
|
c.cfgGRPC.performAndSave(func(_ string, _ net.Listener, s *grpc.Server) {
|
||||||
|
|
|
@ -2,18 +2,26 @@ package tree
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/ecdsa"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/ape/converter"
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/ape/converter"
|
||||||
aperequest "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/ape/request"
|
aperequest "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/ape/request"
|
||||||
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/ape/router"
|
||||||
core "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
core "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
||||||
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
||||||
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/ape"
|
||||||
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
||||||
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
||||||
cnrSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
cnrSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/acl"
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/acl"
|
||||||
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
||||||
|
cidSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
||||||
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
||||||
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
||||||
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
||||||
commonschema "git.frostfs.info/TrueCloudLab/policy-engine/schema/common"
|
commonschema "git.frostfs.info/TrueCloudLab/policy-engine/schema/common"
|
||||||
|
@ -22,20 +30,24 @@ import (
|
||||||
"google.golang.org/grpc/peer"
|
"google.golang.org/grpc/peer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Service) checkAPE(ctx context.Context, container *core.Container, cid cid.ID, operation acl.Op, role acl.Role, publicKey *keys.PublicKey) error {
|
var (
|
||||||
namespace := ""
|
errInvalidTargetType = errors.New("bearer token defines non-container target override")
|
||||||
cntNamespace, hasNamespace := strings.CutSuffix(cnrSDK.ReadDomain(container.Value).Zone(), ".ns")
|
errBearerExpired = errors.New("bearer token has expired")
|
||||||
if hasNamespace {
|
errBearerInvalidSignature = errors.New("bearer token has invalid signature")
|
||||||
namespace = cntNamespace
|
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")
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Service) newAPERequest(ctx context.Context, namespace string,
|
||||||
|
cid cid.ID, operation acl.Op, role acl.Role, publicKey *keys.PublicKey) (aperequest.Request, error) {
|
||||||
schemaMethod, err := converter.SchemaMethodFromACLOperation(operation)
|
schemaMethod, err := converter.SchemaMethodFromACLOperation(operation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return apeErr(err)
|
return aperequest.Request{}, err
|
||||||
}
|
}
|
||||||
schemaRole, err := converter.SchemaRoleFromACLRole(role)
|
schemaRole, err := converter.SchemaRoleFromACLRole(role)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return apeErr(err)
|
return aperequest.Request{}, err
|
||||||
}
|
}
|
||||||
reqProps := map[string]string{
|
reqProps := map[string]string{
|
||||||
nativeschema.PropertyKeyActorPublicKey: hex.EncodeToString(publicKey.Bytes()),
|
nativeschema.PropertyKeyActorPublicKey: hex.EncodeToString(publicKey.Bytes()),
|
||||||
|
@ -43,7 +55,7 @@ func (s *Service) checkAPE(ctx context.Context, container *core.Container, cid c
|
||||||
}
|
}
|
||||||
reqProps, err = s.fillWithUserClaimTags(reqProps, publicKey)
|
reqProps, err = s.fillWithUserClaimTags(reqProps, publicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return apeErr(err)
|
return aperequest.Request{}, err
|
||||||
}
|
}
|
||||||
if p, ok := peer.FromContext(ctx); ok {
|
if p, ok := peer.FromContext(ctx); ok {
|
||||||
if tcpAddr, ok := p.Addr.(*net.TCPAddr); ok {
|
if tcpAddr, ok := p.Addr.(*net.TCPAddr); ok {
|
||||||
|
@ -58,11 +70,97 @@ func (s *Service) checkAPE(ctx context.Context, container *core.Container, cid c
|
||||||
resourceName = fmt.Sprintf(nativeschema.ResourceFormatNamespaceContainerObjects, namespace, cid.EncodeToString())
|
resourceName = fmt.Sprintf(nativeschema.ResourceFormatNamespaceContainerObjects, namespace, cid.EncodeToString())
|
||||||
}
|
}
|
||||||
|
|
||||||
request := aperequest.NewRequest(
|
return aperequest.NewRequest(
|
||||||
schemaMethod,
|
schemaMethod,
|
||||||
aperequest.NewResource(resourceName, make(map[string]string)),
|
aperequest.NewResource(resourceName, make(map[string]string)),
|
||||||
reqProps,
|
reqProps,
|
||||||
)
|
), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// isValidBearer checks whether bearer token was correctly signed by authorized
|
||||||
|
// entity. This method might be defined on whole ACL service because it will
|
||||||
|
// require fetching current epoch to check lifetime.
|
||||||
|
func isValidBearer(token *bearer.Token, ownerCnr user.ID, cid cid.ID, publicKey *keys.PublicKey, st netmap.State) error {
|
||||||
|
if token == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. First check token lifetime. Simplest verification.
|
||||||
|
if token.InvalidAt(st.CurrentEpoch()) {
|
||||||
|
return errBearerExpired
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Then check if bearer token is signed correctly.
|
||||||
|
if !token.VerifySignature() {
|
||||||
|
return errBearerInvalidSignature
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Then check if container is either empty or equal to the container in the request.
|
||||||
|
apeOverride := token.APEOverride()
|
||||||
|
if apeOverride != nil {
|
||||||
|
if apeOverride.Target.TargetType != ape.TargetTypeContainer {
|
||||||
|
return errInvalidTargetType
|
||||||
|
}
|
||||||
|
|
||||||
|
var targetCnr cidSDK.ID
|
||||||
|
err := targetCnr.DecodeString(apeOverride.Target.Name)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid cid format: %s", apeOverride.Target.Name)
|
||||||
|
}
|
||||||
|
if !cid.Equals(targetCnr) {
|
||||||
|
return errBearerInvalidContainerID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Then check if container owner signed this token.
|
||||||
|
if !bearer.ResolveIssuer(*token).Equals(ownerCnr) {
|
||||||
|
return errBearerNotSignedByOwner
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Then check if request sender has rights to use this token.
|
||||||
|
var usrSender user.ID
|
||||||
|
user.IDFromKey(&usrSender, (ecdsa.PublicKey)(*publicKey))
|
||||||
|
|
||||||
|
if !token.AssertUser(usrSender) {
|
||||||
|
return errBearerInvalidOwner
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) checkAPE(ctx context.Context, bt *bearer.Token,
|
||||||
|
container *core.Container, cid cid.ID, operation acl.Op, role acl.Role, publicKey *keys.PublicKey) error {
|
||||||
|
namespace := ""
|
||||||
|
cntNamespace, hasNamespace := strings.CutSuffix(cnrSDK.ReadDomain(container.Value).Zone(), ".ns")
|
||||||
|
if hasNamespace {
|
||||||
|
namespace = cntNamespace
|
||||||
|
}
|
||||||
|
|
||||||
|
request, err := s.newAPERequest(ctx, namespace, cid, operation, role, publicKey)
|
||||||
|
if err != nil {
|
||||||
|
return apeErr(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if bt != nil && !bt.Impersonate() {
|
||||||
|
if err := isValidBearer(bt, container.Value.Owner(), cid, publicKey, s.state); err != nil {
|
||||||
|
return fmt.Errorf("bearer validation error: %w", err)
|
||||||
|
}
|
||||||
|
btRouter, err := router.SingleUseRouterWithBearerTokenChains([]*bearer.APEOverride{bt.APEOverride()})
|
||||||
|
if err != nil {
|
||||||
|
return apeErr(err)
|
||||||
|
}
|
||||||
|
status, found, err := btRouter.IsAllowed(apechain.Ingress, engine.NewRequestTargetWithContainer(cid.EncodeToString()), request)
|
||||||
|
if err != nil {
|
||||||
|
return apeErr(err)
|
||||||
|
}
|
||||||
|
if found && status == apechain.Allow {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if status != apechain.NoRuleFound {
|
||||||
|
err = fmt.Errorf("access to operation %s is denied by access policy engine (bearer token): %s", request.Operation(), status.String())
|
||||||
|
return apeErr(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rt := engine.NewRequestTargetExtended(namespace, cid.EncodeToString(), fmt.Sprintf("%s:%s", namespace, publicKey.Address()), nil)
|
rt := engine.NewRequestTargetExtended(namespace, cid.EncodeToString(), fmt.Sprintf("%s:%s", namespace, publicKey.Address()), nil)
|
||||||
status, found, err := s.router.IsAllowed(apechain.Ingress, rt, request)
|
status, found, err := s.router.IsAllowed(apechain.Ingress, rt, request)
|
||||||
|
@ -72,7 +170,7 @@ func (s *Service) checkAPE(ctx context.Context, container *core.Container, cid c
|
||||||
if found && status == apechain.Allow {
|
if found && status == apechain.Allow {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err = fmt.Errorf("access to operation %s is denied by access policy engine: %s", schemaMethod, status.String())
|
err = fmt.Errorf("access to operation %s is denied by access policy engine: %s", request.Operation(), status.String())
|
||||||
return apeErr(err)
|
return apeErr(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,7 @@ type cfg struct {
|
||||||
log *logger.Logger
|
log *logger.Logger
|
||||||
key *ecdsa.PrivateKey
|
key *ecdsa.PrivateKey
|
||||||
rawPub []byte
|
rawPub []byte
|
||||||
|
state netmap.State
|
||||||
nmSource netmap.Source
|
nmSource netmap.Source
|
||||||
cnrSource ContainerSource
|
cnrSource ContainerSource
|
||||||
frostfsidSubjectProvider frostfsidcore.SubjectProvider
|
frostfsidSubjectProvider frostfsidcore.SubjectProvider
|
||||||
|
@ -156,3 +157,9 @@ func WithAPERouter(router policyengine.ChainRouter) Option {
|
||||||
c.router = router
|
c.router = router
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithNetmapState(state netmap.State) Option {
|
||||||
|
return func(c *cfg) {
|
||||||
|
c.state = state
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -84,7 +84,7 @@ func (s *Service) verifyClient(ctx context.Context, req message, cid cidSDK.ID,
|
||||||
// FIXME(@aarifullin): tree service temporiraly performs APE checks on
|
// FIXME(@aarifullin): tree service temporiraly performs APE checks on
|
||||||
// object verbs, because tree verbs have not been introduced yet.
|
// object verbs, because tree verbs have not been introduced yet.
|
||||||
if basicACL == 0x0 {
|
if basicACL == 0x0 {
|
||||||
return s.checkAPE(ctx, cnr, cid, op, role, pubKey)
|
return s.checkAPE(ctx, bt, cnr, cid, op, role, pubKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !basicACL.IsOpAllowed(op, role) {
|
if !basicACL.IsOpAllowed(op, role) {
|
||||||
|
|
Loading…
Reference in a new issue