middleware.go 4.2 KB

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