diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-03-23 15:09:33 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-03-23 15:09:33 +0300 |
commit | b38fc20ae0e90d5b1c538a139aa0a7da1b7b5726 (patch) | |
tree | 3ce77cdb707b75c9d74c6ff2a8386dd06bd48b44 /workhorse/internal | |
parent | b3647b2a67930e8aa3c1b1dd9bda29c368c862ba (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'workhorse/internal')
-rw-r--r-- | workhorse/internal/api/api.go | 30 | ||||
-rw-r--r-- | workhorse/internal/upstream/routes.go | 26 |
2 files changed, 43 insertions, 13 deletions
diff --git a/workhorse/internal/api/api.go b/workhorse/internal/api/api.go index a420288a95a..e434a847bf2 100644 --- a/workhorse/internal/api/api.go +++ b/workhorse/internal/api/api.go @@ -151,7 +151,7 @@ type Response struct { MaximumSize int64 } -// singleJoiningSlash is taken from reverseproxy.go:NewSingleHostReverseProxy +// singleJoiningSlash is taken from reverseproxy.go:singleJoiningSlash func singleJoiningSlash(a, b string) string { aslash := strings.HasSuffix(a, "/") bslash := strings.HasPrefix(b, "/") @@ -164,14 +164,36 @@ func singleJoiningSlash(a, b string) string { return a + b } +// joinURLPath is taken from reverseproxy.go:joinURLPath +func joinURLPath(a *url.URL, b string) (path string, rawpath string) { + if a.RawPath == "" && b == "" { + return singleJoiningSlash(a.Path, b), "" + } + + // Same as singleJoiningSlash, but uses EscapedPath to determine + // whether a slash should be added + apath := a.EscapedPath() + bpath := b + + aslash := strings.HasSuffix(apath, "/") + bslash := strings.HasPrefix(bpath, "/") + + switch { + case aslash && bslash: + return a.Path + bpath[1:], apath + bpath[1:] + case !aslash && !bslash: + return a.Path + "/" + bpath, apath + "/" + bpath + } + return a.Path + bpath, apath + bpath +} + // rebaseUrl is taken from reverseproxy.go:NewSingleHostReverseProxy func rebaseUrl(url *url.URL, onto *url.URL, suffix string) *url.URL { newUrl := *url newUrl.Scheme = onto.Scheme newUrl.Host = onto.Host - if suffix != "" { - newUrl.Path = singleJoiningSlash(url.Path, suffix) - } + newUrl.Path, newUrl.RawPath = joinURLPath(url, suffix) + if onto.RawQuery == "" || newUrl.RawQuery == "" { newUrl.RawQuery = onto.RawQuery + newUrl.RawQuery } else { diff --git a/workhorse/internal/upstream/routes.go b/workhorse/internal/upstream/routes.go index fb8a07a8031..b77cb06d1d0 100644 --- a/workhorse/internal/upstream/routes.go +++ b/workhorse/internal/upstream/routes.go @@ -57,6 +57,7 @@ const ( ciAPIPattern = `^/ci/api/` gitProjectPattern = `^/.+\.git/` projectPattern = `^/([^/]+/){1,}[^/]+/` + apiProjectPattern = apiPattern + `v4/projects/[^/]+/` // API: Projects can be encoded via group%2Fsubgroup%2Fproject snippetUploadPattern = `^/uploads/personal_snippet` userUploadPattern = `^/uploads/user` importPattern = `^/import/` @@ -253,32 +254,39 @@ func configureRoutes(u *upstream) { u.route("", apiPattern+`v4/jobs/request\z`, ciAPILongPolling), u.route("", ciAPIPattern+`v1/builds/register.json\z`, ciAPILongPolling), + // Not all API endpoints support encoded project IDs + // (e.g. `group%2Fproject`), but for the sake of consistency we + // use the apiProjectPattern regex throughout. API endpoints + // that do not support this will return 400 regardless of + // whether they are accelerated by Workhorse or not. See + // https://gitlab.com/gitlab-org/gitlab/-/merge_requests/56731. + // Maven Artifact Repository - u.route("PUT", apiPattern+`v4/projects/[0-9]+/packages/maven/`, upload.BodyUploader(api, signingProxy, preparers.packages)), + u.route("PUT", apiProjectPattern+`packages/maven/`, upload.BodyUploader(api, signingProxy, preparers.packages)), // Conan Artifact Repository u.route("PUT", apiPattern+`v4/packages/conan/`, upload.BodyUploader(api, signingProxy, preparers.packages)), - u.route("PUT", apiPattern+`v4/projects/[0-9]+/packages/conan/`, upload.BodyUploader(api, signingProxy, preparers.packages)), + u.route("PUT", apiProjectPattern+`packages/conan/`, upload.BodyUploader(api, signingProxy, preparers.packages)), // Generic Packages Repository - u.route("PUT", apiPattern+`v4/projects/[0-9]+/packages/generic/`, upload.BodyUploader(api, signingProxy, preparers.packages)), + u.route("PUT", apiProjectPattern+`packages/generic/`, upload.BodyUploader(api, signingProxy, preparers.packages)), // NuGet Artifact Repository - u.route("PUT", apiPattern+`v4/projects/[0-9]+/packages/nuget/`, upload.Accelerate(api, signingProxy, preparers.packages)), + u.route("PUT", apiProjectPattern+`packages/nuget/`, upload.Accelerate(api, signingProxy, preparers.packages)), // PyPI Artifact Repository - u.route("POST", apiPattern+`v4/projects/[0-9]+/packages/pypi`, upload.Accelerate(api, signingProxy, preparers.packages)), + u.route("POST", apiProjectPattern+`packages/pypi`, upload.Accelerate(api, signingProxy, preparers.packages)), // Debian Artifact Repository - u.route("PUT", apiPattern+`v4/projects/[0-9]+/packages/debian/`, upload.BodyUploader(api, signingProxy, preparers.packages)), + u.route("PUT", apiProjectPattern+`packages/debian/`, upload.BodyUploader(api, signingProxy, preparers.packages)), // Gem Artifact Repository - u.route("POST", apiPattern+`v4/projects/[0-9]+/packages/rubygems/`, upload.BodyUploader(api, signingProxy, preparers.packages)), + u.route("POST", apiProjectPattern+`packages/rubygems/`, upload.BodyUploader(api, signingProxy, preparers.packages)), // We are porting API to disk acceleration // we need to declare each routes until we have fixed all the routes on the rails codebase. // Overall status can be seen at https://gitlab.com/groups/gitlab-org/-/epics/1802#current-status - u.route("POST", apiPattern+`v4/projects/[0-9]+/wikis/attachments\z`, uploadAccelerateProxy), + u.route("POST", apiProjectPattern+`wikis/attachments\z`, uploadAccelerateProxy), u.route("POST", apiPattern+`graphql\z`, uploadAccelerateProxy), u.route("POST", apiPattern+`v4/groups/import`, upload.Accelerate(api, signingProxy, preparers.uploads)), u.route("POST", apiPattern+`v4/projects/import`, upload.Accelerate(api, signingProxy, preparers.uploads)), @@ -289,7 +297,7 @@ func configureRoutes(u *upstream) { u.route("POST", importPattern+`gitlab_group`, upload.Accelerate(api, signingProxy, preparers.uploads)), // Metric image upload - u.route("POST", apiPattern+`v4/projects/[0-9]+/issues/[0-9]+/metric_images\z`, upload.Accelerate(api, signingProxy, preparers.uploads)), + u.route("POST", apiProjectPattern+`issues/[0-9]+/metric_images\z`, upload.Accelerate(api, signingProxy, preparers.uploads)), // Requirements Import via UI upload acceleration u.route("POST", projectPattern+`requirements_management/requirements/import_csv`, upload.Accelerate(api, signingProxy, preparers.uploads)), |