Settings: Fix data race when dynamically overriding settings with environment variables (#81667)

Chore: Fix data race when dynamically overriding settings with environment variables
This commit is contained in:
Diego Augusto Molina
2024-02-05 12:25:54 -03:00
committed by GitHub
parent c87e4eb724
commit b02f0b926a
4 changed files with 135 additions and 7 deletions
+12 -7
View File
@@ -32,6 +32,7 @@ import (
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models/roletype"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/util/osutil"
)
type Scheme string
@@ -822,6 +823,9 @@ func (cfg *Cfg) loadSpecifiedConfigFile(configFile string, masterFile *ini.File)
return fmt.Errorf("failed to parse %q: %w", configFile, err)
}
// micro-optimization since we don't need to share this ini file. In
// general, prefer to leave this flag as true as it is by default to prevent
// data races
userConfig.BlockMode = false
for _, section := range userConfig.Sections() {
@@ -865,8 +869,6 @@ func (cfg *Cfg) loadConfiguration(args CommandLineArgs) (*ini.File, error) {
return nil, err
}
parsedFile.BlockMode = false
// command line props
commandLineProps := cfg.getCommandLineProperties(args.Args)
// load default overrides
@@ -987,8 +989,6 @@ func NewCfgFromBytes(bytes []byte) (*Cfg, error) {
return nil, fmt.Errorf("failed to parse bytes as INI file: %w", err)
}
parsedFile.BlockMode = false
return NewCfgFromINIFile(parsedFile)
}
@@ -1398,13 +1398,14 @@ func (cfg *Cfg) LogConfigSources() {
type DynamicSection struct {
section *ini.Section
Logger log.Logger
env osutil.Env
}
// Key dynamically overrides keys with environment variables.
// As a side effect, the value of the setting key will be updated if an environment variable is present.
func (s *DynamicSection) Key(k string) *ini.Key {
envKey := EnvKey(s.section.Name(), k)
envValue := os.Getenv(envKey)
envValue := s.env.Getenv(envKey)
key := s.section.Key(k)
if len(envValue) == 0 {
@@ -1421,7 +1422,7 @@ func (s *DynamicSection) KeysHash() map[string]string {
hash := s.section.KeysHash()
for k := range hash {
envKey := EnvKey(s.section.Name(), k)
envValue := os.Getenv(envKey)
envValue := s.env.Getenv(envKey)
if len(envValue) > 0 {
hash[k] = envValue
}
@@ -1432,7 +1433,11 @@ func (s *DynamicSection) KeysHash() map[string]string {
// SectionWithEnvOverrides dynamically overrides keys with environment variables.
// As a side effect, the value of the setting key will be updated if an environment variable is present.
func (cfg *Cfg) SectionWithEnvOverrides(s string) *DynamicSection {
return &DynamicSection{cfg.Raw.Section(s), cfg.Logger}
return &DynamicSection{
section: cfg.Raw.Section(s),
Logger: cfg.Logger,
env: osutil.RealEnv{},
}
}
func readSecuritySettings(iniFile *ini.File, cfg *Cfg) error {