readwriter_test.go 5.2 KB

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