mirror of
https://github.com/minio/minio.git
synced 2025-11-07 21:02:58 -05:00
Authorize prometheus endpoint with bearer token (#7640)
This commit is contained in:
committed by
kannappanr
parent
4925bc3e80
commit
ad75683bde
@@ -221,7 +221,7 @@ func guessIsMetricsReq(req *http.Request) bool {
|
||||
return false
|
||||
}
|
||||
aType := getRequestAuthType(req)
|
||||
return aType == authTypeAnonymous &&
|
||||
return (aType == authTypeAnonymous || aType == authTypeJWT) &&
|
||||
req.URL.Path == minioReservedBucketPath+prometheusMetricsPath
|
||||
}
|
||||
|
||||
|
||||
@@ -28,5 +28,5 @@ const (
|
||||
func registerMetricsRouter(router *mux.Router) {
|
||||
// metrics router
|
||||
metricsRouter := router.NewRoute().PathPrefix(minioReservedBucketPath).Subrouter()
|
||||
metricsRouter.Handle(prometheusMetricsPath, metricsHandler())
|
||||
metricsRouter.Handle(prometheusMetricsPath, AuthMiddleware(metricsHandler()))
|
||||
}
|
||||
|
||||
@@ -199,6 +199,7 @@ func (c *minioCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
}
|
||||
|
||||
func metricsHandler() http.Handler {
|
||||
|
||||
registry := prometheus.NewRegistry()
|
||||
|
||||
err := registry.Register(minioVersionInfo)
|
||||
@@ -222,4 +223,17 @@ func metricsHandler() http.Handler {
|
||||
ErrorHandling: promhttp.ContinueOnError,
|
||||
}),
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
// AuthMiddleware checks if the bearer token is valid and authorized.
|
||||
func AuthMiddleware(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
claims, _, authErr := webRequestAuthenticate(r)
|
||||
if authErr != nil || !claims.VerifyIssuer("prometheus", true) {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user