middleware.go 4.2 KB

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