Merge pull request #3492 from aledbf/fix-units

Fix data size validations
This commit is contained in:
Kubernetes Prow Robot 2018-12-02 09:01:12 -08:00 committed by GitHub
commit ccd7b890fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 56 deletions

View file

@ -26,7 +26,7 @@ import (
"net/url" "net/url"
"os" "os"
"os/exec" "os/exec"
"strconv" "regexp"
"strings" "strings"
text_template "text/template" text_template "text/template"
"time" "time"
@ -727,6 +727,10 @@ func buildNextUpstream(i, r interface{}) string {
return strings.Join(nextUpstreamCodes, " ") return strings.Join(nextUpstreamCodes, " ")
} }
var sizeUnitRegex = regexp.MustCompile("^[0-9]+[kKmMgG]{0,1}$")
// isValidByteSize validates size units valid in nginx
// http://nginx.org/en/docs/syntax.html
func isValidByteSize(input interface{}) bool { func isValidByteSize(input interface{}) bool {
s, ok := input.(string) s, ok := input.(string)
if !ok { if !ok {
@ -734,32 +738,13 @@ func isValidByteSize(input interface{}) bool {
return false return false
} }
s = strings.TrimSpace(s)
if s == "" { if s == "" {
glog.V(2).Info("empty byte size, hence it will not be set") glog.V(2).Info("empty byte size, hence it will not be set")
return false return false
} }
_, err := strconv.Atoi(s) return sizeUnitRegex.MatchString(s)
if err != nil {
sLowercase := strings.ToLower(s)
check := strings.TrimSuffix(sLowercase, "k")
_, err := strconv.Atoi(check)
if err == nil {
return true
}
mCheck := strings.TrimSuffix(sLowercase, "m")
_, err = strconv.Atoi(mCheck)
if err == nil {
return true
}
glog.Errorf("incorrect byte size format '%v', hence it will not be set.", s)
return false
}
return true
} }
type ingressInformation struct { type ingressInformation struct {

View file

@ -558,41 +558,28 @@ func TestBuildDenyVariable(t *testing.T) {
} }
func TestBuildByteSize(t *testing.T) { func TestBuildByteSize(t *testing.T) {
a := isValidByteSize("1000") cases := []struct {
if !a { input interface{}
t.Errorf("Expected '%v' but returned '%v'", true, a) expected bool
}{
{"1000", true},
{"1000k", true},
{"1m", true},
{"10g", true},
{" 1m ", true},
{"1000kk", false},
{"1000km", false},
{"1mm", false},
{nil, false},
{"", false},
{" ", false},
} }
b := isValidByteSize("1000k")
if !b { for _, tc := range cases {
t.Errorf("Expected '%v' but returned '%v'", true, b) val := isValidByteSize(tc.input)
} if tc.expected != val {
c := isValidByteSize("1000m") t.Errorf("Expected '%v' but returned '%v'", tc.expected, val)
if !c { }
t.Errorf("Expected '%v' but returned '%v'", true, c)
}
d := isValidByteSize("1000km")
if d {
t.Errorf("Expected '%v' but returned '%v'", false, d)
}
e := isValidByteSize("1000mk")
if e {
t.Errorf("Expected '%v' but returned '%v'", false, e)
}
f := isValidByteSize("1000kk")
if f {
t.Errorf("Expected '%v' but returned '%v'", false, f)
}
g := isValidByteSize("1000mm")
if g {
t.Errorf("Expected '%v' but returned '%v'", false, g)
}
h := isValidByteSize(nil)
if h {
t.Errorf("Expected '%v' but returned '%v'", false, h)
}
i := isValidByteSize("")
if i {
t.Errorf("Expected '%v' but returned '%v'", false, i)
} }
} }