2023-12-27 14:18:15 +00:00
|
|
|
package ape
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-05-29 08:11:45 +00:00
|
|
|
"crypto/ecdsa"
|
2024-03-11 14:55:50 +00:00
|
|
|
"errors"
|
2023-12-27 14:18:15 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
objectV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
2024-06-20 12:39:39 +00:00
|
|
|
aperequest "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/ape/request"
|
2024-05-29 08:11:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/ape/router"
|
2024-05-08 07:02:54 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
2024-05-02 17:26:06 +00:00
|
|
|
frostfsidcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/frostfsid"
|
2024-05-08 07:02:54 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
2024-05-29 08:11:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/ape"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
2023-12-27 14:18:15 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2024-03-15 12:16:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2023-12-27 14:18:15 +00:00
|
|
|
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
|
|
|
policyengine "git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
2024-03-12 12:09:55 +00:00
|
|
|
nativeschema "git.frostfs.info/TrueCloudLab/policy-engine/schema/native"
|
2024-04-11 10:51:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2023-12-27 14:18:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type checkerImpl struct {
|
2024-05-08 07:02:54 +00:00
|
|
|
chainRouter policyengine.ChainRouter
|
|
|
|
headerProvider HeaderProvider
|
2024-05-02 17:26:06 +00:00
|
|
|
frostFSIDClient frostfsidcore.SubjectProvider
|
2024-05-08 07:02:54 +00:00
|
|
|
nm netmap.Source
|
2024-05-29 08:11:45 +00:00
|
|
|
st netmap.State
|
2024-05-08 07:02:54 +00:00
|
|
|
cnrSource container.Source
|
|
|
|
nodePK []byte
|
2024-04-15 13:51:19 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 08:11:45 +00:00
|
|
|
func NewChecker(chainRouter policyengine.ChainRouter, headerProvider HeaderProvider, frostFSIDClient frostfsidcore.SubjectProvider, nm netmap.Source, st netmap.State, cnrSource container.Source, nodePK []byte) Checker {
|
2023-12-27 14:18:15 +00:00
|
|
|
return &checkerImpl{
|
2024-05-08 07:02:54 +00:00
|
|
|
chainRouter: chainRouter,
|
|
|
|
headerProvider: headerProvider,
|
2024-04-15 13:51:19 +00:00
|
|
|
frostFSIDClient: frostFSIDClient,
|
2024-05-08 07:02:54 +00:00
|
|
|
nm: nm,
|
2024-05-29 08:11:45 +00:00
|
|
|
st: st,
|
2024-05-08 07:02:54 +00:00
|
|
|
cnrSource: cnrSource,
|
|
|
|
nodePK: nodePK,
|
2023-12-27 14:18:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Prm struct {
|
|
|
|
Namespace string
|
|
|
|
|
|
|
|
Container cid.ID
|
|
|
|
|
|
|
|
// Object ID is omitted for some methods.
|
|
|
|
Object *oid.ID
|
|
|
|
|
|
|
|
// If Header is set, then object attributes and properties will be parsed from
|
|
|
|
// a request/response's header.
|
|
|
|
Header *objectV2.Header
|
|
|
|
|
|
|
|
// Method must be represented only as a constant represented in native schema.
|
|
|
|
Method string
|
|
|
|
|
|
|
|
// Role must be representedonly as a constant represented in native schema.
|
|
|
|
Role string
|
|
|
|
|
|
|
|
// An encoded sender's public key string.
|
|
|
|
SenderKey string
|
2024-02-14 11:14:07 +00:00
|
|
|
|
2024-03-15 12:16:52 +00:00
|
|
|
// An encoded container's owner user ID.
|
|
|
|
ContainerOwner user.ID
|
|
|
|
|
2024-02-14 11:14:07 +00:00
|
|
|
// If SoftAPECheck is set to true, then NoRuleFound is interpreted as allow.
|
|
|
|
SoftAPECheck bool
|
2024-04-04 11:55:38 +00:00
|
|
|
|
|
|
|
// If true, object headers will not retrieved from storage engine.
|
|
|
|
WithoutHeaderRequest bool
|
2024-05-29 08:11:45 +00:00
|
|
|
|
|
|
|
// The request's bearer token. It is used in order to check APE overrides with the token.
|
|
|
|
BearerToken *bearer.Token
|
2023-12-27 14:18:15 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 08:11:45 +00:00
|
|
|
var (
|
|
|
|
errMissingOID = errors.New("object ID is not set")
|
|
|
|
errInvalidTargetType = errors.New("bearer token defines non-container target override")
|
|
|
|
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")
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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, containerID 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.Target.TargetType != ape.TargetTypeContainer {
|
|
|
|
return errInvalidTargetType
|
|
|
|
}
|
|
|
|
|
|
|
|
var targetCnr cid.ID
|
|
|
|
err := targetCnr.DecodeString(apeOverride.Target.Name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid cid format: %s", apeOverride.Target.Name)
|
|
|
|
}
|
|
|
|
if !containerID.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
|
|
|
|
}
|
2023-12-27 14:18:15 +00:00
|
|
|
|
|
|
|
// CheckAPE checks if a request or a response is permitted creating an ape request and passing
|
|
|
|
// it to chain router.
|
|
|
|
func (c *checkerImpl) CheckAPE(ctx context.Context, prm Prm) error {
|
2024-03-12 12:09:55 +00:00
|
|
|
// APE check is ignored for some inter-node requests.
|
|
|
|
if prm.Role == nativeschema.PropertyValueContainerRoleContainer {
|
|
|
|
return nil
|
|
|
|
} else if prm.Role == nativeschema.PropertyValueContainerRoleIR {
|
|
|
|
switch prm.Method {
|
|
|
|
case nativeschema.MethodGetObject,
|
|
|
|
nativeschema.MethodHeadObject,
|
|
|
|
nativeschema.MethodSearchObject,
|
|
|
|
nativeschema.MethodRangeObject,
|
|
|
|
nativeschema.MethodHashObject:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-27 14:18:15 +00:00
|
|
|
r, err := c.newAPERequest(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create ape request: %w", err)
|
|
|
|
}
|
2024-04-11 10:51:39 +00:00
|
|
|
pub, err := keys.NewPublicKeyFromString(prm.SenderKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-20 12:39:39 +00:00
|
|
|
groups, err := aperequest.Groups(c.frostFSIDClient, pub)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get group ids: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Policy contract keeps group related chains as namespace-group pair.
|
|
|
|
for i := range groups {
|
|
|
|
groups[i] = fmt.Sprintf("%s:%s", prm.Namespace, groups[i])
|
|
|
|
}
|
2024-04-11 10:51:39 +00:00
|
|
|
|
2024-05-29 08:11:45 +00:00
|
|
|
if prm.BearerToken != nil && !prm.BearerToken.Impersonate() {
|
|
|
|
if err := isValidBearer(prm.BearerToken, prm.ContainerOwner, prm.Container, pub, c.st); err != nil {
|
|
|
|
return fmt.Errorf("bearer token validation error: %w", err)
|
|
|
|
}
|
|
|
|
btRouter, err := router.SingleUseRouterWithBearerTokenChains([]bearer.APEOverride{prm.BearerToken.APEOverride()})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
status, found, err := btRouter.IsAllowed(apechain.Ingress, policyengine.NewRequestTargetWithContainer(prm.Container.EncodeToString()), r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if found && status == apechain.Allow {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if status != apechain.NoRuleFound {
|
|
|
|
return fmt.Errorf("bearer token: method %s: %s", prm.Method, status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-20 12:39:39 +00:00
|
|
|
rt := policyengine.NewRequestTargetExtended(prm.Namespace, prm.Container.EncodeToString(), fmt.Sprintf("%s:%s", prm.Namespace, pub.Address()), groups)
|
2024-04-11 10:51:39 +00:00
|
|
|
status, ruleFound, err := c.chainRouter.IsAllowed(apechain.Ingress, rt, r)
|
2023-12-27 14:18:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-14 11:14:07 +00:00
|
|
|
if !ruleFound && prm.SoftAPECheck || status == apechain.Allow {
|
2023-12-27 14:18:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-14 11:14:07 +00:00
|
|
|
return fmt.Errorf("method %s: %s", prm.Method, status)
|
2023-12-27 14:18:15 +00:00
|
|
|
}
|