handler.go 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. requireEABEnabled := func(next http.HandlerFunc) http.HandlerFunc {
  32. return h.requireEABEnabled(next)
  33. }
  34. enabledInStandalone := func(next http.HandlerFunc) http.HandlerFunc {
  35. return h.checkAction(next, true)
  36. }
  37. disabledInStandalone := func(next http.HandlerFunc) http.HandlerFunc {
  38. return h.checkAction(next, false)
  39. }
  40. // Provisioners
  41. r.MethodFunc("GET", "/provisioners/{name}", authnz(h.GetProvisioner))
  42. r.MethodFunc("GET", "/provisioners", authnz(h.GetProvisioners))
  43. r.MethodFunc("POST", "/provisioners", authnz(h.CreateProvisioner))
  44. r.MethodFunc("PUT", "/provisioners/{name}", authnz(h.UpdateProvisioner))
  45. r.MethodFunc("DELETE", "/provisioners/{name}", authnz(h.DeleteProvisioner))
  46. // Admins
  47. r.MethodFunc("GET", "/admins/{id}", authnz(h.GetAdmin))
  48. r.MethodFunc("GET", "/admins", authnz(h.GetAdmins))
  49. r.MethodFunc("POST", "/admins", authnz(h.CreateAdmin))
  50. r.MethodFunc("PATCH", "/admins/{id}", authnz(h.UpdateAdmin))
  51. r.MethodFunc("DELETE", "/admins/{id}", authnz(h.DeleteAdmin))
  52. // ACME External Account Binding Keys
  53. r.MethodFunc("GET", "/acme/eab/{provisionerName}/{reference}", authnz(requireEABEnabled(h.acmeResponder.GetExternalAccountKeys)))
  54. r.MethodFunc("GET", "/acme/eab/{provisionerName}", authnz(requireEABEnabled(h.acmeResponder.GetExternalAccountKeys)))
  55. r.MethodFunc("POST", "/acme/eab/{provisionerName}", authnz(requireEABEnabled(h.acmeResponder.CreateExternalAccountKey)))
  56. r.MethodFunc("DELETE", "/acme/eab/{provisionerName}/{id}", authnz(requireEABEnabled(h.acmeResponder.DeleteExternalAccountKey)))
  57. // Policy - Authority
  58. r.MethodFunc("GET", "/policy", authnz(enabledInStandalone(h.policyResponder.GetAuthorityPolicy)))
  59. r.MethodFunc("POST", "/policy", authnz(enabledInStandalone(h.policyResponder.CreateAuthorityPolicy)))
  60. r.MethodFunc("PUT", "/policy", authnz(enabledInStandalone(h.policyResponder.UpdateAuthorityPolicy)))
  61. r.MethodFunc("DELETE", "/policy", authnz(enabledInStandalone(h.policyResponder.DeleteAuthorityPolicy)))
  62. // Policy - Provisioner
  63. //r.MethodFunc("GET", "/provisioners/{name}/policy", noauth(h.policyResponder.GetProvisionerPolicy))
  64. r.MethodFunc("GET", "/provisioners/{provisionerName}/policy", authnz(disabledInStandalone(h.loadProvisionerByName(h.policyResponder.GetProvisionerPolicy))))
  65. r.MethodFunc("POST", "/provisioners/{provisionerName}/policy", authnz(disabledInStandalone(h.loadProvisionerByName(h.policyResponder.CreateProvisionerPolicy))))
  66. r.MethodFunc("PUT", "/provisioners/{provisionerName}/policy", authnz(disabledInStandalone(h.loadProvisionerByName(h.policyResponder.UpdateProvisionerPolicy))))
  67. r.MethodFunc("DELETE", "/provisioners/{provisionerName}/policy", authnz(disabledInStandalone(h.loadProvisionerByName(h.policyResponder.DeleteProvisionerPolicy))))
  68. // Policy - ACME Account
  69. // 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)
  70. r.MethodFunc("GET", "/acme/{provisionerName}/{accountID}/policy", authnz(disabledInStandalone(h.policyResponder.GetACMEAccountPolicy)))
  71. r.MethodFunc("POST", "/acme/{provisionerName}/{accountID}/policy", authnz(disabledInStandalone(h.policyResponder.CreateACMEAccountPolicy)))
  72. r.MethodFunc("PUT", "/acme/{provisionerName}/{accountID}/policy", authnz(disabledInStandalone(h.policyResponder.UpdateACMEAccountPolicy)))
  73. r.MethodFunc("DELETE", "/acme/{provisionerName}/{accountID}/policy", authnz(disabledInStandalone(h.policyResponder.DeleteACMEAccountPolicy)))
  74. }