From a2aa3a60ebafbe5dc5aed6512d9e7acde6c062d3 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 31 Jul 2020 18:00:21 +0300 Subject: [PATCH] app/vmselect: show `X-Forwarded-For` contents on `/api/v1/status/active_queries` page Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/659 --- app/vmselect/prometheus/prometheus.go | 31 ++++++++++++++------------- app/vmselect/promql/active_queries.go | 24 ++++++++++----------- app/vmselect/promql/eval.go | 6 ++++-- lib/httpserver/httpserver.go | 12 ++++++++--- 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/app/vmselect/prometheus/prometheus.go b/app/vmselect/prometheus/prometheus.go index 8e0f263221..6393743f61 100644 --- a/app/vmselect/prometheus/prometheus.go +++ b/app/vmselect/prometheus/prometheus.go @@ -18,6 +18,7 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/lib/auth" "github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime" "github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver" "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/VictoriaMetrics/lib/storage" "github.com/VictoriaMetrics/metrics" @@ -770,13 +771,13 @@ func QueryHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r } ec := promql.EvalConfig{ - AuthToken: at, - Start: start, - End: start, - Step: step, - RemoteAddr: r.RemoteAddr, - Deadline: deadline, - LookbackDelta: lookbackDelta, + AuthToken: at, + Start: start, + End: start, + Step: step, + QuotedRemoteAddr: httpserver.GetQuotedRemoteAddr(r), + Deadline: deadline, + LookbackDelta: lookbackDelta, DenyPartialResponse: getDenyPartialResponse(r), } @@ -858,14 +859,14 @@ func queryRangeHandler(startTime time.Time, at *auth.Token, w http.ResponseWrite } ec := promql.EvalConfig{ - AuthToken: at, - Start: start, - End: end, - Step: step, - RemoteAddr: r.RemoteAddr, - Deadline: deadline, - MayCache: mayCache, - LookbackDelta: lookbackDelta, + AuthToken: at, + Start: start, + End: end, + Step: step, + QuotedRemoteAddr: httpserver.GetQuotedRemoteAddr(r), + Deadline: deadline, + MayCache: mayCache, + LookbackDelta: lookbackDelta, DenyPartialResponse: getDenyPartialResponse(r), } diff --git a/app/vmselect/promql/active_queries.go b/app/vmselect/promql/active_queries.go index e27a2b9769..e808c8565b 100644 --- a/app/vmselect/promql/active_queries.go +++ b/app/vmselect/promql/active_queries.go @@ -20,8 +20,8 @@ func WriteActiveQueries(w io.Writer) { now := time.Now() for _, aqe := range aqes { d := now.Sub(aqe.startTime) - fmt.Fprintf(w, "\tduration: %.3fs, id=%016X, remote_addr=%q, accountID=%d, projectID=%d, query=%q, start=%d, end=%d, step=%d\n", - d.Seconds(), aqe.qid, aqe.remoteAddr, aqe.accountID, aqe.projectID, aqe.q, aqe.start, aqe.end, aqe.step) + fmt.Fprintf(w, "\tduration: %.3fs, id=%016X, remote_addr=%s, accountID=%d, projectID=%d, query=%q, start=%d, end=%d, step=%d\n", + d.Seconds(), aqe.qid, aqe.quotedRemoteAddr, aqe.accountID, aqe.projectID, aqe.q, aqe.start, aqe.end, aqe.step) } } @@ -33,15 +33,15 @@ type activeQueries struct { } type activeQueryEntry struct { - accountID uint32 - projectID uint32 - start int64 - end int64 - step int64 - qid uint64 - remoteAddr string - q string - startTime time.Time + accountID uint32 + projectID uint32 + start int64 + end int64 + step int64 + qid uint64 + quotedRemoteAddr string + q string + startTime time.Time } func newActiveQueries() *activeQueries { @@ -58,7 +58,7 @@ func (aq *activeQueries) Add(ec *EvalConfig, q string) uint64 { aqe.end = ec.End aqe.step = ec.Step aqe.qid = atomic.AddUint64(&nextActiveQueryID, 1) - aqe.remoteAddr = ec.RemoteAddr + aqe.quotedRemoteAddr = ec.QuotedRemoteAddr aqe.q = q aqe.startTime = time.Now() diff --git a/app/vmselect/promql/eval.go b/app/vmselect/promql/eval.go index e5b829a352..4e94775052 100644 --- a/app/vmselect/promql/eval.go +++ b/app/vmselect/promql/eval.go @@ -83,8 +83,10 @@ type EvalConfig struct { End int64 Step int64 - RemoteAddr string - Deadline netstorage.Deadline + // QuotedRemoteAddr contains quoted remote address. + QuotedRemoteAddr string + + Deadline netstorage.Deadline MayCache bool diff --git a/lib/httpserver/httpserver.go b/lib/httpserver/httpserver.go index b05bbb3c90..eec3c19a7e 100644 --- a/lib/httpserver/httpserver.go +++ b/lib/httpserver/httpserver.go @@ -430,13 +430,19 @@ var ( requestsTotal = metrics.NewCounter(`vm_http_requests_all_total`) ) -// Errorf writes formatted error message to w and to logger. -func Errorf(w http.ResponseWriter, r *http.Request, format string, args ...interface{}) { - errStr := fmt.Sprintf(format, args...) +// GetQuotedRemoteAddr returns quoted remote address. +func GetQuotedRemoteAddr(r *http.Request) string { remoteAddr := strconv.Quote(r.RemoteAddr) // quote remoteAddr and X-Forwarded-For, since they may contain untrusted input if addr := r.Header.Get("X-Forwarded-For"); addr != "" { remoteAddr += ", X-Forwarded-For: " + strconv.Quote(addr) } + return remoteAddr +} + +// Errorf writes formatted error message to w and to logger. +func Errorf(w http.ResponseWriter, r *http.Request, format string, args ...interface{}) { + errStr := fmt.Sprintf(format, args...) + remoteAddr := GetQuotedRemoteAddr(r) errStr = fmt.Sprintf("remoteAddr: %s; %s", remoteAddr, errStr) logger.WarnfSkipframes(1, "%s", errStr)