1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

caddyhttp: Add default SNI tests (#3146)

* added sni tests

* set the default sni when there is no host to match

* removed invalid sni test. Disabled tests that rely on host headers.

* readded SNI tests. Added logging of config load times
This commit is contained in:
Mark Sargent 2020-03-18 07:39:01 +13:00 committed by GitHub
parent f192ae5ea5
commit 3f48a2eb45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 116 additions and 11 deletions

View file

@ -530,6 +530,7 @@ func (st *ServerType) serversFromPairings(
"sni": caddyconfig.JSON(hosts, warnings), // make sure to match all hosts, not just auto-HTTPS-qualified ones "sni": caddyconfig.JSON(hosts, warnings), // make sure to match all hosts, not just auto-HTTPS-qualified ones
} }
} else { } else {
cp.DefaultSNI = defaultSNI
hasCatchAllTLSConnPolicy = true hasCatchAllTLSConnPolicy = true
} }

View file

@ -0,0 +1,23 @@
-----BEGIN CERTIFICATE-----
MIID5zCCAs8CFG4+w/pqR5AZQ+aVB330uRRRKMF0MA0GCSqGSIb3DQEBCwUAMIGv
MQswCQYDVQQGEwJVUzELMAkGA1UECAwCTlkxGzAZBgNVBAoMEkxvY2FsIERldmVs
b3BlbWVudDEbMBkGA1UEBwwSTG9jYWwgRGV2ZWxvcGVtZW50MRowGAYDVQQDDBFh
LmNhZGR5LmxvY2FsaG9zdDEbMBkGA1UECwwSTG9jYWwgRGV2ZWxvcGVtZW50MSAw
HgYJKoZIhvcNAQkBFhFhZG1pbkBjYWRkeS5sb2NhbDAeFw0yMDAzMTMxODUwMTda
Fw0zMDAzMTExODUwMTdaMIGvMQswCQYDVQQGEwJVUzELMAkGA1UECAwCTlkxGzAZ
BgNVBAoMEkxvY2FsIERldmVsb3BlbWVudDEbMBkGA1UEBwwSTG9jYWwgRGV2ZWxv
cGVtZW50MRowGAYDVQQDDBFhLmNhZGR5LmxvY2FsaG9zdDEbMBkGA1UECwwSTG9j
YWwgRGV2ZWxvcGVtZW50MSAwHgYJKo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U/VCApeuLX1TfWDpU8i1uK0zv3d5
oLKkT1i5KzTak3SEO9HgC1qf8PoS8tfUio26UICHe99rnHehOfivzEq+qNdgyF+A
M3BoeZMdgzcL5oh640k+Zte4LtDlddcWdhUhCepD7iPYrNNbQ3pkBwL2a9lRuOxc
7OC2IPECgYBH8f3OrwXjDltIG1dDvuDPNljxLZbFEFbQyVzMePYNftgZknAyGEdh
NW/LuWeTzstnmz/s6RE3jN5ZrrMa4sW77VA9+yU9QW2dkHqFyukQ4sfuNg6kDDNZ
+lqZYMCLw0M5P9fIbmnIYwey7tXkHfmzoCpnYHGQDN6hL0Bh0zGwmg==
-----END RSA PRIVATE KEY-----

View file

