123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735 |
- /*
- Copyright 2016 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package pkiutil
- import (
- "crypto"
- "crypto/ecdsa"
- "crypto/elliptic"
- "crypto/rand"
- "crypto/rsa"
- "crypto/x509"
- "io/ioutil"
- "net"
- "os"
- "testing"
- certutil "k8s.io/client-go/util/cert"
- kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
- )
- func TestNewCertificateAuthority(t *testing.T) {
- cert, key, err := NewCertificateAuthority(&certutil.Config{CommonName: "kubernetes"})
- if cert == nil {
- t.Error("failed NewCertificateAuthority, cert == nil")
- } else if !cert.IsCA {
- t.Error("cert is not a valida CA")
- }
- if key == nil {
- t.Error("failed NewCertificateAuthority, key == nil")
- }
- if err != nil {
- t.Errorf("failed NewCertificateAuthority with an error: %+v", err)
- }
- }
- func TestNewCertAndKey(t *testing.T) {
- var tests = []struct {
- name string
- keyGenFunc func() (crypto.Signer, error)
- expected bool
- }{
- {
- name: "RSA key too small",
- keyGenFunc: func() (crypto.Signer, error) {
- return rsa.GenerateKey(rand.Reader, 128)
- },
- expected: false,
- },
- {
- name: "RSA should succeed",
- keyGenFunc: func() (crypto.Signer, error) {
- return rsa.GenerateKey(rand.Reader, 2048)
- },
- expected: true,
- },
- {
- name: "ECDSA should succeed",
- keyGenFunc: func() (crypto.Signer, error) {
- return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
- },
- expected: true,
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t *testing.T) {
- caKey, err := rt.keyGenFunc()
- if err != nil {
- t.Fatalf("Couldn't create Private Key")
- }
- caCert := &x509.Certificate{}
- config := &certutil.Config{
- CommonName: "test",
- Organization: []string{"test"},
- Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
- }
- _, _, actual := NewCertAndKey(caCert, caKey, config)
- if (actual == nil) != rt.expected {
- t.Errorf(
- "failed NewCertAndKey:\n\texpected: %t\n\t actual: %t",
- rt.expected,
- (actual == nil),
- )
- }
- })
- }
- }
- func TestHasServerAuth(t *testing.T) {
- caCert, caKey, _ := NewCertificateAuthority(&certutil.Config{CommonName: "kubernetes"})
- var tests = []struct {
- name string
- config certutil.Config
- expected bool
- }{
- {
- name: "has ServerAuth",
- config: certutil.Config{
- CommonName: "test",
- Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
- },
- expected: true,
- },
- {
- name: "doesn't have ServerAuth",
- config: certutil.Config{
- CommonName: "test",
- Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
- },
- expected: false,
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t *testing.T) {
- cert, _, err := NewCertAndKey(caCert, caKey, &rt.config)
- if err != nil {
- t.Fatalf("Couldn't create cert: %v", err)
- }
- actual := HasServerAuth(cert)
- if actual != rt.expected {
- t.Errorf(
- "failed HasServerAuth:\n\texpected: %t\n\t actual: %t",
- rt.expected,
- actual,
- )
- }
- })
- }
- }
- func TestWriteCertAndKey(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "")
- if err != nil {
- t.Fatalf("Couldn't create tmpdir")
- }
- defer os.RemoveAll(tmpdir)
- caKey, err := rsa.GenerateKey(rand.Reader, 2048)
- if err != nil {
- t.Fatalf("Couldn't create rsa Private Key")
- }
- caCert := &x509.Certificate{}
- actual := WriteCertAndKey(tmpdir, "foo", caCert, caKey)
- if actual != nil {
- t.Errorf(
- "failed WriteCertAndKey with an error: %v",
- actual,
- )
- }
- }
- func TestWriteCert(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "")
- if err != nil {
- t.Fatalf("Couldn't create tmpdir")
- }
- defer os.RemoveAll(tmpdir)
- caCert := &x509.Certificate{}
- actual := WriteCert(tmpdir, "foo", caCert)
- if actual != nil {
- t.Errorf(
- "failed WriteCertAndKey with an error: %v",
- actual,
- )
- }
- }
- func TestWriteKey(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "")
- if err != nil {
- t.Fatalf("Couldn't create tmpdir")
- }
- defer os.RemoveAll(tmpdir)
- caKey, err := rsa.GenerateKey(rand.Reader, 2048)
- if err != nil {
- t.Fatalf("Couldn't create rsa Private Key")
- }
- actual := WriteKey(tmpdir, "foo", caKey)
- if actual != nil {
- t.Errorf(
- "failed WriteCertAndKey with an error: %v",
- actual,
- )
- }
- }
- func TestWritePublicKey(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "")
- if err != nil {
- t.Fatalf("Couldn't create tmpdir")
- }
- defer os.RemoveAll(tmpdir)
- caKey, err := rsa.GenerateKey(rand.Reader, 2048)
- if err != nil {
- t.Fatalf("Couldn't create rsa Private Key")
- }
- actual := WritePublicKey(tmpdir, "foo", &caKey.PublicKey)
- if actual != nil {
- t.Errorf(
- "failed WriteCertAndKey with an error: %v",
- actual,
- )
- }
- }
- func TestCertOrKeyExist(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "")
- if err != nil {
- t.Fatalf("Couldn't create tmpdir")
- }
- defer os.RemoveAll(tmpdir)
- caKey, err := rsa.GenerateKey(rand.Reader, 2048)
- if err != nil {
- t.Fatalf("Couldn't create rsa Private Key")
- }
- caCert := &x509.Certificate{}
- actual := WriteCertAndKey(tmpdir, "foo", caCert, caKey)
- if actual != nil {
- t.Errorf(
- "failed WriteCertAndKey with an error: %v",
- actual,
- )
- }
- var tests = []struct {
- desc string
- path string
- name string
- expected bool
- }{
- {
- desc: "empty path and name",
- path: "",
- name: "",
- expected: false,
- },
- {
- desc: "valid path and name",
- path: tmpdir,
- name: "foo",
- expected: true,
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t *testing.T) {
- actual := CertOrKeyExist(rt.path, rt.name)
- if actual != rt.expected {
- t.Errorf(
- "failed CertOrKeyExist:\n\texpected: %t\n\t actual: %t",
- rt.expected,
- actual,
- )
- }
- })
- }
- }
- func TestTryLoadCertAndKeyFromDisk(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "")
- if err != nil {
- t.Fatalf("Couldn't create tmpdir")
- }
- defer os.RemoveAll(tmpdir)
- caCert, caKey, err := NewCertificateAuthority(&certutil.Config{CommonName: "kubernetes"})
- if err != nil {
- t.Errorf(
- "failed to create cert and key with an error: %v",
- err,
- )
- }
- err = WriteCertAndKey(tmpdir, "foo", caCert, caKey)
- if err != nil {
- t.Errorf(
- "failed to write cert and key with an error: %v",
- err,
- )
- }
- var tests = []struct {
- desc string
- path string
- name string
- expected bool
- }{
- {
- desc: "empty path and name",
- path: "",
- name: "",
- expected: false,
- },
- {
- desc: "valid path and name",
- path: tmpdir,
- name: "foo",
- expected: true,
- },
- }
- for _, rt := range tests {
- t.Run(rt.desc, func(t *testing.T) {
- _, _, actual := TryLoadCertAndKeyFromDisk(rt.path, rt.name)
- if (actual == nil) != rt.expected {
- t.Errorf(
- "failed TryLoadCertAndKeyFromDisk:\n\texpected: %t\n\t actual: %t",
- rt.expected,
- (actual == nil),
- )
- }
- })
- }
- }
- func TestTryLoadCertFromDisk(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "")
- if err != nil {
- t.Fatalf("Couldn't create tmpdir")
- }
- defer os.RemoveAll(tmpdir)
- caCert, _, err := NewCertificateAuthority(&certutil.Config{CommonName: "kubernetes"})
- if err != nil {
- t.Errorf(
- "failed to create cert and key with an error: %v",
- err,
- )
- }
- err = WriteCert(tmpdir, "foo", caCert)
- if err != nil {
- t.Errorf(
- "failed to write cert and key with an error: %v",
- err,
- )
- }
- var tests = []struct {
- desc string
- path string
- name string
- expected bool
- }{
- {
- desc: "empty path and name",
- path: "",
- name: "",
- expected: false,
- },
- {
- desc: "valid path and name",
- path: tmpdir,
- name: "foo",
- expected: true,
- },
- }
- for _, rt := range tests {
- t.Run(rt.desc, func(t *testing.T) {
- _, actual := TryLoadCertFromDisk(rt.path, rt.name)
- if (actual == nil) != rt.expected {
- t.Errorf(
- "failed TryLoadCertAndKeyFromDisk:\n\texpected: %t\n\t actual: %t",
- rt.expected,
- (actual == nil),
- )
- }
- })
- }
- }
- func TestTryLoadKeyFromDisk(t *testing.T) {
- var tests = []struct {
- desc string
- pathSuffix string
- name string
- keyGenFunc func() (crypto.Signer, error)
- expected bool
- }{
- {
- desc: "empty path and name",
- pathSuffix: "somegarbage",
- name: "",
- keyGenFunc: func() (crypto.Signer, error) {
- return rsa.GenerateKey(rand.Reader, 2048)
- },
- expected: false,
- },
- {
- desc: "RSA valid path and name",
- pathSuffix: "",
- name: "foo",
- keyGenFunc: func() (crypto.Signer, error) {
- return rsa.GenerateKey(rand.Reader, 2048)
- },
- expected: true,
- },
- {
- desc: "ECDSA valid path and name",
- pathSuffix: "",
- name: "foo",
- keyGenFunc: func() (crypto.Signer, error) {
- return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
- },
- expected: true,
- },
- }
- for _, rt := range tests {
- t.Run(rt.desc, func(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "")
- if err != nil {
- t.Fatalf("Couldn't create tmpdir")
- }
- defer os.RemoveAll(tmpdir)
- caKey, err := rt.keyGenFunc()
- if err != nil {
- t.Errorf(
- "failed to create key with an error: %v",
- err,
- )
- }
- err = WriteKey(tmpdir, "foo", caKey)
- if err != nil {
- t.Errorf(
- "failed to write key with an error: %v",
- err,
- )
- }
- _, actual := TryLoadKeyFromDisk(tmpdir+rt.pathSuffix, rt.name)
- if (actual == nil) != rt.expected {
- t.Errorf(
- "failed TryLoadCertAndKeyFromDisk:\n\texpected: %t\n\t actual: %t",
- rt.expected,
- (actual == nil),
- )
- }
- })
- }
- }
- func TestPathsForCertAndKey(t *testing.T) {
- crtPath, keyPath := PathsForCertAndKey("/foo", "bar")
- if crtPath != "/foo/bar.crt" {
- t.Errorf("unexpected certificate path: %s", crtPath)
- }
- if keyPath != "/foo/bar.key" {
- t.Errorf("unexpected key path: %s", keyPath)
- }
- }
- func TestPathForCert(t *testing.T) {
- crtPath := pathForCert("/foo", "bar")
- if crtPath != "/foo/bar.crt" {
- t.Errorf("unexpected certificate path: %s", crtPath)
- }
- }
- func TestPathForKey(t *testing.T) {
- keyPath := pathForKey("/foo", "bar")
- if keyPath != "/foo/bar.key" {
- t.Errorf("unexpected certificate path: %s", keyPath)
- }
- }
- func TestPathForPublicKey(t *testing.T) {
- pubPath := pathForPublicKey("/foo", "bar")
- if pubPath != "/foo/bar.pub" {
- t.Errorf("unexpected certificate path: %s", pubPath)
- }
- }
- func TestPathForCSR(t *testing.T) {
- csrPath := pathForCSR("/foo", "bar")
- if csrPath != "/foo/bar.csr" {
- t.Errorf("unexpected certificate path: %s", csrPath)
- }
- }
- func TestGetAPIServerAltNames(t *testing.T) {
- var tests = []struct {
- desc string
- name string
- cfg *kubeadmapi.InitConfiguration
- expectedDNSNames []string
- expectedIPAddresses []string
- }{
- {
- desc: "empty name",
- name: "",
- cfg: &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{AdvertiseAddress: "1.2.3.4"},
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "api.k8s.io:6443",
- Networking: kubeadmapi.Networking{ServiceSubnet: "10.96.0.0/12", DNSDomain: "cluster.local"},
- APIServer: kubeadmapi.APIServer{
- CertSANs: []string{"10.1.245.94", "10.1.245.95", "1.2.3.L", "invalid,commas,in,DNS"},
- },
- },
- NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-hostname"},
- },
- expectedDNSNames: []string{"valid-hostname", "kubernetes", "kubernetes.default", "kubernetes.default.svc", "kubernetes.default.svc.cluster.local", "api.k8s.io"},
- expectedIPAddresses: []string{"10.96.0.1", "1.2.3.4", "10.1.245.94", "10.1.245.95"},
- },
- {
- desc: "ControlPlaneEndpoint IP",
- name: "ControlPlaneEndpoint IP",
- cfg: &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{AdvertiseAddress: "1.2.3.4"},
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "4.5.6.7:6443",
- Networking: kubeadmapi.Networking{ServiceSubnet: "10.96.0.0/12", DNSDomain: "cluster.local"},
- APIServer: kubeadmapi.APIServer{
- CertSANs: []string{"10.1.245.94", "10.1.245.95", "1.2.3.L", "invalid,commas,in,DNS"},
- },
- },
- NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-hostname"},
- },
- expectedDNSNames: []string{"valid-hostname", "kubernetes", "kubernetes.default", "kubernetes.default.svc", "kubernetes.default.svc.cluster.local"},
- expectedIPAddresses: []string{"10.96.0.1", "1.2.3.4", "10.1.245.94", "10.1.245.95", "4.5.6.7"},
- },
- }
- for _, rt := range tests {
- t.Run(rt.desc, func(t *testing.T) {
- altNames, err := GetAPIServerAltNames(rt.cfg)
- if err != nil {
- t.Fatalf("failed calling GetAPIServerAltNames: %s: %v", rt.name, err)
- }
- for _, DNSName := range rt.expectedDNSNames {
- found := false
- for _, val := range altNames.DNSNames {
- if val == DNSName {
- found = true
- break
- }
- }
- if !found {
- t.Errorf("%s: altNames does not contain DNSName %s but %v", rt.name, DNSName, altNames.DNSNames)
- }
- }
- for _, IPAddress := range rt.expectedIPAddresses {
- found := false
- for _, val := range altNames.IPs {
- if val.Equal(net.ParseIP(IPAddress)) {
- found = true
- break
- }
- }
- if !found {
- t.Errorf("%s: altNames does not contain IPAddress %s but %v", rt.name, IPAddress, altNames.IPs)
- }
- }
- })
- }
- }
- func TestGetEtcdAltNames(t *testing.T) {
- proxy := "user-etcd-proxy"
- proxyIP := "10.10.10.100"
- cfg := &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{
- AdvertiseAddress: "1.2.3.4",
- },
- NodeRegistration: kubeadmapi.NodeRegistrationOptions{
- Name: "myNode",
- },
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- Etcd: kubeadmapi.Etcd{
- Local: &kubeadmapi.LocalEtcd{
- ServerCertSANs: []string{
- proxy,
- proxyIP,
- "1.2.3.L",
- "invalid,commas,in,DNS",
- },
- },
- },
- },
- }
- altNames, err := GetEtcdAltNames(cfg)
- if err != nil {
- t.Fatalf("failed calling GetEtcdAltNames: %v", err)
- }
- expectedDNSNames := []string{"myNode", "localhost", proxy}
- for _, DNSName := range expectedDNSNames {
- t.Run(DNSName, func(t *testing.T) {
- found := false
- for _, val := range altNames.DNSNames {
- if val == DNSName {
- found = true
- break
- }
- }
- if !found {
- t.Errorf("altNames does not contain DNSName %s", DNSName)
- }
- })
- }
- expectedIPAddresses := []string{"1.2.3.4", "127.0.0.1", net.IPv6loopback.String(), proxyIP}
- for _, IPAddress := range expectedIPAddresses {
- t.Run(IPAddress, func(t *testing.T) {
- found := false
- for _, val := range altNames.IPs {
- if val.Equal(net.ParseIP(IPAddress)) {
- found = true
- break
- }
- }
- if !found {
- t.Errorf("altNames does not contain IPAddress %s", IPAddress)
- }
- })
- }
- }
- func TestGetEtcdPeerAltNames(t *testing.T) {
- hostname := "valid-hostname"
- proxy := "user-etcd-proxy"
- proxyIP := "10.10.10.100"
- advertiseIP := "1.2.3.4"
- cfg := &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{AdvertiseAddress: advertiseIP},
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- Etcd: kubeadmapi.Etcd{
- Local: &kubeadmapi.LocalEtcd{
- PeerCertSANs: []string{
- proxy,
- proxyIP,
- "1.2.3.L",
- "invalid,commas,in,DNS",
- },
- },
- },
- },
- NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: hostname},
- }
- altNames, err := GetEtcdPeerAltNames(cfg)
- if err != nil {
- t.Fatalf("failed calling GetEtcdPeerAltNames: %v", err)
- }
- expectedDNSNames := []string{hostname, proxy}
- for _, DNSName := range expectedDNSNames {
- t.Run(DNSName, func(t *testing.T) {
- found := false
- for _, val := range altNames.DNSNames {
- if val == DNSName {
- found = true
- break
- }
- }
- if !found {
- t.Errorf("altNames does not contain DNSName %s", DNSName)
- }
- expectedIPAddresses := []string{advertiseIP, proxyIP}
- for _, IPAddress := range expectedIPAddresses {
- found := false
- for _, val := range altNames.IPs {
- if val.Equal(net.ParseIP(IPAddress)) {
- found = true
- break
- }
- }
- if !found {
- t.Errorf("altNames does not contain IPAddress %s", IPAddress)
- }
- }
- })
- }
- }
- func TestAppendSANsToAltNames(t *testing.T) {
- var tests = []struct {
- sans []string
- expected int
- }{
- {[]string{}, 0},
- {[]string{"abc"}, 1},
- {[]string{"*.abc"}, 1},
- {[]string{"**.abc"}, 0},
- {[]string{"a.*.bc"}, 0},
- {[]string{"a.*.bc", "abc.def"}, 1},
- {[]string{"a*.bc", "abc.def"}, 1},
- }
- for _, rt := range tests {
- altNames := certutil.AltNames{}
- appendSANsToAltNames(&altNames, rt.sans, "foo")
- actual := len(altNames.DNSNames)
- if actual != rt.expected {
- t.Errorf(
- "failed AppendSANsToAltNames Numbers:\n\texpected: %d\n\t actual: %d",
- rt.expected,
- actual,
- )
- }
- }
- }
|