get_gitlab_repo_procfile.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package project_integration
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "regexp"
  8. "strings"
  9. "github.com/porter-dev/porter/api/server/handlers"
  10. "github.com/porter-dev/porter/api/server/shared"
  11. "github.com/porter-dev/porter/api/server/shared/apierrors"
  12. "github.com/porter-dev/porter/api/server/shared/commonutils"
  13. "github.com/porter-dev/porter/api/server/shared/config"
  14. "github.com/porter-dev/porter/api/server/shared/requestutils"
  15. "github.com/porter-dev/porter/api/types"
  16. "github.com/porter-dev/porter/internal/models"
  17. "github.com/porter-dev/porter/internal/oauth"
  18. "github.com/xanzy/go-gitlab"
  19. "gorm.io/gorm"
  20. )
  21. var procfileRegex = regexp.MustCompile("^([A-Za-z0-9_]+):\\s*(.+)$")
  22. type GetGitlabRepoProcfileHandler struct {
  23. handlers.PorterHandlerReadWriter
  24. }
  25. func NewGetGitlabRepoProcfileHandler(
  26. config *config.Config,
  27. decoderValidator shared.RequestDecoderValidator,
  28. writer shared.ResultWriter,
  29. ) *GetGitlabRepoProcfileHandler {
  30. return &GetGitlabRepoProcfileHandler{
  31. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  32. }
  33. }
  34. func (p *GetGitlabRepoProcfileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  35. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  36. user, _ := r.Context().Value(types.UserScope).(*models.User)
  37. request := &types.GetProcfileRequest{}
  38. ok := p.DecodeAndValidate(w, r, request)
  39. if !ok {
  40. return
  41. }
  42. integrationID, reqErr := requestutils.GetURLParamUint(r, "integration_id")
  43. if reqErr != nil {
  44. p.HandleAPIError(w, r, apierrors.NewErrInternal(reqErr))
  45. return
  46. }
  47. owner, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoOwner)
  48. if reqErr != nil {
  49. p.HandleAPIError(w, r, apierrors.NewErrInternal(reqErr))
  50. return
  51. }
  52. name, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoName)
  53. if reqErr != nil {
  54. p.HandleAPIError(w, r, apierrors.NewErrInternal(reqErr))
  55. return
  56. }
  57. branch, reqErr := requestutils.GetURLParamString(r, types.URLParamGitBranch)
  58. if reqErr != nil {
  59. p.HandleAPIError(w, r, apierrors.NewErrInternal(reqErr))
  60. return
  61. }
  62. path, err := url.QueryUnescape(request.Path)
  63. if err != nil {
  64. p.HandleAPIError(w, r, apierrors.NewErrForbidden(fmt.Errorf("malformed query param path")))
  65. return
  66. }
  67. gi, err := p.Repo().GitlabIntegration().ReadGitlabIntegration(project.ID, integrationID)
  68. if err != nil {
  69. if errors.Is(err, gorm.ErrRecordNotFound) {
  70. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("no gitlab integration with ID: %d", integrationID), http.StatusNotFound))
  71. return
  72. }
  73. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  74. return
  75. }
  76. giAppOAuth, err := p.Repo().GitlabAppOAuthIntegration().ReadGitlabAppOAuthIntegration(user.ID, project.ID, integrationID)
  77. if err != nil {
  78. if errors.Is(err, gorm.ErrRecordNotFound) {
  79. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unauthorized gitlab user"), http.StatusUnauthorized))
  80. return
  81. }
  82. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  83. return
  84. }
  85. oauthInt, err := p.Repo().OAuthIntegration().ReadOAuthIntegration(project.ID, giAppOAuth.OAuthIntegrationID)
  86. if err != nil {
  87. if errors.Is(err, gorm.ErrRecordNotFound) {
  88. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unauthorized gitlab user"), http.StatusUnauthorized))
  89. return
  90. }
  91. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  92. return
  93. }
  94. accessToken, _, err := oauth.GetAccessToken(oauthInt.SharedOAuthModel, commonutils.GetGitlabOAuthConf(
  95. p.Config(), gi,
  96. ), oauth.MakeUpdateGitlabAppOAuthIntegrationFunction(project.ID, giAppOAuth, p.Repo()))
  97. if err != nil {
  98. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("invalid gitlab access token"),
  99. http.StatusUnauthorized))
  100. return
  101. }
  102. client, err := gitlab.NewOAuthClient(accessToken, gitlab.WithBaseURL(gi.InstanceURL))
  103. if err != nil {
  104. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  105. return
  106. }
  107. file, resp, err := client.RepositoryFiles.GetRawFile(fmt.Sprintf("%s/%s", owner, name),
  108. strings.TrimPrefix(path, "./"), &gitlab.GetRawFileOptions{
  109. Ref: gitlab.String(branch),
  110. },
  111. )
  112. if resp.StatusCode == http.StatusUnauthorized {
  113. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unauthorized gitlab user"), http.StatusUnauthorized))
  114. return
  115. } else if resp.StatusCode == http.StatusNotFound {
  116. w.WriteHeader(http.StatusNotFound)
  117. p.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("no such procfile exists")))
  118. return
  119. }
  120. if err != nil {
  121. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  122. return
  123. }
  124. parsedContents := make(types.GetProcfileResponse)
  125. // parse the procfile information
  126. for _, line := range strings.Split(string(file), "\n") {
  127. if matches := procfileRegex.FindStringSubmatch(line); matches != nil {
  128. parsedContents[matches[1]] = matches[2]
  129. }
  130. }
  131. p.WriteResult(w, r, parsedContents)
  132. }