@ -45,6 +45,11 @@ type configLoadError struct {
func (e configLoadError) Error() string { return e.Response } func (e configLoadError) Error() string { return e.Response }
func timeElapsed(start time.Time, name string) {
elapsed := time.Since(start)
log.Printf("%s took %s", name, elapsed)
}
// InitServer this will configure the server with a configurion of a specific // InitServer this will configure the server with a configurion of a specific
// type. The configType must be either "json" or the adapter type. // type. The configType must be either "json" or the adapter type.
func InitServer(t *testing.T, rawConfig string, configType string) { func InitServer(t *testing.T, rawConfig string, configType string) {
@ -83,6 +88,8 @@ func initServer(t *testing.T, rawConfig string, configType string) error {
client := &http.Client{ client := &http.Client{
Timeout: time.Second * 2, Timeout: time.Second * 2,
} }
start := time.Now()
req, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/load", Default.AdminPort), strings.NewReader(rawConfig)) req, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/load", Default.AdminPort), strings.NewReader(rawConfig))
if err != nil { if err != nil {
t.Errorf("failed to create request. %s", err) t.Errorf("failed to create request. %s", err)
@ -100,6 +107,8 @@ func initServer(t *testing.T, rawConfig string, configType string) error {
t.Errorf("unable to contact caddy server. %s", err) t.Errorf("unable to contact caddy server. %s", err)
return err return err
} }
timeElapsed(start, "caddytest: config load time")
defer res.Body.Close() defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
@ -208,7 +217,7 @@ func AssertLoadError(t *testing.T, rawConfig string, configType string, expected
func AssertGetResponse(t *testing.T, requestURI string, statusCode int, expectedBody string) (*http.Response, string) { func AssertGetResponse(t *testing.T, requestURI string, statusCode int, expectedBody string) (*http.Response, string) {
resp, body := AssertGetResponseBody(t, requestURI, statusCode) resp, body := AssertGetResponseBody(t, requestURI, statusCode)
if !strings.Contains(body, expectedBody) { if !strings.Contains(body, expectedBody) {
t.Errorf("expected response body \"%s\" but got \"%s\"", expectedBody, body) t.Errorf("requesting \"%s\" expected response body \"%s\" but got \"%s\"", requestURI, expectedBody, body)
} }
return resp, string(body) return resp, string(body)
} }
@ -229,7 +238,7 @@ func AssertGetResponseBody(t *testing.T, requestURI string, expectedStatusCode i
defer resp.Body.Close() defer resp.Body.Close()
if expectedStatusCode != resp.StatusCode { if expectedStatusCode != resp.StatusCode {
t.Errorf("expected status code: %d but got %d", expectedStatusCode, resp.StatusCode) t.Errorf("requesting \"%s\" expected status code: %d but got %d", requestURI, expectedStatusCode, resp.StatusCode)
} }
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
@ -260,12 +269,12 @@ func AssertRedirect(t *testing.T, requestURI string, expectedToLocation string,
} }
if expectedStatusCode != resp.StatusCode { if expectedStatusCode != resp.StatusCode {
t.Errorf("expected status code: %d but got %d", expectedStatusCode, resp.StatusCode) t.Errorf("requesting \"%s\" expected status code: %d but got %d", requestURI, expectedStatusCode, resp.StatusCode)
} }
loc, err := resp.Location() loc, err := resp.Location()
if expectedToLocation != loc.String() { if expectedToLocation != loc.String() {
t.Errorf("expected location: \"%s\" but got \"%s\"", expectedToLocation, loc.String()) t.Errorf("requesting \"%s\" expected location: \"%s\" but got \"%s\"", requestURI, expectedToLocation, loc.String())
} }
return resp return resp

View file

@ -17,13 +17,13 @@ func TestRespond(t *testing.T) {
localhost:9080 { localhost:9080 {
respond /version 200 { respond /version 200 {
body "hello from a.caddy.localhost" body "hello from localhost"
} }
} }
`, "caddyfile") `, "caddyfile")
// act and assert // act and assert
caddytest.AssertGetResponse(t, "http://localhost:9080/version", 200, "hello from a.caddy.localhost") caddytest.AssertGetResponse(t, "http://localhost:9080/version", 200, "hello from localhost")
} }
func TestRedirect(t *testing.T) { func TestRedirect(t *testing.T) {
@ -40,7 +40,7 @@ func TestRedirect(t *testing.T) {
redir / http://localhost:9080/hello 301 redir / http://localhost:9080/hello 301
respond /hello 200 { respond /hello 200 {
body "hello from b" body "hello from localhost"
} }
} }
`, "caddyfile") `, "caddyfile")
@ -49,7 +49,7 @@ func TestRedirect(t *testing.T) {
caddytest.AssertRedirect(t, "http://localhost:9080/", "http://localhost:9080/hello", 301) caddytest.AssertRedirect(t, "http://localhost:9080/", "http://localhost:9080/hello", 301)
// follow redirect // follow redirect
caddytest.AssertGetResponse(t, "http://localhost:9080/", 200, "hello from b") caddytest.AssertGetResponse(t, "http://localhost:9080/", 200, "hello from localhost")
} }
func TestDuplicateHosts(t *testing.T) { func TestDuplicateHosts(t *testing.T) {
@ -78,8 +78,52 @@ func TestDefaultSNI(t *testing.T) {
} }
127.0.0.1:9443 { 127.0.0.1:9443 {
tls /caddy.localhost.crt /caddy.localhost.key { tls /caddy.localhost.crt /caddy.localhost.key
} respond /version 200 {
body "hello from a"
}
}
`, "caddyfile")
// act and assert
caddytest.AssertGetResponse(t, "https://127.0.0.1:9443/version", 200, "hello from a")
}
func TestDefaultSNIWithNamedHostAndExplicitIP(t *testing.T) {
// arrange
caddytest.InitServer(t, `
{
http_port 9080
https_port 9443
default_sni a.caddy.localhost
}
a.caddy.localhost:9443, 127.0.0.1:9443 {
tls /a.caddy.localhost.crt /a.caddy.localhost.key
respond /version 200 {
body "hello from a"
}
}
`, "caddyfile")
// act and assert
// makes a request with no sni
caddytest.AssertGetResponse(t, "https://127.0.0.1:9443/version", 200, "hello from a")
}
func TestDefaultSNIWithPortMappingOnly(t *testing.T) {
// arrange
caddytest.InitServer(t, `
{
http_port 9080
https_port 9443
default_sni a.caddy.localhost
}
:9443 {
tls /a.caddy.localhost.crt /a.caddy.localhost.key
respond /version 200 { respond /version 200 {
body "hello from a.caddy.localhost" body "hello from a.caddy.localhost"
} }
@ -87,5 +131,6 @@ func TestDefaultSNI(t *testing.T) {
`, "caddyfile") `, "caddyfile")
// act and assert // act and assert
caddytest.AssertGetResponse(t, "https://127.0.0.1:9443/version", 200, "hello from a.caddy.localhost") // makes a request with no sni
caddytest.AssertGetResponse(t, "https://127.0.0.1:9443/version", 200, "hello from a")
} }