Files
mattermost/config/common.go
Christopher Poile 7f7f511d1c MM-11697: Environment overrides do not overwrite config.json on save (#10413)
* MM-11697: Environment overrides do not overwrite config.json on save
#10388

The config store now keeps a copy of the config as loaded from the store
without environment overrides. Whenever persisting, we now check if the
current setting is different from the loaded setting. If it is, then use
the loaded setting instead.

As described in the comments to `removeEnvOverrides` in `common.go`,
this behavior will have to change if we ever let the user change a
setting that has been environmentally overriden.

This was interesting because the `load` function in `common.go` also
persists, so we have to tee the provided `io.ReadCloser` and construct a
config that doesn't have the environment overrides. And then we have to
find the path to the (maybe) changed variable in the config struct
using reflection.

Possible WIP: I had to expose a `GetWithoutEnvOverrides` function in the
Store interface just for the tests -- this is because the `file_test`
and `database_test`s are in the config_test package instead of the
`config` package.

* added function documentation

* fixed a small problem with tests

* MM-11697: big cleanup based on Jesse's PR comments

* MM-11697: edits per PR feedback

* MM-11697: licence header

* MM-11697: now testing that on disk config is not changed by env
overrides

* MM-11697: remove unneeded exports
2019-03-26 13:28:41 -07:00

162 lines
5.2 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package config
import (
"bytes"
"io"
"sync"
"github.com/mattermost/mattermost-server/model"
"github.com/pkg/errors"
)
// commonStore enables code sharing between different backing implementations
type commonStore struct {
emitter
configLock sync.RWMutex
config *model.Config
configWithoutOverrides *model.Config
environmentOverrides map[string]interface{}
}
// Get fetches the current, cached configuration.
func (cs *commonStore) Get() *model.Config {
cs.configLock.RLock()
defer cs.configLock.RUnlock()
return cs.config
}
// GetEnvironmentOverrides fetches the configuration fields overridden by environment variables.
func (cs *commonStore) GetEnvironmentOverrides() map[string]interface{} {
cs.configLock.RLock()
defer cs.configLock.RUnlock()
return cs.environmentOverrides
}
// set replaces the current configuration in its entirety, and updates the backing store
// using the persist function argument.
//
// This function assumes no lock has been acquired, as it acquires a write lock itself.
func (cs *commonStore) set(newCfg *model.Config, validate func(*model.Config) error, persist func(*model.Config) error) (*model.Config, error) {
cs.configLock.Lock()
var unlockOnce sync.Once
defer unlockOnce.Do(cs.configLock.Unlock)
oldCfg := cs.config
// TODO: disallow attempting to save a directly modified config (comparing pointers). This
// wouldn't be an exhaustive check, given the use of pointers throughout the data
// structure, but might prevent common mistakes. Requires upstream changes first.
// if newCfg == oldCfg {
// return nil, errors.New("old configuration modified instead of cloning")
// }
newCfg = newCfg.Clone()
newCfg.SetDefaults()
// Sometimes the config is received with "fake" data in sensitive fields. Apply the real
// data from the existing config as necessary.
desanitize(oldCfg, newCfg)
if validate != nil {
if err := validate(newCfg); err != nil {
return nil, errors.Wrap(err, "new configuration is invalid")
}
}
if err := persist(cs.removeEnvOverrides(newCfg)); err != nil {
return nil, errors.Wrap(err, "failed to persist")
}
cs.config = newCfg
unlockOnce.Do(cs.configLock.Unlock)
// Notify listeners synchronously. Ideally, this would be asynchronous, but existing code
// assumes this and there would be increased complexity to avoid racing updates.
cs.invokeConfigListeners(oldCfg, newCfg)
return oldCfg, nil
}
// load updates the current configuration from the given io.ReadCloser.
//
// This function assumes no lock has been acquired, as it acquires a write lock itself.
func (cs *commonStore) load(f io.ReadCloser, needsSave bool, validate func(*model.Config) error, persist func(*model.Config) error) error {
// Duplicate f so that we can read a configuration without applying environment overrides
f2 := new(bytes.Buffer)
tee := io.TeeReader(f, f2)
allowEnvironmentOverrides := true
loadedCfg, environmentOverrides, err := unmarshalConfig(tee, allowEnvironmentOverrides)
if err != nil {
return errors.Wrapf(err, "failed to unmarshal config with env overrides")
}
// Keep track of the original values that the Environment settings overrode
loadedCfgWithoutEnvOverrides, _, err := unmarshalConfig(f2, false)
if err != nil {
return errors.Wrapf(err, "failed to unmarshal config without env overrides")
}
// SetDefaults generates various keys and salts if not previously configured. Determine if
// such a change will be made before invoking.
needsSave = needsSave || loadedCfg.SqlSettings.AtRestEncryptKey == nil || len(*loadedCfg.SqlSettings.AtRestEncryptKey) == 0
needsSave = needsSave || loadedCfg.FileSettings.PublicLinkSalt == nil || len(*loadedCfg.FileSettings.PublicLinkSalt) == 0
loadedCfg.SetDefaults()
if validate != nil {
if err = validate(loadedCfg); err != nil {
return errors.Wrap(err, "invalid config")
}
}
if changed := fixConfig(loadedCfg); changed {
needsSave = true
}
cs.configLock.Lock()
var unlockOnce sync.Once
defer unlockOnce.Do(cs.configLock.Unlock)
if needsSave && persist != nil {
cfgWithoutEnvOverrides := removeEnvOverrides(loadedCfg, loadedCfgWithoutEnvOverrides, environmentOverrides)
if err = persist(cfgWithoutEnvOverrides); err != nil {
return errors.Wrap(err, "failed to persist required changes after load")
}
}
oldCfg := cs.config
cs.config = loadedCfg
cs.configWithoutOverrides = loadedCfgWithoutEnvOverrides
cs.environmentOverrides = environmentOverrides
unlockOnce.Do(cs.configLock.Unlock)
// Notify listeners synchronously. Ideally, this would be asynchronous, but existing code
// assumes this and there would be increased complexity to avoid racing updates.
cs.invokeConfigListeners(oldCfg, loadedCfg)
return nil
}
// validate checks if the given configuration is valid
func (cs *commonStore) validate(cfg *model.Config) error {
if err := cfg.IsValid(); err != nil {
return err
}
return nil
}
// removeEnvOverrides returns a new config without the given environment overrides.
func (cs *commonStore) removeEnvOverrides(cfg *model.Config) *model.Config {
return removeEnvOverrides(cfg, cs.configWithoutOverrides, cs.environmentOverrides)
}