mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-23 22:17:14 +02:00
authenticate: allow changing the authenticate service URL at runtime (#3378)
* config: better change detection * wip * fix middleware * add middleware before handlers * use ctx
This commit is contained in:
parent
9baaea5e85
commit
fd82cc7870
6 changed files with 104 additions and 51 deletions
|
@ -6,7 +6,6 @@ import (
|
|||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog"
|
||||
|
||||
|
@ -90,6 +89,7 @@ func (src *StaticSource) OnConfigChange(ctx context.Context, li ChangeListener)
|
|||
// A FileOrEnvironmentSource retrieves config options from a file or the environment.
|
||||
type FileOrEnvironmentSource struct {
|
||||
configFile string
|
||||
watcher *fileutil.Watcher
|
||||
|
||||
mu sync.RWMutex
|
||||
config *Config
|
||||
|
@ -134,35 +134,40 @@ func NewFileOrEnvironmentSource(
|
|||
|
||||
src := &FileOrEnvironmentSource{
|
||||
configFile: configFile,
|
||||
watcher: fileutil.NewWatcher(),
|
||||
config: cfg,
|
||||
}
|
||||
options.viper.OnConfigChange(src.onConfigChange(ctx))
|
||||
go options.viper.WatchConfig()
|
||||
src.watcher.Add(configFile)
|
||||
ch := src.watcher.Bind()
|
||||
go func() {
|
||||
for range ch {
|
||||
src.check(ctx)
|
||||
}
|
||||
}()
|
||||
|
||||
return src, nil
|
||||
}
|
||||
|
||||
func (src *FileOrEnvironmentSource) onConfigChange(ctx context.Context) func(fsnotify.Event) {
|
||||
return func(evt fsnotify.Event) {
|
||||
ctx := log.WithContext(ctx, func(c zerolog.Context) zerolog.Context {
|
||||
return c.Str("config_change_id", uuid.New().String())
|
||||
})
|
||||
log.Info(ctx).Msg("config: file updated, reconfiguring...")
|
||||
src.mu.Lock()
|
||||
cfg := src.config
|
||||
options, err := newOptionsFromConfig(src.configFile)
|
||||
if err == nil {
|
||||
cfg = cfg.Clone()
|
||||
cfg.Options = options
|
||||
metrics.SetConfigInfo(ctx, cfg.Options.Services, "local", cfg.Checksum(), true)
|
||||
} else {
|
||||
log.Error(ctx).Err(err).Msg("config: error updating config")
|
||||
metrics.SetConfigInfo(ctx, cfg.Options.Services, "local", cfg.Checksum(), false)
|
||||
}
|
||||
src.mu.Unlock()
|
||||
|
||||
src.Trigger(ctx, cfg)
|
||||
func (src *FileOrEnvironmentSource) check(ctx context.Context) {
|
||||
ctx = log.WithContext(ctx, func(c zerolog.Context) zerolog.Context {
|
||||
return c.Str("config_change_id", uuid.New().String())
|
||||
})
|
||||
log.Info(ctx).Msg("config: file updated, reconfiguring...")
|
||||
src.mu.Lock()
|
||||
cfg := src.config
|
||||
options, err := newOptionsFromConfig(src.configFile)
|
||||
if err == nil {
|
||||
cfg = cfg.Clone()
|
||||
cfg.Options = options
|
||||
metrics.SetConfigInfo(ctx, cfg.Options.Services, "local", cfg.Checksum(), true)
|
||||
} else {
|
||||
log.Error(ctx).Err(err).Msg("config: error updating config")
|
||||
metrics.SetConfigInfo(ctx, cfg.Options.Services, "local", cfg.Checksum(), false)
|
||||
}
|
||||
src.config = cfg
|
||||
src.mu.Unlock()
|
||||
|
||||
src.Trigger(ctx, cfg)
|
||||
}
|
||||
|
||||
// GetConfig gets the config.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue