4
0

sshRevoke.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package api
  2. import (
  3. "net/http"
  4. "golang.org/x/crypto/ocsp"
  5. "github.com/smallstep/certificates/api/read"
  6. "github.com/smallstep/certificates/api/render"
  7. "github.com/smallstep/certificates/authority"
  8. "github.com/smallstep/certificates/authority/provisioner"
  9. "github.com/smallstep/certificates/errs"
  10. "github.com/smallstep/certificates/logging"
  11. )
  12. // SSHRevokeResponse is the response object that returns the health of the server.
  13. type SSHRevokeResponse struct {
  14. Status string `json:"status"`
  15. }
  16. // SSHRevokeRequest is the request body for a revocation request.
  17. type SSHRevokeRequest struct {
  18. Serial string `json:"serial"`
  19. OTT string `json:"ott"`
  20. ReasonCode int `json:"reasonCode"`
  21. Reason string `json:"reason"`
  22. Passive bool `json:"passive"`
  23. }
  24. // Validate checks the fields of the RevokeRequest and returns nil if they are ok
  25. // or an error if something is wrong.
  26. func (r *SSHRevokeRequest) Validate() (err error) {
  27. if r.Serial == "" {
  28. return errs.BadRequest("missing serial")
  29. }
  30. if r.ReasonCode < ocsp.Unspecified || r.ReasonCode > ocsp.AACompromise {
  31. return errs.BadRequest("reasonCode out of bounds")
  32. }
  33. if !r.Passive {
  34. return errs.NotImplemented("non-passive revocation not implemented")
  35. }
  36. if r.OTT == "" {
  37. return errs.BadRequest("missing ott")
  38. }
  39. return
  40. }
  41. // Revoke supports handful of different methods that revoke a Certificate.
  42. //
  43. // NOTE: currently only Passive revocation is supported.
  44. func SSHRevoke(w http.ResponseWriter, r *http.Request) {
  45. var body SSHRevokeRequest
  46. if err := read.JSON(r.Body, &body); err != nil {
  47. render.Error(w, errs.BadRequestErr(err, "error reading request body"))
  48. return
  49. }
  50. if err := body.Validate(); err != nil {
  51. render.Error(w, err)
  52. return
  53. }
  54. opts := &authority.RevokeOptions{
  55. Serial: body.Serial,
  56. Reason: body.Reason,
  57. ReasonCode: body.ReasonCode,
  58. PassiveOnly: body.Passive,
  59. }
  60. ctx := provisioner.NewContextWithMethod(r.Context(), provisioner.SSHRevokeMethod)
  61. a := mustAuthority(ctx)
  62. // A token indicates that we are using the api via a provisioner token,
  63. // otherwise it is assumed that the certificate is revoking itself over mTLS.
  64. logOtt(w, body.OTT)
  65. if _, err := a.Authorize(ctx, body.OTT); err != nil {
  66. render.Error(w, errs.UnauthorizedErr(err))
  67. return
  68. }
  69. opts.OTT = body.OTT
  70. if err := a.Revoke(ctx, opts); err != nil {
  71. render.Error(w, errs.ForbiddenErr(err, "error revoking ssh certificate"))
  72. return
  73. }
  74. logSSHRevoke(w, opts)
  75. render.JSON(w, &SSHRevokeResponse{Status: "ok"})
  76. }
  77. func logSSHRevoke(w http.ResponseWriter, ri *authority.RevokeOptions) {
  78. if rl, ok := w.(logging.ResponseLogger); ok {
  79. rl.WithFields(map[string]interface{}{
  80. "serial": ri.Serial,
  81. "reasonCode": ri.ReasonCode,
  82. "reason": ri.Reason,
  83. "passiveOnly": ri.PassiveOnly,
  84. "mTLS": ri.MTLS,
  85. "ssh": true,
  86. })
  87. }
  88. }