middleware.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 requireAPIEnabled(next http.HandlerFunc) http.HandlerFunc {
  16. return func(w http.ResponseWriter, r *http.Request) {
  17. if !mustAuthority(r.Context()).IsAdminAPIEnabled() {
  18. render.Error(w, admin.NewError(admin.ErrorNotImplementedType, "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 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. ctx := r.Context()
  34. adm, err := mustAuthority(ctx).AuthorizeAdminToken(r, tok)
  35. if err != nil {
  36. render.Error(w, err)
  37. return
  38. }
  39. ctx = linkedca.NewContextWithAdmin(ctx, 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 loadProvisionerByName(next http.HandlerFunc) http.HandlerFunc {
  46. return func(w http.ResponseWriter, r *http.Request) {
  47. var (
  48. p provisioner.Interface
  49. err error
  50. )
  51. ctx := r.Context()
  52. auth := mustAuthority(ctx)
  53. adminDB := admin.MustFromContext(ctx)
  54. name := chi.URLParam(r, "provisionerName")
  55. // TODO(hs): distinguish 404 vs. 500
  56. if p, err = auth.LoadProvisionerByName(name); err != nil {
  57. render.Error(w, admin.WrapErrorISE(err, "error loading provisioner %s", name))
  58. return
  59. }
  60. prov, err := adminDB.GetProvisioner(ctx, p.GetID())
  61. if err != nil {
  62. render.Error(w, admin.WrapErrorISE(err, "error retrieving provisioner %s", name))
  63. return
  64. }
  65. ctx = linkedca.NewContextWithProvisioner(ctx, prov)
  66. next(w, r.WithContext(ctx))
  67. }
  68. }
  69. // checkAction checks if an action is supported in standalone or not
  70. func checkAction(next http.HandlerFunc, supportedInStandalone bool) http.HandlerFunc {
  71. return func(w http.ResponseWriter, r *http.Request) {
  72. // actions allowed in standalone mode are always supported
  73. if supportedInStandalone {
  74. next(w, r)
  75. return
  76. }
  77. // when an action is not supported in standalone mode and when
  78. // using a nosql.DB backend, actions are not supported
  79. if _, ok := admin.MustFromContext(r.Context()).(*nosql.DB); ok {
  80. render.Error(w, admin.NewError(admin.ErrorNotImplementedType,
  81. "operation not supported in standalone mode"))
  82. return
  83. }
  84. // continue to next http handler
  85. next(w, r)
  86. }
  87. }
  88. // loadExternalAccountKey is a middleware that searches for an ACME
  89. // External Account Key by reference or keyID and stores it in the context.
  90. func loadExternalAccountKey(next http.HandlerFunc) http.HandlerFunc {
  91. return func(w http.ResponseWriter, r *http.Request) {
  92. ctx := r.Context()
  93. prov := linkedca.MustProvisionerFromContext(ctx)
  94. acmeDB := acme.MustDatabaseFromContext(ctx)
  95. reference := chi.URLParam(r, "reference")
  96. keyID := chi.URLParam(r, "keyID")
  97. var (
  98. eak *acme.ExternalAccountKey
  99. err error
  100. )
  101. if keyID != "" {
  102. eak, err = acmeDB.GetExternalAccountKey(ctx, prov.GetId(), keyID)
  103. } else {
  104. eak, err = acmeDB.GetExternalAccountKeyByReference(ctx, prov.GetId(), reference)
  105. }
  106. if err != nil {
  107. if errors.Is(err, acme.ErrNotFound) {
  108. render.Error(w, admin.NewError(admin.ErrorNotFoundType, "ACME External Account Key not found"))
  109. return
  110. }
  111. render.Error(w, admin.WrapErrorISE(err, "error retrieving ACME External Account Key"))
  112. return
  113. }
  114. if eak == nil {
  115. render.Error(w, admin.NewError(admin.ErrorNotFoundType, "ACME External Account Key not found"))
  116. return
  117. }
  118. linkedEAK := eakToLinked(eak)
  119. ctx = linkedca.NewContextWithExternalAccountKey(ctx, linkedEAK)
  120. next(w, r.WithContext(ctx))
  121. }
  122. }