mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
85fd4917b1
New labels `reason="wrong basic auth creds"` and `reason="wrong auth key"` were
added to metric `vm_http_request_errors_total` to help identify auth errors.
https://github.com/victoriaMetrics/victoriaMetrics/issues/4590
Co-authored-by: Rao, B V Chalapathi <b_v_chalapathi.rao@nokia.com>
Co-authored-by: Roman Khavronenko <roman@victoriametrics.com>
(cherry picked from commit 0638bbe69c
)
154 lines
3.8 KiB
Go
154 lines
3.8 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetQuotedRemoteAddr(t *testing.T) {
|
|
f := func(remoteAddr, xForwardedFor, expectedAddr string) {
|
|
t.Helper()
|
|
|
|
req := &http.Request{
|
|
RemoteAddr: remoteAddr,
|
|
}
|
|
if xForwardedFor != "" {
|
|
req.Header = map[string][]string{
|
|
"X-Forwarded-For": {xForwardedFor},
|
|
}
|
|
}
|
|
addr := GetQuotedRemoteAddr(req)
|
|
if addr != expectedAddr {
|
|
t.Fatalf("unexpected remote addr;\ngot\n%s\nwant\n%s", addr, expectedAddr)
|
|
}
|
|
|
|
// Verify that the addr can be unmarshaled as JSON string
|
|
var s string
|
|
if err := json.Unmarshal([]byte(addr), &s); err != nil {
|
|
t.Fatalf("cannot unmarshal addr: %s", err)
|
|
}
|
|
}
|
|
|
|
f("1.2.3.4", "", `"1.2.3.4"`)
|
|
f("1.2.3.4", "foo.bar", `"1.2.3.4, X-Forwarded-For: foo.bar"`)
|
|
f("1.2\n\"3.4", "foo\nb\"ar", `"1.2\n\"3.4, X-Forwarded-For: foo\nb\"ar"`)
|
|
}
|
|
|
|
func TestBasicAuthMetrics(t *testing.T) {
|
|
origUsername := *httpAuthUsername
|
|
origPasswd := *httpAuthPassword
|
|
defer func() {
|
|
*httpAuthPassword = origPasswd
|
|
*httpAuthUsername = origUsername
|
|
}()
|
|
|
|
f := func(user, pass string, expCode int) {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
|
|
req.SetBasicAuth(user, pass)
|
|
|
|
w := httptest.NewRecorder()
|
|
CheckBasicAuth(w, req)
|
|
|
|
res := w.Result()
|
|
_ = res.Body.Close()
|
|
if expCode != res.StatusCode {
|
|
t.Fatalf("wanted status code: %d, got: %d\n", res.StatusCode, expCode)
|
|
}
|
|
}
|
|
|
|
*httpAuthUsername = "test"
|
|
*httpAuthPassword = "pass"
|
|
f("test", "pass", 200)
|
|
f("test", "wrong", 401)
|
|
f("wrong", "pass", 401)
|
|
f("wrong", "wrong", 401)
|
|
|
|
*httpAuthUsername = ""
|
|
*httpAuthPassword = ""
|
|
f("test", "pass", 200)
|
|
f("test", "wrong", 200)
|
|
f("wrong", "pass", 200)
|
|
f("wrong", "wrong", 200)
|
|
}
|
|
|
|
func TestAuthKeyMetrics(t *testing.T) {
|
|
origUsername := *httpAuthUsername
|
|
origPasswd := *httpAuthPassword
|
|
defer func() {
|
|
*httpAuthPassword = origPasswd
|
|
*httpAuthUsername = origUsername
|
|
}()
|
|
|
|
tstWithAuthKey := func(key string, expCode int) {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodPost, "/metrics", strings.NewReader("authKey="+key))
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded;param=value")
|
|
w := httptest.NewRecorder()
|
|
|
|
CheckAuthFlag(w, req, "rightKey", "metricsAuthkey")
|
|
|
|
res := w.Result()
|
|
defer res.Body.Close()
|
|
if expCode != res.StatusCode {
|
|
t.Fatalf("Unexpected status code: %d, Expected code is: %d\n", res.StatusCode, expCode)
|
|
}
|
|
}
|
|
|
|
tstWithAuthKey("rightKey", 200)
|
|
tstWithAuthKey("wrongKey", 401)
|
|
|
|
tstWithOutAuthKey := func(user, pass string, expCode int) {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
|
|
req.SetBasicAuth(user, pass)
|
|
|
|
w := httptest.NewRecorder()
|
|
CheckAuthFlag(w, req, "", "metricsAuthkey")
|
|
|
|
res := w.Result()
|
|
_ = res.Body.Close()
|
|
if expCode != res.StatusCode {
|
|
t.Fatalf("wanted status code: %d, got: %d\n", res.StatusCode, expCode)
|
|
}
|
|
}
|
|
|
|
*httpAuthUsername = "test"
|
|
*httpAuthPassword = "pass"
|
|
tstWithOutAuthKey("test", "pass", 200)
|
|
tstWithOutAuthKey("test", "wrong", 401)
|
|
tstWithOutAuthKey("wrong", "pass", 401)
|
|
tstWithOutAuthKey("wrong", "wrong", 401)
|
|
}
|
|
|
|
func TestHandlerWrapper(t *testing.T) {
|
|
*headerHSTS = "foo"
|
|
*headerFrameOptions = "bar"
|
|
*headerCSP = "baz"
|
|
defer func() {
|
|
*headerHSTS = ""
|
|
*headerFrameOptions = ""
|
|
*headerCSP = ""
|
|
}()
|
|
|
|
req, _ := http.NewRequest("GET", "/health", nil)
|
|
|
|
srv := &server{s: &http.Server{}}
|
|
w := &httptest.ResponseRecorder{}
|
|
handlerWrapper(srv, w, req, func(_ http.ResponseWriter, _ *http.Request) bool {
|
|
return true
|
|
})
|
|
|
|
if w.Header().Get("Strict-Transport-Security") != "foo" {
|
|
t.Errorf("HSTS header not set")
|
|
}
|
|
if w.Header().Get("X-Frame-Options") != "bar" {
|
|
t.Errorf("X-Frame-Options header not set")
|
|
}
|
|
if w.Header().Get("Content-Security-Policy") != "baz" {
|
|
t.Errorf("CSP header not set")
|
|
}
|
|
}
|