2022-03-04 21:39:48 +00:00
|
|
|
// Copyright 2022 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-10-26 19:44:17 +00:00
|
|
|
"log/slog"
|
2022-03-04 21:39:48 +00:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2022-06-29 02:22:14 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
2022-03-04 21:39:48 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-06-29 02:22:14 +00:00
|
|
|
configReloadSuccess = promauto.NewGauge(prometheus.GaugeOpts{
|
2022-03-04 21:39:48 +00:00
|
|
|
Namespace: "postgres_exporter",
|
|
|
|
Name: "config_last_reload_successful",
|
|
|
|
Help: "Postgres exporter config loaded successfully.",
|
|
|
|
})
|
|
|
|
|
2022-06-29 02:22:14 +00:00
|
|
|
configReloadSeconds = promauto.NewGauge(prometheus.GaugeOpts{
|
2022-03-04 21:39:48 +00:00
|
|
|
Namespace: "postgres_exporter",
|
|
|
|
Name: "config_last_reload_success_timestamp_seconds",
|
|
|
|
Help: "Timestamp of the last successful configuration reload.",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
AuthModules map[string]AuthModule `yaml:"auth_modules"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type AuthModule struct {
|
|
|
|
Type string `yaml:"type"`
|
|
|
|
UserPass UserPass `yaml:"userpass,omitempty"`
|
|
|
|
// Add alternative auth modules here
|
|
|
|
Options map[string]string `yaml:"options"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserPass struct {
|
|
|
|
Username string `yaml:"username"`
|
|
|
|
Password string `yaml:"password"`
|
|
|
|
}
|
|
|
|
|
2023-06-03 07:28:17 +00:00
|
|
|
type Handler struct {
|
2022-03-04 21:39:48 +00:00
|
|
|
sync.RWMutex
|
|
|
|
Config *Config
|
|
|
|
}
|
|
|
|
|
2023-06-03 07:28:17 +00:00
|
|
|
func (ch *Handler) GetConfig() *Config {
|
2022-03-04 21:39:48 +00:00
|
|
|
ch.RLock()
|
|
|
|
defer ch.RUnlock()
|
|
|
|
return ch.Config
|
|
|
|
}
|
|
|
|
|
2024-10-26 19:44:17 +00:00
|
|
|
func (ch *Handler) ReloadConfig(f string, logger *slog.Logger) error {
|
2022-03-04 21:39:48 +00:00
|
|
|
config := &Config{}
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
configReloadSuccess.Set(0)
|
|
|
|
} else {
|
|
|
|
configReloadSuccess.Set(1)
|
|
|
|
configReloadSeconds.SetToCurrentTime()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
yamlReader, err := os.Open(f)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error opening config file %q: %s", f, err)
|
|
|
|
}
|
|
|
|
defer yamlReader.Close()
|
|
|
|
decoder := yaml.NewDecoder(yamlReader)
|
|
|
|
decoder.KnownFields(true)
|
|
|
|
|
|
|
|
if err = decoder.Decode(config); err != nil {
|
|
|
|
return fmt.Errorf("Error parsing config file %q: %s", f, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ch.Lock()
|
|
|
|
ch.Config = config
|
|
|
|
ch.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:32:44 +00:00
|
|
|
func (m AuthModule) ConfigureTarget(target string) (DSN, error) {
|
|
|
|
dsn, err := dsnFromString(target)
|
2022-03-04 21:39:48 +00:00
|
|
|
if err != nil {
|
2022-09-02 14:32:44 +00:00
|
|
|
return DSN{}, err
|
2022-03-04 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 14:32:44 +00:00
|
|
|
// Set the credentials from the authentication module
|
|
|
|
// TODO(@sysadmind): What should the order of precedence be?
|
2022-03-04 21:39:48 +00:00
|
|
|
if m.Type == "userpass" {
|
2022-09-02 14:32:44 +00:00
|
|
|
if m.UserPass.Username != "" {
|
|
|
|
dsn.username = m.UserPass.Username
|
|
|
|
}
|
|
|
|
if m.UserPass.Password != "" {
|
|
|
|
dsn.password = m.UserPass.Password
|
|
|
|
}
|
2022-03-04 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range m.Options {
|
2022-09-02 14:32:44 +00:00
|
|
|
dsn.query.Set(k, v)
|
2022-03-04 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 14:32:44 +00:00
|
|
|
return dsn, nil
|
2022-03-04 21:39:48 +00:00
|
|
|
}
|