4
0

revoke_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package api
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/tls"
  6. "crypto/x509"
  7. "encoding/json"
  8. "io/ioutil"
  9. "net/http"
  10. "net/http/httptest"
  11. "strings"
  12. "testing"
  13. "github.com/pkg/errors"
  14. "github.com/smallstep/assert"
  15. "github.com/smallstep/certificates/authority"
  16. "github.com/smallstep/certificates/authority/provisioner"
  17. "github.com/smallstep/certificates/errs"
  18. "github.com/smallstep/certificates/logging"
  19. )
  20. func TestRevokeRequestValidate(t *testing.T) {
  21. type test struct {
  22. rr *RevokeRequest
  23. err *errs.Error
  24. }
  25. tests := map[string]test{
  26. "error/missing serial": {
  27. rr: &RevokeRequest{},
  28. err: &errs.Error{Err: errors.New("missing serial"), Status: http.StatusBadRequest},
  29. },
  30. "error/bad reasonCode": {
  31. rr: &RevokeRequest{
  32. Serial: "sn",
  33. ReasonCode: 15,
  34. Passive: true,
  35. },
  36. err: &errs.Error{Err: errors.New("reasonCode out of bounds"), Status: http.StatusBadRequest},
  37. },
  38. "error/non-passive not implemented": {
  39. rr: &RevokeRequest{
  40. Serial: "sn",
  41. ReasonCode: 8,
  42. Passive: false,
  43. },
  44. err: &errs.Error{Err: errors.New("non-passive revocation not implemented"), Status: http.StatusNotImplemented},
  45. },
  46. "ok": {
  47. rr: &RevokeRequest{
  48. Serial: "sn",
  49. ReasonCode: 9,
  50. Passive: true,
  51. },
  52. },
  53. }
  54. for name, tc := range tests {
  55. t.Run(name, func(t *testing.T) {
  56. if err := tc.rr.Validate(); err != nil {
  57. switch v := err.(type) {
  58. case *errs.Error:
  59. assert.HasPrefix(t, v.Error(), tc.err.Error())
  60. assert.Equals(t, v.StatusCode(), tc.err.Status)
  61. default:
  62. t.Errorf("unexpected error type: %T", v)
  63. }
  64. } else {
  65. assert.Nil(t, tc.err)
  66. }
  67. })
  68. }
  69. }
  70. func Test_caHandler_Revoke(t *testing.T) {
  71. type test struct {
  72. input string
  73. auth Authority
  74. tls *tls.ConnectionState
  75. statusCode int
  76. expected []byte
  77. }
  78. tests := map[string]func(*testing.T) test{
  79. "400/json read error": func(t *testing.T) test {
  80. return test{
  81. input: "{",
  82. statusCode: http.StatusBadRequest,
  83. }
  84. },
  85. "400/invalid request body": func(t *testing.T) test {
  86. input, err := json.Marshal(RevokeRequest{})
  87. assert.FatalError(t, err)
  88. return test{
  89. input: string(input),
  90. statusCode: http.StatusBadRequest,
  91. }
  92. },
  93. "200/ott": func(t *testing.T) test {
  94. input, err := json.Marshal(RevokeRequest{
  95. Serial: "sn",
  96. ReasonCode: 4,
  97. Reason: "foo",
  98. OTT: "valid",
  99. Passive: true,
  100. })
  101. assert.FatalError(t, err)
  102. return test{
  103. input: string(input),
  104. statusCode: http.StatusOK,
  105. auth: &mockAuthority{
  106. authorizeSign: func(ott string) ([]provisioner.SignOption, error) {
  107. return nil, nil
  108. },
  109. revoke: func(ctx context.Context, opts *authority.RevokeOptions) error {
  110. assert.True(t, opts.PassiveOnly)
  111. assert.False(t, opts.MTLS)
  112. assert.Equals(t, opts.Serial, "sn")
  113. assert.Equals(t, opts.ReasonCode, 4)
  114. assert.Equals(t, opts.Reason, "foo")
  115. return nil
  116. },
  117. },
  118. expected: []byte(`{"status":"ok"}`),
  119. }
  120. },
  121. "400/no OTT and no peer certificate": func(t *testing.T) test {
  122. input, err := json.Marshal(RevokeRequest{
  123. Serial: "sn",
  124. ReasonCode: 4,
  125. Passive: true,
  126. })
  127. assert.FatalError(t, err)
  128. return test{
  129. input: string(input),
  130. statusCode: http.StatusBadRequest,
  131. }
  132. },
  133. "200/no ott": func(t *testing.T) test {
  134. cs := &tls.ConnectionState{
  135. PeerCertificates: []*x509.Certificate{parseCertificate(certPEM)},
  136. }
  137. input, err := json.Marshal(RevokeRequest{
  138. Serial: "1404354960355712309",
  139. ReasonCode: 4,
  140. Reason: "foo",
  141. Passive: true,
  142. })
  143. assert.FatalError(t, err)
  144. return test{
  145. input: string(input),
  146. statusCode: http.StatusOK,
  147. tls: cs,
  148. auth: &mockAuthority{
  149. authorizeSign: func(ott string) ([]provisioner.SignOption, error) {
  150. return nil, nil
  151. },
  152. revoke: func(ctx context.Context, ri *authority.RevokeOptions) error {
  153. assert.True(t, ri.PassiveOnly)
  154. assert.True(t, ri.MTLS)
  155. assert.Equals(t, ri.Serial, "1404354960355712309")
  156. assert.Equals(t, ri.ReasonCode, 4)
  157. assert.Equals(t, ri.Reason, "foo")
  158. return nil
  159. },
  160. loadProvisionerByCertificate: func(crt *x509.Certificate) (provisioner.Interface, error) {
  161. return &mockProvisioner{
  162. getID: func() string {
  163. return "mock-provisioner-id"
  164. },
  165. }, err
  166. },
  167. },
  168. expected: []byte(`{"status":"ok"}`),
  169. }
  170. },
  171. "500/ott authority.Revoke": func(t *testing.T) test {
  172. input, err := json.Marshal(RevokeRequest{
  173. Serial: "sn",
  174. ReasonCode: 4,
  175. Reason: "foo",
  176. OTT: "valid",
  177. Passive: true,
  178. })
  179. assert.FatalError(t, err)
  180. return test{
  181. input: string(input),
  182. statusCode: http.StatusInternalServerError,
  183. auth: &mockAuthority{
  184. authorizeSign: func(ott string) ([]provisioner.SignOption, error) {
  185. return nil, nil
  186. },
  187. revoke: func(ctx context.Context, opts *authority.RevokeOptions) error {
  188. return errs.InternalServer("force")
  189. },
  190. },
  191. }
  192. },
  193. "403/ott authority.Revoke": func(t *testing.T) test {
  194. input, err := json.Marshal(RevokeRequest{
  195. Serial: "sn",
  196. ReasonCode: 4,
  197. Reason: "foo",
  198. OTT: "valid",
  199. Passive: true,
  200. })
  201. assert.FatalError(t, err)
  202. return test{
  203. input: string(input),
  204. statusCode: http.StatusForbidden,
  205. auth: &mockAuthority{
  206. authorizeSign: func(ott string) ([]provisioner.SignOption, error) {
  207. return nil, nil
  208. },
  209. revoke: func(ctx context.Context, opts *authority.RevokeOptions) error {
  210. return errors.New("force")
  211. },
  212. },
  213. }
  214. },
  215. }
  216. for name, _tc := range tests {
  217. tc := _tc(t)
  218. t.Run(name, func(t *testing.T) {
  219. h := New(tc.auth).(*caHandler)
  220. req := httptest.NewRequest("POST", "http://example.com/revoke", strings.NewReader(tc.input))
  221. if tc.tls != nil {
  222. req.TLS = tc.tls
  223. }
  224. w := httptest.NewRecorder()
  225. h.Revoke(logging.NewResponseLogger(w), req)
  226. res := w.Result()
  227. assert.Equals(t, tc.statusCode, res.StatusCode)
  228. body, err := ioutil.ReadAll(res.Body)
  229. res.Body.Close()
  230. assert.FatalError(t, err)
  231. if tc.statusCode < http.StatusBadRequest {
  232. if !bytes.Equal(bytes.TrimSpace(body), tc.expected) {
  233. t.Errorf("caHandler.Root Body = %s, wants %s", body, tc.expected)
  234. }
  235. }
  236. })
  237. }
  238. }