Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: to support --rootpath #3503

Merged
merged 9 commits into from
Apr 29, 2020
28 changes: 19 additions & 9 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,16 @@ func (a *ArgoCDServer) Run(ctx context.Context, port int, metricsPort int) {
var httpS *http.Server
var httpsS *http.Server
if a.useTLS() {
httpS = newRedirectServer(port)
httpS = newRedirectServer(port, a.RootPath)
httpsS = a.newHTTPServer(ctx, port, grpcWebS)
} else {
httpS = a.newHTTPServer(ctx, port, grpcWebS)
}
if a.RootPath != "" {
httpS.Handler = withRootPath(httpS.Handler, a.RootPath)
httpS.Handler = withRootPath(httpS.Handler, a)

if httpsS != nil {
httpsS.Handler = withRootPath(httpsS.Handler, a.RootPath)
httpsS.Handler = withRootPath(httpsS.Handler, a)
alexmt marked this conversation as resolved.
Show resolved Hide resolved
}
}
metricsServ := newAPIServerMetricsServer(metricsPort)
Expand Down Expand Up @@ -522,7 +523,8 @@ func (a *ArgoCDServer) newGRPCServer() *grpc.Server {
// TranslateGrpcCookieHeader conditionally sets a cookie on the response.
func (a *ArgoCDServer) translateGrpcCookieHeader(ctx context.Context, w http.ResponseWriter, resp golang_proto.Message) error {
if sessionResp, ok := resp.(*sessionpkg.SessionResponse); ok {
flags := []string{"path=/", "SameSite=lax", "httpOnly"}
cookiePath := fmt.Sprintf("path=/%s", strings.TrimRight(strings.TrimLeft(a.ArgoCDServerOpts.RootPath, "/"), "/"))
flags := []string{cookiePath, "SameSite=lax", "httpOnly"}
if !a.Insecure {
flags = append(flags, "Secure")
}
Expand All @@ -543,12 +545,15 @@ func (a *ArgoCDServer) translateGrpcCookieHeader(ctx context.Context, w http.Res
return nil
}

func withRootPath(handler http.Handler, root string) http.Handler {
func withRootPath(handler http.Handler, a *ArgoCDServer) http.Handler {
// get rid of slashes
root = strings.TrimRight(strings.TrimLeft(root, "/"), "/")
root := strings.TrimRight(strings.TrimLeft(a.RootPath, "/"), "/")

mux := http.NewServeMux()
mux.Handle("/"+root+"/", http.StripPrefix("/"+root, handler))

additionalHandler(mux, a)

return mux
}

Expand Down Expand Up @@ -609,6 +614,11 @@ func (a *ArgoCDServer) newHTTPServer(ctx context.Context, port int, grpcWebHandl
mustRegisterGWHandler(accountpkg.RegisterAccountServiceHandlerFromEndpoint, ctx, gwmux, endpoint, dOpts)
mustRegisterGWHandler(certificatepkg.RegisterCertificateServiceHandlerFromEndpoint, ctx, gwmux, endpoint, dOpts)

additionalHandler(mux, a)
return &httpS
}

func additionalHandler(mux *http.ServeMux, a *ArgoCDServer) {
// Swagger UI
swagger.ServeSwaggerUI(mux, assets.SwaggerJSON, "/swagger-ui")
healthz.ServeHealthCheck(mux, func() error {
Expand All @@ -630,7 +640,6 @@ func (a *ArgoCDServer) newHTTPServer(ctx context.Context, port int, grpcWebHandl
if a.StaticAssetsDir != "" {
mux.HandleFunc("/", a.newStaticAssetsHandler(a.StaticAssetsDir, a.BaseHRef))
}
return &httpS
}

// registerDexHandlers will register dex HTTP handlers, creating the the OAuth client app
Expand All @@ -652,9 +661,10 @@ func (a *ArgoCDServer) registerDexHandlers(mux *http.ServeMux) {
}

// newRedirectServer returns an HTTP server which does a 307 redirect to the HTTPS server
func newRedirectServer(port int) *http.Server {
func newRedirectServer(port int, rootPath string) *http.Server {
addr := fmt.Sprintf("localhost:%d/%s/", port, strings.TrimRight(strings.TrimLeft(rootPath, "/"), "/"))
return &http.Server{
Addr: fmt.Sprintf("localhost:%d", port),
Addr: addr,
Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
target := "https://" + req.Host + req.URL.Path
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server still redirects to root path. The changes have to be implemented in handler implementation. E.g.

		Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
			target := "https://" + req.Host
			if rootPath != "" {
				target += strings.TrimRight(strings.TrimLeft(rootPath, "/"), "/")
			}
			target += req.URL.Path
			if len(req.URL.RawQuery) > 0 {
				target += "?" + req.URL.RawQuery
			}
			http.Redirect(w, req, target, http.StatusTemporaryRedirect)
		}),

if len(req.URL.RawQuery) > 0 {
Expand Down