fixup! apiserver correctly validates encoding/decodable versions

This commit is contained in:
Haowei Cai 2020-11-09 15:23:13 -08:00
parent 1c2d446648
commit 23f77ce7c6
2 changed files with 12 additions and 12 deletions

View File

@ -160,7 +160,7 @@ func validateStorageVersionCondition(conditions []apiserverinternal.StorageVersi
} }
const dns1035LabelFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?" const dns1035LabelFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?"
const dns1035LabelErrMsg string = "a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character" const dns1035LabelErrMsg string = "a DNS-1035 label, which must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character"
// isValidAPIVersion tests whether the value passed is a valid apiVersion. A // isValidAPIVersion tests whether the value passed is a valid apiVersion. A
// valid apiVersion contains a version string that matches DNS_LABEL format, // valid apiVersion contains a version string that matches DNS_LABEL format,
@ -178,19 +178,19 @@ func isValidAPIVersion(apiVersion string) []string {
var group string var group string
group, version = parts[0], parts[1] group, version = parts[0], parts[1]
if len(group) == 0 { if len(group) == 0 {
errs = append(errs, "group part "+utilvalidation.EmptyError()) errs = append(errs, "group part: "+utilvalidation.EmptyError())
} else if msgs := utilvalidation.IsDNS1123Subdomain(group); len(msgs) != 0 { } else if msgs := utilvalidation.IsDNS1123Subdomain(group); len(msgs) != 0 {
errs = append(errs, prefixEach(msgs, "group part ")...) errs = append(errs, prefixEach(msgs, "group part: ")...)
} }
default: default:
return append(errs, "an apiVersion "+utilvalidation.RegexError(dns1035LabelErrMsg, dns1035LabelFmt, "my-name", "abc-123")+ return append(errs, "an apiVersion is "+utilvalidation.RegexError(dns1035LabelErrMsg, dns1035LabelFmt, "my-name", "abc-123")+
" with an optional DNS subdomain prefix and '/' (e.g. 'example.com/MyVersion')") " with an optional DNS subdomain prefix and '/' (e.g. 'example.com/MyVersion')")
} }
if len(version) == 0 { if len(version) == 0 {
errs = append(errs, "version part "+utilvalidation.EmptyError()) errs = append(errs, "version part: "+utilvalidation.EmptyError())
} else if msgs := utilvalidation.IsDNS1035Label(version); len(msgs) != 0 { } else if msgs := utilvalidation.IsDNS1035Label(version); len(msgs) != 0 {
errs = append(errs, prefixEach(msgs, "version part ")...) errs = append(errs, prefixEach(msgs, "version part: ")...)
} }
return errs return errs
} }

View File

@ -75,7 +75,7 @@ func TestValidateServerStorageVersion(t *testing.T) {
EncodingVersion: "mygroup.com/v2", EncodingVersion: "mygroup.com/v2",
DecodableVersions: []string{"mygroup.com/v2", "/v3"}, DecodableVersions: []string{"mygroup.com/v2", "/v3"},
}, },
expectedErr: `[].decodableVersions[1]: Invalid value: "/v3": group part must be non-empty`, expectedErr: `[].decodableVersions[1]: Invalid value: "/v3": group part: must be non-empty`,
}, },
{ {
ssv: apiserverinternal.ServerStorageVersion{ ssv: apiserverinternal.ServerStorageVersion{
@ -83,7 +83,7 @@ func TestValidateServerStorageVersion(t *testing.T) {
EncodingVersion: "mygroup.com/v2", EncodingVersion: "mygroup.com/v2",
DecodableVersions: []string{"mygroup.com/v2", "mygroup.com/"}, DecodableVersions: []string{"mygroup.com/v2", "mygroup.com/"},
}, },
expectedErr: `[].decodableVersions[1]: Invalid value: "mygroup.com/": version part must be non-empty`, expectedErr: `[].decodableVersions[1]: Invalid value: "mygroup.com/": version part: must be non-empty`,
}, },
{ {
ssv: apiserverinternal.ServerStorageVersion{ ssv: apiserverinternal.ServerStorageVersion{
@ -91,7 +91,7 @@ func TestValidateServerStorageVersion(t *testing.T) {
EncodingVersion: "/v3", EncodingVersion: "/v3",
DecodableVersions: []string{"mygroup.com/v2", "/v3"}, DecodableVersions: []string{"mygroup.com/v2", "/v3"},
}, },
expectedErr: `[].encodingVersion: Invalid value: "/v3": group part must be non-empty`, expectedErr: `[].encodingVersion: Invalid value: "/v3": group part: must be non-empty`,
}, },
{ {
ssv: apiserverinternal.ServerStorageVersion{ ssv: apiserverinternal.ServerStorageVersion{
@ -99,7 +99,7 @@ func TestValidateServerStorageVersion(t *testing.T) {
EncodingVersion: "v1", EncodingVersion: "v1",
DecodableVersions: []string{"v1", "mygroup_com/v2"}, DecodableVersions: []string{"v1", "mygroup_com/v2"},
}, },
expectedErr: `[].decodableVersions[1]: Invalid value: "mygroup_com/v2": group part a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')`, expectedErr: `[].decodableVersions[1]: Invalid value: "mygroup_com/v2": group part: a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')`,
}, },
{ {
ssv: apiserverinternal.ServerStorageVersion{ ssv: apiserverinternal.ServerStorageVersion{
@ -107,7 +107,7 @@ func TestValidateServerStorageVersion(t *testing.T) {
EncodingVersion: "v1", EncodingVersion: "v1",
DecodableVersions: []string{"v1", "mygroup.com/v2_"}, DecodableVersions: []string{"v1", "mygroup.com/v2_"},
}, },
expectedErr: `[].decodableVersions[1]: Invalid value: "mygroup.com/v2_": version part a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')`, expectedErr: `[].decodableVersions[1]: Invalid value: "mygroup.com/v2_": version part: a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')`,
}, },
{ {
ssv: apiserverinternal.ServerStorageVersion{ ssv: apiserverinternal.ServerStorageVersion{
@ -115,7 +115,7 @@ func TestValidateServerStorageVersion(t *testing.T) {
EncodingVersion: "v1", EncodingVersion: "v1",
DecodableVersions: []string{"v1", "mygroup.com/v2/myresource"}, DecodableVersions: []string{"v1", "mygroup.com/v2/myresource"},
}, },
expectedErr: `[].decodableVersions[1]: Invalid value: "mygroup.com/v2/myresource": an apiVersion a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?') with an optional DNS subdomain prefix and '/' (e.g. 'example.com/MyVersion')`, expectedErr: `[].decodableVersions[1]: Invalid value: "mygroup.com/v2/myresource": an apiVersion is a DNS-1035 label, which must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?') with an optional DNS subdomain prefix and '/' (e.g. 'example.com/MyVersion')`,
}, },
} }