acme.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package api
  2. import (
  3. "fmt"
  4. "net/http"
  5. "go.step.sm/linkedca"
  6. "github.com/smallstep/certificates/api/render"
  7. "github.com/smallstep/certificates/authority/admin"
  8. )
  9. // CreateExternalAccountKeyRequest is the type for POST /admin/acme/eab requests
  10. type CreateExternalAccountKeyRequest struct {
  11. Reference string `json:"reference"`
  12. }
  13. // Validate validates a new ACME EAB Key request body.
  14. func (r *CreateExternalAccountKeyRequest) Validate() error {
  15. if len(r.Reference) > 256 { // an arbitrary, but sensible (IMO), limit
  16. return fmt.Errorf("reference length %d exceeds the maximum (256)", len(r.Reference))
  17. }
  18. return nil
  19. }
  20. // GetExternalAccountKeysResponse is the type for GET /admin/acme/eab responses
  21. type GetExternalAccountKeysResponse struct {
  22. EAKs []*linkedca.EABKey `json:"eaks"`
  23. NextCursor string `json:"nextCursor"`
  24. }
  25. // requireEABEnabled is a middleware that ensures ACME EAB is enabled
  26. // before serving requests that act on ACME EAB credentials.
  27. func (h *Handler) requireEABEnabled(next http.HandlerFunc) http.HandlerFunc {
  28. return func(w http.ResponseWriter, r *http.Request) {
  29. ctx := r.Context()
  30. prov := linkedca.ProvisionerFromContext(ctx)
  31. details := prov.GetDetails()
  32. if details == nil {
  33. render.Error(w, admin.NewErrorISE("error getting details for provisioner '%s'", prov.GetName()))
  34. return
  35. }
  36. acmeProvisioner := details.GetACME()
  37. if acmeProvisioner == nil {
  38. render.Error(w, admin.NewErrorISE("error getting ACME details for provisioner '%s'", prov.GetName()))
  39. return
  40. }
  41. if !acmeProvisioner.RequireEab {
  42. render.Error(w, admin.NewError(admin.ErrorBadRequestType, "ACME EAB not enabled for provisioner '%s'", prov.GetName()))
  43. return
  44. }
  45. next(w, r)
  46. }
  47. }
  48. type acmeAdminResponderInterface interface {
  49. GetExternalAccountKeys(w http.ResponseWriter, r *http.Request)
  50. CreateExternalAccountKey(w http.ResponseWriter, r *http.Request)
  51. DeleteExternalAccountKey(w http.ResponseWriter, r *http.Request)
  52. }
  53. // ACMEAdminResponder is responsible for writing ACME admin responses
  54. type ACMEAdminResponder struct{}
  55. // NewACMEAdminResponder returns a new ACMEAdminResponder
  56. func NewACMEAdminResponder() *ACMEAdminResponder {
  57. return &ACMEAdminResponder{}
  58. }
  59. // GetExternalAccountKeys writes the response for the EAB keys GET endpoint
  60. func (h *ACMEAdminResponder) GetExternalAccountKeys(w http.ResponseWriter, r *http.Request) {
  61. render.Error(w, admin.NewError(admin.ErrorNotImplementedType, "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm"))
  62. }
  63. // CreateExternalAccountKey writes the response for the EAB key POST endpoint
  64. func (h *ACMEAdminResponder) CreateExternalAccountKey(w http.ResponseWriter, r *http.Request) {
  65. render.Error(w, admin.NewError(admin.ErrorNotImplementedType, "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm"))
  66. }
  67. // DeleteExternalAccountKey writes the response for the EAB key DELETE endpoint
  68. func (h *ACMEAdminResponder) DeleteExternalAccountKey(w http.ResponseWriter, r *http.Request) {
  69. render.Error(w, admin.NewError(admin.ErrorNotImplementedType, "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm"))
  70. }