Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort whitelist list to avoid random orders #64

Merged
merged 1 commit into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions core/pkg/ingress/annotations/ipwhitelist/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package ipwhitelist

import (
"errors"
"sort"
"strings"

"k8s.io/kubernetes/pkg/apis/extensions"
Expand Down Expand Up @@ -47,8 +48,7 @@ type SourceRange struct {
// Multiple ranges can specified using commas as separator
// e.g. `18.0.0.0/8,56.0.0.0/8`
func ParseAnnotations(cfg defaults.Backend, ing *extensions.Ingress) (*SourceRange, error) {
cidrs := []string{}

sort.Strings(cfg.WhitelistSourceRange)
if ing.GetAnnotations() == nil {
return &SourceRange{CIDR: cfg.WhitelistSourceRange}, parser.ErrMissingAnnotations
}
Expand All @@ -64,9 +64,12 @@ func ParseAnnotations(cfg defaults.Backend, ing *extensions.Ingress) (*SourceRan
return &SourceRange{CIDR: cfg.WhitelistSourceRange}, ErrInvalidCIDR
}

cidrs := []string{}
for k := range ipnets {
cidrs = append(cidrs, k)
}

sort.Strings(cidrs)

return &SourceRange{cidrs}, nil
}
43 changes: 31 additions & 12 deletions core/pkg/ingress/annotations/ipwhitelist/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func buildIngress() *extensions.Ingress {
}

func TestParseAnnotations(t *testing.T) {
// TODO: convert test cases to tables
ing := buildIngress()

testNet := "10.0.0.0/24"
Expand All @@ -86,24 +87,42 @@ func TestParseAnnotations(t *testing.T) {
}

data[whitelist] = "www"
ing.SetAnnotations(data)
_, err = ParseAnnotations(defaults.Backend{}, ing)
if err == nil {
t.Errorf("Expected error parsing an invalid cidr")
}

delete(data, "whitelist")
delete(data, whitelist)
ing.SetAnnotations(data)
//sr, _ = ParseAnnotations(defaults.Backend{}, ing)
// TODO: fix test
/*
if !reflect.DeepEqual(sr.CIDR, []string{}) {
t.Errorf("Expected empty CIDR but %v returned", sr.CIDR)
}
sr, err = ParseAnnotations(defaults.Backend{}, ing)
if err == nil {
t.Errorf("Expected error parsing an invalid cidr")
}
if !strsEquals(sr.CIDR, []string{}) {
t.Errorf("Expected empty CIDR but %v returned", sr.CIDR)
}

sr, _ = ParseAnnotations(defaults.Upstream{}, &extensions.Ingress{})
if !reflect.DeepEqual(sr.CIDR, []string{}) {
t.Errorf("Expected empty CIDR but %v returned", sr.CIDR)
sr, _ = ParseAnnotations(defaults.Backend{}, &extensions.Ingress{})
if !strsEquals(sr.CIDR, []string{}) {
t.Errorf("Expected empty CIDR but %v returned", sr.CIDR)
}

data[whitelist] = "2.2.2.2/32,1.1.1.1/32,3.3.3.0/24"
sr, _ = ParseAnnotations(defaults.Backend{}, ing)
ecidr := []string{"1.1.1.1/32", "2.2.2.2/32", "3.3.3.0/24"}
if !strsEquals(sr.CIDR, ecidr) {
t.Errorf("Expected %v CIDR but %v returned", ecidr, sr.CIDR)
}
}

func strsEquals(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
*/
}
return true
}