Skip to content

Commit

Permalink
fix: default ref regex should not include spaces and quotes when matc…
Browse files Browse the repository at this point in the history
…hing (#160)

* fix: default ref regex should not include spaces and quotes when matching

Signed-off-by: Amit Lin <amitlin.dev@gmail.com>

* test: added test cases

Signed-off-by: Amit Lin <amitlin.dev@gmail.com>

---------

Signed-off-by: Amit Lin <amitlin.dev@gmail.com>
  • Loading branch information
amitlin authored Aug 4, 2023
1 parent aeb6ced commit 2cea569
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/expansion/expand_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ExpandRegexMatch struct {
Only []string
}

var DefaultRefRegexp = regexp.MustCompile(`((secret)?ref)\+([^\+:]*://[^\+\n]+)\+?`)
var DefaultRefRegexp = regexp.MustCompile(`((secret)?ref)\+([^\+:]*:\/\/[^\+\n "]+)\+?`)

func (e *ExpandRegexMatch) InString(s string) (string, error) {
var sb strings.Builder
Expand Down
21 changes: 21 additions & 0 deletions pkg/expansion/expand_match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ func TestExpandRegexpMatchInString(t *testing.T) {
input: "ref+vault://srv/foo/bar\n",
expected: "vault-srv-/foo/bar\n",
},
{
name: "it should not match closing quotes when using query params",
regex: DefaultRefRegexp,
only: []string{"ref", "secretref"},
input: "\"ref+awsssm://srv/foo/bar?mode=singleparam\"",
expected: "\"awsssm-srv-/foo/bar\"",
},
{
name: "it should match greedily upto a space when using query params",
regex: DefaultRefRegexp,
only: []string{"ref", "secretref"},
input: "ref+awsssm://srv/foo/bar?mode=singleparam some-string",
expected: "awsssm-srv-/foo/bar some-string",
},
{
name: "it should handle multiple refs when using query params",
regex: DefaultRefRegexp,
only: []string{"ref", "secretref"},
input: "ref+awsssm://srv/foo/bar?mode=singleparam some-string ref+awsssm://srv/foo/bar?mode=singleparam",
expected: "awsssm-srv-/foo/bar some-string awsssm-srv-/foo/bar",
},
}

for i := range testcases {
Expand Down

0 comments on commit 2cea569

Please sign in to comment.