readwriter_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Copyright 2019 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 renewal
  14. import (
  15. "crypto"
  16. "crypto/x509"
  17. "net"
  18. "os"
  19. "path/filepath"
  20. "testing"
  21. "k8s.io/client-go/tools/clientcmd"
  22. certutil "k8s.io/client-go/util/cert"
  23. "k8s.io/client-go/util/keyutil"
  24. kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  25. kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
  26. pkiutil "k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
  27. testutil "k8s.io/kubernetes/cmd/kubeadm/test"
  28. )
  29. func TestPKICertificateReadWriter(t *testing.T) {
  30. // creates a tmp folder
  31. dir := testutil.SetupTempDir(t)
  32. defer os.RemoveAll(dir)
  33. // creates a certificate
  34. cert := writeTestCertificate(t, dir, "test", testCACert, testCAKey)
  35. // Creates a pkiCertificateReadWriter
  36. pkiReadWriter := newPKICertificateReadWriter(dir, "test")
  37. // Reads the certificate
  38. readCert, err := pkiReadWriter.Read()
  39. if err != nil {
  40. t.Fatalf("couldn't read certificate: %v", err)
  41. }
  42. // Check if the certificate read from disk is equal to the original one
  43. if !cert.Equal(readCert) {
  44. t.Errorf("read cert does not match with expected cert")
  45. }
  46. // Create a new cert
  47. newCert, newkey, err := pkiutil.NewCertAndKey(testCACert, testCAKey, testCertCfg)
  48. if err != nil {
  49. t.Fatalf("couldn't generate certificate: %v", err)
  50. }
  51. // Writes the new certificate
  52. err = pkiReadWriter.Write(newCert, newkey)
  53. if err != nil {
  54. t.Fatalf("couldn't write new certificate: %v", err)
  55. }
  56. // Reads back the new certificate
  57. readCert, err = pkiReadWriter.Read()
  58. if err != nil {
  59. t.Fatalf("couldn't read new certificate: %v", err)
  60. }
  61. // Check if the new certificate read from disk is equal to the original one
  62. if !newCert.Equal(readCert) {
  63. t.Error("read cert does not match with expected new cert")
  64. }
  65. }
  66. func TestKubeconfigReadWriter(t *testing.T) {
  67. // creates tmp folders
  68. dirKubernetes := testutil.SetupTempDir(t)
  69. defer os.RemoveAll(dirKubernetes)
  70. dirPKI := testutil.SetupTempDir(t)
  71. defer os.RemoveAll(dirPKI)
  72. // write the CA cert and key to the temporary PKI dir
  73. caName := kubeadmconstants.CACertAndKeyBaseName
  74. if err := pkiutil.WriteCertAndKey(
  75. dirPKI,
  76. caName,
  77. testCACert,
  78. testCAKey); err != nil {
  79. t.Fatalf("couldn't write out certificate %s to %s", caName, dirPKI)
  80. }
  81. // creates a certificate and then embeds it into a kubeconfig file
  82. cert := writeTestKubeconfig(t, dirKubernetes, "test", testCACert, testCAKey)
  83. // Creates a KubeconfigReadWriter
  84. kubeconfigReadWriter := newKubeconfigReadWriter(dirKubernetes, "test", dirPKI, caName)
  85. // Reads the certificate embedded in a kubeconfig
  86. readCert, err := kubeconfigReadWriter.Read()
  87. if err != nil {
  88. t.Fatalf("couldn't read embedded certificate: %v", err)
  89. }
  90. // Check if the certificate read from disk is equal to the original one
  91. if !cert.Equal(readCert) {
  92. t.Errorf("read cert does not match with expected cert")
  93. }
  94. // Create a new cert
  95. newCert, newkey, err := pkiutil.NewCertAndKey(testCACert, testCAKey, testCertCfg)
  96. if err != nil {
  97. t.Fatalf("couldn't generate certificate: %v", err)
  98. }
  99. // Writes the new certificate embedded in a kubeconfig
  100. err = kubeconfigReadWriter.Write(newCert, newkey)
  101. if err != nil {
  102. t.Fatalf("couldn't write new embedded certificate: %v", err)
  103. }
  104. // Reads back the new certificate embedded in a kubeconfig writer
  105. readCert, err = kubeconfigReadWriter.Read()
  106. if err != nil {
  107. t.Fatalf("couldn't read new embedded certificate: %v", err)
  108. }
  109. // Check if the new certificate read from disk is equal to the original one
  110. if !newCert.Equal(readCert) {
  111. t.Errorf("read cert does not match with expected new cert")
  112. }
  113. }
  114. // writeTestCertificate is a utility for creating a test certificate
  115. func writeTestCertificate(t *testing.T, dir, name string, caCert *x509.Certificate, caKey crypto.Signer) *x509.Certificate {
  116. cert, key, err := pkiutil.NewCertAndKey(caCert, caKey, testCertCfg)
  117. if err != nil {
  118. t.Fatalf("couldn't generate certificate: %v", err)
  119. }
  120. if err := pkiutil.WriteCertAndKey(dir, name, cert, key); err != nil {
  121. t.Fatalf("couldn't write out certificate %s to %s", name, dir)
  122. }
  123. return cert
  124. }
  125. // writeTestKubeconfig is a utility for creating a test kubeconfig with an embedded certificate
  126. func writeTestKubeconfig(t *testing.T, dir, name string, caCert *x509.Certificate, caKey crypto.Signer) *x509.Certificate {
  127. cfg := &pkiutil.CertConfig{
  128. Config: certutil.Config{
  129. CommonName: "test-common-name",
  130. Organization: []string{"sig-cluster-lifecycle"},
  131. Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  132. AltNames: certutil.AltNames{
  133. IPs: []net.IP{net.ParseIP("10.100.0.1")},
  134. DNSNames: []string{"test-domain.space"},
  135. },
  136. },
  137. }
  138. cert, key, err := pkiutil.NewCertAndKey(caCert, caKey, cfg)
  139. if err != nil {
  140. t.Fatalf("couldn't generate certificate: %v", err)
  141. }
  142. encodedClientKey, err := keyutil.MarshalPrivateKeyToPEM(key)
  143. if err != nil {
  144. t.Fatalf("failed to marshal private key to PEM: %v", err)
  145. }
  146. certificateAuthorityData := pkiutil.EncodeCertPEM(caCert)
  147. config := kubeconfigutil.CreateWithCerts(
  148. "https://localhost:1234",
  149. "kubernetes-test",
  150. "user-test",
  151. certificateAuthorityData,
  152. encodedClientKey,
  153. pkiutil.EncodeCertPEM(cert),
  154. )
  155. if err := clientcmd.WriteToFile(*config, filepath.Join(dir, name)); err != nil {
  156. t.Fatalf("couldn't write out certificate")
  157. }
  158. return cert
  159. }