middleware.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package api
  2. import (
  3. "net/http"
  4. "github.com/go-chi/chi"
  5. "google.golang.org/protobuf/types/known/timestamppb"
  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. // // temporarily only support the admin nosql DB
  71. // if _, ok := h.adminDB.(*nosql.DB); !ok {
  72. // render.Error(w, admin.NewError(admin.ErrorNotImplementedType,
  73. // "operation not supported"))
  74. // return
  75. // }
  76. // actions allowed in standalone mode are always supported
  77. if supportedInStandalone {
  78. next(w, r)
  79. return
  80. }
  81. // when an action is not supported in standalone mode and when
  82. // using a nosql.DB backend, actions are not supported
  83. if _, ok := h.adminDB.(*nosql.DB); ok {
  84. render.Error(w, admin.NewError(admin.ErrorNotImplementedType,
  85. "operation not supported in standalone mode"))
  86. return
  87. }
  88. // continue to next http handler
  89. next(w, r)
  90. }
  91. }
  92. // loadExternalAccountKey is a middleware that searches for an ACME
  93. // External Account Key by accountID, keyID or reference and stores it in the context.
  94. func (h *Handler) loadExternalAccountKey(next http.HandlerFunc) http.HandlerFunc {
  95. return func(w http.ResponseWriter, r *http.Request) {
  96. ctx := r.Context()
  97. prov := linkedca.ProvisionerFromContext(ctx)
  98. reference := chi.URLParam(r, "reference")
  99. keyID := chi.URLParam(r, "keyID")
  100. var (
  101. eak *acme.ExternalAccountKey
  102. err error
  103. )
  104. if keyID != "" {
  105. eak, err = h.acmeDB.GetExternalAccountKey(ctx, prov.GetId(), keyID)
  106. } else {
  107. eak, err = h.acmeDB.GetExternalAccountKeyByReference(ctx, prov.GetId(), reference)
  108. }
  109. if err != nil {
  110. // TODO: handle error; not found vs. some internal server error
  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 does not exist"))
  116. return
  117. }
  118. linkedEAK := eakToLinked(eak)
  119. ctx = linkedca.NewContextWithExternalAccountKey(ctx, linkedEAK)
  120. next(w, r.WithContext(ctx))
  121. }
  122. }
  123. func eakToLinked(k *acme.ExternalAccountKey) *linkedca.EABKey {
  124. if k == nil {
  125. return nil
  126. }
  127. eak := &linkedca.EABKey{
  128. Id: k.ID,
  129. HmacKey: k.KeyBytes,
  130. Provisioner: k.ProvisionerID,
  131. Reference: k.Reference,
  132. Account: k.AccountID,
  133. CreatedAt: timestamppb.New(k.CreatedAt),
  134. BoundAt: timestamppb.New(k.BoundAt),
  135. }
  136. if k.Policy != nil {
  137. eak.Policy = &linkedca.Policy{
  138. X509: &linkedca.X509Policy{
  139. Allow: &linkedca.X509Names{},
  140. Deny: &linkedca.X509Names{},
  141. },
  142. }
  143. eak.Policy.X509.Allow.Dns = k.Policy.X509.Allowed.DNSNames
  144. eak.Policy.X509.Allow.Ips = k.Policy.X509.Allowed.IPRanges
  145. eak.Policy.X509.Deny.Dns = k.Policy.X509.Denied.DNSNames
  146. eak.Policy.X509.Deny.Ips = k.Policy.X509.Denied.IPRanges
  147. }
  148. return eak
  149. }
  150. func linkedEAKToCertificates(k *linkedca.EABKey) *acme.ExternalAccountKey {
  151. if k == nil {
  152. return nil
  153. }
  154. eak := &acme.ExternalAccountKey{
  155. ID: k.Id,
  156. ProvisionerID: k.Provisioner,
  157. Reference: k.Reference,
  158. AccountID: k.Account,
  159. KeyBytes: k.HmacKey,
  160. CreatedAt: k.CreatedAt.AsTime(),
  161. BoundAt: k.BoundAt.AsTime(),
  162. }
  163. if k.Policy == nil {
  164. return eak
  165. }
  166. eak.Policy = &acme.Policy{}
  167. if k.Policy.X509 == nil {
  168. return eak
  169. }
  170. eak.Policy.X509 = acme.X509Policy{
  171. Allowed: acme.PolicyNames{},
  172. Denied: acme.PolicyNames{},
  173. }
  174. if k.Policy.X509.Allow != nil {
  175. eak.Policy.X509.Allowed.DNSNames = k.Policy.X509.Allow.Dns
  176. eak.Policy.X509.Allowed.IPRanges = k.Policy.X509.Allow.Ips
  177. }
  178. if k.Policy.X509.Deny != nil {
  179. eak.Policy.X509.Denied.DNSNames = k.Policy.X509.Deny.Dns
  180. eak.Policy.X509.Denied.IPRanges = k.Policy.X509.Deny.Ips
  181. }
  182. return eak
  183. }