acme.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package api
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/go-chi/chi"
  6. "github.com/smallstep/certificates/acme"
  7. "github.com/smallstep/certificates/api"
  8. "github.com/smallstep/certificates/authority/admin"
  9. "github.com/smallstep/certificates/authority/provisioner"
  10. "go.step.sm/linkedca"
  11. "google.golang.org/protobuf/types/known/timestamppb"
  12. )
  13. // CreateExternalAccountKeyRequest is the type for POST /admin/acme/eab requests
  14. type CreateExternalAccountKeyRequest struct {
  15. Reference string `json:"reference"`
  16. }
  17. // Validate validates a new ACME EAB Key request body.
  18. func (r *CreateExternalAccountKeyRequest) Validate() error {
  19. return nil
  20. }
  21. // GetExternalAccountKeysResponse is the type for GET /admin/acme/eab responses
  22. type GetExternalAccountKeysResponse struct {
  23. EAKs []*linkedca.EABKey `json:"eaks"`
  24. NextCursor string `json:"nextCursor"`
  25. }
  26. // requireEABEnabled is a middleware that ensures ACME EAB is enabled
  27. // before serving requests that act on ACME EAB credentials.
  28. func (h *Handler) requireEABEnabled(next nextHTTP) nextHTTP {
  29. return func(w http.ResponseWriter, r *http.Request) {
  30. prov := chi.URLParam(r, "prov")
  31. eabEnabled, err := h.provisionerHasEABEnabled(r.Context(), prov)
  32. if err != nil {
  33. api.WriteError(w, err)
  34. return
  35. }
  36. if !eabEnabled {
  37. api.WriteError(w, admin.NewError(admin.ErrorBadRequestType, "ACME EAB not enabled for provisioner %s", prov))
  38. return
  39. }
  40. next(w, r)
  41. }
  42. }
  43. // provisionerHasEABEnabled determines if the "requireEAB" setting for an ACME
  44. // provisioner is set to true and thus has EAB enabled.
  45. func (h *Handler) provisionerHasEABEnabled(ctx context.Context, provisionerName string) (bool, error) {
  46. var (
  47. p provisioner.Interface
  48. err error
  49. )
  50. if p, err = h.auth.LoadProvisionerByName(provisionerName); err != nil {
  51. return false, admin.WrapErrorISE(err, "error loading provisioner %s", provisionerName)
  52. }
  53. prov, err := h.db.GetProvisioner(ctx, p.GetID())
  54. if err != nil {
  55. return false, admin.WrapErrorISE(err, "error getting provisioner with ID: %s", p.GetID())
  56. }
  57. details := prov.GetDetails()
  58. if details == nil {
  59. return false, admin.NewErrorISE("error getting details for provisioner with ID: %s", p.GetID())
  60. }
  61. acmeProvisioner := details.GetACME()
  62. if acmeProvisioner == nil {
  63. return false, admin.NewErrorISE("error getting ACME details for provisioner with ID: %s", p.GetID())
  64. }
  65. return acmeProvisioner.GetRequireEab(), nil
  66. }
  67. // CreateExternalAccountKey creates a new External Account Binding key
  68. func (h *Handler) CreateExternalAccountKey(w http.ResponseWriter, r *http.Request) {
  69. var body CreateExternalAccountKeyRequest
  70. if err := api.ReadJSON(r.Body, &body); err != nil {
  71. api.WriteError(w, admin.WrapError(admin.ErrorBadRequestType, err, "error reading request body"))
  72. return
  73. }
  74. if err := body.Validate(); err != nil {
  75. api.WriteError(w, err)
  76. return
  77. }
  78. prov := chi.URLParam(r, "prov")
  79. reference := body.Reference
  80. if reference != "" {
  81. k, err := h.acmeDB.GetExternalAccountKeyByReference(r.Context(), prov, reference)
  82. // retrieving an EAB key from DB results in error if it doesn't exist, which is what we're looking for
  83. if err == nil || k != nil {
  84. err := admin.NewError(admin.ErrorBadRequestType, "an ACME EAB key for provisioner %s with reference %s already exists", prov, reference)
  85. err.Status = 409
  86. api.WriteError(w, err)
  87. return
  88. }
  89. }
  90. eak, err := h.acmeDB.CreateExternalAccountKey(r.Context(), prov, reference)
  91. if err != nil {
  92. api.WriteError(w, admin.WrapErrorISE(err, "error creating ACME EAB key for provisioner %s and reference %s", prov, reference))
  93. return
  94. }
  95. response := &linkedca.EABKey{
  96. Id: eak.ID,
  97. HmacKey: eak.KeyBytes,
  98. Provisioner: eak.Provisioner,
  99. Reference: eak.Reference,
  100. }
  101. api.ProtoJSONStatus(w, response, http.StatusCreated)
  102. }
  103. // DeleteExternalAccountKey deletes an ACME External Account Key.
  104. func (h *Handler) DeleteExternalAccountKey(w http.ResponseWriter, r *http.Request) {
  105. prov := chi.URLParam(r, "prov")
  106. keyID := chi.URLParam(r, "id")
  107. if err := h.acmeDB.DeleteExternalAccountKey(r.Context(), prov, keyID); err != nil {
  108. api.WriteError(w, admin.WrapErrorISE(err, "error deleting ACME EAB Key %s", keyID))
  109. return
  110. }
  111. api.JSON(w, &DeleteResponse{Status: "ok"})
  112. }
  113. // GetExternalAccountKeys returns a segment of ACME EAB Keys.
  114. func (h *Handler) GetExternalAccountKeys(w http.ResponseWriter, r *http.Request) {
  115. prov := chi.URLParam(r, "prov")
  116. reference := chi.URLParam(r, "ref")
  117. // TODO: support paging? It'll probably leak to the DB layer, as we have to loop through all keys
  118. var (
  119. key *acme.ExternalAccountKey
  120. keys []*acme.ExternalAccountKey
  121. err error
  122. )
  123. if reference != "" {
  124. key, err = h.acmeDB.GetExternalAccountKeyByReference(r.Context(), prov, reference)
  125. if err != nil {
  126. api.WriteError(w, admin.WrapErrorISE(err, "error getting external account key with reference %s", reference))
  127. return
  128. }
  129. keys = []*acme.ExternalAccountKey{key}
  130. } else {
  131. keys, err = h.acmeDB.GetExternalAccountKeys(r.Context(), prov)
  132. if err != nil {
  133. api.WriteError(w, admin.WrapErrorISE(err, "error getting external account keys"))
  134. return
  135. }
  136. }
  137. eaks := make([]*linkedca.EABKey, len(keys))
  138. for i, k := range keys {
  139. eaks[i] = &linkedca.EABKey{
  140. Id: k.ID,
  141. HmacKey: []byte{},
  142. Provisioner: k.Provisioner,
  143. Reference: k.Reference,
  144. Account: k.AccountID,
  145. CreatedAt: timestamppb.New(k.CreatedAt),
  146. BoundAt: timestamppb.New(k.BoundAt),
  147. }
  148. }
  149. nextCursor := ""
  150. api.JSON(w, &GetExternalAccountKeysResponse{
  151. EAKs: eaks,
  152. NextCursor: nextCursor,
  153. })
  154. }