handler.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package api
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/smallstep/certificates/acme"
  6. "github.com/smallstep/certificates/api"
  7. "github.com/smallstep/certificates/authority"
  8. "github.com/smallstep/certificates/authority/admin"
  9. )
  10. // Handler is the Admin API request handler.
  11. type Handler struct {
  12. acmeResponder acmeAdminResponderInterface
  13. policyResponder policyAdminResponderInterface
  14. }
  15. // Route traffic and implement the Router interface.
  16. //
  17. // Deprecated: use Route(r api.Router, acmeResponder acmeAdminResponderInterface)
  18. func (h *Handler) Route(r api.Router) {
  19. Route(r, h.acmeResponder, h.policyResponder)
  20. }
  21. // NewHandler returns a new Authority Config Handler.
  22. func NewHandler(auth adminAuthority, adminDB admin.DB, acmeDB acme.DB, acmeResponder acmeAdminResponderInterface, policyResponder policyAdminResponderInterface) api.RouterHandler {
  23. return &Handler{
  24. acmeResponder: acmeResponder,
  25. policyResponder: policyResponder,
  26. }
  27. }
  28. var mustAuthority = func(ctx context.Context) adminAuthority {
  29. return authority.MustFromContext(ctx)
  30. }
  31. // Route traffic and implement the Router interface.
  32. func Route(r api.Router, acmeResponder acmeAdminResponderInterface, policyResponder policyAdminResponderInterface) {
  33. authnz := func(next http.HandlerFunc) http.HandlerFunc {
  34. return extractAuthorizeTokenAdmin(requireAPIEnabled(next))
  35. }
  36. enabledInStandalone := func(next http.HandlerFunc) http.HandlerFunc {
  37. return checkAction(next, true)
  38. }
  39. disabledInStandalone := func(next http.HandlerFunc) http.HandlerFunc {
  40. return checkAction(next, false)
  41. }
  42. acmeEABMiddleware := func(next http.HandlerFunc) http.HandlerFunc {
  43. return authnz(loadProvisionerByName(requireEABEnabled(next)))
  44. }
  45. authorityPolicyMiddleware := func(next http.HandlerFunc) http.HandlerFunc {
  46. return authnz(enabledInStandalone(next))
  47. }
  48. provisionerPolicyMiddleware := func(next http.HandlerFunc) http.HandlerFunc {
  49. return authnz(disabledInStandalone(loadProvisionerByName(next)))
  50. }
  51. acmePolicyMiddleware := func(next http.HandlerFunc) http.HandlerFunc {
  52. return authnz(disabledInStandalone(loadProvisionerByName(requireEABEnabled(loadExternalAccountKey(next)))))
  53. }
  54. // Provisioners
  55. r.MethodFunc("GET", "/provisioners/{name}", authnz(GetProvisioner))
  56. r.MethodFunc("GET", "/provisioners", authnz(GetProvisioners))
  57. r.MethodFunc("POST", "/provisioners", authnz(CreateProvisioner))
  58. r.MethodFunc("PUT", "/provisioners/{name}", authnz(UpdateProvisioner))
  59. r.MethodFunc("DELETE", "/provisioners/{name}", authnz(DeleteProvisioner))
  60. // Admins
  61. r.MethodFunc("GET", "/admins/{id}", authnz(GetAdmin))
  62. r.MethodFunc("GET", "/admins", authnz(GetAdmins))
  63. r.MethodFunc("POST", "/admins", authnz(CreateAdmin))
  64. r.MethodFunc("PATCH", "/admins/{id}", authnz(UpdateAdmin))
  65. r.MethodFunc("DELETE", "/admins/{id}", authnz(DeleteAdmin))
  66. // ACME External Account Binding Keys
  67. r.MethodFunc("GET", "/acme/eab/{provisionerName}/{reference}", acmeEABMiddleware(acmeResponder.GetExternalAccountKeys))
  68. r.MethodFunc("GET", "/acme/eab/{provisionerName}", acmeEABMiddleware(acmeResponder.GetExternalAccountKeys))
  69. r.MethodFunc("POST", "/acme/eab/{provisionerName}", acmeEABMiddleware(acmeResponder.CreateExternalAccountKey))
  70. r.MethodFunc("DELETE", "/acme/eab/{provisionerName}/{id}", acmeEABMiddleware(acmeResponder.DeleteExternalAccountKey))
  71. // Policy - Authority
  72. r.MethodFunc("GET", "/policy", authorityPolicyMiddleware(policyResponder.GetAuthorityPolicy))
  73. r.MethodFunc("POST", "/policy", authorityPolicyMiddleware(policyResponder.CreateAuthorityPolicy))
  74. r.MethodFunc("PUT", "/policy", authorityPolicyMiddleware(policyResponder.UpdateAuthorityPolicy))
  75. r.MethodFunc("DELETE", "/policy", authorityPolicyMiddleware(policyResponder.DeleteAuthorityPolicy))
  76. // Policy - Provisioner
  77. r.MethodFunc("GET", "/provisioners/{provisionerName}/policy", provisionerPolicyMiddleware(policyResponder.GetProvisionerPolicy))
  78. r.MethodFunc("POST", "/provisioners/{provisionerName}/policy", provisionerPolicyMiddleware(policyResponder.CreateProvisionerPolicy))
  79. r.MethodFunc("PUT", "/provisioners/{provisionerName}/policy", provisionerPolicyMiddleware(policyResponder.UpdateProvisionerPolicy))
  80. r.MethodFunc("DELETE", "/provisioners/{provisionerName}/policy", provisionerPolicyMiddleware(policyResponder.DeleteProvisionerPolicy))
  81. // Policy - ACME Account
  82. r.MethodFunc("GET", "/acme/policy/{provisionerName}/reference/{reference}", acmePolicyMiddleware(policyResponder.GetACMEAccountPolicy))
  83. r.MethodFunc("GET", "/acme/policy/{provisionerName}/key/{keyID}", acmePolicyMiddleware(policyResponder.GetACMEAccountPolicy))
  84. r.MethodFunc("POST", "/acme/policy/{provisionerName}/reference/{reference}", acmePolicyMiddleware(policyResponder.CreateACMEAccountPolicy))
  85. r.MethodFunc("POST", "/acme/policy/{provisionerName}/key/{keyID}", acmePolicyMiddleware(policyResponder.CreateACMEAccountPolicy))
  86. r.MethodFunc("PUT", "/acme/policy/{provisionerName}/reference/{reference}", acmePolicyMiddleware(policyResponder.UpdateACMEAccountPolicy))
  87. r.MethodFunc("PUT", "/acme/policy/{provisionerName}/key/{keyID}", acmePolicyMiddleware(policyResponder.UpdateACMEAccountPolicy))
  88. r.MethodFunc("DELETE", "/acme/policy/{provisionerName}/reference/{reference}", acmePolicyMiddleware(policyResponder.DeleteACMEAccountPolicy))
  89. r.MethodFunc("DELETE", "/acme/policy/{provisionerName}/key/{keyID}", acmePolicyMiddleware(policyResponder.DeleteACMEAccountPolicy))
  90. }