certlist_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package certs
  14. import (
  15. "crypto"
  16. "crypto/tls"
  17. "crypto/x509"
  18. "io/ioutil"
  19. "os"
  20. "path"
  21. "testing"
  22. certutil "k8s.io/client-go/util/cert"
  23. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  24. "k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
  25. )
  26. func TestCertListOrder(t *testing.T) {
  27. tests := []struct {
  28. certs Certificates
  29. name string
  30. }{
  31. {
  32. name: "Default Certificate List",
  33. certs: GetDefaultCertList(),
  34. },
  35. {
  36. name: "Cert list less etcd",
  37. certs: GetCertsWithoutEtcd(),
  38. },
  39. }
  40. for _, test := range tests {
  41. t.Run(test.name, func(t *testing.T) {
  42. var lastCA *KubeadmCert
  43. for i, cert := range test.certs {
  44. if i > 0 && lastCA == nil {
  45. t.Fatalf("CA not present in list before certificate %q", cert.Name)
  46. }
  47. if cert.CAName == "" {
  48. lastCA = cert
  49. } else {
  50. if cert.CAName != lastCA.Name {
  51. t.Fatalf("expected CA name %q, got %q, for certificate %q", lastCA.Name, cert.CAName, cert.Name)
  52. }
  53. }
  54. }
  55. })
  56. }
  57. }
  58. func TestCAPointersValid(t *testing.T) {
  59. tests := []struct {
  60. certs Certificates
  61. name string
  62. }{
  63. {
  64. name: "Default Certificate List",
  65. certs: GetDefaultCertList(),
  66. },
  67. {
  68. name: "Cert list less etcd",
  69. certs: GetCertsWithoutEtcd(),
  70. },
  71. }
  72. for _, test := range tests {
  73. t.Run(test.name, func(t *testing.T) {
  74. certMap := test.certs.AsMap()
  75. for _, cert := range test.certs {
  76. if cert.CAName != "" && certMap[cert.CAName] == nil {
  77. t.Errorf("Certificate %q references nonexistent CA %q", cert.Name, cert.CAName)
  78. }
  79. }
  80. })
  81. }
  82. }
  83. func TestMakeCertTree(t *testing.T) {
  84. rootCert := &KubeadmCert{
  85. Name: "root",
  86. }
  87. leaf0 := &KubeadmCert{
  88. Name: "leaf0",
  89. CAName: "root",
  90. }
  91. leaf1 := &KubeadmCert{
  92. Name: "leaf1",
  93. CAName: "root",
  94. }
  95. selfSigned := &KubeadmCert{
  96. Name: "self-signed",
  97. }
  98. certMap := CertificateMap{
  99. "root": rootCert,
  100. "leaf0": leaf0,
  101. "leaf1": leaf1,
  102. "self-signed": selfSigned,
  103. }
  104. orphanCertMap := CertificateMap{
  105. "leaf0": leaf0,
  106. }
  107. if _, err := orphanCertMap.CertTree(); err == nil {
  108. t.Error("expected orphan cert map to error, but got nil")
  109. }
  110. certTree, err := certMap.CertTree()
  111. t.Logf("cert tree: %v", certTree)
  112. if err != nil {
  113. t.Errorf("expected no error, but got %v", err)
  114. }
  115. if len(certTree) != 2 {
  116. t.Errorf("Expected tree to have 2 roots, got %d", len(certTree))
  117. }
  118. if len(certTree[rootCert]) != 2 {
  119. t.Errorf("Expected root to have 2 leaves, got %d", len(certTree[rootCert]))
  120. }
  121. if _, ok := certTree[selfSigned]; !ok {
  122. t.Error("Expected selfSigned to be present in tree, but missing")
  123. }
  124. }
  125. func TestCreateCertificateChain(t *testing.T) {
  126. dir, err := ioutil.TempDir("", t.Name())
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. defer os.RemoveAll(dir)
  131. ic := &kubeadmapi.InitConfiguration{
  132. NodeRegistration: kubeadmapi.NodeRegistrationOptions{
  133. Name: "test-node",
  134. },
  135. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  136. CertificatesDir: dir,
  137. },
  138. }
  139. caCfg := Certificates{
  140. {
  141. config: pkiutil.CertConfig{},
  142. Name: "test-ca",
  143. BaseName: "test-ca",
  144. },
  145. {
  146. config: pkiutil.CertConfig{
  147. Config: certutil.Config{
  148. AltNames: certutil.AltNames{
  149. DNSNames: []string{"test-domain.space"},
  150. },
  151. Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  152. },
  153. },
  154. configMutators: []configMutatorsFunc{
  155. setCommonNameToNodeName(),
  156. },
  157. CAName: "test-ca",
  158. Name: "test-daughter",
  159. BaseName: "test-daughter",
  160. },
  161. }
  162. certTree, err := caCfg.AsMap().CertTree()
  163. if err != nil {
  164. t.Fatalf("unexpected error getting tree: %v", err)
  165. }
  166. if certTree.CreateTree(ic); err != nil {
  167. t.Fatal(err)
  168. }
  169. caCert, _ := parseCertAndKey(path.Join(dir, "test-ca"), t)
  170. daughterCert, _ := parseCertAndKey(path.Join(dir, "test-daughter"), t)
  171. pool := x509.NewCertPool()
  172. pool.AddCert(caCert)
  173. _, err = daughterCert.Verify(x509.VerifyOptions{
  174. DNSName: "test-domain.space",
  175. Roots: pool,
  176. KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  177. })
  178. if err != nil {
  179. t.Errorf("couldn't verify daughter cert: %v", err)
  180. }
  181. }
  182. func parseCertAndKey(basePath string, t *testing.T) (*x509.Certificate, crypto.PrivateKey) {
  183. certPair, err := tls.LoadX509KeyPair(basePath+".crt", basePath+".key")
  184. if err != nil {
  185. t.Fatalf("couldn't parse certificate and key: %v", err)
  186. }
  187. parsedCert, err := x509.ParseCertificate(certPair.Certificate[0])
  188. if err != nil {
  189. t.Fatalf("couldn't parse certificate: %v", err)
  190. }
  191. return parsedCert, certPair.PrivateKey
  192. }