mirror of https://github.com/k3s-io/k3s
Add dns-952-identifier validation to service ids.
parent
07bef429b1
commit
20a8f03d62
|
@ -166,7 +166,7 @@ func runAtomicPutTest(c *client.Client) {
|
|||
var svc api.Service
|
||||
err := c.Post().Path("services").Body(
|
||||
api.Service{
|
||||
JSONBase: api.JSONBase{ID: "atomicService", APIVersion: "v1beta1"},
|
||||
JSONBase: api.JSONBase{ID: "atomicservice", APIVersion: "v1beta1"},
|
||||
Port: 12345,
|
||||
Labels: map[string]string{
|
||||
"name": "atomicService",
|
||||
|
|
|
@ -302,6 +302,8 @@ func ValidateService(service *Service) []error {
|
|||
allErrs := errorList{}
|
||||
if service.ID == "" {
|
||||
allErrs.Append(makeInvalidError("Service.ID", service.ID))
|
||||
} else if !util.IsDNS952Label(service.ID) {
|
||||
allErrs.Append(makeInvalidError("Service.ID", service.ID))
|
||||
}
|
||||
if labels.Set(service.Selector).AsSelector().Empty() {
|
||||
allErrs.Append(makeInvalidError("Service.Selector", service.Selector))
|
||||
|
|
|
@ -58,3 +58,13 @@ func IsCIdentifier(value string) bool {
|
|||
func IsValidPortNum(port int) bool {
|
||||
return 0 < port && port < 65536
|
||||
}
|
||||
|
||||
const dns952IdentifierFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?"
|
||||
|
||||
var dns952Regexp = regexp.MustCompile("^" + dns952IdentifierFmt + "$")
|
||||
|
||||
const dns952MaxLength = 24
|
||||
|
||||
func IsDNS952Label(value string) bool {
|
||||
return len(value) <= dns952MaxLength && dns952Regexp.MatchString(value)
|
||||
}
|
||||
|
|
|
@ -100,6 +100,7 @@ func TestIsCIdentifier(t *testing.T) {
|
|||
"-", "a-", "-a", "1-", "-1", "1_", "1_2",
|
||||
".", "a.", ".a", "a.b", "1.", ".1", "1.2",
|
||||
" ", "a ", " a", "a b", "1 ", " 1", "1 2",
|
||||
"#a#",
|
||||
}
|
||||
for _, val := range badValues {
|
||||
if IsCIdentifier(val) {
|
||||
|
@ -123,3 +124,26 @@ func TestIsValidPortNum(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsDNS952(t *testing.T) {
|
||||
goodValues := []string{
|
||||
"a", "ab", "abc", "a1", "a-b", "a-1", "a-1-2-b", "abc-123",
|
||||
}
|
||||
for _, val := range goodValues {
|
||||
if !IsDNS952Label(val) {
|
||||
t.Errorf("expected true for '%s'", val)
|
||||
}
|
||||
}
|
||||
|
||||
badValues := []string{
|
||||
"", "1", "123", "1a",
|
||||
"-", "a-", "-a", "1-", "-1", "1-2",
|
||||
" ", "a ", " a", "a b", "1 ", " 1", "1 2",
|
||||
"A", "AB", "AbC", "A1", "A-B", "A-1", "A-1-2-B",
|
||||
}
|
||||
for _, val := range badValues {
|
||||
if IsDNS952Label(val) {
|
||||
t.Errorf("expected false for '%s'", val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue