helpers.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. }
  76. // GetGithubAppClientFromRequest gets the github app installation id from the request and authenticates
  77. // using it and a private key file
  78. func GetGithubAppPermissions(config *config.Config, r *http.Request) (*GithubAppPermissions, error) {
  79. // get installation id from context
  80. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  81. itr, err := ghinstallation.NewKeyFromFile(
  82. http.DefaultTransport,
  83. config.GithubAppConf.AppID,
  84. ga.InstallationID,
  85. config.GithubAppConf.SecretPath,
  86. )
  87. if err != nil {
  88. return nil, err
  89. }
  90. // need to request the token before permissions can be verified
  91. _, err = itr.Token(context.Background())
  92. if err != nil {
  93. return nil, err
  94. }
  95. permissions, err := itr.Permissions()
  96. return &GithubAppPermissions{
  97. Actions: permissionToString(permissions.Actions),
  98. Administration: permissionToString(permissions.Administration),
  99. Contents: permissionToString(permissions.Contents),
  100. Deployments: permissionToString(permissions.Deployments),
  101. Environments: permissionToString(permissions.Environments),
  102. Metadata: permissionToString(permissions.Metadata),
  103. PullRequests: permissionToString(permissions.PullRequests),
  104. Secrets: permissionToString(permissions.Secrets),
  105. Workflows: permissionToString(permissions.Workflows),
  106. }, err
  107. }
  108. func permissionToString(permission *string) string {
  109. if permission == nil {
  110. return ""
  111. }
  112. return *permission
  113. }
  114. // GetOwnerAndNameParams gets the owner and name ref for the Github repo
  115. func GetOwnerAndNameParams(c handlers.PorterHandler, w http.ResponseWriter, r *http.Request) (string, string, bool) {
  116. owner, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoOwner)
  117. if reqErr != nil {
  118. c.HandleAPIError(w, r, reqErr)
  119. return "", "", false
  120. }
  121. name, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoName)
  122. if reqErr != nil {
  123. c.HandleAPIError(w, r, reqErr)
  124. return "", "", false
  125. }
  126. return owner, name, true
  127. }
  128. // GetBranch gets the unencoded branch
  129. func GetBranch(c handlers.PorterHandler, w http.ResponseWriter, r *http.Request) (string, bool) {
  130. branch, reqErr := requestutils.GetURLParamString(r, types.URLParamGitBranch)
  131. if reqErr != nil {
  132. c.HandleAPIError(w, r, reqErr)
  133. return "", false
  134. }
  135. branch, err := url.QueryUnescape(branch)
  136. if reqErr != nil {
  137. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  138. return "", false
  139. }
  140. return branch, true
  141. }