-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Correctly forward Host header in healthcheck #3203
Conversation
f4bf066
to
af42c1c
Compare
agent/check_test.go
Outdated
@@ -213,6 +213,7 @@ func TestCheckHTTP(t *testing.T) { | |||
|
|||
// custom header | |||
{desc: "custom header", code: 200, header: http.Header{"A": []string{"b", "c"}}, status: api.HealthPassing}, | |||
{desc: "host header", code: 200, header: http.Header{"Host": []string{"customhost"}}, status: api.HealthPassing}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/customhost/a/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
agent/check.go
Outdated
@@ -386,6 +386,10 @@ func (c *CheckHTTP) check() { | |||
req.Header = make(http.Header) | |||
} | |||
|
|||
if host, ok := c.Header["Host"]; ok && len(host) > 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if host := c.Header.Get("Host"); host != "" {
req.Host = host
}
agent/check_test.go
Outdated
@@ -234,7 +235,9 @@ func TestCheckHTTP(t *testing.T) { | |||
"User-Agent": []string{"Consul Health Check"}, | |||
} | |||
for k, v := range tt.header { | |||
expectedHeader[k] = v | |||
if k != "Host" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only tests that the Host
header is not part of the Header
object but it does not test whether the custom value was actually set. I think for this you have to compare the r.Host
with the custom host header. Maybe like this:
for k, v := range tt.header {
expectedHeader[k] = v
}
// the Host header is in r.Host and not in the headers
host := expectedHeaders.Get("Host")
if host != "" && host != r.Host {
w.WriteHeader(999)
return
}
expectedHeaders.Del("Host")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
9a1f3d6
to
c7dd22e
Compare
thanks @magiconair for your comments (you've simply rewritten my patch :) ) |
agent/check_test.go
Outdated
@@ -234,8 +235,17 @@ func TestCheckHTTP(t *testing.T) { | |||
"User-Agent": []string{"Consul Health Check"}, | |||
} | |||
for k, v := range tt.header { | |||
expectedHeader[k] = v | |||
expectedHeader[k] = v |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pls run gofmt
. This doesn't look right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done thanks
Host header must be set explicitely on http requests Change-Id: I91a32f0fb1ec3fbc713adf0e10869797e91172c7 Signed-off-by: Grégoire Seux <[email protected]>
c7dd22e
to
074b4a9
Compare
This LGTM. @slackpad do you want to have a quick peek? |
Looks good - thanks! |
Host header must be set explicitely on http requests
I'll write test a bit later