authority_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 authority
  14. import (
  15. "crypto/ecdsa"
  16. "crypto/elliptic"
  17. "crypto/rand"
  18. "crypto/x509"
  19. "crypto/x509/pkix"
  20. "math/big"
  21. "net/url"
  22. "testing"
  23. "time"
  24. "github.com/google/go-cmp/cmp"
  25. "github.com/google/go-cmp/cmp/cmpopts"
  26. capi "k8s.io/api/certificates/v1beta1"
  27. "k8s.io/apimachinery/pkg/util/diff"
  28. )
  29. func TestCertificateAuthority(t *testing.T) {
  30. caKey, err := ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. now := time.Now()
  35. tmpl := &x509.Certificate{
  36. SerialNumber: big.NewInt(42),
  37. Subject: pkix.Name{
  38. CommonName: "test-ca",
  39. },
  40. NotBefore: now.Add(-24 * time.Hour),
  41. NotAfter: now.Add(24 * time.Hour),
  42. KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
  43. BasicConstraintsValid: true,
  44. IsCA: true,
  45. }
  46. der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, caKey.Public(), caKey)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. caCert, err := x509.ParseCertificate(der)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. uri, err := url.Parse("help://me@what:8080/where/when?why=true")
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. tests := []struct {
  59. name string
  60. cr x509.CertificateRequest
  61. backdate time.Duration
  62. policy SigningPolicy
  63. want x509.Certificate
  64. wantErr bool
  65. }{
  66. {
  67. name: "ca info",
  68. policy: PermissiveSigningPolicy{TTL: time.Hour},
  69. want: x509.Certificate{
  70. Issuer: caCert.Subject,
  71. AuthorityKeyId: caCert.SubjectKeyId,
  72. NotBefore: now,
  73. NotAfter: now.Add(1 * time.Hour),
  74. BasicConstraintsValid: true,
  75. },
  76. },
  77. {
  78. name: "key usage",
  79. policy: PermissiveSigningPolicy{TTL: time.Hour, Usages: []capi.KeyUsage{"signing"}},
  80. want: x509.Certificate{
  81. NotBefore: now,
  82. NotAfter: now.Add(1 * time.Hour),
  83. BasicConstraintsValid: true,
  84. KeyUsage: x509.KeyUsageDigitalSignature,
  85. },
  86. },
  87. {
  88. name: "ext key usage",
  89. policy: PermissiveSigningPolicy{TTL: time.Hour, Usages: []capi.KeyUsage{"client auth"}},
  90. want: x509.Certificate{
  91. NotBefore: now,
  92. NotAfter: now.Add(1 * time.Hour),
  93. BasicConstraintsValid: true,
  94. ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  95. },
  96. },
  97. {
  98. name: "backdate",
  99. policy: PermissiveSigningPolicy{TTL: time.Hour},
  100. backdate: 5 * time.Minute,
  101. want: x509.Certificate{
  102. NotBefore: now.Add(-5 * time.Minute),
  103. NotAfter: now.Add(55 * time.Minute),
  104. BasicConstraintsValid: true,
  105. },
  106. },
  107. {
  108. name: "truncate expiration",
  109. policy: PermissiveSigningPolicy{TTL: 48 * time.Hour},
  110. want: x509.Certificate{
  111. NotBefore: now,
  112. NotAfter: now.Add(24 * time.Hour),
  113. BasicConstraintsValid: true,
  114. },
  115. },
  116. {
  117. name: "uri sans",
  118. policy: PermissiveSigningPolicy{TTL: time.Hour},
  119. cr: x509.CertificateRequest{
  120. URIs: []*url.URL{uri},
  121. },
  122. want: x509.Certificate{
  123. URIs: []*url.URL{uri},
  124. NotBefore: now,
  125. NotAfter: now.Add(1 * time.Hour),
  126. BasicConstraintsValid: true,
  127. },
  128. },
  129. }
  130. crKey, err := ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. for _, test := range tests {
  135. t.Run(test.name, func(t *testing.T) {
  136. ca := &CertificateAuthority{
  137. Certificate: caCert,
  138. PrivateKey: caKey,
  139. Now: func() time.Time {
  140. return now
  141. },
  142. Backdate: test.backdate,
  143. }
  144. csr, err := x509.CreateCertificateRequest(rand.Reader, &test.cr, crKey)
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. certDER, err := ca.Sign(csr, test.policy)
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. if test.wantErr {
  153. if err == nil {
  154. t.Fatal("expected error")
  155. }
  156. return
  157. }
  158. cert, err := x509.ParseCertificate(certDER)
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. opts := cmp.Options{
  163. cmpopts.IgnoreFields(x509.Certificate{},
  164. "SignatureAlgorithm",
  165. "PublicKeyAlgorithm",
  166. "Version",
  167. "MaxPathLen",
  168. ),
  169. diff.IgnoreUnset(),
  170. cmp.Transformer("RoundTime", func(x time.Time) time.Time {
  171. return x.Truncate(time.Second)
  172. }),
  173. cmp.Comparer(func(x, y *url.URL) bool {
  174. return ((x == nil) && (y == nil)) || x.String() == y.String()
  175. }),
  176. }
  177. if !cmp.Equal(*cert, test.want, opts) {
  178. t.Errorf("unexpected diff: %v", cmp.Diff(*cert, test.want, opts))
  179. }
  180. })
  181. }
  182. }