From 79d5dac9ffe8ca3f4214f6aba6d157dce89cbbab Mon Sep 17 00:00:00 2001 From: Brice Colucci Date: Wed, 10 Mar 2021 15:23:11 -0500 Subject: [PATCH] feat: Support more complicated path_regexp (issues/826) (#829) * feat: Support more complicated path_regexp (https://github.com/mozilla/sops/issues/826) * feat: Support more complicated path_regexp (https://github.com/mozilla/sops/issues/826) * fix review: do not panic and return an error instead if the regexp is not valid. * fix merge mess Co-authored-by: AJ Bahnken <1144310+ajvb@users.noreply.github.com> --- config/config.go | 14 ++++++++------ config/config_test.go | 36 ++++++++++++++++++++++++++++++++++++ go.sum | 2 -- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/config/config.go b/config/config.go index 18a71c3df..e89336ddc 100644 --- a/config/config.go +++ b/config/config.go @@ -10,7 +10,6 @@ import ( "path" "regexp" - "gopkg.in/yaml.v3" "github.com/sirupsen/logrus" "go.mozilla.org/sops/v3" "go.mozilla.org/sops/v3/age" @@ -21,6 +20,7 @@ import ( "go.mozilla.org/sops/v3/logging" "go.mozilla.org/sops/v3/pgp" "go.mozilla.org/sops/v3/publish" + "gopkg.in/yaml.v3" ) var log *logrus.Logger @@ -326,11 +326,13 @@ func parseCreationRuleForFile(conf *configFile, filePath string, kmsEncryptionCo rule = &r break } - if r.PathRegex != "" { - if match, _ := regexp.MatchString(r.PathRegex, filePath); match { - rule = &r - break - } + reg, err := regexp.Compile(r.PathRegex) + if err != nil { + return nil, fmt.Errorf("can not compile regexp: %w", err) + } + if reg.MatchString(filePath) { + rule = &r + break } } diff --git a/config/config_test.go b/config/config_test.go index 183eb7836..ac8aca6f3 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -208,6 +208,24 @@ destination_rules: path_regex: "vault-v1/*" `) +var sampleConfigWithInvalidComplicatedRegexp = []byte(` +creation_rules: + - path_regex: "[ ]\\K(?