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

reverseproxy: Replace health header placeholders #5861

Merged
merged 1 commit into from
Oct 11, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ reverse_proxy 127.0.0.1:65535 {
X-Header-Key 95ca39e3cbe7
X-Header-Keys VbG4NZwWnipo 335Q9/MhqcNU3s2TO
X-Empty-Value
Same-Key 1
Same-Key 2
X-System-Hostname {system.hostname}
}
health_uri /health
}
Expand All @@ -29,6 +32,10 @@ reverse_proxy 127.0.0.1:65535 {
"Host": [
"example.com"
],
"Same-Key": [
"1",
"2"
],
"X-Empty-Value": [
""
],
Expand All @@ -38,6 +45,9 @@ reverse_proxy 127.0.0.1:65535 {
"X-Header-Keys": [
"VbG4NZwWnipo",
"335Q9/MhqcNU3s2TO"
],
"X-System-Hostname": [
"{system.hostname}"
]
},
"uri": "/health"
Expand Down
2 changes: 1 addition & 1 deletion modules/caddyhttp/reverseproxy/caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
if len(values) == 0 {
values = append(values, "")
}
healthHeaders[key] = values
healthHeaders[key] = append(healthHeaders[key], values...)
}
if h.HealthChecks == nil {
h.HealthChecks = new(HealthChecks)
Expand Down
14 changes: 10 additions & 4 deletions modules/caddyhttp/reverseproxy/healthchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,17 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, upstre
}
ctx = context.WithValue(ctx, caddyhttp.OriginalRequestCtxKey, *req)
req = req.WithContext(ctx)
for key, hdrs := range h.HealthChecks.Active.Headers {

// set headers, using a replacer with only globals (env vars, system info, etc.)
repl := caddy.NewReplacer()
for key, vals := range h.HealthChecks.Active.Headers {
key = repl.ReplaceAll(key, "")
if key == "Host" {
req.Host = h.HealthChecks.Active.Headers.Get(key)
} else {
req.Header[key] = hdrs
req.Host = repl.ReplaceAll(h.HealthChecks.Active.Headers.Get(key), "")
continue
}
for _, val := range vals {
req.Header.Add(key, repl.ReplaceKnown(val, ""))
}
}

Expand Down
Loading