policy.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. apiToken, _ := r.Context().Value("api_token").(*models.APIToken)
  45. policyLoaderOpts.Token = apiToken
  46. policyLoaderOpts.ProjectID = apiToken.ProjectID
  47. } else {
  48. projID := reqScopes[types.ProjectScope].Resource.UInt
  49. user, _ := r.Context().Value(types.UserScope).(*models.User)
  50. policyLoaderOpts.ProjectID = projID
  51. policyLoaderOpts.UserID = user.ID
  52. }
  53. // load policy documents for the user + project
  54. policyDocs, reqErr := h.loader.LoadPolicyDocuments(policyLoaderOpts)
  55. if reqErr != nil {
  56. apierrors.HandleAPIError(h.config.Logger, h.config.Alerter, w, r, reqErr, true)
  57. return
  58. }
  59. // validate that the policy permits the action
  60. hasAccess := policy.HasScopeAccess(policyDocs, reqScopes)
  61. if !hasAccess {
  62. apierrors.HandleAPIError(
  63. h.config.Logger,
  64. h.config.Alerter,
  65. w,
  66. r,
  67. apierrors.NewErrForbidden(fmt.Errorf("policy forbids action in project %d", policyLoaderOpts.ProjectID)),
  68. true,
  69. )
  70. return
  71. }
  72. // add the set of resource ids to the request context
  73. ctx := NewRequestScopeCtx(r.Context(), reqScopes)
  74. r = r.Clone(ctx)
  75. h.next.ServeHTTP(w, r)
  76. }
  77. func NewRequestScopeCtx(ctx context.Context, reqScopes map[types.PermissionScope]*types.RequestAction) context.Context {
  78. return context.WithValue(ctx, types.RequestScopeCtxKey, reqScopes)
  79. }
  80. func getRequestActionForEndpoint(
  81. r *http.Request,
  82. endpointMeta types.APIRequestMetadata,
  83. ) (res map[types.PermissionScope]*types.RequestAction, reqErr apierrors.RequestError) {
  84. res = make(map[types.PermissionScope]*types.RequestAction)
  85. // iterate through scopes, attach policies as needed
  86. for _, scope := range endpointMeta.Scopes {
  87. // find the resource ID and create the resource
  88. resource := types.NameOrUInt{}
  89. switch scope {
  90. case types.ProjectScope:
  91. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamProjectID)
  92. case types.ClusterScope:
  93. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamClusterID)
  94. case types.RegistryScope:
  95. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamRegistryID)
  96. case types.HelmRepoScope:
  97. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamHelmRepoID)
  98. case types.GitInstallationScope:
  99. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamGitInstallationID)
  100. case types.InfraScope:
  101. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamInfraID)
  102. case types.OperationScope:
  103. resource.Name, reqErr = requestutils.GetURLParamString(r, types.URLParamOperationID)
  104. case types.NamespaceScope:
  105. resource.Name, reqErr = requestutils.GetURLParamString(r, types.URLParamNamespace)
  106. case types.ReleaseScope:
  107. resource.Name, reqErr = requestutils.GetURLParamString(r, types.URLParamReleaseName)
  108. case types.InviteScope:
  109. resource.UInt, reqErr = requestutils.GetURLParamUint(r, types.URLParamInviteID)
  110. }
  111. if reqErr != nil {
  112. return nil, reqErr
  113. }
  114. res[scope] = &types.RequestAction{
  115. Verb: endpointMeta.Verb,
  116. Resource: resource,
  117. }
  118. }
  119. return res, nil
  120. }