get_gitlab_repo_procfile.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. accessToken, _, err := oauth.GetAccessToken(giAppOAuth.SharedOAuthModel, commonutils.GetGitlabOAuthConf(
  86. p.Config(), gi,
  87. ), oauth.MakeUpdateGitlabAppOAuthIntegrationFunction(giAppOAuth, p.Repo()))
  88. if err != nil {
  89. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("invalid gitlab access token"),
  90. http.StatusUnauthorized))
  91. return
  92. }
  93. client, err := gitlab.NewOAuthClient(accessToken, gitlab.WithBaseURL(gi.InstanceURL))
  94. if err != nil {
  95. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  96. return
  97. }
  98. file, resp, err := client.RepositoryFiles.GetRawFile(fmt.Sprintf("%s/%s", owner, name),
  99. strings.TrimPrefix(path, "./"), &gitlab.GetRawFileOptions{
  100. Ref: gitlab.String(branch),
  101. },
  102. )
  103. if resp.StatusCode == http.StatusUnauthorized {
  104. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unauthorized gitlab user"), http.StatusUnauthorized))
  105. return
  106. } else if resp.StatusCode == http.StatusNotFound {
  107. w.WriteHeader(http.StatusNotFound)
  108. p.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("no such procfile exists")))
  109. return
  110. }
  111. if err != nil {
  112. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  113. return
  114. }
  115. parsedContents := make(types.GetProcfileResponse)
  116. // parse the procfile information
  117. for _, line := range strings.Split(string(file), "\n") {
  118. if matches := procfileRegex.FindStringSubmatch(line); matches != nil {
  119. parsedContents[matches[1]] = matches[2]
  120. }
  121. }
  122. p.WriteResult(w, r, parsedContents)
  123. }