-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added exclude-matchers support for template & matchers (#2218)
* Added exclude-matchers support for template & matchers * Fixed panics due to typo * Added support for only template ID + misc cleanup
- Loading branch information
1 parent
9073b75
commit 7875b06
Showing
20 changed files
with
158 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
v2/pkg/protocols/common/utils/excludematchers/excludematchers.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package excludematchers | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// ExcludeMatchers is an instance for excluding matchers with template IDs | ||
type ExcludeMatchers struct { | ||
values map[string]struct{} | ||
templateIDs map[string]struct{} | ||
matcherNames map[string]struct{} | ||
} | ||
|
||
// New returns a new exclude matchers instance | ||
// | ||
// Wildcard and non-wildcard values are supported. | ||
// <template-id>:<matcher-name> is the syntax. Wildcards can be specified | ||
// using * character for either value. | ||
// | ||
// Ex- http-missing-security-headers:* skips all http-missing-security-header templates | ||
func New(values []string) *ExcludeMatchers { | ||
excludeMatchers := &ExcludeMatchers{ | ||
values: make(map[string]struct{}), | ||
templateIDs: make(map[string]struct{}), | ||
matcherNames: make(map[string]struct{}), | ||
} | ||
for _, value := range values { | ||
partValues := strings.SplitN(value, ":", 2) | ||
if len(partValues) < 2 { | ||
// If there is no matcher name, consider it as template ID | ||
if _, ok := excludeMatchers.templateIDs[value]; !ok { | ||
excludeMatchers.templateIDs[value] = struct{}{} | ||
} | ||
continue | ||
} | ||
templateID, matcherName := partValues[0], partValues[1] | ||
|
||
// Handle wildcards | ||
if templateID == "*" { | ||
if _, ok := excludeMatchers.matcherNames[matcherName]; !ok { | ||
excludeMatchers.matcherNames[matcherName] = struct{}{} | ||
} | ||
} else if matcherName == "*" { | ||
if _, ok := excludeMatchers.templateIDs[templateID]; !ok { | ||
excludeMatchers.templateIDs[templateID] = struct{}{} | ||
} | ||
} else { | ||
if _, ok := excludeMatchers.values[value]; !ok { | ||
excludeMatchers.values[value] = struct{}{} | ||
} | ||
} | ||
} | ||
return excludeMatchers | ||
} | ||
|
||
// Match returns true if templateID and matcherName matches the blocklist | ||
func (e *ExcludeMatchers) Match(templateID, matcherName string) bool { | ||
if _, ok := e.templateIDs[templateID]; ok { | ||
return true | ||
} | ||
if _, ok := e.matcherNames[matcherName]; ok { | ||
return true | ||
} | ||
matchName := strings.Join([]string{templateID, matcherName}, ":") | ||
_, found := e.values[matchName] | ||
return found | ||
} |
19 changes: 19 additions & 0 deletions
19
v2/pkg/protocols/common/utils/excludematchers/excludematchers_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package excludematchers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestExcludeMatchers(t *testing.T) { | ||
em := New([]string{"test-template:test-matcher", "new-template:*", "*:new-matcher", "only-template-id"}) | ||
|
||
require.True(t, em.Match("test-template", "test-matcher"), "could not get template-matcher value") | ||
require.False(t, em.Match("test-template", "random-matcher"), "could get template-matcher value") | ||
|
||
require.True(t, em.Match("new-template", "random-matcher"), "could not get template-matcher value wildcard") | ||
require.True(t, em.Match("random-template", "new-matcher"), "could not get template-matcher value wildcard") | ||
|
||
require.True(t, em.Match("only-template-id", "test"), "could not get only template id match value") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.