Skip to content

Commit 35b2784

Browse files
authored
Merge pull request kubernetes#76920 from sempr/master
MOD: support wildcard DNS for apiserver certSANs
2 parents 5ccda18 + 81e896e commit 35b2784

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

cmd/kubeadm/app/apis/kubeadm/validation/validation.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,10 @@ func ValidateCertSANs(altnames []string, fldPath *field.Path) field.ErrorList {
308308
allErrs := field.ErrorList{}
309309
for _, altname := range altnames {
310310
if errs := validation.IsDNS1123Subdomain(altname); len(errs) != 0 {
311-
if net.ParseIP(altname) == nil {
312-
allErrs = append(allErrs, field.Invalid(fldPath, altname, fmt.Sprintf("altname is not a valid IP address or DNS label: %s", strings.Join(errs, "; "))))
311+
if errs2 := validation.IsWildcardDNS1123Subdomain(altname); len(errs2) != 0 {
312+
if net.ParseIP(altname) == nil {
313+
allErrs = append(allErrs, field.Invalid(fldPath, altname, fmt.Sprintf("altname is not a valid IP address, DNS label or a DNS label with subdomain wildcards: %s; %s", strings.Join(errs, "; "), strings.Join(errs2, "; "))))
314+
}
313315
}
314316
}
315317
}

cmd/kubeadm/app/apis/kubeadm/validation/validation_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ func TestValidateCertSANs(t *testing.T) {
144144
{[]string{"my-hostname2", "my.other.subdomain", "10.0.0.10"}, true}, // supported
145145
{[]string{"my-hostname", "my.subdomain", "2001:db8::4"}, true}, // supported
146146
{[]string{"my-hostname2", "my.other.subdomain", "2001:db8::10"}, true}, // supported
147+
{[]string{"*.my-hostname2", "*.my.other.subdomain"}, true}, // supported Wildcard DNS label
148+
{[]string{"**.my-hostname2", "my.other.subdomain"}, false}, // not a Wildcard DNS label
149+
{[]string{"*.*.my-hostname2", "my.other.subdomain"}, false}, // not a Wildcard DNS label
150+
{[]string{"a.*.my-hostname2", "my.other.subdomain"}, false}, // not a Wildcard DNS label
151+
{[]string{"*", "my.other.subdomain", "2001:db8::10"}, false}, // not a Wildcard DNS label
147152
}
148153
for _, rt := range tests {
149154
actual := ValidateCertSANs(rt.sans, nil)

cmd/kubeadm/app/util/pkiutil/pki_helpers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,13 +446,16 @@ func getAltNames(cfg *kubeadmapi.InitConfiguration, certName string) (*certutil.
446446
// altNames is passed in with a pointer, and the struct is modified
447447
// valid IP address strings are parsed and added to altNames.IPs as net.IP's
448448
// RFC-1123 compliant DNS strings are added to altNames.DNSNames as strings
449+
// RFC-1123 compliant wildcard DNS strings are added to altNames.DNSNames as strings
449450
// certNames is used to print user facing warnings and should be the name of the cert the altNames will be used for
450451
func appendSANsToAltNames(altNames *certutil.AltNames, SANs []string, certName string) {
451452
for _, altname := range SANs {
452453
if ip := net.ParseIP(altname); ip != nil {
453454
altNames.IPs = append(altNames.IPs, ip)
454455
} else if len(validation.IsDNS1123Subdomain(altname)) == 0 {
455456
altNames.DNSNames = append(altNames.DNSNames, altname)
457+
} else if len(validation.IsWildcardDNS1123Subdomain(altname)) == 0 {
458+
altNames.DNSNames = append(altNames.DNSNames, altname)
456459
} else {
457460
fmt.Printf(
458461
"[certificates] WARNING: '%s' was not added to the '%s' SAN, because it is not a valid IP or RFC-1123 compliant DNS entry\n",

cmd/kubeadm/app/util/pkiutil/pki_helpers_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,3 +704,31 @@ func TestGetEtcdPeerAltNames(t *testing.T) {
704704
})
705705
}
706706
}
707+
708+
func TestAppendSANsToAltNames(t *testing.T) {
709+
var tests = []struct {
710+
sans []string
711+
expected int
712+
}{
713+
{[]string{}, 0},
714+
{[]string{"abc"}, 1},
715+
{[]string{"*.abc"}, 1},
716+
{[]string{"**.abc"}, 0},
717+
{[]string{"a.*.bc"}, 0},
718+
{[]string{"a.*.bc", "abc.def"}, 1},
719+
{[]string{"a*.bc", "abc.def"}, 1},
720+
}
721+
for _, rt := range tests {
722+
altNames := certutil.AltNames{}
723+
appendSANsToAltNames(&altNames, rt.sans, "foo")
724+
actual := len(altNames.DNSNames)
725+
if actual != rt.expected {
726+
t.Errorf(
727+
"failed AppendSANsToAltNames Numbers:\n\texpected: %d\n\t actual: %d",
728+
rt.expected,
729+
actual,
730+
)
731+
}
732+
}
733+
734+
}

0 commit comments

Comments
 (0)