signer_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 signer
  14. import (
  15. "crypto/x509"
  16. "crypto/x509/pkix"
  17. "io/ioutil"
  18. "testing"
  19. "time"
  20. "github.com/google/go-cmp/cmp"
  21. capi "k8s.io/api/certificates/v1beta1"
  22. "k8s.io/apimachinery/pkg/util/clock"
  23. "k8s.io/apimachinery/pkg/util/diff"
  24. "k8s.io/client-go/util/cert"
  25. )
  26. func TestSigner(t *testing.T) {
  27. clock := clock.FakeClock{}
  28. s, err := newSigner("./testdata/ca.crt", "./testdata/ca.key", nil, 1*time.Hour)
  29. if err != nil {
  30. t.Fatalf("failed to create signer: %v", err)
  31. }
  32. currCA, err := s.caProvider.currentCA()
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. currCA.Now = clock.Now
  37. currCA.Backdate = 0
  38. s.caProvider.caValue.Store(currCA)
  39. csrb, err := ioutil.ReadFile("./testdata/kubelet.csr")
  40. if err != nil {
  41. t.Fatalf("failed to read CSR: %v", err)
  42. }
  43. csr := &capi.CertificateSigningRequest{
  44. Spec: capi.CertificateSigningRequestSpec{
  45. Request: []byte(csrb),
  46. Usages: []capi.KeyUsage{
  47. capi.UsageSigning,
  48. capi.UsageKeyEncipherment,
  49. capi.UsageServerAuth,
  50. capi.UsageClientAuth,
  51. },
  52. },
  53. }
  54. csr, err = s.sign(csr)
  55. if err != nil {
  56. t.Fatalf("failed to sign CSR: %v", err)
  57. }
  58. certData := csr.Status.Certificate
  59. if len(certData) == 0 {
  60. t.Fatalf("expected a certificate after signing")
  61. }
  62. certs, err := cert.ParseCertsPEM(certData)
  63. if err != nil {
  64. t.Fatalf("failed to parse certificate: %v", err)
  65. }
  66. if len(certs) != 1 {
  67. t.Fatalf("expected one certificate")
  68. }
  69. want := x509.Certificate{
  70. Version: 3,
  71. Subject: pkix.Name{
  72. CommonName: "system:node:k-a-node-s36b",
  73. Organization: []string{"system:nodes"},
  74. },
  75. KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
  76. ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
  77. BasicConstraintsValid: true,
  78. NotAfter: clock.Now().Add(1 * time.Hour),
  79. PublicKeyAlgorithm: x509.ECDSA,
  80. SignatureAlgorithm: x509.SHA256WithRSA,
  81. MaxPathLen: -1,
  82. }
  83. if !cmp.Equal(*certs[0], want, diff.IgnoreUnset()) {
  84. t.Errorf("unexpected diff: %v", cmp.Diff(certs[0], want, diff.IgnoreUnset()))
  85. }
  86. }