2024-04-11 15:10:33 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
2024-05-20 13:34:31 +00:00
|
|
|
"context"
|
2024-04-11 15:10:33 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2024-05-20 13:34:31 +00:00
|
|
|
"net"
|
2024-04-11 15:10:33 +00:00
|
|
|
"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"
|
2024-09-10 08:15:30 +00:00
|
|
|
checkercore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/common/ape"
|
2024-05-29 08:18:31 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
2024-04-11 15:10:33 +00:00
|
|
|
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"
|
2024-05-20 13:34:31 +00:00
|
|
|
commonschema "git.frostfs.info/TrueCloudLab/policy-engine/schema/common"
|
2024-04-11 15:10:33 +00:00
|
|
|
nativeschema "git.frostfs.info/TrueCloudLab/policy-engine/schema/native"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2024-05-20 13:34:31 +00:00
|
|
|
"google.golang.org/grpc/peer"
|
2024-04-11 15:10:33 +00:00
|
|
|
)
|
|
|
|
|
2024-05-29 08:18:31 +00:00
|
|
|
func (s *Service) newAPERequest(ctx context.Context, namespace string,
|
|
|
|
cid cid.ID, operation acl.Op, role acl.Role, publicKey *keys.PublicKey,
|
|
|
|
) (aperequest.Request, error) {
|
2024-04-11 15:10:33 +00:00
|
|
|
schemaMethod, err := converter.SchemaMethodFromACLOperation(operation)
|
|
|
|
if err != nil {
|
2024-05-29 08:18:31 +00:00
|
|
|
return aperequest.Request{}, err
|
2024-04-11 15:10:33 +00:00
|
|
|
}
|
|
|
|
schemaRole, err := converter.SchemaRoleFromACLRole(role)
|
|
|
|
if err != nil {
|
2024-05-29 08:18:31 +00:00
|
|
|
return aperequest.Request{}, err
|
2024-04-11 15:10:33 +00:00
|
|
|
}
|
|
|
|
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 {
|
2024-05-29 08:18:31 +00:00
|
|
|
return aperequest.Request{}, err
|
2024-04-15 14:04:34 +00:00
|
|
|
}
|
2024-05-20 13:34:31 +00:00
|
|
|
if p, ok := peer.FromContext(ctx); ok {
|
|
|
|
if tcpAddr, ok := p.Addr.(*net.TCPAddr); ok {
|
|
|
|
reqProps[commonschema.PropertyKeyFrostFSSourceIP] = tcpAddr.IP.String()
|
|
|
|
}
|
|
|
|
}
|
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())
|
|
|
|
}
|
|
|
|
|
2024-05-29 08:18:31 +00:00
|
|
|
return aperequest.NewRequest(
|
2024-04-11 15:10:33 +00:00
|
|
|
schemaMethod,
|
|
|
|
aperequest.NewResource(resourceName, make(map[string]string)),
|
|
|
|
reqProps,
|
2024-05-29 08:18:31 +00:00
|
|
|
), 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 {
|
2024-08-19 12:50:35 +00:00
|
|
|
return fmt.Errorf("failed to create ape request: %w", err)
|
2024-05-29 08:18:31 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 08:15:30 +00:00
|
|
|
return s.apeChecker.CheckAPE(checkercore.CheckPrm{
|
2024-10-02 07:09:10 +00:00
|
|
|
Request: request,
|
|
|
|
Namespace: namespace,
|
|
|
|
Container: cid,
|
|
|
|
ContainerOwner: container.Value.Owner(),
|
|
|
|
PublicKey: publicKey,
|
|
|
|
BearerToken: bt,
|
|
|
|
SoftAPECheck: false,
|
2024-09-10 08:15:30 +00:00
|
|
|
})
|
2024-04-11 15:10:33 +00:00
|
|
|
}
|
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
|
|
|
|
}
|