2023-11-07 17:20:54 +00:00
|
|
|
package chain
|
2023-10-17 14:10:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-04-10 15:11:07 +00:00
|
|
|
"net/netip"
|
2023-10-17 14:10:48 +00:00
|
|
|
"strings"
|
2023-11-07 16:53:23 +00:00
|
|
|
|
2023-11-07 17:20:54 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/resource"
|
2023-11-07 16:53:23 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/util"
|
2024-04-01 14:27:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2023-12-20 10:17:15 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2023-10-17 14:10:48 +00:00
|
|
|
)
|
|
|
|
|
2023-11-07 17:20:54 +00:00
|
|
|
// ID is the ID of rule chain.
|
2024-01-18 08:01:39 +00:00
|
|
|
type ID []byte
|
2023-10-30 22:52:04 +00:00
|
|
|
|
2023-12-08 11:07:39 +00:00
|
|
|
// MatchType is the match type for chain rules.
|
|
|
|
type MatchType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// MatchTypeDenyPriority rejects the request if any `Deny` is specified.
|
|
|
|
MatchTypeDenyPriority MatchType = 0
|
|
|
|
// MatchTypeFirstMatch returns the first rule action matched to the request.
|
|
|
|
MatchTypeFirstMatch MatchType = 1
|
|
|
|
)
|
|
|
|
|
2024-01-17 14:49:03 +00:00
|
|
|
//easyjson:json
|
2023-10-17 14:10:48 +00:00
|
|
|
type Chain struct {
|
2023-11-07 17:20:54 +00:00
|
|
|
ID ID
|
2023-10-30 22:52:04 +00:00
|
|
|
|
2023-10-17 14:10:48 +00:00
|
|
|
Rules []Rule
|
2023-12-08 11:07:39 +00:00
|
|
|
|
|
|
|
MatchType MatchType
|
2023-10-17 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Chain) Bytes() []byte {
|
2024-01-17 12:59:06 +00:00
|
|
|
data, err := c.MarshalBinary()
|
2023-10-17 14:10:48 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Chain) DecodeBytes(b []byte) error {
|
2024-01-17 12:59:06 +00:00
|
|
|
return c.UnmarshalBinary(b)
|
2023-10-17 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Rule struct {
|
|
|
|
Status Status
|
|
|
|
// Actions the operation is applied to.
|
2023-10-30 13:34:11 +00:00
|
|
|
Actions Actions
|
2023-10-17 14:10:48 +00:00
|
|
|
// List of the resources the operation is applied to.
|
2023-10-30 13:34:11 +00:00
|
|
|
Resources Resources
|
2023-10-17 14:10:48 +00:00
|
|
|
// True iff individual conditions must be combined with the logical OR.
|
|
|
|
// By default AND is used, so _each_ condition must pass.
|
|
|
|
Any bool
|
|
|
|
Condition []Condition
|
|
|
|
}
|
|
|
|
|
2023-10-30 13:34:11 +00:00
|
|
|
type Actions struct {
|
|
|
|
Inverted bool
|
|
|
|
Names []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Resources struct {
|
|
|
|
Inverted bool
|
|
|
|
Names []string
|
|
|
|
}
|
|
|
|
|
2023-10-17 14:10:48 +00:00
|
|
|
type Condition struct {
|
|
|
|
Op ConditionType
|
|
|
|
Object ObjectType
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
ObjectResource ObjectType = iota
|
|
|
|
ObjectRequest
|
|
|
|
)
|
|
|
|
|
2023-10-23 12:44:01 +00:00
|
|
|
type ConditionType byte
|
2023-10-17 14:10:48 +00:00
|
|
|
|
|
|
|
// TODO @fyrchik: reduce the number of conditions.
|
|
|
|
// Everything from here should be expressable, but we do not need them all.
|
|
|
|
// https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition_operators.html
|
|
|
|
const (
|
|
|
|
// String condition operators.
|
2023-10-23 12:44:01 +00:00
|
|
|
CondStringEquals ConditionType = iota
|
|
|
|
CondStringNotEquals
|
|
|
|
CondStringEqualsIgnoreCase
|
|
|
|
CondStringNotEqualsIgnoreCase
|
|
|
|
CondStringLike
|
|
|
|
CondStringNotLike
|
|
|
|
CondStringLessThan
|
|
|
|
CondStringLessThanEquals
|
|
|
|
CondStringGreaterThan
|
|
|
|
CondStringGreaterThanEquals
|
2023-10-17 14:10:48 +00:00
|
|
|
|
|
|
|
// Numeric condition operators.
|
2023-10-23 12:44:01 +00:00
|
|
|
CondNumericEquals
|
|
|
|
CondNumericNotEquals
|
|
|
|
CondNumericLessThan
|
|
|
|
CondNumericLessThanEquals
|
|
|
|
CondNumericGreaterThan
|
|
|
|
CondNumericGreaterThanEquals
|
2023-12-20 10:17:15 +00:00
|
|
|
|
|
|
|
CondSliceContains
|
2024-04-10 15:11:07 +00:00
|
|
|
|
|
|
|
CondIPAddress
|
|
|
|
CondNotIPAddress
|
2023-10-17 14:10:48 +00:00
|
|
|
)
|
|
|
|
|
2024-01-17 14:49:03 +00:00
|
|
|
var condToStr = []struct {
|
|
|
|
ct ConditionType
|
|
|
|
str string
|
|
|
|
}{
|
|
|
|
{CondStringEquals, "StringEquals"},
|
|
|
|
{CondStringNotEquals, "StringNotEquals"},
|
|
|
|
{CondStringEqualsIgnoreCase, "StringEqualsIgnoreCase"},
|
|
|
|
{CondStringNotEqualsIgnoreCase, "StringNotEqualsIgnoreCase"},
|
|
|
|
{CondStringLike, "StringLike"},
|
|
|
|
{CondStringNotLike, "StringNotLike"},
|
|
|
|
{CondStringLessThan, "StringLessThan"},
|
|
|
|
{CondStringLessThanEquals, "StringLessThanEquals"},
|
|
|
|
{CondStringGreaterThan, "StringGreaterThan"},
|
|
|
|
{CondStringGreaterThanEquals, "StringGreaterThanEquals"},
|
|
|
|
{CondNumericEquals, "NumericEquals"},
|
|
|
|
{CondNumericNotEquals, "NumericNotEquals"},
|
|
|
|
{CondNumericLessThan, "NumericLessThan"},
|
|
|
|
{CondNumericLessThanEquals, "NumericLessThanEquals"},
|
|
|
|
{CondNumericGreaterThan, "NumericGreaterThan"},
|
|
|
|
{CondNumericGreaterThanEquals, "NumericGreaterThanEquals"},
|
|
|
|
{CondSliceContains, "SliceContains"},
|
2024-04-10 15:11:07 +00:00
|
|
|
{CondIPAddress, "IPAddress"},
|
|
|
|
{CondNotIPAddress, "NotIPAddress"},
|
2024-01-17 14:49:03 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 13:34:11 +00:00
|
|
|
func (c ConditionType) String() string {
|
2024-01-17 14:49:03 +00:00
|
|
|
for _, v := range condToStr {
|
|
|
|
if v.ct == c {
|
|
|
|
return v.str
|
|
|
|
}
|
2023-10-30 13:34:11 +00:00
|
|
|
}
|
2024-01-17 14:49:03 +00:00
|
|
|
return "unknown condition type"
|
2023-10-30 13:34:11 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 10:17:15 +00:00
|
|
|
const condSliceContainsDelimiter = "\x00"
|
|
|
|
|
|
|
|
// FormCondSliceContainsValue builds value for ObjectResource or ObjectRequest property
|
|
|
|
// that can be matched by CondSliceContains condition.
|
|
|
|
func FormCondSliceContainsValue(values []string) string {
|
|
|
|
return strings.Join(values, condSliceContainsDelimiter)
|
|
|
|
}
|
|
|
|
|
2023-11-07 17:20:54 +00:00
|
|
|
func (c *Condition) Match(req resource.Request) bool {
|
2023-10-17 14:10:48 +00:00
|
|
|
var val string
|
|
|
|
switch c.Object {
|
|
|
|
case ObjectResource:
|
|
|
|
val = req.Resource().Property(c.Key)
|
|
|
|
case ObjectRequest:
|
|
|
|
val = req.Property(c.Key)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown condition type: %d", c.Object))
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.Op {
|
|
|
|
default:
|
2023-11-01 08:02:16 +00:00
|
|
|
panic(fmt.Sprintf("unimplemented: %d", c.Op))
|
2023-10-17 14:10:48 +00:00
|
|
|
case CondStringEquals:
|
|
|
|
return val == c.Value
|
|
|
|
case CondStringNotEquals:
|
|
|
|
return val != c.Value
|
|
|
|
case CondStringEqualsIgnoreCase:
|
|
|
|
return strings.EqualFold(val, c.Value)
|
|
|
|
case CondStringNotEqualsIgnoreCase:
|
|
|
|
return !strings.EqualFold(val, c.Value)
|
|
|
|
case CondStringLike:
|
2023-11-07 16:53:23 +00:00
|
|
|
return util.GlobMatch(val, c.Value)
|
2023-10-17 14:10:48 +00:00
|
|
|
case CondStringNotLike:
|
2023-11-07 16:53:23 +00:00
|
|
|
return !util.GlobMatch(val, c.Value)
|
2023-10-23 12:44:01 +00:00
|
|
|
case CondStringLessThan:
|
|
|
|
return val < c.Value
|
|
|
|
case CondStringLessThanEquals:
|
|
|
|
return val <= c.Value
|
|
|
|
case CondStringGreaterThan:
|
|
|
|
return val > c.Value
|
|
|
|
case CondStringGreaterThanEquals:
|
|
|
|
return val >= c.Value
|
2023-12-20 10:17:15 +00:00
|
|
|
case CondSliceContains:
|
|
|
|
return slices.Contains(strings.Split(val, condSliceContainsDelimiter), c.Value)
|
2024-04-01 14:27:45 +00:00
|
|
|
case CondNumericEquals, CondNumericNotEquals, CondNumericLessThan, CondNumericLessThanEquals, CondNumericGreaterThan,
|
|
|
|
CondNumericGreaterThanEquals:
|
|
|
|
return c.matchNumeric(val)
|
2024-04-10 15:11:07 +00:00
|
|
|
case CondIPAddress, CondNotIPAddress:
|
|
|
|
return c.matchIP(val)
|
2024-04-01 14:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Condition) matchNumeric(val string) bool {
|
|
|
|
valDecimal, err := fixedn.Fixed8FromString(val)
|
|
|
|
if err != nil {
|
|
|
|
return c.Op == CondNumericNotEquals
|
|
|
|
}
|
|
|
|
|
|
|
|
condVal, err := fixedn.Fixed8FromString(c.Value)
|
|
|
|
if err != nil {
|
|
|
|
return c.Op == CondNumericNotEquals
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.Op {
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unimplemented: %d", c.Op))
|
|
|
|
case CondNumericEquals:
|
|
|
|
return valDecimal.Equal(condVal)
|
|
|
|
case CondNumericNotEquals:
|
|
|
|
return !valDecimal.Equal(condVal)
|
|
|
|
case CondNumericLessThan:
|
|
|
|
return valDecimal.LessThan(condVal)
|
|
|
|
case CondNumericLessThanEquals:
|
|
|
|
return valDecimal.LessThan(condVal) || valDecimal.Equal(condVal)
|
|
|
|
case CondNumericGreaterThan:
|
|
|
|
return valDecimal.GreaterThan(condVal)
|
|
|
|
case CondNumericGreaterThanEquals:
|
|
|
|
return valDecimal.GreaterThan(condVal) || valDecimal.Equal(condVal)
|
2023-10-17 14:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-10 15:11:07 +00:00
|
|
|
func (c *Condition) matchIP(val string) bool {
|
|
|
|
ipAddr, err := netip.ParseAddr(val)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix, err := netip.ParsePrefix(c.Value)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.Op {
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unimplemented: %d", c.Op))
|
|
|
|
case CondIPAddress:
|
|
|
|
return prefix.Contains(ipAddr)
|
|
|
|
case CondNotIPAddress:
|
|
|
|
return !prefix.Contains(ipAddr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 17:20:54 +00:00
|
|
|
func (r *Rule) Match(req resource.Request) (status Status, matched bool) {
|
2023-10-30 13:34:11 +00:00
|
|
|
found := len(r.Resources.Names) == 0
|
|
|
|
for i := range r.Resources.Names {
|
2023-11-07 16:53:23 +00:00
|
|
|
if util.GlobMatch(req.Resource().Name(), r.Resources.Names[i]) != r.Resources.Inverted {
|
2023-10-17 14:10:48 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return NoRuleFound, false
|
|
|
|
}
|
2023-10-30 13:34:11 +00:00
|
|
|
for i := range r.Actions.Names {
|
2023-11-07 16:53:23 +00:00
|
|
|
if util.GlobMatch(req.Operation(), r.Actions.Names[i]) != r.Actions.Inverted {
|
2023-10-17 14:10:48 +00:00
|
|
|
return r.matchCondition(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoRuleFound, false
|
|
|
|
}
|
|
|
|
|
2023-11-07 17:20:54 +00:00
|
|
|
func (r *Rule) matchCondition(obj resource.Request) (status Status, matched bool) {
|
2023-10-17 14:10:48 +00:00
|
|
|
if r.Any {
|
|
|
|
return r.matchAny(obj)
|
|
|
|
}
|
|
|
|
return r.matchAll(obj)
|
|
|
|
}
|
2023-11-01 08:19:30 +00:00
|
|
|
|
2023-11-07 17:20:54 +00:00
|
|
|
func (r *Rule) matchAny(obj resource.Request) (status Status, matched bool) {
|
2023-10-17 14:10:48 +00:00
|
|
|
for i := range r.Condition {
|
|
|
|
if r.Condition[i].Match(obj) {
|
|
|
|
return r.Status, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoRuleFound, false
|
|
|
|
}
|
2023-11-01 08:19:30 +00:00
|
|
|
|
2023-11-07 17:20:54 +00:00
|
|
|
func (r *Rule) matchAll(obj resource.Request) (status Status, matched bool) {
|
2023-10-17 14:10:48 +00:00
|
|
|
for i := range r.Condition {
|
|
|
|
if !r.Condition[i].Match(obj) {
|
|
|
|
return NoRuleFound, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.Status, true
|
|
|
|
}
|
|
|
|
|
2023-11-07 17:20:54 +00:00
|
|
|
func (c *Chain) Match(req resource.Request) (status Status, matched bool) {
|
2023-12-08 11:07:39 +00:00
|
|
|
switch c.MatchType {
|
|
|
|
case MatchTypeDenyPriority:
|
|
|
|
return c.denyPriority(req)
|
|
|
|
case MatchTypeFirstMatch:
|
|
|
|
return c.firstMatch(req)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown MatchType %d", c.MatchType))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Chain) firstMatch(req resource.Request) (status Status, matched bool) {
|
2023-10-17 14:10:48 +00:00
|
|
|
for i := range c.Rules {
|
|
|
|
status, matched := c.Rules[i].Match(req)
|
|
|
|
if matched {
|
|
|
|
return status, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoRuleFound, false
|
|
|
|
}
|
2023-12-08 11:07:39 +00:00
|
|
|
|
|
|
|
func (c *Chain) denyPriority(req resource.Request) (status Status, matched bool) {
|
|
|
|
var allowFound bool
|
|
|
|
for i := range c.Rules {
|
|
|
|
status, matched := c.Rules[i].Match(req)
|
|
|
|
if !matched {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if status != Allow {
|
|
|
|
return status, true
|
|
|
|
}
|
|
|
|
allowFound = true
|
|
|
|
}
|
|
|
|
if allowFound {
|
|
|
|
return Allow, true
|
|
|
|
}
|
|
|
|
return NoRuleFound, false
|
|
|
|
}
|