mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* 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
62 lines
1.9 KiB
Go
62 lines
1.9 KiB
Go
// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package config
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
)
|
|
|
|
// removeEnvOverrides returns a new config without the given environment overrides.
|
|
// If a config variable has an environment override, that variable is set to the value that was
|
|
// read from the store.
|
|
func removeEnvOverrides(cfg, cfgWithoutEnv *model.Config, envOverrides map[string]interface{}) *model.Config {
|
|
paths := getPaths(envOverrides)
|
|
newCfg := cfg.Clone()
|
|
for _, path := range paths {
|
|
originalVal := getVal(cfgWithoutEnv, path)
|
|
getVal(newCfg, path).Set(originalVal)
|
|
}
|
|
return newCfg
|
|
}
|
|
|
|
// getPaths turns a nested map into a slice of paths describing the keys of the map. Eg:
|
|
// map[string]map[string]map[string]bool{"this":{"is first":{"path":true}, "is second":{"path":true}))) is turned into:
|
|
// [][]string{{"this", "is first", "path"}, {"this", "is second", "path"}}
|
|
func getPaths(m map[string]interface{}) [][]string {
|
|
return getPathsRec(m, nil)
|
|
}
|
|
|
|
// getPathsRec assembles the paths (see `getPaths` above)
|
|
func getPathsRec(src interface{}, curPath []string) [][]string {
|
|
if srcMap, ok := src.(map[string]interface{}); ok {
|
|
paths := [][]string{}
|
|
for k, v := range srcMap {
|
|
paths = append(paths, getPathsRec(v, append(curPath, k))...)
|
|
}
|
|
return paths
|
|
}
|
|
|
|
return [][]string{curPath}
|
|
}
|
|
|
|
// getVal walks `src` (here it starts with a model.Config, then recurses into its leaves)
|
|
// and returns the reflect.Value of the leaf at the end `path`
|
|
func getVal(src interface{}, path []string) reflect.Value {
|
|
var val reflect.Value
|
|
if reflect.ValueOf(src).Kind() == reflect.Ptr {
|
|
val = reflect.ValueOf(src).Elem().FieldByName(path[0])
|
|
} else {
|
|
val = reflect.ValueOf(src).FieldByName(path[0])
|
|
}
|
|
if val.Kind() == reflect.Ptr {
|
|
val = val.Elem()
|
|
}
|
|
if val.Kind() == reflect.Struct {
|
|
return getVal(val.Interface(), path[1:])
|
|
}
|
|
return val
|
|
}
|