middleware.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package api
  2. import (
  3. "errors"
  4. "net/http"
  5. "github.com/go-chi/chi"
  6. "go.step.sm/linkedca"
  7. "github.com/smallstep/certificates/acme"
  8. "github.com/smallstep/certificates/api/render"
  9. "github.com/smallstep/certificates/authority/admin"
  10. "github.com/smallstep/certificates/authority/admin/db/nosql"
  11. "github.com/smallstep/certificates/authority/provisioner"
  12. )
  13. // requireAPIEnabled is a middleware that ensures the Administration API
  14. // is enabled before servicing requests.
  15. func (h *Handler) requireAPIEnabled(next http.HandlerFunc) http.HandlerFunc {
  16. return func(w http.ResponseWriter, r *http.Request) {
  17. if !h.auth.IsAdminAPIEnabled() {
  18. render.Error(w, admin.NewError(admin.ErrorNotImplementedType,
  19. "administration API not enabled"))
  20. return
  21. }
  22. next(w, r)
  23. }
  24. }
  25. // extractAuthorizeTokenAdmin is a middleware that extracts and caches the bearer token.
  26. func (h *Handler) extractAuthorizeTokenAdmin(next http.HandlerFunc) http.HandlerFunc {
  27. return func(w http.ResponseWriter, r *http.Request) {
  28. tok := r.Header.Get("Authorization")
  29. if tok == "" {
  30. render.Error(w, admin.NewError(admin.ErrorUnauthorizedType,
  31. "missing authorization header token"))
  32. return
  33. }
  34. adm, err := h.auth.AuthorizeAdminToken(r, tok)
  35. if err != nil {
  36. render.Error(w, err)
  37. return
  38. }
  39. ctx := linkedca.NewContextWithAdmin(r.Context(), adm)
  40. next(w, r.WithContext(ctx))
  41. }
  42. }
  43. // loadProvisionerByName is a middleware that searches for a provisioner
  44. // by name and stores it in the context.
  45. func (h *Handler) loadProvisionerByName(next http.HandlerFunc) http.HandlerFunc {
  46. return func(w http.ResponseWriter, r *http.Request) {
  47. ctx := r.Context()
  48. name := chi.URLParam(r, "provisionerName")
  49. var (
  50. p provisioner.Interface
  51. err error
  52. )
  53. // TODO(hs): distinguish 404 vs. 500
  54. if p, err = h.auth.LoadProvisionerByName(name); err != nil {
  55. render.Error(w, admin.WrapErrorISE(err, "error loading provisioner %s", name))
  56. return
  57. }
  58. prov, err := h.adminDB.GetProvisioner(ctx, p.GetID())
  59. if err != nil {
  60. render.Error(w, admin.WrapErrorISE(err, "error retrieving provisioner %s", name))
  61. return
  62. }
  63. ctx = linkedca.NewContextWithProvisioner(ctx, prov)
  64. next(w, r.WithContext(ctx))
  65. }
  66. }
  67. // checkAction checks if an action is supported in standalone or not
  68. func (h *Handler) checkAction(next http.HandlerFunc, supportedInStandalone bool) http.HandlerFunc {
  69. return func(w http.ResponseWriter, r *http.Request) {
  70. // actions allowed in standalone mode are always supported
  71. if supportedInStandalone {
  72. next(w, r)
  73. return
  74. }
  75. // when an action is not supported in standalone mode and when
  76. // using a nosql.DB backend, actions are not supported
  77. if _, ok := h.adminDB.(*nosql.DB); ok {
  78. render.Error(w, admin.NewError(admin.ErrorNotImplementedType,
  79. "operation not supported in standalone mode"))
  80. return
  81. }
  82. // continue to next http handler
  83. next(w, r)
  84. }
  85. }
  86. // loadExternalAccountKey is a middleware that searches for an ACME
  87. // External Account Key by reference or keyID and stores it in the context.
  88. func (h *Handler) loadExternalAccountKey(next http.HandlerFunc) http.HandlerFunc {
  89. return func(w http.ResponseWriter, r *http.Request) {
  90. ctx := r.Context()
  91. prov := linkedca.MustProvisionerFromContext(ctx)
  92. reference := chi.URLParam(r, "reference")
  93. keyID := chi.URLParam(r, "keyID")
  94. var (
  95. eak *acme.ExternalAccountKey
  96. err error
  97. )
  98. if keyID != "" {
  99. eak, err = h.acmeDB.GetExternalAccountKey(ctx, prov.GetId(), keyID)
  100. } else {
  101. eak, err = h.acmeDB.GetExternalAccountKeyByReference(ctx, prov.GetId(), reference)
  102. }
  103. if err != nil {
  104. if errors.Is(err, acme.ErrNotFound) {
  105. render.Error(w, admin.NewError(admin.ErrorNotFoundType, "ACME External Account Key not found"))
  106. return
  107. }
  108. render.Error(w, admin.WrapErrorISE(err, "error retrieving ACME External Account Key"))
  109. return
  110. }
  111. if eak == nil {
  112. render.Error(w, admin.NewError(admin.ErrorNotFoundType, "ACME External Account Key not found"))
  113. return
  114. }
  115. linkedEAK := eakToLinked(eak)
  116. ctx = linkedca.NewContextWithExternalAccountKey(ctx, linkedEAK)
  117. next(w, r.WithContext(ctx))
  118. }
  119. }