-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Normal `node_labels` matches are positive only - you must provide an exact label or a regexp that matches all valid labels. This is problematic when you want to grant access to "any label value except X". You could put "X" in the `deny` section of a role, but this prevents any other roles from granting access to "X". For example, users may have many dynamically-named environments and one "prod", and they want to only grant access to "prod" via workflow API. This commit adds support for `+regexp.not` function, for example `+regexp.not(prod)`. The specific prefix was chosen because a valid regexp can't start with `+`.
- Loading branch information
Andrew Lytvynov
committed
Aug 26, 2020
1 parent
dc05b40
commit c52a4ea
Showing
3 changed files
with
125 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSliceMatchesRegexp(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
desc string | ||
input string | ||
expressions []string | ||
wantMatch bool | ||
assertErr assert.ErrorAssertionFunc | ||
}{ | ||
{ | ||
desc: "exact match", | ||
input: "foo", | ||
expressions: []string{`foo`}, | ||
wantMatch: true, | ||
assertErr: assert.NoError, | ||
}, | ||
{ | ||
desc: "exact match, multiple expressions", | ||
input: "foo", | ||
expressions: []string{`bar`, `foo`, `baz`}, | ||
wantMatch: true, | ||
assertErr: assert.NoError, | ||
}, | ||
{ | ||
desc: "no match", | ||
input: "foo", | ||
expressions: []string{`bar`}, | ||
wantMatch: false, | ||
assertErr: assert.NoError, | ||
}, | ||
{ | ||
desc: "wildcard match", | ||
input: "foo", | ||
expressions: []string{`f*`}, | ||
wantMatch: true, | ||
assertErr: assert.NoError, | ||
}, | ||
{ | ||
desc: "wildcard no match", | ||
input: "foo", | ||
expressions: []string{`b*`}, | ||
wantMatch: false, | ||
assertErr: assert.NoError, | ||
}, | ||
{ | ||
desc: "regexp match", | ||
input: "foo", | ||
expressions: []string{`^f.*$`}, | ||
wantMatch: true, | ||
assertErr: assert.NoError, | ||
}, | ||
{ | ||
desc: "regexp no match", | ||
input: "foo", | ||
expressions: []string{`^bar$`}, | ||
wantMatch: false, | ||
assertErr: assert.NoError, | ||
}, | ||
{ | ||
desc: "invalid regexp", | ||
input: "foo", | ||
expressions: []string{`^?+$`}, | ||
wantMatch: false, | ||
assertErr: assert.Error, | ||
}, | ||
{ | ||
desc: "negated regexp match", | ||
input: "foo", | ||
expressions: []string{`+regexp.not(bar)`}, | ||
wantMatch: true, | ||
assertErr: assert.NoError, | ||
}, | ||
{ | ||
desc: "negated regexp no match", | ||
input: "bar", | ||
expressions: []string{`+regexp.not(bar)`}, | ||
wantMatch: false, | ||
assertErr: assert.NoError, | ||
}, | ||
{ | ||
desc: "incomplete negated regexp no match", | ||
input: "foo", | ||
expressions: []string{`+regexp.not(bar`}, | ||
wantMatch: false, | ||
assertErr: assert.NoError, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.desc, func(t *testing.T) { | ||
match, err := SliceMatchesRegex(tt.input, tt.expressions) | ||
tt.assertErr(t, err) | ||
assert.Equal(t, match, tt.wantMatch) | ||
}) | ||
} | ||
} |