router.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package router
  2. import (
  3. "net/http"
  4. "github.com/go-chi/chi"
  5. "github.com/porter-dev/porter/api/server/authn"
  6. "github.com/porter-dev/porter/api/server/authz"
  7. "github.com/porter-dev/porter/api/server/authz/policy"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/config"
  10. "github.com/porter-dev/porter/api/types"
  11. )
  12. func NewAPIRouter(config *config.Config) *chi.Mux {
  13. r := chi.NewRouter()
  14. // set the content type for all API endpoints
  15. r.Use(ContentTypeJSON)
  16. endpointFactory := shared.NewAPIObjectEndpointFactory(config)
  17. baseRegisterer := NewBaseRegisterer()
  18. releaseRegisterer := NewReleaseScopedRegisterer()
  19. namespaceRegisterer := NewNamespaceScopedRegisterer(releaseRegisterer)
  20. clusterRegisterer := NewClusterScopedRegisterer(namespaceRegisterer)
  21. infraRegisterer := NewInfraScopedRegisterer()
  22. gitInstallationRegisterer := NewGitInstallationScopedRegisterer()
  23. registryRegisterer := NewRegistryScopedRegisterer()
  24. helmRepoRegisterer := NewHelmRepoScopedRegisterer()
  25. inviteRegisterer := NewInviteScopedRegisterer()
  26. projRegisterer := NewProjectScopedRegisterer(
  27. clusterRegisterer,
  28. registryRegisterer,
  29. helmRepoRegisterer,
  30. inviteRegisterer,
  31. gitInstallationRegisterer,
  32. infraRegisterer,
  33. )
  34. userRegisterer := NewUserScopedRegisterer(projRegisterer)
  35. r.Route("/api", func(r chi.Router) {
  36. baseRoutes := baseRegisterer.GetRoutes(
  37. r,
  38. config,
  39. &types.Path{
  40. RelativePath: "",
  41. },
  42. endpointFactory,
  43. )
  44. userRoutes := userRegisterer.GetRoutes(
  45. r,
  46. config,
  47. &types.Path{
  48. RelativePath: "",
  49. },
  50. endpointFactory,
  51. userRegisterer.Children...,
  52. )
  53. routes := append(baseRoutes, userRoutes...)
  54. registerRoutes(config, routes)
  55. })
  56. return r
  57. }
  58. type Route struct {
  59. Endpoint *shared.APIEndpoint
  60. Handler http.Handler
  61. Router chi.Router
  62. }
  63. type Registerer struct {
  64. GetRoutes func(
  65. r chi.Router,
  66. config *config.Config,
  67. basePath *types.Path,
  68. factory shared.APIEndpointFactory,
  69. children ...*Registerer,
  70. ) []*Route
  71. Children []*Registerer
  72. }
  73. func registerRoutes(config *config.Config, routes []*Route) {
  74. // Create a new "user-scoped" factory which will create a new user-scoped request
  75. // after authentication. Each subsequent http.Handler can lookup the user in context.
  76. authNFactory := authn.NewAuthNFactory(config)
  77. // Create a new "project-scoped" factory which will create a new project-scoped request
  78. // after authorization. Each subsequent http.Handler can lookup the project in context.
  79. projFactory := authz.NewProjectScopedFactory(config)
  80. // Create a new "cluster-scoped" factory which will create a new cluster-scoped request
  81. // after authorization. Each subsequent http.Handler can lookup the cluster in context.
  82. clusterFactory := authz.NewClusterScopedFactory(config)
  83. // Create a new "namespace-scoped" factory which will create a new namespace-scoped request
  84. // after authorization. Each subsequent http.Handler can lookup the namespace in context.
  85. namespaceFactory := authz.NewNamespaceScopedFactory(config)
  86. // Create a new "helmrepo-scoped" factory which will create a new helmrepo-scoped request
  87. // after authorization. Each subsequent http.Handler can lookup the helm repo in context.
  88. helmRepoFactory := authz.NewHelmRepoScopedFactory(config)
  89. // Create a new "registry-scoped" factory which will create a new registry-scoped request
  90. // after authorization. Each subsequent http.Handler can lookup the registry in context.
  91. registryFactory := authz.NewRegistryScopedFactory(config)
  92. // Create a new "gitinstallation-scoped" factory which will create a new gitinstallation-scoped request
  93. // after authorization. Each subsequent http.Handler can lookup the gitinstallation in context.
  94. gitInstallationFactory := authz.NewGitInstallationScopedFactory(config)
  95. // Create a new "invite-scoped" factory which will create a new invite-scoped request
  96. // after authorization. Each subsequent http.Handler can lookup the invite in context.
  97. inviteFactory := authz.NewInviteScopedFactory(config)
  98. // Create a new "infra-scoped" factory which will create a new infra-scoped request
  99. // after authorization. Each subsequent http.Handler can lookup the infra in context.
  100. infraFactory := authz.NewInfraScopedFactory(config)
  101. // Create a new "release-scoped" factory which will create a new release-scoped request
  102. // after authorization. Each subsequent http.Handler can lookup the release in context.
  103. releaseFactory := authz.NewReleaseScopedFactory(config)
  104. // Policy doc loader loads the policy documents for a specific project.
  105. policyDocLoader := policy.NewBasicPolicyDocumentLoader(config.Repo.Project())
  106. for _, route := range routes {
  107. atomicGroup := route.Router.Group(nil)
  108. for _, scope := range route.Endpoint.Metadata.Scopes {
  109. switch scope {
  110. case types.UserScope:
  111. // if the endpoint should redirect when authn fails, attach redirect handler
  112. if route.Endpoint.Metadata.ShouldRedirect {
  113. atomicGroup.Use(authNFactory.NewAuthenticatedWithRedirect)
  114. } else {
  115. atomicGroup.Use(authNFactory.NewAuthenticated)
  116. }
  117. case types.ProjectScope:
  118. policyFactory := authz.NewPolicyMiddleware(config, *route.Endpoint.Metadata, policyDocLoader)
  119. atomicGroup.Use(policyFactory.Middleware)
  120. atomicGroup.Use(projFactory.Middleware)
  121. case types.ClusterScope:
  122. atomicGroup.Use(clusterFactory.Middleware)
  123. case types.NamespaceScope:
  124. atomicGroup.Use(namespaceFactory.Middleware)
  125. case types.HelmRepoScope:
  126. atomicGroup.Use(helmRepoFactory.Middleware)
  127. case types.RegistryScope:
  128. atomicGroup.Use(registryFactory.Middleware)
  129. case types.InviteScope:
  130. atomicGroup.Use(inviteFactory.Middleware)
  131. case types.GitInstallationScope:
  132. atomicGroup.Use(gitInstallationFactory.Middleware)
  133. case types.InfraScope:
  134. atomicGroup.Use(infraFactory.Middleware)
  135. case types.ReleaseScope:
  136. atomicGroup.Use(releaseFactory.Middleware)
  137. }
  138. }
  139. atomicGroup.Method(
  140. string(route.Endpoint.Metadata.Method),
  141. route.Endpoint.Metadata.Path.RelativePath,
  142. route.Handler,
  143. )
  144. }
  145. }
  146. // ContentTypeJSON sets the content type for requests to application/json
  147. func ContentTypeJSON(next http.Handler) http.Handler {
  148. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  149. w.Header().Set("Content-Type", "application/json;charset=utf8")
  150. next.ServeHTTP(w, r)
  151. })
  152. }