revoke_test.go 6.2 KB

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