Skip to content

Commit

Permalink
fix ipallowlist parser not handling validation type errors
Browse files Browse the repository at this point in the history
Signed-off-by: joey <[email protected]>
  • Loading branch information
chengjoey committed Sep 13, 2024
1 parent 1c2aecb commit e8c72c7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
55 changes: 55 additions & 0 deletions internal/ingress/annotations/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package annotations

import (
"fmt"
"testing"

apiv1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -366,3 +367,57 @@ func TestCustomResponseHeaders(t *testing.T) {
}
}
}

func TestIPAllowList(t *testing.T) {
mockObj := mockCfg{}
mockObj.MockConfigMaps = map[string]*apiv1.ConfigMap{}

ec := NewAnnotationExtractor(mockObj)
ing := buildIngress()
annotationKeys := []string{"allowlist-source-range", "whitelist-source-range"}
for _, tc := range []struct {
name string
net string
expectErr bool
errOut string
}{
{
name: "test parse a valid net",
net: "10.0.0.0/24",
},
{
name: "test parse a invalid net",
net: "ww",
errOut: "annotation nginx.ingress.kubernetes.io/%s contains invalid value",
expectErr: true,
},
{
name: "test parse multiple valid cidr",
net: "2.2.2.2/32,1.1.1.1/32,3.3.3.0/24",
expectErr: false,
},
{
name: "test parse multiple invalid cidr(missing comma)",
net: "1.1.1.1 2.2.2.2",
expectErr: true,
errOut: "annotation nginx.ingress.kubernetes.io/%s contains invalid value",
},
} {
t.Run(tc.name, func(t *testing.T) {
for _, annotationKey := range annotationKeys {
ing.SetAnnotations(map[string]string{
parser.GetAnnotationWithPrefix(annotationKey): tc.net,
})
i, err := ec.Extract(ing)
if (err != nil) != tc.expectErr {
t.Errorf("expected error: %t got error: %t err value: %s. %+v", tc.expectErr, err != nil, err, i)
}
if tc.expectErr && err != nil {
if err.Error() != fmt.Sprintf(tc.errOut, annotationKey) {
t.Errorf("expected error %s but got %s", tc.errOut, err)
}
}
}
})
}
}
3 changes: 3 additions & 0 deletions internal/ingress/annotations/ipallowlist/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func (a ipallowlist) Parse(ing *networking.Ingress) (interface{}, error) {
if err == ing_errors.ErrMissingAnnotations {
return &SourceRange{CIDR: defaultAllowlistSourceRange}, nil
}
if ing_errors.IsValidationError(err) {
return &SourceRange{CIDR: defaultAllowlistSourceRange}, err
}

return &SourceRange{CIDR: defaultAllowlistSourceRange}, ing_errors.LocationDeniedError{
Reason: err,
Expand Down

0 comments on commit e8c72c7

Please sign in to comment.