policy.go 3.9 KB

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