1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

fix: cache regex globally ()

Signed-off-by: Furkan <furkan.turkal@trendyol.com>
Co-authored-by: Emin <emin.aktas@trendyol.com>
This commit is contained in:
Furkan Türkal 2023-06-16 13:07:15 +03:00 committed by GitHub
parent 9811417022
commit a21c5fb347
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 8 deletions
pkg
config
validation/policy

View file

@ -87,13 +87,14 @@ func newFilter(kind, namespace, name string) filter {
}
}
var submatchallRegex = regexp.MustCompile(`\[([^\[\]]*)\]`)
// ParseKinds parses the kinds if a single string contains comma separated kinds
// {"1,2,3","4","5"} => {"1","2","3","4","5"}
func parseKinds(in string) []filter {
resources := []filter{}
var resource filter
re := regexp.MustCompile(`\[([^\[\]]*)\]`)
submatchall := re.FindAllString(in, -1)
submatchall := submatchallRegex.FindAllString(in, -1)
for _, element := range submatchall {
element = strings.Trim(element, "[")
element = strings.Trim(element, "]")

View file

@ -47,17 +47,14 @@ var (
errOperationForbidden = errors.New("variables are forbidden in the path of a JSONPatch")
)
var allowedJsonPatch = regexp.MustCompile("^/")
// validateJSONPatchPathForForwardSlash checks for forward slash
func validateJSONPatchPathForForwardSlash(patch string) error {
// Replace all variables in PatchesJSON6902, all variable checks should have happened already.
// This prevents further checks from failing unexpectedly.
patch = variables.ReplaceAllVars(patch, func(s string) string { return "kyvernojsonpatchvariable" })
re, err := regexp.Compile("^/")
if err != nil {
return err
}
jsonPatch, err := yaml.ToJSON([]byte(patch))
if err != nil {
return err
@ -74,7 +71,7 @@ func validateJSONPatchPathForForwardSlash(patch string) error {
return err
}
val := re.MatchString(path)
val := allowedJsonPatch.MatchString(path)
if !val {
return fmt.Errorf("%s", path)