git_installation.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package authz
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/google/go-github/github"
  7. "golang.org/x/oauth2"
  8. "github.com/porter-dev/porter/api/server/shared/apierrors"
  9. "github.com/porter-dev/porter/api/server/shared/config"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/internal/models"
  12. "github.com/porter-dev/porter/internal/models/integrations"
  13. "github.com/porter-dev/porter/internal/oauth"
  14. )
  15. type GitInstallationScopedFactory struct {
  16. config *config.Config
  17. }
  18. func NewGitInstallationScopedFactory(
  19. config *config.Config,
  20. ) *GitInstallationScopedFactory {
  21. return &GitInstallationScopedFactory{config}
  22. }
  23. func (p *GitInstallationScopedFactory) Middleware(next http.Handler) http.Handler {
  24. return &GitInstallationScopedMiddleware{next, p.config}
  25. }
  26. type GitInstallationScopedMiddleware struct {
  27. next http.Handler
  28. config *config.Config
  29. }
  30. func (p *GitInstallationScopedMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  31. // read the user to perform authorization
  32. user, _ := r.Context().Value(types.UserScope).(*models.User)
  33. // get the registry id from the URL param context
  34. reqScopes, _ := r.Context().Value(types.RequestScopeCtxKey).(map[types.PermissionScope]*types.RequestAction)
  35. gitInstallationID := reqScopes[types.GitInstallationScope].Resource.UInt
  36. gitInstallation, err := p.config.Repo.GithubAppInstallation().ReadGithubAppInstallationByInstallationID(gitInstallationID)
  37. if err != nil {
  38. apierrors.HandleAPIError(p.config, w, r, apierrors.NewErrInternal(err), true)
  39. return
  40. }
  41. if err := p.doesUserHaveGitInstallationAccess(user.GithubAppIntegrationID, gitInstallationID); err != nil {
  42. apierrors.HandleAPIError(p.config, w, r, apierrors.NewErrInternal(err), true)
  43. return
  44. }
  45. ctx := NewGitInstallationContext(r.Context(), gitInstallation)
  46. r = r.Clone(ctx)
  47. p.next.ServeHTTP(w, r)
  48. }
  49. func NewGitInstallationContext(ctx context.Context, ga *integrations.GithubAppInstallation) context.Context {
  50. return context.WithValue(ctx, types.GitInstallationScope, ga)
  51. }
  52. // DoesUserHaveGitInstallationAccess checks that a user has access to an installation id
  53. // by ensuring the installation id exists for one org or account they have access to
  54. // note that this makes a github API request, but the endpoint is fast so this doesn't add
  55. // much overhead
  56. func (p *GitInstallationScopedMiddleware) doesUserHaveGitInstallationAccess(githubIntegrationID, gitInstallationID uint) error {
  57. oauthInt, err := p.config.Repo.GithubAppOAuthIntegration().ReadGithubAppOauthIntegration(githubIntegrationID)
  58. if err != nil {
  59. return err
  60. }
  61. if p.config.GithubAppConf == nil {
  62. return fmt.Errorf("config has invalid GithubAppConf")
  63. }
  64. if _, _, err = oauth.GetAccessToken(oauthInt.SharedOAuthModel,
  65. &p.config.GithubAppConf.Config,
  66. oauth.MakeUpdateGithubAppOauthIntegrationFunction(oauthInt, p.config.Repo)); err != nil {
  67. return err
  68. }
  69. client := github.NewClient(p.config.GithubConf.Client(oauth2.NoContext, &oauth2.Token{
  70. AccessToken: string(oauthInt.AccessToken),
  71. RefreshToken: string(oauthInt.RefreshToken),
  72. TokenType: "Bearer",
  73. }))
  74. accountIDs := make([]int64, 0)
  75. AuthUser, _, err := client.Users.Get(context.Background(), "")
  76. if err != nil {
  77. return err
  78. }
  79. accountIDs = append(accountIDs, *AuthUser.ID)
  80. opts := &github.ListOptions{
  81. PerPage: 100,
  82. Page: 1,
  83. }
  84. for {
  85. orgs, pages, err := client.Organizations.List(context.Background(), "", opts)
  86. if err != nil {
  87. return err
  88. }
  89. for _, org := range orgs {
  90. accountIDs = append(accountIDs, *org.ID)
  91. }
  92. if pages.NextPage == 0 {
  93. break
  94. }
  95. }
  96. installations, err := p.config.Repo.GithubAppInstallation().ReadGithubAppInstallationByAccountIDs(accountIDs)
  97. for _, installation := range installations {
  98. if uint(installation.InstallationID) == gitInstallationID {
  99. return nil
  100. }
  101. }
  102. return apierrors.NewErrForbidden(
  103. fmt.Errorf("user does not have access to github app installation %d", gitInstallationID),
  104. )
  105. }