mirror of
https://github.com/gomods/athens
synced 2026-02-03 12:10:32 +00:00
basicauth: log warning on healthz (#1039)
* basicauth: log warning on healthz * include http path prefix
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user