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

Support Host header in HTTP health checks #2657

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type AgentServiceCheck struct {
TCP string `json:",omitempty"`
Status string `json:",omitempty"`
Notes string `json:",omitempty"`
HostHeader string `json:",omitempty"`
TLSSkipVerify bool `json:",omitempty"`

// In Consul 0.7 and later, checks that are associated with a service
Expand Down
1 change: 1 addition & 0 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@ func (a *Agent) AddCheck(check *structs.HealthCheck, chkType *CheckType, persist
Interval: chkType.Interval,
Timeout: chkType.Timeout,
Logger: a.logger,
HostHeader: chkType.HostHeader,
TLSSkipVerify: chkType.TLSSkipVerify,
}
http.Start()
Expand Down
5 changes: 5 additions & 0 deletions command/agent/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type CheckType struct {
Interval time.Duration
DockerContainerID string
Shell string
HostHeader string
TLSSkipVerify bool

Timeout time.Duration
Expand Down Expand Up @@ -341,6 +342,7 @@ type CheckHTTP struct {
Interval time.Duration
Timeout time.Duration
Logger *log.Logger
HostHeader string
TLSSkipVerify bool

httpClient *http.Client
Expand Down Expand Up @@ -429,6 +431,9 @@ func (c *CheckHTTP) check() {

req.Header.Set("User-Agent", HttpUserAgent)
req.Header.Set("Accept", "text/plain, text/*, */*")
if c.HostHeader != "" {
req.Host = c.HostHeader
}

resp, err := c.httpClient.Do(req)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,9 @@ func FixupCheckType(raw interface{}) error {
case "docker_container_id":
rawMap["DockerContainerID"] = v
delete(rawMap, k)
case "host_header":
rawMap["HostHeader"] = v
delete(rawMap, k)
case "tls_skip_verify":
rawMap["TLSSkipVerify"] = v
delete(rawMap, k)
Expand Down
26 changes: 23 additions & 3 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,7 @@ func TestDecodeConfig_Services(t *testing.T) {
"port": 9200,
"check": {
"HTTP": "http://localhost:9200/_cluster_health",
"host_header": "test.example",
"interval": "10s",
"timeout": "100ms"
}
Expand Down Expand Up @@ -1189,9 +1190,10 @@ func TestDecodeConfig_Services(t *testing.T) {
},
&ServiceDefinition{
Check: CheckType{
HTTP: "http://localhost:9200/_cluster_health",
Interval: 10 * time.Second,
Timeout: 100 * time.Millisecond,
HTTP: "http://localhost:9200/_cluster_health",
HostHeader: "test.example",
Interval: 10 * time.Second,
Timeout: 100 * time.Millisecond,
},
ID: "es0",
Name: "elasticsearch",
Expand Down Expand Up @@ -1287,6 +1289,14 @@ func TestDecodeConfig_Checks(t *testing.T) {
"timeout": "100ms",
"service_id": "insecure-sslservice",
"tls_skip_verify": true
},
{
"id": "chk7",
"name": "service:behind-reverseproxy",
"HTTP": "http://localhost/status",
"host_header": "proxy.example",
"interval": "10s",
"service_id": "behind-reverseproxy"
}
]
}`
Expand Down Expand Up @@ -1355,6 +1365,16 @@ func TestDecodeConfig_Checks(t *testing.T) {
TLSSkipVerify: true,
},
},
&CheckDefinition{
ID: "chk7",
Name: "service:behind-reverseproxy",
ServiceID: "behind-reverseproxy",
CheckType: CheckType{
HTTP: "http://localhost/status",
HostHeader: "proxy.example",
Interval: 10 * time.Second,
},
},
},
}

Expand Down
3 changes: 2 additions & 1 deletion website/source/docs/agent/checks.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ There are five different kinds of checks:
limited to roughly 4K. Responses larger than this will be truncated. HTTP checks
also support SSL. By default, a valid SSL certificate is expected. Certificate
verification can be turned off by setting the `tls_skip_verify` field to `true`
in the check definition.
in the check definition. HTTP `Host` header can be set using the `HostHeader` field
for checks that are perfomed through a reverse proxy.

* TCP + Interval - These checks make an TCP connection attempt every Interval
(e.g. every 30 seconds) to the specified IP/hostname and port. If no hostname
Expand Down
5 changes: 5 additions & 0 deletions website/source/docs/agent/http/agent.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ body must look like:
"TCP": "example.com:22",
"Interval": "10s",
"TTL": "15s",
"HostHeader": "test.example",
"TLSSkipVerify": true
}
```
Expand Down Expand Up @@ -337,6 +338,10 @@ expected. Certificate verification can be controlled using the
If `TLSSkipVerify` is set to `true`, certificate verification will be
disabled. By default, certificate verification is enabled.

`HostHeader` can be used to perform checks through reverse proxy so that
Consul will check the end-user interface (avoid to consider the service as healthy
if the reverse proxy has an issue).

A `TCP` check will perform an TCP connection attempt against the value of `TCP`
(expected to be an IP or hostname plus port combination) every `Interval`. If the
connection attempt is successful, the check is `passing`. If the connection
Expand Down