Skip to content

Commit 83b6b6e

Browse files
authoredMar 20, 2019
KIALI-2548 if the root context path is not "/", and a request comes in for "/", help the user out by forwarding to the true context root. This means a request to "/" will never result in a 404 - it will be forwarded to the true root context. (kiali#930)
1 parent 53a1232 commit 83b6b6e

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed
 

‎routing/router.go

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ func NewRouter() *mux.Router {
2727
rootRouter.HandleFunc(webRoot, func(w http.ResponseWriter, r *http.Request) {
2828
http.Redirect(w, r, webRootWithSlash, http.StatusFound)
2929
})
30+
31+
// help the user out - if a request comes in for "/", redirect to our true webroot
32+
rootRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
33+
http.Redirect(w, r, webRootWithSlash, http.StatusFound)
34+
})
35+
3036
appRouter = rootRouter.PathPrefix(conf.Server.WebRoot).Subrouter()
3137
}
3238

‎server/server_test.go

+62-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"crypto/x509/pkix"
99
"encoding/pem"
1010
"fmt"
11-
"github.com/kiali/kiali/util"
1211
"io/ioutil"
1312
"math/big"
1413
"net"
@@ -20,6 +19,7 @@ import (
2019

2120
"github.com/kiali/kiali/config"
2221
"github.com/kiali/kiali/config/security"
22+
"github.com/kiali/kiali/util"
2323
)
2424

2525
const (
@@ -34,6 +34,67 @@ const (
3434

3535
var tmpDir = os.TempDir()
3636

37+
func TestRootContextPath(t *testing.T) {
38+
testPort, err := getFreePort(testHostname)
39+
if err != nil {
40+
t.Fatalf("Cannot get a free port to run tests on host [%v]", testHostname)
41+
} else {
42+
t.Logf("Will use free port [%v] on host [%v] for tests", testPort, testHostname)
43+
}
44+
45+
testServerHostPort := fmt.Sprintf("%v:%v", testHostname, testPort)
46+
testCustomRoot := "/customroot"
47+
48+
conf := new(config.Config)
49+
conf.Server.WebRoot = testCustomRoot
50+
conf.Server.Address = testHostname
51+
conf.Server.Port = testPort
52+
conf.Server.StaticContentRootDirectory = tmpDir
53+
conf.Server.Credentials.Username = "unused"
54+
conf.Server.Credentials.Passphrase = "unused"
55+
conf.Auth.Strategy = "anonymous"
56+
57+
serverURL := fmt.Sprintf("http://%v", testServerHostPort)
58+
59+
config.Set(conf)
60+
61+
server := NewServer()
62+
server.Start()
63+
t.Logf("Started test http server: %v", serverURL)
64+
defer func() {
65+
server.Stop()
66+
t.Logf("Stopped test server: %v", serverURL)
67+
}()
68+
69+
// the client
70+
httpConfig := httpClientConfig{}
71+
httpClient, err := httpConfig.buildHTTPClient()
72+
if err != nil {
73+
t.Fatalf("Failed to create http client")
74+
}
75+
76+
// no credentials
77+
noCredentials := &security.Credentials{}
78+
79+
// wait for our test http server to come up
80+
checkHTTPReady(httpClient, serverURL)
81+
82+
// we should be able to get to our custom web root
83+
if _, err = getRequestResults(t, httpClient, serverURL+testCustomRoot, noCredentials); err != nil {
84+
t.Fatalf("Failed: Shouldn't have failed going to the web root: %v", err)
85+
}
86+
87+
// we should be able to get to "/" root - this just forwards to our custom web root
88+
if _, err = getRequestResults(t, httpClient, serverURL, noCredentials); err != nil {
89+
t.Fatalf("Failed: Shouldn't have failed going to / root: %v", err)
90+
}
91+
92+
// sanity check - make sure we cannot get to a bogus context path
93+
if _, err = getRequestResults(t, httpClient, serverURL+"/badroot", noCredentials); err == nil {
94+
t.Fatalf("Failed: Should have failed going to /badroot")
95+
}
96+
}
97+
3798
func TestAnonymousMode(t *testing.T) {
3899
testPort, err := getFreePort(testHostname)
39100
if err != nil {

0 commit comments

Comments
 (0)
Please sign in to comment.