helpers.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package gitinstallation
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. ghinstallation "github.com/bradleyfalzon/ghinstallation/v2"
  7. "github.com/google/go-github/v41/github"
  8. "github.com/porter-dev/porter/api/server/handlers"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. "github.com/porter-dev/porter/api/server/shared/requestutils"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/porter-dev/porter/internal/models/integrations"
  15. "github.com/porter-dev/porter/internal/oauth"
  16. "golang.org/x/oauth2"
  17. )
  18. // GetGithubAppOauthTokenFromRequest gets the GH oauth token from the request based on the currently
  19. // logged in user
  20. func GetGithubAppOauthTokenFromRequest(config *config.Config, r *http.Request) (*oauth2.Token, error) {
  21. // read the user from context
  22. user, _ := r.Context().Value(types.UserScope).(*models.User)
  23. getOAuthInt := config.Repo.GithubAppOAuthIntegration().ReadGithubAppOauthIntegration
  24. oauthInt, err := getOAuthInt(user.GithubAppIntegrationID)
  25. if err != nil {
  26. return nil, err
  27. }
  28. _, _, err = oauth.GetAccessToken(oauthInt.SharedOAuthModel,
  29. &config.GithubAppConf.Config,
  30. oauth.MakeUpdateGithubAppOauthIntegrationFunction(oauthInt, config.Repo),
  31. )
  32. if err != nil {
  33. // try again, in case the token got updated
  34. oauthInt2, err := getOAuthInt(user.GithubAppIntegrationID)
  35. if err != nil || oauthInt2.Expiry == oauthInt.Expiry {
  36. return nil, err
  37. }
  38. oauthInt.AccessToken = oauthInt2.AccessToken
  39. oauthInt.RefreshToken = oauthInt2.RefreshToken
  40. oauthInt.Expiry = oauthInt2.Expiry
  41. }
  42. return &oauth2.Token{
  43. AccessToken: string(oauthInt.AccessToken),
  44. RefreshToken: string(oauthInt.RefreshToken),
  45. Expiry: oauthInt.Expiry,
  46. TokenType: "Bearer",
  47. }, nil
  48. }
  49. // GetGithubAppClientFromRequest gets the github app installation id from the request and authenticates
  50. // using it and a private key file
  51. func GetGithubAppClientFromRequest(config *config.Config, r *http.Request) (*github.Client, error) {
  52. // get installation id from context
  53. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  54. itr, err := ghinstallation.NewKeyFromFile(
  55. http.DefaultTransport,
  56. config.GithubAppConf.AppID,
  57. ga.InstallationID,
  58. config.GithubAppConf.SecretPath,
  59. )
  60. if err != nil {
  61. return nil, err
  62. }
  63. return github.NewClient(&http.Client{Transport: itr}), nil
  64. }
  65. type GithubAppPermissions struct {
  66. Actions string
  67. Administration string
  68. Contents string
  69. Deployments string
  70. Environments string
  71. Metadata string
  72. PullRequests string
  73. Secrets string
  74. Workflows string
  75. RepositoryWebhook string
  76. }
  77. // GetGithubAppClientFromRequest gets the github app installation id from the request and authenticates
  78. // using it and a private key file
  79. func GetGithubAppPermissions(config *config.Config, r *http.Request) (*GithubAppPermissions, error) {
  80. // get installation id from context
  81. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  82. itr, err := ghinstallation.NewKeyFromFile(
  83. http.DefaultTransport,
  84. config.GithubAppConf.AppID,
  85. ga.InstallationID,
  86. config.GithubAppConf.SecretPath,
  87. )
  88. if err != nil {
  89. return nil, err
  90. }
  91. // need to request the token before permissions can be verified
  92. _, err = itr.Token(context.Background())
  93. if err != nil {
  94. return nil, err
  95. }
  96. permissions, err := itr.Permissions()
  97. return &GithubAppPermissions{
  98. Actions: permissionToString(permissions.Actions),
  99. Administration: permissionToString(permissions.Administration),
  100. Contents: permissionToString(permissions.Contents),
  101. Deployments: permissionToString(permissions.Deployments),
  102. Environments: permissionToString(permissions.Environments),
  103. Metadata: permissionToString(permissions.Metadata),
  104. PullRequests: permissionToString(permissions.PullRequests),
  105. Secrets: permissionToString(permissions.Secrets),
  106. Workflows: permissionToString(permissions.Workflows),
  107. RepositoryWebhook: permissionToString(permissions.RepositoryHooks),
  108. }, err
  109. }
  110. func permissionToString(permission *string) string {
  111. if permission == nil {
  112. return ""
  113. }
  114. return *permission
  115. }
  116. // GetOwnerAndNameParams gets the owner and name ref for the Github repo
  117. func GetOwnerAndNameParams(c handlers.PorterHandler, w http.ResponseWriter, r *http.Request) (string, string, bool) {
  118. owner, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoOwner)
  119. if reqErr != nil {
  120. c.HandleAPIError(w, r, reqErr)
  121. return "", "", false
  122. }
  123. name, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoName)
  124. if reqErr != nil {
  125. c.HandleAPIError(w, r, reqErr)
  126. return "", "", false
  127. }
  128. return owner, name, true
  129. }
  130. // GetBranch gets the unencoded branch
  131. func GetBranch(c handlers.PorterHandler, w http.ResponseWriter, r *http.Request) (string, bool) {
  132. branch, reqErr := requestutils.GetURLParamString(r, types.URLParamGitBranch)
  133. if reqErr != nil {
  134. c.HandleAPIError(w, r, reqErr)
  135. return "", false
  136. }
  137. branch, err := url.QueryUnescape(branch)
  138. if reqErr != nil {
  139. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  140. return "", false
  141. }
  142. return branch, true
  143. }