2023-11-10 08:40:06 +00:00
|
|
|
package ape
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
|
2024-11-07 14:32:10 +00:00
|
|
|
v2acl "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/acl"
|
2023-11-10 08:40:06 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ConvertEACLError struct {
|
|
|
|
nested error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ConvertEACLError) Error() string {
|
|
|
|
if e == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2024-03-11 14:55:50 +00:00
|
|
|
return "failed to convert eACL table to policy engine chain: " + e.nested.Error()
|
2023-11-10 08:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ConvertEACLError) Unwrap() error {
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e.nested
|
|
|
|
}
|
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
// ConvertEACLToAPE converts eacl.Table to apechain.Chain.
|
|
|
|
func ConvertEACLToAPE(eaclTable *eacl.Table) (*apechain.Chain, error) {
|
2023-11-10 08:40:06 +00:00
|
|
|
if eaclTable == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-12-11 13:55:12 +00:00
|
|
|
res := &apechain.Chain{
|
|
|
|
MatchType: apechain.MatchTypeFirstMatch,
|
|
|
|
}
|
2023-11-10 08:40:06 +00:00
|
|
|
|
|
|
|
resource := getResource(eaclTable)
|
|
|
|
|
|
|
|
for _, eaclRecord := range eaclTable.Records() {
|
|
|
|
if len(eaclRecord.Targets()) == 0 {
|
|
|
|
// see https://git.frostfs.info/TrueCloudLab/frostfs-sdk-go/src/commit/ab75edd70939564421936d207ef80d6c1398b51b/eacl/validator.go#L101
|
|
|
|
// and https://git.frostfs.info/TrueCloudLab/frostfs-sdk-go/src/commit/ab75edd70939564421936d207ef80d6c1398b51b/eacl/validator.go#L36
|
|
|
|
// such record doesn't have any effect
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := actionToStatus(eaclRecord.Action())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
act, err := operationToAction(eaclRecord.Operation())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(eaclRecord.Filters()) == 0 {
|
|
|
|
res.Rules = appendTargetsOnly(res.Rules, st, act, resource, eaclRecord.Targets())
|
|
|
|
} else {
|
|
|
|
res.Rules, err = appendTargetsAndFilters(res.Rules, st, act, resource, eaclRecord.Targets(), eaclRecord.Filters())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-06-18 15:29:56 +00:00
|
|
|
func apeRoleConds(role eacl.Role) (res []apechain.Condition) {
|
|
|
|
switch role {
|
|
|
|
case eacl.RoleSystem:
|
|
|
|
res = append(res,
|
|
|
|
apechain.Condition{
|
|
|
|
Op: apechain.CondStringEquals,
|
|
|
|
Kind: apechain.KindRequest,
|
|
|
|
Key: nativeschema.PropertyKeyActorRole,
|
|
|
|
Value: nativeschema.PropertyValueContainerRoleContainer,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
res = append(res,
|
|
|
|
apechain.Condition{
|
|
|
|
Op: apechain.CondStringEquals,
|
|
|
|
Kind: apechain.KindRequest,
|
|
|
|
Key: nativeschema.PropertyKeyActorRole,
|
|
|
|
Value: nativeschema.PropertyValueContainerRoleIR,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
case eacl.RoleOthers:
|
|
|
|
res = append(res,
|
|
|
|
apechain.Condition{
|
|
|
|
Op: apechain.CondStringEquals,
|
|
|
|
Kind: apechain.KindRequest,
|
|
|
|
Key: nativeschema.PropertyKeyActorRole,
|
|
|
|
Value: nativeschema.PropertyValueContainerRoleOthers,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
case eacl.RoleUser:
|
|
|
|
res = append(res,
|
|
|
|
apechain.Condition{
|
|
|
|
Op: apechain.CondStringEquals,
|
|
|
|
Kind: apechain.KindRequest,
|
|
|
|
Key: nativeschema.PropertyKeyActorRole,
|
|
|
|
Value: nativeschema.PropertyValueContainerRoleOwner,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
case eacl.RoleUnknown:
|
|
|
|
// such condition has no effect
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
func appendTargetsOnly(source []apechain.Rule, st apechain.Status, act apechain.Actions, res apechain.Resources, targets []eacl.Target) []apechain.Rule {
|
2023-11-10 08:40:06 +00:00
|
|
|
// see https://git.frostfs.info/TrueCloudLab/frostfs-sdk-go/src/commit/ab75edd70939564421936d207ef80d6c1398b51b/eacl/validator.go#L101
|
|
|
|
// role OR public key must be equal
|
2023-11-16 07:58:55 +00:00
|
|
|
rule := apechain.Rule{
|
2023-11-10 08:40:06 +00:00
|
|
|
Status: st,
|
|
|
|
Actions: act,
|
|
|
|
Resources: res,
|
|
|
|
Any: true,
|
|
|
|
}
|
|
|
|
for _, target := range targets {
|
2024-06-18 15:29:56 +00:00
|
|
|
rule.Condition = append(rule.Condition, apeRoleConds(target.Role())...)
|
2023-11-10 08:40:06 +00:00
|
|
|
for _, binKey := range target.BinaryKeys() {
|
2023-11-16 07:58:55 +00:00
|
|
|
var pubKeyCondition apechain.Condition
|
2024-05-14 09:23:26 +00:00
|
|
|
pubKeyCondition.Kind = apechain.KindRequest
|
2023-11-10 08:40:06 +00:00
|
|
|
pubKeyCondition.Key = nativeschema.PropertyKeyActorPublicKey
|
|
|
|
pubKeyCondition.Value = hex.EncodeToString(binKey)
|
2023-11-16 07:58:55 +00:00
|
|
|
pubKeyCondition.Op = apechain.CondStringEquals
|
2023-11-10 08:40:06 +00:00
|
|
|
rule.Condition = append(rule.Condition, pubKeyCondition)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(source, rule)
|
|
|
|
}
|
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
func appendTargetsAndFilters(source []apechain.Rule, st apechain.Status, act apechain.Actions, res apechain.Resources,
|
2023-11-10 08:40:06 +00:00
|
|
|
targets []eacl.Target, filters []eacl.Filter,
|
2023-11-16 07:58:55 +00:00
|
|
|
) ([]apechain.Rule, error) {
|
2023-11-10 08:40:06 +00:00
|
|
|
// see https://git.frostfs.info/TrueCloudLab/frostfs-sdk-go/src/commit/ab75edd70939564421936d207ef80d6c1398b51b/eacl/validator.go#L101
|
|
|
|
// role OR public key must be equal
|
|
|
|
// so filters are repeated for each role and public key
|
|
|
|
var err error
|
|
|
|
for _, target := range targets {
|
2023-11-16 07:58:55 +00:00
|
|
|
rule := apechain.Rule{
|
2023-11-10 08:40:06 +00:00
|
|
|
Status: st,
|
|
|
|
Actions: act,
|
|
|
|
Resources: res,
|
|
|
|
}
|
2024-06-18 15:29:56 +00:00
|
|
|
rule.Condition = append(rule.Condition, apeRoleConds(target.Role())...)
|
2023-11-10 08:40:06 +00:00
|
|
|
rule.Condition, err = appendFilters(rule.Condition, filters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
source = append(source, rule)
|
|
|
|
|
|
|
|
for _, binKey := range target.BinaryKeys() {
|
2023-11-16 07:58:55 +00:00
|
|
|
rule := apechain.Rule{
|
2023-11-10 08:40:06 +00:00
|
|
|
Status: st,
|
|
|
|
Actions: act,
|
|
|
|
Resources: res,
|
|
|
|
}
|
2023-11-16 07:58:55 +00:00
|
|
|
var pubKeyCondition apechain.Condition
|
2024-05-14 09:23:26 +00:00
|
|
|
pubKeyCondition.Kind = apechain.KindRequest
|
2023-11-10 08:40:06 +00:00
|
|
|
pubKeyCondition.Key = nativeschema.PropertyKeyActorPublicKey
|
|
|
|
pubKeyCondition.Value = hex.EncodeToString(binKey)
|
2023-11-16 07:58:55 +00:00
|
|
|
pubKeyCondition.Op = apechain.CondStringEquals
|
2023-11-10 08:40:06 +00:00
|
|
|
|
|
|
|
rule.Condition = append(rule.Condition, pubKeyCondition)
|
|
|
|
rule.Condition, err = appendFilters(rule.Condition, filters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
source = append(source, rule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return source, nil
|
|
|
|
}
|
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
func appendFilters(source []apechain.Condition, filters []eacl.Filter) ([]apechain.Condition, error) {
|
2023-11-10 08:40:06 +00:00
|
|
|
for _, filter := range filters {
|
2023-11-16 07:58:55 +00:00
|
|
|
var cond apechain.Condition
|
2023-11-10 08:40:06 +00:00
|
|
|
var isObject bool
|
|
|
|
if filter.From() == eacl.HeaderFromObject {
|
2024-05-14 09:23:26 +00:00
|
|
|
cond.Kind = apechain.KindResource
|
2023-11-10 08:40:06 +00:00
|
|
|
isObject = true
|
|
|
|
} else if filter.From() == eacl.HeaderFromRequest {
|
2024-05-14 09:23:26 +00:00
|
|
|
cond.Kind = apechain.KindRequest
|
2023-11-10 08:40:06 +00:00
|
|
|
} else {
|
|
|
|
return nil, &ConvertEACLError{nested: fmt.Errorf("unknown filter from: %d", filter.From())}
|
|
|
|
}
|
|
|
|
|
|
|
|
if filter.Matcher() == eacl.MatchStringEqual {
|
2023-11-16 07:58:55 +00:00
|
|
|
cond.Op = apechain.CondStringEquals
|
2023-11-10 08:40:06 +00:00
|
|
|
} else if filter.Matcher() == eacl.MatchStringNotEqual {
|
2023-11-16 07:58:55 +00:00
|
|
|
cond.Op = apechain.CondStringNotEquals
|
2023-11-10 08:40:06 +00:00
|
|
|
} else {
|
|
|
|
return nil, &ConvertEACLError{nested: fmt.Errorf("unknown filter matcher: %d", filter.Matcher())}
|
|
|
|
}
|
|
|
|
|
|
|
|
cond.Key = eaclKeyToAPEKey(filter.Key(), isObject)
|
|
|
|
cond.Value = filter.Value()
|
|
|
|
|
|
|
|
source = append(source, cond)
|
|
|
|
}
|
|
|
|
return source, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func eaclKeyToAPEKey(key string, isObject bool) string {
|
|
|
|
if !isObject {
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
switch key {
|
|
|
|
default:
|
|
|
|
return key
|
|
|
|
case v2acl.FilterObjectVersion:
|
|
|
|
return nativeschema.PropertyKeyObjectVersion
|
|
|
|
case v2acl.FilterObjectID:
|
|
|
|
return nativeschema.PropertyKeyObjectID
|
|
|
|
case v2acl.FilterObjectContainerID:
|
|
|
|
return nativeschema.PropertyKeyObjectContainerID
|
|
|
|
case v2acl.FilterObjectOwnerID:
|
|
|
|
return nativeschema.PropertyKeyObjectOwnerID
|
|
|
|
case v2acl.FilterObjectCreationEpoch:
|
|
|
|
return nativeschema.PropertyKeyObjectCreationEpoch
|
|
|
|
case v2acl.FilterObjectPayloadLength:
|
|
|
|
return nativeschema.PropertyKeyObjectPayloadLength
|
|
|
|
case v2acl.FilterObjectPayloadHash:
|
|
|
|
return nativeschema.PropertyKeyObjectPayloadHash
|
|
|
|
case v2acl.FilterObjectType:
|
|
|
|
return nativeschema.PropertyKeyObjectType
|
|
|
|
case v2acl.FilterObjectHomomorphicHash:
|
|
|
|
return nativeschema.PropertyKeyObjectHomomorphicHash
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
func getResource(eaclTable *eacl.Table) apechain.Resources {
|
2023-11-10 08:40:06 +00:00
|
|
|
cnrID, isSet := eaclTable.CID()
|
|
|
|
if isSet {
|
2023-11-16 07:58:55 +00:00
|
|
|
return apechain.Resources{
|
2023-11-10 08:40:06 +00:00
|
|
|
Names: []string{fmt.Sprintf(nativeschema.ResourceFormatRootContainerObjects, cnrID.EncodeToString())},
|
|
|
|
}
|
|
|
|
}
|
2023-11-16 07:58:55 +00:00
|
|
|
return apechain.Resources{
|
2023-11-10 08:40:06 +00:00
|
|
|
Names: []string{nativeschema.ResourceFormatRootObjects},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
func actionToStatus(a eacl.Action) (apechain.Status, error) {
|
2023-11-10 08:40:06 +00:00
|
|
|
switch a {
|
|
|
|
case eacl.ActionAllow:
|
2023-11-16 07:58:55 +00:00
|
|
|
return apechain.Allow, nil
|
2023-11-10 08:40:06 +00:00
|
|
|
case eacl.ActionDeny:
|
2023-11-16 07:58:55 +00:00
|
|
|
return apechain.AccessDenied, nil
|
2023-11-10 08:40:06 +00:00
|
|
|
default:
|
2023-11-16 07:58:55 +00:00
|
|
|
return apechain.NoRuleFound, &ConvertEACLError{nested: fmt.Errorf("unknown action: %d", a)}
|
2023-11-10 08:40:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
var eaclOperationToEngineAction = map[eacl.Operation]apechain.Actions{
|
2023-11-10 08:40:06 +00:00
|
|
|
eacl.OperationGet: {Names: []string{nativeschema.MethodGetObject}},
|
|
|
|
eacl.OperationHead: {Names: []string{nativeschema.MethodHeadObject}},
|
|
|
|
eacl.OperationPut: {Names: []string{nativeschema.MethodPutObject}},
|
|
|
|
eacl.OperationDelete: {Names: []string{nativeschema.MethodDeleteObject}},
|
|
|
|
eacl.OperationSearch: {Names: []string{nativeschema.MethodSearchObject}},
|
|
|
|
eacl.OperationRange: {Names: []string{nativeschema.MethodRangeObject}},
|
|
|
|
eacl.OperationRangeHash: {Names: []string{nativeschema.MethodHashObject}},
|
|
|
|
}
|
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
func operationToAction(op eacl.Operation) (apechain.Actions, error) {
|
2023-11-10 08:40:06 +00:00
|
|
|
if v, ok := eaclOperationToEngineAction[op]; ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
2023-11-16 07:58:55 +00:00
|
|
|
return apechain.Actions{}, &ConvertEACLError{nested: fmt.Errorf("unknown operation: %d", op)}
|
2023-11-10 08:40:06 +00:00
|
|
|
}
|