4
0

revoke_test.go 6.6 KB

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