basicauth: log warning on healthz (#1039)

* basicauth: log warning on healthz

* include http path prefix
This commit is contained in:
Marwan Sulaiman
2019-01-11 13:23:28 -05:00
committed by GitHub
parent fd10bed609
commit b19cbcc137
+12
View File
@@ -3,14 +3,26 @@ package actions
import (
"crypto/subtle"
"net/http"
"strings"
"github.com/gomods/athens/pkg/log"
"github.com/gorilla/mux"
)
const healthWarning = "/healthz received none or incorrect Basic-Auth headers"
func basicAuth(user, pass string) mux.MiddlewareFunc {
return func(h http.Handler) http.Handler {
f := func(w http.ResponseWriter, r *http.Request) {
if !checkAuth(r, user, pass) {
// Helpful hint for Kubernetes users:
// if they forget to send auth headers
// kubernetes silently fails, so a log
// might help them.
if strings.HasSuffix(r.URL.Path, "/healthz") {
lggr := log.EntryFromContext(r.Context())
lggr.Warnf(healthWarning)
}
w.Header().Set("WWW-Authenticate", `Basic realm="basic auth required"`)
w.WriteHeader(http.StatusUnauthorized)
return