router.go 6.3 KB

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