2018-11-05 04:20:00 +01: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.
|
|
|
|
|
2021-06-09 01:33:54 +02:00
|
|
|
package web
|
2018-11-05 04:20:00 +01:00
|
|
|
|
|
|
|
import (
|
2019-07-06 19:03:13 +02:00
|
|
|
"crypto/subtle"
|
2020-11-17 21:50:06 +01:00
|
|
|
"net/http"
|
2019-07-06 19:03:13 +02:00
|
|
|
|
2018-11-05 04:20:00 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-08-23 18:40:30 +02:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2018-11-05 04:20:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Metrics validate auth token and render prometheus metrics
|
2020-11-17 21:50:06 +01:00
|
|
|
func Metrics(resp http.ResponseWriter, req *http.Request) {
|
2018-11-05 04:20:00 +01:00
|
|
|
if setting.Metrics.Token == "" {
|
2020-11-17 21:50:06 +01:00
|
|
|
promhttp.Handler().ServeHTTP(resp, req)
|
2018-11-05 04:20:00 +01:00
|
|
|
return
|
|
|
|
}
|
2020-11-17 21:50:06 +01:00
|
|
|
header := req.Header.Get("Authorization")
|
2018-11-05 04:20:00 +01:00
|
|
|
if header == "" {
|
2020-11-17 21:50:06 +01:00
|
|
|
http.Error(resp, "", 401)
|
2018-11-05 04:20:00 +01:00
|
|
|
return
|
|
|
|
}
|
2019-07-06 19:03:13 +02:00
|
|
|
got := []byte(header)
|
|
|
|
want := []byte("Bearer " + setting.Metrics.Token)
|
|
|
|
if subtle.ConstantTimeCompare(got, want) != 1 {
|
2020-11-17 21:50:06 +01:00
|
|
|
http.Error(resp, "", 401)
|
2018-11-05 04:20:00 +01:00
|
|
|
return
|
|
|
|
}
|
2020-11-17 21:50:06 +01:00
|
|
|
promhttp.Handler().ServeHTTP(resp, req)
|
2018-11-05 04:20:00 +01:00
|
|
|
}
|