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
37 changes: 25 additions & 12 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"strings"
"time"

"github.com/argoproj/argo-cd/util/healthz"
"github.com/argoproj/argo-cd/util/swagger"
"github.com/argoproj/argo-cd/util/webhook"

"github.com/dgrijalva/jwt-go"
rediscache "github.com/go-redis/cache"
"github.com/go-redis/redis"
Expand Down Expand Up @@ -82,7 +86,6 @@ import (
dexutil "github.com/argoproj/argo-cd/util/dex"
"github.com/argoproj/argo-cd/util/env"
grpc_util "github.com/argoproj/argo-cd/util/grpc"
"github.com/argoproj/argo-cd/util/healthz"
httputil "github.com/argoproj/argo-cd/util/http"
jsonutil "github.com/argoproj/argo-cd/util/json"
"github.com/argoproj/argo-cd/util/jwt/zjwt"
Expand All @@ -91,9 +94,7 @@ import (
"github.com/argoproj/argo-cd/util/rbac"
util_session "github.com/argoproj/argo-cd/util/session"
settings_util "github.com/argoproj/argo-cd/util/settings"
"github.com/argoproj/argo-cd/util/swagger"
tlsutil "github.com/argoproj/argo-cd/util/tls"
"github.com/argoproj/argo-cd/util/webhook"
)

const (
Expand Down Expand Up @@ -236,15 +237,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
}
}

Expand Down Expand Up @@ -555,12 +557,18 @@ 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))

healthz.ServeHealthCheck(mux, func() error {
_, err := a.KubeClientset.(*kubernetes.Clientset).ServerVersion()
return err
})

return mux
}

Expand Down Expand Up @@ -622,7 +630,7 @@ func (a *ArgoCDServer) newHTTPServer(ctx context.Context, port int, grpcWebHandl
mustRegisterGWHandler(certificatepkg.RegisterCertificateServiceHandlerFromEndpoint, ctx, gwmux, endpoint, dOpts)

// Swagger UI
swagger.ServeSwaggerUI(mux, assets.SwaggerJSON, "/swagger-ui")
swagger.ServeSwaggerUI(mux, assets.SwaggerJSON, "/swagger-ui", a.RootPath)
healthz.ServeHealthCheck(mux, func() error {
_, err := a.KubeClientset.(*kubernetes.Clientset).ServerVersion()
return err
Expand Down Expand Up @@ -664,11 +672,16 @@ 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
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
}
Expand Down
8 changes: 4 additions & 4 deletions util/swagger/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
)

// ServeSwaggerUI serves the Swagger UI and JSON spec.
func ServeSwaggerUI(mux *http.ServeMux, swaggerJSON string, uiPath string) {
func ServeSwaggerUI(mux *http.ServeMux, swaggerJSON string, uiPath string, rootPath string) {
prefix := path.Dir(uiPath)
specURL := path.Join(prefix, "swagger.json")

mux.HandleFunc(specURL, func(w http.ResponseWriter, r *http.Request) {
swaggerPath := path.Join(prefix, "swagger.json")
mux.HandleFunc(swaggerPath, func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, swaggerJSON)
})

specURL := path.Join(prefix, rootPath, "swagger.json")
mux.Handle(uiPath, middleware.Redoc(middleware.RedocOpts{
BasePath: prefix,
SpecURL: specURL,
Expand Down
2 changes: 1 addition & 1 deletion util/swagger/swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestSwaggerUI(t *testing.T) {
c <- listener.Addr().String()

mux := http.NewServeMux()
ServeSwaggerUI(mux, assets.SwaggerJSON, "/swagger-ui")
ServeSwaggerUI(mux, assets.SwaggerJSON, "/swagger-ui", "")
panic(http.Serve(listener, mux))
}

Expand Down