Feat add prometheus auth (#2252)

* feat: add support for basic auth on prometheus

* fix: properly name middleware generator

* refactor: move all prometheus configs into common object

* chore: add suggestions from review

add back old config switch and print warning if it is used

* fix: undo accidental change

* fix: rebase/merge issue
This commit is contained in:
holzgeist
2024-06-27 02:13:51 -07:00
committed by GitHub
parent fa88fbf028
commit 6523c9c099
4 changed files with 59 additions and 8 deletions
+8
View File
@@ -60,7 +60,9 @@ var (
type Config struct { type Config struct {
Port uint32 `yaml:"port,omitempty"` Port uint32 `yaml:"port,omitempty"`
BindAddresses []string `yaml:"bind_addresses,omitempty"` BindAddresses []string `yaml:"bind_addresses,omitempty"`
// PrometheusPort is deprecated
PrometheusPort uint32 `yaml:"prometheus_port,omitempty"` PrometheusPort uint32 `yaml:"prometheus_port,omitempty"`
Prometheus PrometheusConfig `yaml:"prometheus,omitempty"`
RTC RTCConfig `yaml:"rtc,omitempty"` RTC RTCConfig `yaml:"rtc,omitempty"`
Redis redisLiveKit.RedisConfig `yaml:"redis,omitempty"` Redis redisLiveKit.RedisConfig `yaml:"redis,omitempty"`
Audio AudioConfig `yaml:"audio,omitempty"` Audio AudioConfig `yaml:"audio,omitempty"`
@@ -328,6 +330,12 @@ type APIConfig struct {
MaxCheckInterval time.Duration `yaml:"max_check_interval,omitempty"` MaxCheckInterval time.Duration `yaml:"max_check_interval,omitempty"`
} }
type PrometheusConfig struct {
Port uint32 `yaml:"port,omitempty"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
}
type ForwardStatsConfig struct { type ForwardStatsConfig struct {
SummaryInterval time.Duration `yaml:"summary_interval,omitempty"` SummaryInterval time.Duration `yaml:"summary_interval,omitempty"`
ReportInterval time.Duration `yaml:"report_interval,omitempty"` ReportInterval time.Duration `yaml:"report_interval,omitempty"`
+2 -2
View File
@@ -60,7 +60,7 @@ func TestGeneratedFlags(t *testing.T) {
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
set.Bool("rtc.use_ice_lite", true, "") // bool set.Bool("rtc.use_ice_lite", true, "") // bool
set.String("redis.address", "localhost:6379", "") // string set.String("redis.address", "localhost:6379", "") // string
set.Uint("prometheus_port", 9999, "") // uint32 set.Uint("prometheus.port", 9999, "") // uint32
set.Bool("rtc.allow_tcp_fallback", true, "") // pointer set.Bool("rtc.allow_tcp_fallback", true, "") // pointer
set.Bool("rtc.reconnect_on_publication_error", true, "") // pointer set.Bool("rtc.reconnect_on_publication_error", true, "") // pointer
set.Bool("rtc.reconnect_on_subscription_error", false, "") // pointer set.Bool("rtc.reconnect_on_subscription_error", false, "") // pointer
@@ -71,7 +71,7 @@ func TestGeneratedFlags(t *testing.T) {
require.True(t, conf.RTC.UseICELite) require.True(t, conf.RTC.UseICELite)
require.Equal(t, "localhost:6379", conf.Redis.Address) require.Equal(t, "localhost:6379", conf.Redis.Address)
require.Equal(t, uint32(9999), conf.PrometheusPort) require.Equal(t, uint32(9999), conf.Prometheus.Port)
require.NotNil(t, conf.RTC.AllowTCPFallback) require.NotNil(t, conf.RTC.AllowTCPFallback)
require.True(t, *conf.RTC.AllowTCPFallback) require.True(t, *conf.RTC.AllowTCPFallback)
+31
View File
@@ -0,0 +1,31 @@
package service
import (
"net/http"
)
func GenBasicAuthMiddleware(username string, password string) (func(http.ResponseWriter, *http.Request, http.HandlerFunc) ) {
return func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
given_username, given_password, ok := r.BasicAuth()
unauthorized := func() {
rw.Header().Set("WWW-Authenticate", "Basic realm=\"Protected Area\"")
rw.WriteHeader(http.StatusUnauthorized)
}
if !ok {
unauthorized()
return
}
if given_username != username {
unauthorized()
return
}
if given_password != password {
unauthorized()
return
}
next(rw, r)
}
}
+16 -4
View File
@@ -140,8 +140,20 @@ func NewLivekitServer(conf *config.Config,
} }
if conf.PrometheusPort > 0 { if conf.PrometheusPort > 0 {
logger.Warnw("prometheus_port is deprecated, please switch prometheus.port instead", nil)
conf.Prometheus.Port = conf.PrometheusPort
}
if conf.Prometheus.Port > 0 {
promHandler := promhttp.Handler()
if conf.Prometheus.Username != "" && conf.Prometheus.Password != "" {
protectedHandler := negroni.New()
protectedHandler.Use(negroni.HandlerFunc(GenBasicAuthMiddleware(conf.Prometheus.Username, conf.Prometheus.Password)))
protectedHandler.UseHandler(promHandler)
promHandler = protectedHandler
}
s.promServer = &http.Server{ s.promServer = &http.Server{
Handler: promhttp.Handler(), Handler: promHandler,
} }
} }
@@ -207,7 +219,7 @@ func (s *LivekitServer) Start() error {
listeners = append(listeners, ln) listeners = append(listeners, ln)
if s.promServer != nil { if s.promServer != nil {
ln, err = net.Listen("tcp", net.JoinHostPort(addr, strconv.Itoa(int(s.config.PrometheusPort)))) ln, err = net.Listen("tcp", net.JoinHostPort(addr, strconv.Itoa(int(s.config.Prometheus.Port))))
if err != nil { if err != nil {
return err return err
} }
@@ -234,8 +246,8 @@ func (s *LivekitServer) Start() error {
"rtc.portICERange", []uint32{s.config.RTC.ICEPortRangeStart, s.config.RTC.ICEPortRangeEnd}, "rtc.portICERange", []uint32{s.config.RTC.ICEPortRangeStart, s.config.RTC.ICEPortRangeEnd},
) )
} }
if s.config.PrometheusPort != 0 { if s.config.Prometheus.Port != 0 {
values = append(values, "portPrometheus", s.config.PrometheusPort) values = append(values, "portPrometheus", s.config.Prometheus.Port)
} }
if s.config.Region != "" { if s.config.Region != "" {
values = append(values, "region", s.config.Region) values = append(values, "region", s.config.Region)