policy.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package authz
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/porter-dev/porter/api/server/authz/policy"
  7. "github.com/porter-dev/porter/api/server/shared/apierrors"
  8. "github.com/porter-dev/porter/api/server/shared/config"
  9. "github.com/porter-dev/porter/api/server/shared/requestutils"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/internal/models"
  12. )
  13. type PolicyMiddleware struct {
  14. config *config.Config
  15. endpointMeta types.APIRequestMetadata
  16. loader policy.PolicyDocumentLoader
  17. }
  18. func NewPolicyMiddleware(
  19. config *config.Config,
  20. endpointMeta types.APIRequestMetadata,
  21. loader policy.PolicyDocumentLoader,
  22. ) *PolicyMiddleware {
  23. return &PolicyMiddleware{config, endpointMeta, loader}
  24. }
  25. func (p *PolicyMiddleware) Middleware(next http.Handler) http.Handler {
  26. return &PolicyHandler{next, p.config, p.endpointMeta, p.loader}
  27. }
  28. type PolicyHandler struct {
  29. next http.Handler
  30. config *config.Config
  31. endpointMeta types.APIRequestMetadata
  32. loader policy.PolicyDocumentLoader
  33. }
  34. func (h *PolicyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  35. // get the full map of scopes to resource actions
  36. reqScopes, reqErr := getRequestActionForEndpoint(r, h.endpointMeta)
  37. if reqErr != nil {
  38. apierrors.HandleAPIError(h.config.Logger, h.config.Alerter, w, r, reqErr, true)
  39. return
  40. }
  41. policyLoaderOpts := &policy.PolicyLoaderOpts{}
  42. // first check if an api token exists in context
  43. if r.Context().Value("api_token") != nil {
  44. projID := reqScopes[types.ProjectScope].Resource.UInt
  45. // FIXME: find a clean way to get the project
  46. apiToken, _ := r.Context().Value("api_token").(*models.APIToken)
  47. policyLoaderOpts.ProjectToken = apiToken
  48. policyLoaderOpts.ProjectID = projID
  49. } else {
  50. projID := reqScopes[types.ProjectScope].Resource.UInt
  51. user, _ := r.Context().Value(types.UserScope).(*models.User)
  52. policyLoaderOpts.ProjectID = projID
  53. policyLoaderOpts.UserID = user.ID
  54. }
  55. // load policy documents for the user + project
  56. policyDocs, reqErr := h.loader.LoadPolicyDocuments(policyLoaderOpts)
  57. if reqErr != nil {
  58. apierrors.HandleAPIError(h.config.Logger, h.config.Alerter, w, r, reqErr, true)
  59. return
  60. }
  61. // validate that the policy permits the action
  62. hasAccess := policy.HasScopeAccess(policyDocs, reqScopes)
  63. if !hasAccess {
  64. apierrors.HandleAPIError(
  65. h.config.Logger,
  66. h.config.Alerter,
  67. w,
  68. r,
  69. apierrors.NewErrForbidden(fmt.Errorf("policy forbids action in project %d", policyLoaderOpts.ProjectID)),
  70. true,
  71. )
  72. return
  73. }
  74. // add the set of resource ids to the request context
  75. ctx := NewRequestScopeCtx(r.Context(), reqScopes)
  76. r = r.Clone(ctx)
  77. h.next.ServeHTTP(w, r)
  78. }
  79. func NewRequestScopeCtx(ctx context.Context, reqScopes map[types.PermissionScope]*types.RequestAction) context.Context {
  80. return context.WithValue(ctx, types.RequestScopeCtxKey, reqScopes)
  81. }
  82. func getRequestActionForEndpoint(
  83. r *http.Request,
  84. endpointMeta types.APIRequestMetadata,
  85. ) (res map[types.PermissionScope]*types.RequestAction, reqErr apierrors.RequestError) {
  86. res = make(map[types.PermissionScope]*types.RequestAction)
  87. // iterate through scopes, attach policies as needed
  88. for _, scope := range endpointMeta.Scopes {
  89. // find the resource ID and create the resource
  90. resource := types.NameOrUInt{}
  91. switch scope {
  92. case types.ProjectScope:
  93. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamProjectID)
  94. case types.ClusterScope:
  95. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamClusterID)
  96. case types.RegistryScope:
  97. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamRegistryID)
  98. case types.HelmRepoScope:
  99. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamHelmRepoID)
  100. case types.GitInstallationScope:
  101. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamGitInstallationID)
  102. case types.InfraScope:
  103. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamInfraID)
  104. case types.OperationScope:
  105. resource.Name, reqErr = requestutils.GetURLParamString(r, types.URLParamOperationID)
  106. case types.NamespaceScope:
  107. resource.Name, reqErr = requestutils.GetURLParamString(r, types.URLParamNamespace)
  108. case types.ReleaseScope:
  109. resource.Name, reqErr = requestutils.GetURLParamString(r, types.URLParamReleaseName)
  110. case types.StackScope:
  111. resource.Name, reqErr = requestutils.GetURLParamString(r, types.URLParamStackID)
  112. case types.InviteScope:
  113. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamInviteID)
  114. case types.GitlabIntegrationScope:
  115. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamIntegrationID)
  116. case types.APIContractRevisionScope:
  117. resource.Name, reqErr = requestutils.GetURLParamString(r, types.URLParamAPIContractRevisionID)
  118. }
  119. if reqErr != nil {
  120. return nil, reqErr
  121. }
  122. res[scope] = &types.RequestAction{
  123. Verb: endpointMeta.Verb,
  124. Resource: resource,
  125. }
  126. }
  127. return res, nil
  128. }