Skip to content

Commit 2b26646

Browse files
Added command line option to set arbitrary headers on upstream query to prometheus
Added option to set host header on upstream query to prometheus - addresses #135 Signed-off-by: Graeme Christie <[email protected]>
1 parent c1160c9 commit 2b26646

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

Diff for: injectproxy/routes.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"encoding/json"
1919
"fmt"
2020
"io"
21+
"log"
2122
"net/http"
2223
"net/http/httputil"
2324
"net/url"
@@ -261,7 +262,7 @@ func (sle StaticLabelEnforcer) ExtractLabel(next http.HandlerFunc) http.Handler
261262
})
262263
}
263264

264-
func NewRoutes(upstream *url.URL, label string, extractLabeler ExtractLabeler, opts ...Option) (*routes, error) {
265+
func NewRoutes(upstream *url.URL, label string, extractLabeler ExtractLabeler, extraHttpHeaders []string, rewriteHostHeader string, opts ...Option) (*routes, error) {
265266
opt := options{}
266267
for _, o := range opts {
267268
o.apply(&opt)
@@ -271,7 +272,24 @@ func NewRoutes(upstream *url.URL, label string, extractLabeler ExtractLabeler, o
271272
opt.registerer = prometheus.NewRegistry()
272273
}
273274

274-
proxy := httputil.NewSingleHostReverseProxy(upstream)
275+
proxy := &httputil.ReverseProxy{
276+
Rewrite: func(r *httputil.ProxyRequest) {
277+
r.SetURL(upstream)
278+
if len(strings.TrimSpace(rewriteHostHeader)) == 0 {
279+
r.Out.Host = r.In.Host
280+
} else {
281+
r.Out.Host = strings.TrimSpace(rewriteHostHeader)
282+
}
283+
for _, headerArg := range extraHttpHeaders {
284+
header, val, found := strings.Cut(headerArg, ":")
285+
if !found {
286+
log.Printf("Header %s specified but ':' delimited not found", headerArg)
287+
continue
288+
}
289+
r.Out.Header[strings.TrimSpace(header)] = []string{strings.TrimSpace(val)}
290+
}
291+
},
292+
}
275293

276294
r := &routes{
277295
upstream: upstream,

Diff for: main.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ func main() {
6464
enableLabelAPIs bool
6565
unsafePassthroughPaths string // Comma-delimited string.
6666
errorOnReplace bool
67+
extraHttpHeaders arrayFlags
68+
rewriteHostHeader string
6769
)
6870

6971
flagset := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
@@ -81,6 +83,8 @@ func main() {
8183
"This option is checked after Prometheus APIs, you cannot override enforced API endpoints to be not enforced with this option. Use carefully as it can easily cause a data leak if the provided path is an important "+
8284
"API (like /api/v1/configuration) which isn't enforced by prom-label-proxy. NOTE: \"all\" matching paths like \"/\" or \"\" and regex are not allowed.")
8385
flagset.BoolVar(&errorOnReplace, "error-on-replace", false, "When specified, the proxy will return HTTP status code 400 if the query already contains a label matcher that differs from the one the proxy would inject.")
86+
flagset.Var(&extraHttpHeaders, "extra-http-header", "Additional HTTP headers to add to the upstream prometheus query in the format 'header: value'. Can be repeated multiple times for additional headers.")
87+
flagset.StringVar(&rewriteHostHeader, "rewrite-host-header-to", "", "Rewrite host header to supplied value when sending the query to the upstream URL.")
8488

8589
//nolint: errcheck // Parse() will exit on error.
8690
flagset.Parse(os.Args[1:])
@@ -109,6 +113,13 @@ func main() {
109113
log.Fatalf("Invalid scheme for upstream URL %q, only 'http' and 'https' are supported", upstream)
110114
}
111115

116+
for _, headerArg := range extraHttpHeaders {
117+
header, val, found := strings.Cut(headerArg, ":")
118+
if !found || len(strings.TrimSpace(header)) == 0 || len(strings.TrimSpace(val)) == 0 {
119+
log.Fatalf("extra-http-header %s is not in the format 'key:value'", headerArg)
120+
}
121+
}
122+
112123
reg := prometheus.NewRegistry()
113124
reg.MustRegister(
114125
collectors.NewGoCollector(),
@@ -140,7 +151,7 @@ func main() {
140151

141152
{
142153
// Run the insecure HTTP server.
143-
routes, err := injectproxy.NewRoutes(upstreamURL, label, extractLabeler, opts...)
154+
routes, err := injectproxy.NewRoutes(upstreamURL, label, extractLabeler, extraHttpHeaders, rewriteHostHeader, opts...)
144155
if err != nil {
145156
log.Fatalf("Failed to create injectproxy Routes: %v", err)
146157
}

0 commit comments

Comments
 (0)