2018-11-04 22:20:00 -05:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package routers
|
|
|
|
|
|
|
|
import (
|
2019-07-06 13:03:13 -04:00
|
|
|
"crypto/subtle"
|
|
|
|
|
2018-11-04 22:20:00 -05:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-08-23 12:40:30 -04:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2018-11-04 22:20:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Metrics validate auth token and render prometheus metrics
|
|
|
|
func Metrics(ctx *context.Context) {
|
|
|
|
if setting.Metrics.Token == "" {
|
|
|
|
promhttp.Handler().ServeHTTP(ctx.Resp, ctx.Req.Request)
|
|
|
|
return
|
|
|
|
}
|
2019-02-09 00:58:31 -05:00
|
|
|
header := ctx.Req.Header.Get("Authorization")
|
2018-11-04 22:20:00 -05:00
|
|
|
if header == "" {
|
|
|
|
ctx.Error(401)
|
|
|
|
return
|
|
|
|
}
|
2019-07-06 13:03:13 -04:00
|
|
|
got := []byte(header)
|
|
|
|
want := []byte("Bearer " + setting.Metrics.Token)
|
|
|
|
if subtle.ConstantTimeCompare(got, want) != 1 {
|
2018-11-04 22:20:00 -05:00
|
|
|
ctx.Error(401)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
promhttp.Handler().ServeHTTP(ctx.Resp, ctx.Req.Request)
|
|
|
|
}
|