2024-11-18 09:54:02 +00:00
|
|
|
package ape
|
2023-10-31 08:55:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-02-16 09:36:42 +00:00
|
|
|
"os"
|
2023-10-31 08:55:42 +00:00
|
|
|
"strings"
|
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
2023-11-10 08:40:06 +00:00
|
|
|
nativeschema "git.frostfs.info/TrueCloudLab/policy-engine/schema/native"
|
2023-10-31 08:55:42 +00:00
|
|
|
"github.com/flynn-archive/go-shlex"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-01-25 17:25:23 +00:00
|
|
|
errInvalidStatementFormat = errors.New("invalid statement format")
|
|
|
|
errInvalidConditionFormat = errors.New("invalid condition format")
|
|
|
|
errUnknownStatus = errors.New("status is not recognized")
|
|
|
|
errUnknownStatusDetail = errors.New("status detail is not recognized")
|
|
|
|
errUnknownAction = errors.New("action is not recognized")
|
|
|
|
errUnknownBinaryOperator = errors.New("binary operator is not recognized")
|
|
|
|
errUnknownCondObjectType = errors.New("condition object type is not recognized")
|
2024-05-14 09:23:26 +00:00
|
|
|
errMixedTypesInRule = errors.New("found mixed type of actions in rule")
|
2024-01-25 17:25:23 +00:00
|
|
|
errNoActionsInRule = errors.New("there are no actions in rule")
|
|
|
|
errUnsupportedResourceFormat = errors.New("unsupported resource format")
|
2024-03-11 14:55:50 +00:00
|
|
|
errFailedToParseAllAny = errors.New("any/all is not parsed")
|
2023-10-31 08:55:42 +00:00
|
|
|
)
|
|
|
|
|
2024-02-16 09:36:42 +00:00
|
|
|
func ParseAPEChainBinaryOrJSON(chain *apechain.Chain, path string) error {
|
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read file <%s>: %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = chain.UnmarshalBinary(data)
|
|
|
|
if err != nil {
|
|
|
|
err = chain.UnmarshalJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid format: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-31 08:55:42 +00:00
|
|
|
// ParseAPEChain parses APE chain rules.
|
2023-11-16 07:58:55 +00:00
|
|
|
func ParseAPEChain(chain *apechain.Chain, rules []string) error {
|
2023-10-31 08:55:42 +00:00
|
|
|
if len(rules) == 0 {
|
|
|
|
return errors.New("no APE rules provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rule := range rules {
|
2023-11-16 07:58:55 +00:00
|
|
|
r := new(apechain.Rule)
|
2023-10-31 08:55:42 +00:00
|
|
|
if err := ParseAPERule(r, rule); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
chain.Rules = append(chain.Rules, *r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseAPERule parses access-policy-engine statement from the following form:
|
2024-01-25 17:25:23 +00:00
|
|
|
// <status>[:status_detail] <action>... [<condition>...] <resource>...
|
2023-10-31 08:55:42 +00:00
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
// deny Object.Put *
|
|
|
|
// deny:QuotaLimitReached Object.Put *
|
|
|
|
// allow Object.Put *
|
2024-05-14 09:23:26 +00:00
|
|
|
// allow Object.Get ResourceCondition:Department=HR RequestCondition:Actor=ownerA *
|
|
|
|
// allow Object.Get any ResourceCondition:Department=HR RequestCondition:Actor=ownerA *
|
|
|
|
// allow Object.Get all ResourceCondition:Department=HR RequestCondition:Actor=ownerA *
|
2024-02-14 14:57:19 +00:00
|
|
|
// allow Object.* *
|
|
|
|
// allow Container.* *
|
2023-10-31 08:55:42 +00:00
|
|
|
//
|
|
|
|
//nolint:godot
|
2023-11-16 07:58:55 +00:00
|
|
|
func ParseAPERule(r *apechain.Rule, rule string) error {
|
2023-10-31 08:55:42 +00:00
|
|
|
lexemes, err := shlex.Split(rule)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't parse rule '%s': %v", rule, err)
|
|
|
|
}
|
|
|
|
return parseRuleLexemes(r, lexemes)
|
|
|
|
}
|
|
|
|
|
2024-02-14 14:57:19 +00:00
|
|
|
func unique(inputSlice []string) []string {
|
|
|
|
uniqueSlice := make([]string, 0, len(inputSlice))
|
|
|
|
seen := make(map[string]bool, len(inputSlice))
|
|
|
|
for _, element := range inputSlice {
|
|
|
|
if !seen[element] {
|
|
|
|
uniqueSlice = append(uniqueSlice, element)
|
|
|
|
seen[element] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uniqueSlice
|
|
|
|
}
|
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
func parseRuleLexemes(r *apechain.Rule, lexemes []string) error {
|
2023-10-31 08:55:42 +00:00
|
|
|
if len(lexemes) < 2 {
|
|
|
|
return errInvalidStatementFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
r.Status, err = parseStatus(lexemes[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-14 09:23:26 +00:00
|
|
|
var objectTargeted bool
|
|
|
|
var containerTargeted bool
|
|
|
|
|
2024-01-25 17:25:23 +00:00
|
|
|
for i, lexeme := range lexemes[1:] {
|
2024-02-16 10:13:54 +00:00
|
|
|
anyExpr, anyErr := parseAnyAll(lexeme)
|
|
|
|
if anyErr == nil {
|
|
|
|
r.Any = anyExpr
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-14 14:57:19 +00:00
|
|
|
var names []string
|
2024-01-25 17:25:23 +00:00
|
|
|
var actionType bool
|
2024-02-14 14:57:19 +00:00
|
|
|
names, actionType, err = parseAction(lexeme)
|
2024-01-25 17:25:23 +00:00
|
|
|
if err != nil {
|
|
|
|
condition, errCond := parseCondition(lexeme)
|
|
|
|
if errCond != nil {
|
|
|
|
err = fmt.Errorf("%w:%w", err, errCond)
|
|
|
|
lexemes = lexemes[i+1:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
r.Condition = append(r.Condition, *condition)
|
|
|
|
} else {
|
2024-05-14 09:23:26 +00:00
|
|
|
if actionType {
|
|
|
|
objectTargeted = true
|
|
|
|
} else {
|
|
|
|
containerTargeted = true
|
|
|
|
}
|
|
|
|
if objectTargeted && containerTargeted {
|
|
|
|
// Actually, APE chain allows to define rules for several resources, for example, if
|
|
|
|
// chain target is namespace, but the parser primitevly compiles verbs,
|
|
|
|
// conditions and resources in one rule. So, for the parser, one rule relates only to
|
|
|
|
// one resource type - object or container.
|
|
|
|
return errMixedTypesInRule
|
|
|
|
}
|
|
|
|
|
2024-02-14 14:57:19 +00:00
|
|
|
r.Actions.Names = append(r.Actions.Names, names...)
|
2024-01-25 17:25:23 +00:00
|
|
|
}
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
2024-02-14 14:57:19 +00:00
|
|
|
r.Actions.Names = unique(r.Actions.Names)
|
2024-01-25 17:25:23 +00:00
|
|
|
if len(r.Actions.Names) == 0 {
|
|
|
|
return fmt.Errorf("%w:%w", err, errNoActionsInRule)
|
|
|
|
}
|
|
|
|
for _, lexeme := range lexemes {
|
2024-05-14 09:23:26 +00:00
|
|
|
resource, errRes := parseResource(lexeme, objectTargeted)
|
2024-01-25 17:25:23 +00:00
|
|
|
if errRes != nil {
|
|
|
|
return fmt.Errorf("%w:%w", err, errRes)
|
|
|
|
}
|
|
|
|
r.Resources.Names = append(r.Resources.Names, resource)
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 17:25:23 +00:00
|
|
|
return nil
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
|
2024-02-16 10:13:54 +00:00
|
|
|
func parseAnyAll(lexeme string) (bool, error) {
|
|
|
|
switch strings.ToLower(lexeme) {
|
|
|
|
case "any":
|
|
|
|
return true, nil
|
|
|
|
case "all":
|
|
|
|
return false, nil
|
|
|
|
default:
|
2024-03-11 14:55:50 +00:00
|
|
|
return false, errFailedToParseAllAny
|
2024-02-16 10:13:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
func parseStatus(lexeme string) (apechain.Status, error) {
|
2023-10-31 08:55:42 +00:00
|
|
|
action, expression, found := strings.Cut(lexeme, ":")
|
2024-01-25 17:25:23 +00:00
|
|
|
switch strings.ToLower(action) {
|
2023-10-31 08:55:42 +00:00
|
|
|
case "deny":
|
|
|
|
if !found {
|
2023-11-16 07:58:55 +00:00
|
|
|
return apechain.AccessDenied, nil
|
2023-10-31 08:55:42 +00:00
|
|
|
} else if strings.EqualFold(expression, "QuotaLimitReached") {
|
2023-11-16 07:58:55 +00:00
|
|
|
return apechain.QuotaLimitReached, nil
|
2023-10-31 08:55:42 +00:00
|
|
|
} else {
|
2024-01-25 17:25:23 +00:00
|
|
|
return 0, fmt.Errorf("%w: %s", errUnknownStatusDetail, expression)
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
case "allow":
|
|
|
|
if found {
|
2024-01-25 17:25:23 +00:00
|
|
|
return 0, errUnknownStatusDetail
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
2023-11-16 07:58:55 +00:00
|
|
|
return apechain.Allow, nil
|
2023-10-31 08:55:42 +00:00
|
|
|
default:
|
2024-01-25 17:25:23 +00:00
|
|
|
return 0, errUnknownStatus
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-14 14:57:19 +00:00
|
|
|
func parseAction(lexeme string) ([]string, bool, error) {
|
2023-10-31 08:55:42 +00:00
|
|
|
switch strings.ToLower(lexeme) {
|
|
|
|
case "object.put":
|
2024-02-14 14:57:19 +00:00
|
|
|
return []string{nativeschema.MethodPutObject}, true, nil
|
2023-10-31 08:55:42 +00:00
|
|
|
case "object.get":
|
2024-02-14 14:57:19 +00:00
|
|
|
return []string{nativeschema.MethodGetObject}, true, nil
|
2023-10-31 08:55:42 +00:00
|
|
|
case "object.head":
|
2024-02-14 14:57:19 +00:00
|
|
|
return []string{nativeschema.MethodHeadObject}, true, nil
|
2023-10-31 08:55:42 +00:00
|
|
|
case "object.delete":
|
2024-02-14 14:57:19 +00:00
|
|
|
return []string{nativeschema.MethodDeleteObject}, true, nil
|
2023-10-31 08:55:42 +00:00
|
|
|
case "object.search":
|
2024-02-14 14:57:19 +00:00
|
|
|
return []string{nativeschema.MethodSearchObject}, true, nil
|
2023-10-31 08:55:42 +00:00
|
|
|
case "object.range":
|
2024-02-14 14:57:19 +00:00
|
|
|
return []string{nativeschema.MethodRangeObject}, true, nil
|
2023-10-31 08:55:42 +00:00
|
|
|
case "object.hash":
|
2024-02-14 14:57:19 +00:00
|
|
|
return []string{nativeschema.MethodHashObject}, true, nil
|
2024-08-14 11:39:13 +00:00
|
|
|
case "object.patch":
|
|
|
|
return []string{nativeschema.MethodPatchObject}, true, nil
|
2024-02-14 14:57:19 +00:00
|
|
|
case "object.*":
|
|
|
|
return []string{
|
|
|
|
nativeschema.MethodPutObject,
|
|
|
|
nativeschema.MethodGetObject,
|
|
|
|
nativeschema.MethodHeadObject,
|
|
|
|
nativeschema.MethodDeleteObject,
|
|
|
|
nativeschema.MethodSearchObject,
|
2024-07-08 12:29:29 +00:00
|
|
|
nativeschema.MethodRangeObject,
|
2024-02-14 14:57:19 +00:00
|
|
|
nativeschema.MethodHashObject,
|
2024-08-14 11:39:13 +00:00
|
|
|
nativeschema.MethodPatchObject,
|
2024-02-14 14:57:19 +00:00
|
|
|
}, true, nil
|
2024-01-25 17:25:23 +00:00
|
|
|
case "container.put":
|
2024-02-14 14:57:19 +00:00
|
|
|
return []string{nativeschema.MethodPutContainer}, false, nil
|
2024-01-25 17:25:23 +00:00
|
|
|
case "container.delete":
|
2024-02-14 14:57:19 +00:00
|
|
|
return []string{nativeschema.MethodDeleteContainer}, false, nil
|
2024-01-25 17:25:23 +00:00
|
|
|
case "container.get":
|
2024-02-14 14:57:19 +00:00
|
|
|
return []string{nativeschema.MethodGetContainer}, false, nil
|
|
|
|
case "container.list":
|
|
|
|
return []string{nativeschema.MethodListContainers}, false, nil
|
|
|
|
case "container.*":
|
|
|
|
return []string{
|
|
|
|
nativeschema.MethodPutContainer,
|
|
|
|
nativeschema.MethodDeleteContainer,
|
|
|
|
nativeschema.MethodGetContainer,
|
|
|
|
nativeschema.MethodListContainers,
|
|
|
|
}, false, nil
|
2023-10-31 08:55:42 +00:00
|
|
|
default:
|
|
|
|
}
|
2024-02-14 14:57:19 +00:00
|
|
|
return nil, false, fmt.Errorf("%w: %s", errUnknownAction, lexeme)
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 17:25:23 +00:00
|
|
|
func parseResource(lexeme string, isObj bool) (string, error) {
|
|
|
|
if len(lexeme) > 0 && !strings.HasSuffix(lexeme, "/") {
|
|
|
|
if isObj {
|
|
|
|
if lexeme == "*" {
|
|
|
|
return nativeschema.ResourceFormatAllObjects, nil
|
2024-06-14 10:06:11 +00:00
|
|
|
} else if lexeme == "/*" || lexeme == "root/*" {
|
2024-01-25 17:25:23 +00:00
|
|
|
return nativeschema.ResourceFormatRootObjects, nil
|
|
|
|
} else if strings.HasPrefix(lexeme, "/") {
|
|
|
|
lexeme = lexeme[1:]
|
|
|
|
delimCount := strings.Count(lexeme, "/")
|
|
|
|
if delimCount == 1 && len(lexeme) >= 3 { // container/object
|
|
|
|
return nativeschema.ObjectPrefix + "//" + lexeme, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delimCount := strings.Count(lexeme, "/")
|
|
|
|
if delimCount == 1 && len(lexeme) >= 3 ||
|
|
|
|
delimCount == 2 && len(lexeme) >= 5 { // namespace/container/object
|
|
|
|
return nativeschema.ObjectPrefix + "/" + lexeme, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if lexeme == "*" {
|
|
|
|
return nativeschema.ResourceFormatAllContainers, nil
|
|
|
|
} else if lexeme == "/*" {
|
|
|
|
return nativeschema.ResourceFormatRootContainers, nil
|
|
|
|
} else if strings.HasPrefix(lexeme, "/") && len(lexeme) > 1 {
|
|
|
|
lexeme = lexeme[1:]
|
|
|
|
delimCount := strings.Count(lexeme, "/")
|
|
|
|
if delimCount == 0 {
|
|
|
|
return nativeschema.ContainerPrefix + "//" + lexeme, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delimCount := strings.Count(lexeme, "/")
|
|
|
|
if delimCount == 1 && len(lexeme) > 3 { // namespace/container
|
|
|
|
return nativeschema.ContainerPrefix + "/" + lexeme, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-10 08:40:06 +00:00
|
|
|
}
|
2024-01-25 17:25:23 +00:00
|
|
|
return "", errUnsupportedResourceFormat
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2024-05-14 09:23:26 +00:00
|
|
|
ResourceCondition = "resourcecondition"
|
|
|
|
RequestCondition = "requestcondition"
|
2023-10-31 08:55:42 +00:00
|
|
|
)
|
|
|
|
|
2024-05-14 09:23:26 +00:00
|
|
|
var typeToCondKindType = map[string]apechain.ConditionKindType{
|
|
|
|
ResourceCondition: apechain.KindResource,
|
|
|
|
RequestCondition: apechain.KindRequest,
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 17:25:23 +00:00
|
|
|
func parseCondition(lexeme string) (*apechain.Condition, error) {
|
|
|
|
typ, expression, found := strings.Cut(lexeme, ":")
|
|
|
|
typ = strings.ToLower(typ)
|
2023-10-31 08:55:42 +00:00
|
|
|
|
2024-05-14 09:23:26 +00:00
|
|
|
condKindType, ok := typeToCondKindType[typ]
|
2024-01-25 17:25:23 +00:00
|
|
|
if ok {
|
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("%w: %s", errInvalidConditionFormat, lexeme)
|
|
|
|
}
|
2023-10-31 08:55:42 +00:00
|
|
|
|
2024-01-25 17:25:23 +00:00
|
|
|
var cond apechain.Condition
|
2024-05-14 09:23:26 +00:00
|
|
|
cond.Kind = condKindType
|
2023-10-31 08:55:42 +00:00
|
|
|
|
2024-01-25 17:25:23 +00:00
|
|
|
lhs, rhs, binExpFound := strings.Cut(expression, "!=")
|
|
|
|
if !binExpFound {
|
|
|
|
lhs, rhs, binExpFound = strings.Cut(expression, "=")
|
2023-10-31 08:55:42 +00:00
|
|
|
if !binExpFound {
|
2024-01-25 17:25:23 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", errUnknownBinaryOperator, expression)
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
2024-01-25 17:25:23 +00:00
|
|
|
cond.Op = apechain.CondStringEquals
|
2023-10-31 08:55:42 +00:00
|
|
|
} else {
|
2024-01-25 17:25:23 +00:00
|
|
|
cond.Op = apechain.CondStringNotEquals
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 17:25:23 +00:00
|
|
|
cond.Key, cond.Value = lhs, rhs
|
|
|
|
return &cond, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("%w: %s", errUnknownCondObjectType, typ)
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|