Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support HTTP Basic authentication for Pushgateway access #98

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var (
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
persistenceFile = flag.String("persistence.file", "", "File to persist metrics. If empty, metrics are only kept in memory.")
persistenceInterval = flag.Duration("persistence.interval", 5*time.Minute, "The minimum interval at which to write out the persistence file.")
basicAuthUsername = flag.String("basic_auth.username", "", "Username for basic authorization.")
basicAuthPassword = flag.String("basic_auth.password", "", "Password for basic authorization.")
)

func init() {
Expand Down Expand Up @@ -105,7 +107,10 @@ func main() {
log.Fatal(err)
}
go interruptHandler(l)
err = (&http.Server{Addr: *listenAddress, Handler: r}).Serve(l)

basicAuth := basicAuthHandler(*basicAuthUsername, *basicAuthPassword)

err = (&http.Server{Addr: *listenAddress, Handler: basicAuth(r)}).Serve(l)
log.Errorln("HTTP server stopped:", err)
// To give running connections a chance to submit their payload, we wait
// for 1sec, but we don't want to wait long (e.g. until all connections
Expand Down Expand Up @@ -136,3 +141,27 @@ func interruptHandler(l net.Listener) {
log.Info("Received SIGINT/SIGTERM; exiting gracefully...")
l.Close()
}

func basicAuthHandler(requiredUser, requiredPassword string) func(http.Handler) http.Handler {
if requiredUser == "" || requiredPassword == "" {
return func(h http.Handler) http.Handler {
return h
}
}
return func(h http.Handler) http.Handler {
f := func(w http.ResponseWriter, r *http.Request) {
// Get the Basic Authentication credentials
user, password, hasAuth := r.BasicAuth()

if hasAuth && user == requiredUser && password == requiredPassword {
// Delegate request to the given handle
h.ServeHTTP(w, r)
} else {
// Request Basic Authentication otherwise
w.Header().Set("WWW-Authenticate", "Basic realm=Restricted")
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
}
}
return http.HandlerFunc(f)
}
}