Skip to content

Commit

Permalink
fix(proxy): properly redirect from /gitlab urls (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
olevski authored Aug 22, 2023
1 parent 5ab4447 commit 2fac96f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/revproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func setupServer(ctx context.Context, config revProxyConfig) *echo.Echo {
// Routes that end up proxied to Gitlab
if config.ExternalGitlabURL != nil {
// Redirect "old" style bundled /gitlab pathing if an external Gitlab is used
e.Group("/gitlab", logger, stripPrefix("/gitlab"), gitlabProxyHost, gitlabProxy)
e.Group("/gitlab", logger, gitlabRedirect(config.ExternalGitlabURL.Host))
e.Group("/api/graphql", logger, gitlabAuth, gitlabProxyHost, gitlabProxy)
e.Group("/api/direct", logger, stripPrefix("/api/direct"), gitlabProxyHost, gitlabProxy)
e.Group("/repos", logger, cliGitlabAuth, noCookies, stripPrefix("/repos"), gitlabProxyHost, gitlabProxy)
Expand Down
1 change: 1 addition & 0 deletions cmd/revproxy/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func ParametrizedRouteTest(scenario TestCase) func(*testing.T) {
}
reqURL.RawQuery = reqURLQuery.Encode()
res, err := http.Get(reqURL.String())
assert.NoError(t, err)
reqs := requestTracker.getAllRequests()

// Assert the request was routed as expected
Expand Down
14 changes: 14 additions & 0 deletions cmd/revproxy/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,17 @@ func checkCoreServiceMetadataVersion(coreSvcPaths []string) echo.MiddlewareFunc
}
}
}

// gitlabRedirect redirects from the old-style internal gitlab url to an external Gitlab instance
func gitlabRedirect(newGitlabHost string) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
oldURL := c.Request().URL
newURL := *oldURL
newURL.Host = newGitlabHost
newURL.Path = strings.TrimPrefix(newURL.Path, "/gitlab")
newURL.RawPath = strings.TrimPrefix(newURL.RawPath, "/gitlab")
return c.Redirect(http.StatusMovedPermanently, newURL.String())
}
}
}

0 comments on commit 2fac96f

Please sign in to comment.