From 9472a7123ed3e838812562acbebbf2107b22fd17 Mon Sep 17 00:00:00 2001 From: aarifullin Date: Tue, 7 Nov 2023 19:53:23 +0300 Subject: [PATCH] [#7] engine: Move globMatch to common util package Signed-off-by: Airat Arifullin --- chain.go | 10 ++++++---- inmemory.go | 6 +++++- glob.go => util/glob.go | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) rename glob.go => util/glob.go (87%) diff --git a/chain.go b/chain.go index 9be3cde..cb17f70 100644 --- a/chain.go +++ b/chain.go @@ -4,6 +4,8 @@ import ( "encoding/json" "fmt" "strings" + + "git.frostfs.info/TrueCloudLab/policy-engine/util" ) // Engine ... @@ -159,9 +161,9 @@ func (c *Condition) Match(req Request) bool { case CondStringNotEqualsIgnoreCase: return !strings.EqualFold(val, c.Value) case CondStringLike: - return globMatch(val, c.Value) + return util.GlobMatch(val, c.Value) case CondStringNotLike: - return !globMatch(val, c.Value) + return !util.GlobMatch(val, c.Value) case CondStringLessThan: return val < c.Value case CondStringLessThanEquals: @@ -176,7 +178,7 @@ func (c *Condition) Match(req Request) bool { func (r *Rule) Match(req Request) (status Status, matched bool) { found := len(r.Resources.Names) == 0 for i := range r.Resources.Names { - if globMatch(req.Resource().Name(), r.Resources.Names[i]) != r.Resources.Inverted { + if util.GlobMatch(req.Resource().Name(), r.Resources.Names[i]) != r.Resources.Inverted { found = true break } @@ -185,7 +187,7 @@ func (r *Rule) Match(req Request) (status Status, matched bool) { return NoRuleFound, false } for i := range r.Actions.Names { - if globMatch(req.Operation(), r.Actions.Names[i]) != r.Actions.Inverted { + if util.GlobMatch(req.Operation(), r.Actions.Names[i]) != r.Actions.Inverted { return r.matchCondition(req) } } diff --git a/inmemory.go b/inmemory.go index 5bd4f4c..50c146d 100644 --- a/inmemory.go +++ b/inmemory.go @@ -1,5 +1,9 @@ package policyengine +import ( + "git.frostfs.info/TrueCloudLab/policy-engine/util" +) + type inmemory struct { namespace map[Name][]chain resource map[Name][]chain @@ -52,7 +56,7 @@ func (s *inmemory) IsAllowed(name Name, namespace string, r Request) (Status, bo func matchArray(cs []chain, object string, r Request) (Status, bool) { for _, c := range cs { - if !globMatch(object, c.object) { + if !util.GlobMatch(object, c.object) { continue } if status, matched := c.chain.Match(r); matched { diff --git a/glob.go b/util/glob.go similarity index 87% rename from glob.go rename to util/glob.go index 41a28d3..9e8ed36 100644 --- a/glob.go +++ b/util/glob.go @@ -1,4 +1,4 @@ -package policyengine +package util import ( "strings" @@ -9,7 +9,7 @@ import ( // ? in pattern correspond to any symbol. // * in pattern correspond to any sequence of symbols. // Currently only '*' in the suffix is supported. -func globMatch(s, pattern string) bool { +func GlobMatch(s, pattern string) bool { index := strings.IndexByte(pattern, '*') switch index { default: