Skip to content

Commit

Permalink
Merge pull request prometheus#5 from kesselborn/feature/basic-auth-su…
Browse files Browse the repository at this point in the history
…pport

Implement simple basic auth for the exporter
  • Loading branch information
matttproud committed Jan 9, 2013
2 parents 397d684 + 3dbf5d5 commit 872a959
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ the LICENSE file.
package registry

import (
"encoding/base64"
"encoding/json"
"github.com/matttproud/golang_instrumentation/maths"
"github.com/matttproud/golang_instrumentation/metrics"
Expand All @@ -23,6 +24,7 @@ const (
jsonContentType = "application/json"
contentType = "Content-Type"
jsonSuffix = ".json"
authorization = "Authorization"
)

/*
Expand Down Expand Up @@ -117,6 +119,31 @@ func (r *Registry) Register(name string, metric metrics.Metric) {
}
}

func (register *Registry) YieldBasicAuthExporter(username, password string) http.HandlerFunc {
exporter := register.YieldExporter()

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authenticated := false

if auth := r.Header.Get(authorization); auth != "" {
base64Encoded := strings.SplitAfter(auth, " ")[1]
decoded, err := base64.URLEncoding.DecodeString(base64Encoded)
if err == nil {
usernamePassword := strings.Split(string(decoded), ":")
if usernamePassword[0] == username && usernamePassword[1] == password {
authenticated = true
}
}
}

if authenticated {
exporter.ServeHTTP(w, r)
} else {
http.Error(w, "access forbidden", 403)
}
})
}

/*
Create a http.HandlerFunc that is tied to r Registry such that requests
against it generate a representation of the housed metrics.
Expand Down

0 comments on commit 872a959

Please sign in to comment.