handler.go 5.1 KB

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