Skip to content

Commit 883f318

Browse files
committed
Fix duplicate altnames in cert
1 parent a138be8 commit 883f318

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

cmd/kubeadm/app/util/pkiutil/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ go_library(
2525
"//cmd/kubeadm/app/constants:go_default_library",
2626
"//cmd/kubeadm/app/features:go_default_library",
2727
"//cmd/kubeadm/app/util:go_default_library",
28+
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
2829
"//staging/src/k8s.io/apimachinery/pkg/util/validation:go_default_library",
2930
"//staging/src/k8s.io/client-go/util/cert:go_default_library",
3031
"//staging/src/k8s.io/client-go/util/keyutil:go_default_library",

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636

3737
"github.com/pkg/errors"
3838

39+
"k8s.io/apimachinery/pkg/util/sets"
3940
"k8s.io/apimachinery/pkg/util/validation"
4041
certutil "k8s.io/client-go/util/cert"
4142
"k8s.io/client-go/util/keyutil"
@@ -566,6 +567,8 @@ func NewSignedCert(cfg *CertConfig, key crypto.Signer, caCert *x509.Certificate,
566567
return nil, errors.New("must specify at least one ExtKeyUsage")
567568
}
568569

570+
RemoveDuplicateAltNames(&cfg.AltNames)
571+
569572
certTmpl := x509.Certificate{
570573
Subject: pkix.Name{
571574
CommonName: cfg.CommonName,
@@ -585,3 +588,24 @@ func NewSignedCert(cfg *CertConfig, key crypto.Signer, caCert *x509.Certificate,
585588
}
586589
return x509.ParseCertificate(certDERBytes)
587590
}
591+
592+
// RemoveDuplicateAltNames removes duplicate items in altNames.
593+
func RemoveDuplicateAltNames(altNames *certutil.AltNames) {
594+
if altNames == nil {
595+
return
596+
}
597+
598+
if altNames.DNSNames != nil {
599+
altNames.DNSNames = sets.NewString(altNames.DNSNames...).List()
600+
}
601+
602+
ipsKeys := make(map[string]struct{})
603+
var ips []net.IP
604+
for _, one := range altNames.IPs {
605+
if _, ok := ipsKeys[one.String()]; !ok {
606+
ipsKeys[one.String()] = struct{}{}
607+
ips = append(ips, one)
608+
}
609+
}
610+
altNames.IPs = ips
611+
}

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"io/ioutil"
2727
"net"
2828
"os"
29+
"reflect"
2930
"testing"
3031

3132
certutil "k8s.io/client-go/util/cert"
@@ -755,3 +756,51 @@ func TestAppendSANsToAltNames(t *testing.T) {
755756
}
756757

757758
}
759+
760+
func TestRemoveDuplicateAltNames(t *testing.T) {
761+
tests := []struct {
762+
args *certutil.AltNames
763+
want *certutil.AltNames
764+
}{
765+
{
766+
&certutil.AltNames{},
767+
&certutil.AltNames{},
768+
},
769+
{
770+
&certutil.AltNames{
771+
DNSNames: []string{"a", "a"},
772+
IPs: []net.IP{{127, 0, 0, 1}},
773+
},
774+
&certutil.AltNames{
775+
DNSNames: []string{"a"},
776+
IPs: []net.IP{{127, 0, 0, 1}},
777+
},
778+
},
779+
{
780+
&certutil.AltNames{
781+
DNSNames: []string{"a"},
782+
IPs: []net.IP{{127, 0, 0, 1}, {127, 0, 0, 1}},
783+
},
784+
&certutil.AltNames{
785+
DNSNames: []string{"a"},
786+
IPs: []net.IP{{127, 0, 0, 1}},
787+
},
788+
},
789+
{
790+
&certutil.AltNames{
791+
DNSNames: []string{"a", "a"},
792+
IPs: []net.IP{{127, 0, 0, 1}, {127, 0, 0, 1}},
793+
},
794+
&certutil.AltNames{
795+
DNSNames: []string{"a"},
796+
IPs: []net.IP{{127, 0, 0, 1}},
797+
},
798+
},
799+
}
800+
for _, tt := range tests {
801+
RemoveDuplicateAltNames(tt.args)
802+
if !reflect.DeepEqual(tt.args, tt.want) {
803+
t.Errorf("Wanted %v, got %v", tt.want, tt.args)
804+
}
805+
}
806+
}

0 commit comments

Comments
 (0)