sshRenew.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package api
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/certificates/authority/provisioner"
  7. "github.com/smallstep/certificates/errs"
  8. )
  9. // SSHRenewRequest is the request body of an SSH certificate request.
  10. type SSHRenewRequest struct {
  11. OTT string `json:"ott"`
  12. }
  13. // Validate validates the SSHSignRequest.
  14. func (s *SSHRenewRequest) Validate() error {
  15. switch {
  16. case len(s.OTT) == 0:
  17. return errors.New("missing or empty ott")
  18. default:
  19. return nil
  20. }
  21. }
  22. // SSHRenewResponse is the response object that returns the SSH certificate.
  23. type SSHRenewResponse struct {
  24. Certificate SSHCertificate `json:"crt"`
  25. }
  26. // SSHRenew is an HTTP handler that reads an RenewSSHRequest with a one-time-token
  27. // (ott) from the body and creates a new SSH certificate with the information in
  28. // the request.
  29. func (h *caHandler) SSHRenew(w http.ResponseWriter, r *http.Request) {
  30. var body SSHRenewRequest
  31. if err := ReadJSON(r.Body, &body); err != nil {
  32. WriteError(w, errs.BadRequest(errors.Wrap(err, "error reading request body")))
  33. return
  34. }
  35. logOtt(w, body.OTT)
  36. if err := body.Validate(); err != nil {
  37. WriteError(w, errs.BadRequest(err))
  38. return
  39. }
  40. ctx := provisioner.NewContextWithMethod(context.Background(), provisioner.RenewSSHMethod)
  41. _, err := h.Authority.Authorize(ctx, body.OTT)
  42. if err != nil {
  43. WriteError(w, errs.Unauthorized(err))
  44. return
  45. }
  46. oldCert, err := provisioner.ExtractSSHPOPCert(body.OTT)
  47. if err != nil {
  48. WriteError(w, errs.InternalServerError(err))
  49. }
  50. newCert, err := h.Authority.RenewSSH(oldCert)
  51. if err != nil {
  52. WriteError(w, errs.Forbidden(err))
  53. return
  54. }
  55. JSONStatus(w, &SSHSignResponse{
  56. Certificate: SSHCertificate{newCert},
  57. }, http.StatusCreated)
  58. }