frostfs-node/pkg/services/object/ape/checker.go
Alex Vanin f41d743203
Some checks failed
DCO action / DCO (pull_request) Failing after 3m8s
Vulncheck / Vulncheck (pull_request) Failing after 3m47s
Tests and linters / Staticcheck (pull_request) Failing after 4m22s
Build / Build Components (1.21) (pull_request) Successful in 4m25s
Build / Build Components (1.20) (pull_request) Successful in 4m32s
Tests and linters / gopls check (pull_request) Failing after 5m7s
Tests and linters / Lint (pull_request) Successful in 7m14s
Tests and linters / Tests (1.20) (pull_request) Failing after 7m13s
Tests and linters / Tests (1.21) (pull_request) Failing after 7m19s
Tests and linters / Tests with -race (pull_request) Failing after 7m21s
ape: Add container source to object policy checker
Signed-off-by: Alex Vanin <a.vanin@yadro.com>
2024-03-14 21:10:31 +03:00

83 lines
2.1 KiB
Go

package ape
import (
"context"
"fmt"
objectV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
containercore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
policyengine "git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
)
type containers interface {
Get(cid.ID) (*containercore.Container, error)
}
type checkerImpl struct {
chainRouter policyengine.ChainRouter
headerProvider HeaderProvider
reader containers
}
func NewChecker(chainRouter policyengine.ChainRouter, headerProvider HeaderProvider, reader containers) Checker {
return &checkerImpl{
chainRouter: chainRouter,
headerProvider: headerProvider,
reader: reader,
}
}
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
// If SoftAPECheck is set to true, then NoRuleFound is interpreted as allow.
SoftAPECheck bool
}
var errMissingOID = fmt.Errorf("object ID is not set")
// 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 {
r, err := c.newAPERequest(ctx, prm)
if err != nil {
return fmt.Errorf("failed to create ape request: %w", err)
}
status, ruleFound, err := c.chainRouter.IsAllowed(apechain.Ingress,
policyengine.NewRequestTarget(prm.Namespace, prm.Container.EncodeToString()), r)
if err != nil {
return err
}
if !ruleFound && prm.SoftAPECheck || status == apechain.Allow {
return nil
}
return fmt.Errorf("method %s: %s", prm.Method, status)
}