2024-04-11 15:10:33 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/ape/converter"
|
|
|
|
aperequest "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/ape/request"
|
|
|
|
core "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cnrSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/acl"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
|
|
|
nativeschema "git.frostfs.info/TrueCloudLab/policy-engine/schema/native"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Service) checkAPE(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
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaMethod, err := converter.SchemaMethodFromACLOperation(operation)
|
|
|
|
if err != nil {
|
|
|
|
return apeErr(err)
|
|
|
|
}
|
|
|
|
schemaRole, err := converter.SchemaRoleFromACLRole(role)
|
|
|
|
if err != nil {
|
|
|
|
return apeErr(err)
|
|
|
|
}
|
|
|
|
reqProps := map[string]string{
|
|
|
|
nativeschema.PropertyKeyActorPublicKey: hex.EncodeToString(publicKey.Bytes()),
|
|
|
|
nativeschema.PropertyKeyActorRole: schemaRole,
|
|
|
|
}
|
2024-04-15 14:04:34 +00:00
|
|
|
reqProps, err = s.fillWithUserClaimTags(reqProps, publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return apeErr(err)
|
|
|
|
}
|
2024-04-11 15:10:33 +00:00
|
|
|
|
|
|
|
var resourceName string
|
|
|
|
if namespace == "root" || namespace == "" {
|
|
|
|
resourceName = fmt.Sprintf(nativeschema.ResourceFormatRootContainerObjects, cid.EncodeToString())
|
|
|
|
} else {
|
|
|
|
resourceName = fmt.Sprintf(nativeschema.ResourceFormatNamespaceContainerObjects, namespace, cid.EncodeToString())
|
|
|
|
}
|
|
|
|
|
|
|
|
request := aperequest.NewRequest(
|
|
|
|
schemaMethod,
|
|
|
|
aperequest.NewResource(resourceName, make(map[string]string)),
|
|
|
|
reqProps,
|
|
|
|
)
|
|
|
|
|
2024-04-11 10:51:39 +00:00
|
|
|
rt := engine.NewRequestTargetExtended(namespace, cid.EncodeToString(), fmt.Sprintf("%s:%s", namespace, publicKey.Address()), nil)
|
|
|
|
status, found, err := s.router.IsAllowed(apechain.Ingress, rt, request)
|
2024-04-11 15:10:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return apeErr(err)
|
|
|
|
}
|
|
|
|
if found && status == apechain.Allow {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = fmt.Errorf("access to operation %s is denied by access policy engine: %s", schemaMethod, status.String())
|
|
|
|
return apeErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func apeErr(err error) error {
|
|
|
|
errAccessDenied := &apistatus.ObjectAccessDenied{}
|
|
|
|
errAccessDenied.WriteReason(err.Error())
|
|
|
|
return errAccessDenied
|
|
|
|
}
|
2024-04-15 14:04:34 +00:00
|
|
|
|
|
|
|
// fillWithUserClaimTags fills ape request properties with user claim tags getting them from frostfsid contract by actor public key.
|
|
|
|
func (s *Service) fillWithUserClaimTags(reqProps map[string]string, publicKey *keys.PublicKey) (map[string]string, error) {
|
|
|
|
if reqProps == nil {
|
|
|
|
reqProps = make(map[string]string)
|
|
|
|
}
|
2024-05-02 18:15:58 +00:00
|
|
|
props, err := aperequest.FormFrostfsIDRequestProperties(s.frostfsidSubjectProvider, publicKey)
|
2024-04-15 14:04:34 +00:00
|
|
|
if err != nil {
|
2024-05-02 18:15:58 +00:00
|
|
|
return reqProps, err
|
2024-04-15 14:04:34 +00:00
|
|
|
}
|
2024-05-02 18:15:58 +00:00
|
|
|
for propertyName, properyValue := range props {
|
|
|
|
reqProps[propertyName] = properyValue
|
2024-04-15 14:04:34 +00:00
|
|
|
}
|
|
|
|
return reqProps, nil
|
|
|
|
}
|