acme.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package api
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/go-chi/chi"
  7. "go.step.sm/linkedca"
  8. "github.com/smallstep/certificates/api/render"
  9. "github.com/smallstep/certificates/authority/admin"
  10. "github.com/smallstep/certificates/authority/provisioner"
  11. )
  12. const (
  13. // provisionerContextKey provisioner key
  14. provisionerContextKey = ContextKey("provisioner")
  15. )
  16. // CreateExternalAccountKeyRequest is the type for POST /admin/acme/eab requests
  17. type CreateExternalAccountKeyRequest struct {
  18. Reference string `json:"reference"`
  19. }
  20. // Validate validates a new ACME EAB Key request body.
  21. func (r *CreateExternalAccountKeyRequest) Validate() error {
  22. if len(r.Reference) > 256 { // an arbitrary, but sensible (IMO), limit
  23. return fmt.Errorf("reference length %d exceeds the maximum (256)", len(r.Reference))
  24. }
  25. return nil
  26. }
  27. // GetExternalAccountKeysResponse is the type for GET /admin/acme/eab responses
  28. type GetExternalAccountKeysResponse struct {
  29. EAKs []*linkedca.EABKey `json:"eaks"`
  30. NextCursor string `json:"nextCursor"`
  31. }
  32. // requireEABEnabled is a middleware that ensures ACME EAB is enabled
  33. // before serving requests that act on ACME EAB credentials.
  34. func (h *Handler) requireEABEnabled(next nextHTTP) nextHTTP {
  35. return func(w http.ResponseWriter, r *http.Request) {
  36. ctx := r.Context()
  37. provName := chi.URLParam(r, "provisionerName")
  38. eabEnabled, prov, err := h.provisionerHasEABEnabled(ctx, provName)
  39. if err != nil {
  40. render.Error(w, err)
  41. return
  42. }
  43. if !eabEnabled {
  44. render.Error(w, admin.NewError(admin.ErrorBadRequestType, "ACME EAB not enabled for provisioner %s", prov.GetName()))
  45. return
  46. }
  47. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  48. next(w, r.WithContext(ctx))
  49. }
  50. }
  51. // provisionerHasEABEnabled determines if the "requireEAB" setting for an ACME
  52. // provisioner is set to true and thus has EAB enabled.
  53. func (h *Handler) provisionerHasEABEnabled(ctx context.Context, provisionerName string) (bool, *linkedca.Provisioner, error) {
  54. var (
  55. p provisioner.Interface
  56. err error
  57. )
  58. if p, err = h.auth.LoadProvisionerByName(provisionerName); err != nil {
  59. return false, nil, admin.WrapErrorISE(err, "error loading provisioner %s", provisionerName)
  60. }
  61. prov, err := h.adminDB.GetProvisioner(ctx, p.GetID())
  62. if err != nil {
  63. return false, nil, admin.WrapErrorISE(err, "error getting provisioner with ID: %s", p.GetID())
  64. }
  65. details := prov.GetDetails()
  66. if details == nil {
  67. return false, nil, admin.NewErrorISE("error getting details for provisioner with ID: %s", p.GetID())
  68. }
  69. acmeProvisioner := details.GetACME()
  70. if acmeProvisioner == nil {
  71. return false, nil, admin.NewErrorISE("error getting ACME details for provisioner with ID: %s", p.GetID())
  72. }
  73. return acmeProvisioner.GetRequireEab(), prov, nil
  74. }
  75. type acmeAdminResponderInterface interface {
  76. GetExternalAccountKeys(w http.ResponseWriter, r *http.Request)
  77. CreateExternalAccountKey(w http.ResponseWriter, r *http.Request)
  78. DeleteExternalAccountKey(w http.ResponseWriter, r *http.Request)
  79. }
  80. // ACMEAdminResponder is responsible for writing ACME admin responses
  81. type ACMEAdminResponder struct{}
  82. // NewACMEAdminResponder returns a new ACMEAdminResponder
  83. func NewACMEAdminResponder() *ACMEAdminResponder {
  84. return &ACMEAdminResponder{}
  85. }
  86. // GetExternalAccountKeys writes the response for the EAB keys GET endpoint
  87. func (h *ACMEAdminResponder) GetExternalAccountKeys(w http.ResponseWriter, r *http.Request) {
  88. render.Error(w, admin.NewError(admin.ErrorNotImplementedType, "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm"))
  89. }
  90. // CreateExternalAccountKey writes the response for the EAB key POST endpoint
  91. func (h *ACMEAdminResponder) CreateExternalAccountKey(w http.ResponseWriter, r *http.Request) {
  92. render.Error(w, admin.NewError(admin.ErrorNotImplementedType, "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm"))
  93. }
  94. // DeleteExternalAccountKey writes the response for the EAB key DELETE endpoint
  95. func (h *ACMEAdminResponder) DeleteExternalAccountKey(w http.ResponseWriter, r *http.Request) {
  96. render.Error(w, admin.NewError(admin.ErrorNotImplementedType, "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm"))
  97. }