annotation validation: validate regex in common name annotation (#10657)
* fix common name validation * add tests
This commit is contained in:
parent
5a72a42235
commit
05d68a1512
3 changed files with 71 additions and 3 deletions
|
|
@ -307,3 +307,59 @@ func TestCheckAnnotationRisk(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommonNameAnnotationValidator(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
annotation string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "correct example",
|
||||
annotation: `CN=(my\.common\.name)`,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "no CN= prefix",
|
||||
annotation: `(my\.common\.name)`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "invalid prefix",
|
||||
annotation: `CN(my\.common\.name)`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "invalid regex",
|
||||
annotation: `CN=(my\.common\.name]`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "wildcard regex",
|
||||
annotation: `CN=(my\..*\.name)`,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "somewhat complex regex",
|
||||
annotation: "CN=(my\\.app\\.dev|.*\\.bbb\\.aaaa\\.tld)",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "another somewhat complex regex",
|
||||
annotation: `CN=(my-app.*\.c\.defg\.net|other.app.com)`,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "nested parenthesis regex",
|
||||
annotation: `CN=(api-one\.(asdf)?qwer\.webpage\.organization\.org)`,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := CommonNameAnnotationValidator(tt.annotation); (err != nil) != tt.wantErr {
|
||||
t.Errorf("CommonNameAnnotationValidator() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue