MM-8400 Provide default config values to viper so that it reads all environment variables (#8581)
* MM-8400 Provide default config values to viper so that it reads all environment variables * Added unit tests
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
f9015a37f3
Коммит
ff077c6761
@@ -10,6 +10,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -212,15 +213,8 @@ func (w *ConfigWatcher) Close() {
|
|||||||
|
|
||||||
// ReadConfig reads and parses the given configuration.
|
// ReadConfig reads and parses the given configuration.
|
||||||
func ReadConfig(r io.Reader, allowEnvironmentOverrides bool) (*model.Config, error) {
|
func ReadConfig(r io.Reader, allowEnvironmentOverrides bool) (*model.Config, error) {
|
||||||
v := viper.New()
|
v := newViper(allowEnvironmentOverrides)
|
||||||
|
|
||||||
if allowEnvironmentOverrides {
|
|
||||||
v.SetEnvPrefix("mm")
|
|
||||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
||||||
v.AutomaticEnv()
|
|
||||||
}
|
|
||||||
|
|
||||||
v.SetConfigType("json")
|
|
||||||
if err := v.ReadConfig(r); err != nil {
|
if err := v.ReadConfig(r); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -236,6 +230,89 @@ func ReadConfig(r io.Reader, allowEnvironmentOverrides bool) (*model.Config, err
|
|||||||
return &config, unmarshalErr
|
return &config, unmarshalErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newViper(allowEnvironmentOverrides bool) *viper.Viper {
|
||||||
|
v := viper.New()
|
||||||
|
|
||||||
|
v.SetConfigType("json")
|
||||||
|
|
||||||
|
if allowEnvironmentOverrides {
|
||||||
|
v.SetEnvPrefix("mm")
|
||||||
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||||
|
v.AutomaticEnv()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set zeroed defaults for all the config settings so that Viper knows what environment variables
|
||||||
|
// it needs to be looking for. The correct defaults will later be applied using Config.SetDefaults.
|
||||||
|
defaults := flattenStructToMap(structToMap(reflect.TypeOf(model.Config{})))
|
||||||
|
|
||||||
|
for key, value := range defaults {
|
||||||
|
v.SetDefault(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Converts a struct type into a nested map with keys matching the struct's fields and values
|
||||||
|
// matching the zeroed value of the corresponding field.
|
||||||
|
func structToMap(t reflect.Type) map[string]interface{} {
|
||||||
|
if t.Kind() != reflect.Struct {
|
||||||
|
// Should never hit this, but this will prevent a panic if that does happen somehow
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
out := make(map[string]interface{})
|
||||||
|
|
||||||
|
for i := 0; i < t.NumField(); i++ {
|
||||||
|
field := t.Field(i)
|
||||||
|
|
||||||
|
var value interface{}
|
||||||
|
|
||||||
|
switch field.Type.Kind() {
|
||||||
|
case reflect.Struct:
|
||||||
|
value = structToMap(field.Type)
|
||||||
|
case reflect.Ptr:
|
||||||
|
value = nil
|
||||||
|
default:
|
||||||
|
value = reflect.Zero(field.Type).Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
out[field.Name] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flattens a nested map so that the result is a single map with keys corresponding to the
|
||||||
|
// path through the original map. For example,
|
||||||
|
// {
|
||||||
|
// "a": {
|
||||||
|
// "b": 1
|
||||||
|
// },
|
||||||
|
// "c": "sea"
|
||||||
|
// }
|
||||||
|
// would flatten to
|
||||||
|
// {
|
||||||
|
// "a.b": 1,
|
||||||
|
// "c": "sea"
|
||||||
|
// }
|
||||||
|
func flattenStructToMap(in map[string]interface{}) map[string]interface{} {
|
||||||
|
out := make(map[string]interface{})
|
||||||
|
|
||||||
|
for key, value := range in {
|
||||||
|
if valueAsMap, ok := value.(map[string]interface{}); ok {
|
||||||
|
sub := flattenStructToMap(valueAsMap)
|
||||||
|
|
||||||
|
for subKey, subValue := range sub {
|
||||||
|
out[key+"."+subKey] = subValue
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
out[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
// ReadConfigFile reads and parses the configuration at the given file path.
|
// ReadConfigFile reads and parses the configuration at the given file path.
|
||||||
func ReadConfigFile(path string, allowEnvironmentOverrides bool) (*model.Config, error) {
|
func ReadConfigFile(path string, allowEnvironmentOverrides bool) (*model.Config, error) {
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
|
|||||||
@@ -50,48 +50,80 @@ func TestFindConfigFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConfigFromEnviroVars(t *testing.T) {
|
func TestConfigFromEnviroVars(t *testing.T) {
|
||||||
os.Setenv("MM_TEAMSETTINGS_SITENAME", "From Environment")
|
|
||||||
os.Setenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT", "Custom Brand")
|
|
||||||
os.Setenv("MM_SERVICESETTINGS_ENABLECOMMANDS", "false")
|
|
||||||
os.Setenv("MM_SERVICESETTINGS_READTIMEOUT", "400")
|
|
||||||
|
|
||||||
TranslationsPreInit()
|
TranslationsPreInit()
|
||||||
cfg, cfgPath, err := LoadConfig("config.json")
|
|
||||||
require.Nil(t, err)
|
|
||||||
|
|
||||||
if cfg.TeamSettings.SiteName != "From Environment" {
|
config := `{
|
||||||
t.Fatal("Couldn't read config from environment var")
|
"ServiceSettings": {
|
||||||
}
|
"EnableCommands": true,
|
||||||
|
"ReadTimeout": 100
|
||||||
|
},
|
||||||
|
"TeamSettings": {
|
||||||
|
"SiteName": "Mattermost",
|
||||||
|
"CustomBrandText": ""
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
if *cfg.TeamSettings.CustomBrandText != "Custom Brand" {
|
t.Run("string settings", func(t *testing.T) {
|
||||||
t.Fatal("Couldn't read config from environment var")
|
os.Setenv("MM_TEAMSETTINGS_SITENAME", "From Environment")
|
||||||
}
|
os.Setenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT", "Custom Brand")
|
||||||
|
|
||||||
if *cfg.ServiceSettings.EnableCommands {
|
cfg, err := ReadConfig(strings.NewReader(config), true)
|
||||||
t.Fatal("Couldn't read config from environment var")
|
require.Nil(t, err)
|
||||||
}
|
|
||||||
|
|
||||||
if *cfg.ServiceSettings.ReadTimeout != 400 {
|
if cfg.TeamSettings.SiteName != "From Environment" {
|
||||||
t.Fatal("Couldn't read config from environment var")
|
t.Fatal("Couldn't read config from environment var")
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Unsetenv("MM_TEAMSETTINGS_SITENAME")
|
if *cfg.TeamSettings.CustomBrandText != "Custom Brand" {
|
||||||
os.Unsetenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT")
|
t.Fatal("Couldn't read config from environment var")
|
||||||
os.Unsetenv("MM_SERVICESETTINGS_ENABLECOMMANDS")
|
}
|
||||||
os.Unsetenv("MM_SERVICESETTINGS_READTIMEOUT")
|
|
||||||
|
|
||||||
cfg.TeamSettings.SiteName = "Mattermost"
|
os.Unsetenv("MM_TEAMSETTINGS_SITENAME")
|
||||||
*cfg.ServiceSettings.SiteURL = ""
|
os.Unsetenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT")
|
||||||
*cfg.ServiceSettings.EnableCommands = true
|
|
||||||
*cfg.ServiceSettings.ReadTimeout = 300
|
|
||||||
SaveConfig(cfgPath, cfg)
|
|
||||||
|
|
||||||
cfg, _, err = LoadConfig("config.json")
|
cfg, err = ReadConfig(strings.NewReader(config), true)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|
||||||
if cfg.TeamSettings.SiteName != "Mattermost" {
|
if cfg.TeamSettings.SiteName != "Mattermost" {
|
||||||
t.Fatal("should have been reset")
|
t.Fatal("should have been reset")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("boolean setting", func(t *testing.T) {
|
||||||
|
os.Setenv("MM_SERVICESETTINGS_ENABLECOMMANDS", "false")
|
||||||
|
defer os.Unsetenv("MM_SERVICESETTINGS_ENABLECOMMANDS")
|
||||||
|
|
||||||
|
cfg, err := ReadConfig(strings.NewReader(config), true)
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
if *cfg.ServiceSettings.EnableCommands {
|
||||||
|
t.Fatal("Couldn't read config from environment var")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("integer setting", func(t *testing.T) {
|
||||||
|
os.Setenv("MM_SERVICESETTINGS_READTIMEOUT", "400")
|
||||||
|
defer os.Unsetenv("MM_SERVICESETTINGS_READTIMEOUT")
|
||||||
|
|
||||||
|
cfg, err := ReadConfig(strings.NewReader(config), true)
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
if *cfg.ServiceSettings.ReadTimeout != 400 {
|
||||||
|
t.Fatal("Couldn't read config from environment var")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("setting missing from config.json", func(t *testing.T) {
|
||||||
|
os.Setenv("MM_SERVICESETTINGS_SITEURL", "https://example.com")
|
||||||
|
defer os.Unsetenv("MM_SERVICESETTINGS_SITEURL")
|
||||||
|
|
||||||
|
cfg, err := ReadConfig(strings.NewReader(config), true)
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
if *cfg.ServiceSettings.SiteURL != "https://example.com" {
|
||||||
|
t.Fatal("Couldn't read config from environment var")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidateLocales(t *testing.T) {
|
func TestValidateLocales(t *testing.T) {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user