check for multiple inverse matches (#1332)
* update check for multiple inverse matches Signed-off-by: David Stäheli <mistrdave@gmail.com> * after match, direct return true Signed-off-by: David Stäheli <mistrdave@gmail.com> * apply same behaviour to checkEventGroupConditionPatterns Signed-off-by: David Stäheli <mistrdave@gmail.com> * fix spellmistake of function name Signed-off-by: David Stäheli <mistrdave@gmail.com> --------- Signed-off-by: David Stäheli <mistrdave@gmail.com>
This commit is contained in:
parent
51d8f3b436
commit
f78e4b0443
2 changed files with 64 additions and 10 deletions
|
@ -331,7 +331,7 @@ func (*eventRulesContainer) checkFsEventMatch(conditions *dataprovider.EventCond
|
|||
if !checkEventConditionPatterns(params.Role, conditions.Options.RoleNames) {
|
||||
return false
|
||||
}
|
||||
if !checkEventGroupConditionPatters(params.Groups, conditions.Options.GroupNames) {
|
||||
if !checkEventGroupConditionPatterns(params.Groups, conditions.Options.GroupNames) {
|
||||
return false
|
||||
}
|
||||
if !checkEventConditionPatterns(params.VirtualPath, conditions.Options.FsPaths) {
|
||||
|
@ -1138,7 +1138,7 @@ func checkUserConditionOptions(user *dataprovider.User, conditions *dataprovider
|
|||
if !checkEventConditionPatterns(user.Role, conditions.RoleNames) {
|
||||
return false
|
||||
}
|
||||
if !checkEventGroupConditionPatters(user.Groups, conditions.GroupNames) {
|
||||
if !checkEventGroupConditionPatterns(user.Groups, conditions.GroupNames) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
@ -1149,24 +1149,29 @@ func checkEventConditionPatterns(name string, patterns []dataprovider.ConditionP
|
|||
if len(patterns) == 0 {
|
||||
return true
|
||||
}
|
||||
matches := false
|
||||
for _, p := range patterns {
|
||||
if checkEventConditionPattern(p, name) {
|
||||
// assume, that multiple InverseMatches are set
|
||||
if p.InverseMatch {
|
||||
if checkEventConditionPattern(p, name) {
|
||||
matches = true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} else if checkEventConditionPattern(p, name) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
return matches
|
||||
}
|
||||
|
||||
func checkEventGroupConditionPatters(groups []sdk.GroupMapping, patterns []dataprovider.ConditionPattern) bool {
|
||||
func checkEventGroupConditionPatterns(groups []sdk.GroupMapping, patterns []dataprovider.ConditionPattern) bool {
|
||||
if len(patterns) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, group := range groups {
|
||||
for _, p := range patterns {
|
||||
if checkEventConditionPattern(p, group.Name) {
|
||||
return true
|
||||
}
|
||||
if checkEventConditionPatterns(group.Name, patterns) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -355,6 +355,55 @@ func TestDoubleStarMatching(t *testing.T) {
|
|||
assert.False(t, res)
|
||||
res = checkEventConditionPattern(c, "/mydir/sub/dir/a.txt")
|
||||
assert.True(t, res)
|
||||
|
||||
c.InverseMatch = true
|
||||
assert.True(t, checkEventConditionPattern(c, "/mydir"))
|
||||
assert.True(t, checkEventConditionPattern(c, "/mydirname/f.txt"))
|
||||
assert.True(t, checkEventConditionPattern(c, "/mydir/sub"))
|
||||
assert.True(t, checkEventConditionPattern(c, "/mydir/sub/dir"))
|
||||
assert.False(t, checkEventConditionPattern(c, "/mydir/sub/dir/a.txt"))
|
||||
}
|
||||
|
||||
func TestMutlipleDoubleStarMatching(t *testing.T) {
|
||||
patterns := []dataprovider.ConditionPattern{
|
||||
{
|
||||
Pattern: "/**/*.txt",
|
||||
InverseMatch: false,
|
||||
},
|
||||
{
|
||||
Pattern: "/**/*.tmp",
|
||||
InverseMatch: false,
|
||||
},
|
||||
}
|
||||
assert.False(t, checkEventConditionPatterns("/mydir", patterns))
|
||||
assert.True(t, checkEventConditionPatterns("/mydir/test.tmp", patterns))
|
||||
assert.True(t, checkEventConditionPatterns("/mydir/test.txt", patterns))
|
||||
assert.False(t, checkEventConditionPatterns("/mydir/test.csv", patterns))
|
||||
assert.False(t, checkEventConditionPatterns("/mydir/sub", patterns))
|
||||
assert.True(t, checkEventConditionPatterns("/mydir/sub/test.tmp", patterns))
|
||||
assert.True(t, checkEventConditionPatterns("/mydir/sub/test.txt", patterns))
|
||||
assert.False(t, checkEventConditionPatterns("/mydir/sub/test.csv", patterns))
|
||||
}
|
||||
|
||||
func TestMultipleDoubleStarMatchingInverse(t *testing.T) {
|
||||
patterns := []dataprovider.ConditionPattern{
|
||||
{
|
||||
Pattern: "/**/*.txt",
|
||||
InverseMatch: true,
|
||||
},
|
||||
{
|
||||
Pattern: "/**/*.tmp",
|
||||
InverseMatch: true,
|
||||
},
|
||||
}
|
||||
assert.True(t, checkEventConditionPatterns("/mydir", patterns))
|
||||
assert.False(t, checkEventConditionPatterns("/mydir/test.tmp", patterns))
|
||||
assert.False(t, checkEventConditionPatterns("/mydir/test.txt", patterns))
|
||||
assert.True(t, checkEventConditionPatterns("/mydir/test.csv", patterns))
|
||||
assert.True(t, checkEventConditionPatterns("/mydir/sub", patterns))
|
||||
assert.False(t, checkEventConditionPatterns("/mydir/sub/test.tmp", patterns))
|
||||
assert.False(t, checkEventConditionPatterns("/mydir/sub/test.txt", patterns))
|
||||
assert.True(t, checkEventConditionPatterns("/mydir/sub/test.csv", patterns))
|
||||
}
|
||||
|
||||
func TestEventManager(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue