router.go 6.8 KB

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