handler.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package api
  2. import (
  3. "github.com/smallstep/certificates/acme"
  4. "github.com/smallstep/certificates/api"
  5. "github.com/smallstep/certificates/authority/admin"
  6. )
  7. // Handler is the Admin API request handler.
  8. type Handler struct {
  9. adminDB admin.DB
  10. auth adminAuthority
  11. acmeDB acme.DB
  12. acmeResponder acmeAdminResponderInterface
  13. policyResponder policyAdminResponderInterface
  14. }
  15. // NewHandler returns a new Authority Config Handler.
  16. func NewHandler(auth adminAuthority, adminDB admin.DB, acmeDB acme.DB, acmeResponder acmeAdminResponderInterface, policyResponder policyAdminResponderInterface) api.RouterHandler {
  17. return &Handler{
  18. auth: auth,
  19. adminDB: adminDB,
  20. acmeDB: acmeDB,
  21. acmeResponder: acmeResponder,
  22. policyResponder: policyResponder,
  23. }
  24. }
  25. // Route traffic and implement the Router interface.
  26. func (h *Handler) Route(r api.Router) {
  27. authnz := func(next nextHTTP) nextHTTP {
  28. return h.extractAuthorizeTokenAdmin(h.requireAPIEnabled(next))
  29. }
  30. requireEABEnabled := func(next nextHTTP) nextHTTP {
  31. return h.requireEABEnabled(next)
  32. }
  33. enabledInStandalone := func(next nextHTTP) nextHTTP {
  34. return h.checkAction(next, true)
  35. }
  36. disabledInStandalone := func(next nextHTTP) nextHTTP {
  37. return h.checkAction(next, false)
  38. }
  39. // Provisioners
  40. r.MethodFunc("GET", "/provisioners/{name}", authnz(h.GetProvisioner))
  41. r.MethodFunc("GET", "/provisioners", authnz(h.GetProvisioners))
  42. r.MethodFunc("POST", "/provisioners", authnz(h.CreateProvisioner))
  43. r.MethodFunc("PUT", "/provisioners/{name}", authnz(h.UpdateProvisioner))
  44. r.MethodFunc("DELETE", "/provisioners/{name}", authnz(h.DeleteProvisioner))
  45. // Admins
  46. r.MethodFunc("GET", "/admins/{id}", authnz(h.GetAdmin))
  47. r.MethodFunc("GET", "/admins", authnz(h.GetAdmins))
  48. r.MethodFunc("POST", "/admins", authnz(h.CreateAdmin))
  49. r.MethodFunc("PATCH", "/admins/{id}", authnz(h.UpdateAdmin))
  50. r.MethodFunc("DELETE", "/admins/{id}", authnz(h.DeleteAdmin))
  51. // ACME External Account Binding Keys
  52. r.MethodFunc("GET", "/acme/eab/{provisionerName}/{reference}", authnz(requireEABEnabled(h.acmeResponder.GetExternalAccountKeys)))
  53. r.MethodFunc("GET", "/acme/eab/{provisionerName}", authnz(requireEABEnabled(h.acmeResponder.GetExternalAccountKeys)))
  54. r.MethodFunc("POST", "/acme/eab/{provisionerName}", authnz(requireEABEnabled(h.acmeResponder.CreateExternalAccountKey)))
  55. r.MethodFunc("DELETE", "/acme/eab/{provisionerName}/{id}", authnz(requireEABEnabled(h.acmeResponder.DeleteExternalAccountKey)))
  56. // Policy - Authority
  57. r.MethodFunc("GET", "/policy", authnz(enabledInStandalone(h.policyResponder.GetAuthorityPolicy)))
  58. r.MethodFunc("POST", "/policy", authnz(enabledInStandalone(h.policyResponder.CreateAuthorityPolicy)))
  59. r.MethodFunc("PUT", "/policy", authnz(enabledInStandalone(h.policyResponder.UpdateAuthorityPolicy)))
  60. r.MethodFunc("DELETE", "/policy", authnz(enabledInStandalone(h.policyResponder.DeleteAuthorityPolicy)))
  61. // Policy - Provisioner
  62. //r.MethodFunc("GET", "/provisioners/{name}/policy", noauth(h.policyResponder.GetProvisionerPolicy))
  63. r.MethodFunc("GET", "/provisioners/{name}/policy", authnz(disabledInStandalone(h.policyResponder.GetProvisionerPolicy)))
  64. r.MethodFunc("POST", "/provisioners/{name}/policy", authnz(disabledInStandalone(h.policyResponder.CreateProvisionerPolicy)))
  65. r.MethodFunc("PUT", "/provisioners/{name}/policy", authnz(disabledInStandalone(h.policyResponder.UpdateProvisionerPolicy)))
  66. r.MethodFunc("DELETE", "/provisioners/{name}/policy", authnz(disabledInStandalone(h.policyResponder.DeleteProvisionerPolicy)))
  67. // Policy - ACME Account
  68. // TODO: ensure we don't clash with eab; might want to change eab paths slightly (as long as we don't have it released completely; needs changes in adminClient too)
  69. r.MethodFunc("GET", "/acme/{provisionerName}/{accountID}/policy", authnz(disabledInStandalone(h.policyResponder.GetACMEAccountPolicy)))
  70. r.MethodFunc("POST", "/acme/{provisionerName}/{accountID}/policy", authnz(disabledInStandalone(h.policyResponder.CreateACMEAccountPolicy)))
  71. r.MethodFunc("PUT", "/acme/{provisionerName}/{accountID}/policy", authnz(disabledInStandalone(h.policyResponder.UpdateACMEAccountPolicy)))
  72. r.MethodFunc("DELETE", "/acme/{provisionerName}/{accountID}/policy", authnz(disabledInStandalone(h.policyResponder.DeleteACMEAccountPolicy)))
  73. }