root_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package authority
  2. import (
  3. "crypto/x509"
  4. "crypto/x509/pkix"
  5. "errors"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. "github.com/smallstep/assert"
  10. "github.com/smallstep/certificates/api/render"
  11. "github.com/stretchr/testify/require"
  12. "go.step.sm/crypto/keyutil"
  13. "go.step.sm/crypto/minica"
  14. "go.step.sm/crypto/pemutil"
  15. )
  16. func TestRoot(t *testing.T) {
  17. a := testAuthority(t)
  18. a.certificates.Store("invaliddata", "a string") // invalid cert for testing
  19. tests := map[string]struct {
  20. sum string
  21. err error
  22. code int
  23. }{
  24. "not-found": {"foo", errors.New("certificate with fingerprint foo was not found"), http.StatusNotFound},
  25. "invalid-stored-certificate": {"invaliddata", errors.New("stored value is not a *x509.Certificate"), http.StatusInternalServerError},
  26. "success": {"189f573cfa159251e445530847ef80b1b62a3a380ee670dcb49e33ed34da0616", nil, http.StatusOK},
  27. }
  28. for name, tc := range tests {
  29. t.Run(name, func(t *testing.T) {
  30. crt, err := a.Root(tc.sum)
  31. if err != nil {
  32. if assert.NotNil(t, tc.err) {
  33. var sc render.StatusCodedError
  34. assert.Fatal(t, errors.As(err, &sc), "error does not implement StatusCodedError interface")
  35. assert.Equals(t, sc.StatusCode(), tc.code)
  36. assert.HasPrefix(t, err.Error(), tc.err.Error())
  37. }
  38. } else {
  39. if assert.Nil(t, tc.err) {
  40. assert.Equals(t, crt, a.rootX509Certs[0])
  41. }
  42. }
  43. })
  44. }
  45. }
  46. func TestAuthority_GetRootCertificate(t *testing.T) {
  47. cert, err := pemutil.ReadCertificate("testdata/certs/root_ca.crt")
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. tests := []struct {
  52. name string
  53. want *x509.Certificate
  54. }{
  55. {"ok", cert},
  56. }
  57. for _, tt := range tests {
  58. t.Run(tt.name, func(t *testing.T) {
  59. a := testAuthority(t)
  60. if got := a.GetRootCertificate(); !reflect.DeepEqual(got, tt.want) {
  61. t.Errorf("Authority.GetRootCertificate() = %v, want %v", got, tt.want)
  62. }
  63. })
  64. }
  65. }
  66. func TestAuthority_GetRootCertificates(t *testing.T) {
  67. cert, err := pemutil.ReadCertificate("testdata/certs/root_ca.crt")
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. tests := []struct {
  72. name string
  73. want []*x509.Certificate
  74. }{
  75. {"ok", []*x509.Certificate{cert}},
  76. }
  77. for _, tt := range tests {
  78. t.Run(tt.name, func(t *testing.T) {
  79. a := testAuthority(t)
  80. if got := a.GetRootCertificates(); !reflect.DeepEqual(got, tt.want) {
  81. t.Errorf("Authority.GetRootCertificates() = %v, want %v", got, tt.want)
  82. }
  83. })
  84. }
  85. }
  86. func TestAuthority_GetRoots(t *testing.T) {
  87. cert, err := pemutil.ReadCertificate("testdata/certs/root_ca.crt")
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. tests := []struct {
  92. name string
  93. want []*x509.Certificate
  94. wantErr bool
  95. }{
  96. {"ok", []*x509.Certificate{cert}, false},
  97. }
  98. for _, tt := range tests {
  99. a := testAuthority(t)
  100. t.Run(tt.name, func(t *testing.T) {
  101. got, err := a.GetRoots()
  102. if (err != nil) != tt.wantErr {
  103. t.Errorf("Authority.GetRoots() error = %v, wantErr %v", err, tt.wantErr)
  104. return
  105. }
  106. if !reflect.DeepEqual(got, tt.want) {
  107. t.Errorf("Authority.GetRoots() = %v, want %v", got, tt.want)
  108. }
  109. })
  110. }
  111. }
  112. func TestAuthority_GetFederation(t *testing.T) {
  113. cert, err := pemutil.ReadCertificate("testdata/certs/root_ca.crt")
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. tests := []struct {
  118. name string
  119. wantFederation []*x509.Certificate
  120. wantErr bool
  121. fn func(a *Authority)
  122. }{
  123. {"ok", []*x509.Certificate{cert}, false, nil},
  124. {"fail", nil, true, func(a *Authority) {
  125. a.certificates.Store("foo", "bar")
  126. }},
  127. }
  128. for _, tt := range tests {
  129. t.Run(tt.name, func(t *testing.T) {
  130. a := testAuthority(t)
  131. if tt.fn != nil {
  132. tt.fn(a)
  133. }
  134. gotFederation, err := a.GetFederation()
  135. if (err != nil) != tt.wantErr {
  136. t.Errorf("Authority.GetFederation() error = %v, wantErr %v", err, tt.wantErr)
  137. return
  138. }
  139. if !reflect.DeepEqual(gotFederation, tt.wantFederation) {
  140. t.Errorf("Authority.GetFederation() = %v, want %v", gotFederation, tt.wantFederation)
  141. }
  142. })
  143. }
  144. }
  145. func TestAuthority_GetIntermediateCertificate(t *testing.T) {
  146. ca, err := minica.New(minica.WithRootTemplate(`{
  147. "subject": {{ toJson .Subject }},
  148. "issuer": {{ toJson .Subject }},
  149. "keyUsage": ["certSign", "crlSign"],
  150. "basicConstraints": {
  151. "isCA": true,
  152. "maxPathLen": -1
  153. }
  154. }`), minica.WithIntermediateTemplate(`{
  155. "subject": {{ toJson .Subject }},
  156. "keyUsage": ["certSign", "crlSign"],
  157. "basicConstraints": {
  158. "isCA": true,
  159. "maxPathLen": 1
  160. }
  161. }`))
  162. require.NoError(t, err)
  163. signer, err := keyutil.GenerateDefaultSigner()
  164. require.NoError(t, err)
  165. cert, err := ca.Sign(&x509.Certificate{
  166. Subject: pkix.Name{CommonName: "MiniCA Intermediate CA 0"},
  167. PublicKey: signer.Public(),
  168. BasicConstraintsValid: true,
  169. IsCA: true,
  170. MaxPathLen: 0,
  171. })
  172. require.NoError(t, err)
  173. type fields struct {
  174. intermediateX509Certs []*x509.Certificate
  175. }
  176. tests := []struct {
  177. name string
  178. fields fields
  179. want *x509.Certificate
  180. wantSlice []*x509.Certificate
  181. }{
  182. {"ok one", fields{[]*x509.Certificate{ca.Intermediate}}, ca.Intermediate, []*x509.Certificate{ca.Intermediate}},
  183. {"ok multiple", fields{[]*x509.Certificate{cert, ca.Intermediate}}, cert, []*x509.Certificate{cert, ca.Intermediate}},
  184. {"ok empty", fields{[]*x509.Certificate{}}, nil, []*x509.Certificate{}},
  185. {"ok nil", fields{nil}, nil, nil},
  186. }
  187. for _, tt := range tests {
  188. t.Run(tt.name, func(t *testing.T) {
  189. a := &Authority{
  190. intermediateX509Certs: tt.fields.intermediateX509Certs,
  191. }
  192. if got := a.GetIntermediateCertificate(); !reflect.DeepEqual(got, tt.want) {
  193. t.Errorf("Authority.GetIntermediateCertificate() = %v, want %v", got, tt.want)
  194. }
  195. if got := a.GetIntermediateCertificates(); !reflect.DeepEqual(got, tt.wantSlice) {
  196. t.Errorf("Authority.GetIntermediateCertificates() = %v, want %v", got, tt.wantSlice)
  197. }
  198. })
  199. }
  200. }