diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/config.go | 38 | ||||
-rw-r--r-- | utils/config_test.go | 79 | ||||
-rw-r--r-- | utils/license.go | 19 |
3 files changed, 136 insertions, 0 deletions
diff --git a/utils/config.go b/utils/config.go index 234acae11..95cfc43aa 100644 --- a/utils/config.go +++ b/utils/config.go @@ -50,6 +50,22 @@ func SetSiteURL(url string) { siteURL = strings.TrimRight(url, "/") } +var cfgListeners = map[string]func(*model.Config, *model.Config){} + +// Registers a function with a given to be called when the config is reloaded and may have changed. The function +// will be called with two arguments: the old config and the new config. AddConfigListener returns a unique ID +// for the listener that can later be used to remove it. +func AddConfigListener(listener func(*model.Config, *model.Config)) string { + id := model.NewId() + cfgListeners[id] = listener + return id +} + +// Removes a listener function by the unique ID returned when AddConfigListener was called +func RemoveConfigListener(id string) { + delete(cfgListeners, id) +} + func FindConfigFile(fileName string) string { if _, err := os.Stat("./config/" + fileName); err == nil { fileName, _ = filepath.Abs("./config/" + fileName) @@ -242,6 +258,21 @@ func DisableConfigWatch() { } } +func InitAndLoadConfig(filename string) (err string) { + defer func() { + if r := recover(); r != nil { + err = fmt.Sprintf("%v", r) + } + }() + TranslationsPreInit() + EnableConfigFromEnviromentVars() + LoadConfig(filename) + InitializeConfigWatch() + EnableConfigWatch() + + return "" +} + // LoadConfig will try to search around for the corresponding config file. // It will search /tmp/fileName then attempt ./config/fileName, // then ../config/fileName and last it will look at fileName @@ -249,6 +280,9 @@ func LoadConfig(fileName string) { cfgMutex.Lock() defer cfgMutex.Unlock() + // Cfg should never be null + oldConfig := *Cfg + fileNameWithExtension := filepath.Base(fileName) fileExtension := filepath.Ext(fileNameWithExtension) fileDir := filepath.Dir(fileName) @@ -339,6 +373,10 @@ func LoadConfig(fileName string) { SetDefaultRolesBasedOnConfig() SetSiteURL(*Cfg.ServiceSettings.SiteURL) + + for _, listener := range cfgListeners { + listener(&oldConfig, &config) + } } func RegenerateClientConfig() { diff --git a/utils/config_test.go b/utils/config_test.go index 755cd9acd..bce85d2ae 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -6,6 +6,8 @@ package utils import ( "os" "testing" + + "github.com/mattermost/platform/model" ) func TestConfig(t *testing.T) { @@ -59,3 +61,80 @@ func TestConfigFromEnviroVars(t *testing.T) { } } + +func TestAddRemoveConfigListener(t *testing.T) { + if len(cfgListeners) != 0 { + t.Fatal("should've started with 0 listeners") + } + + id1 := AddConfigListener(func(*model.Config, *model.Config) { + }) + if len(cfgListeners) != 1 { + t.Fatal("should now have 1 listener") + } + + id2 := AddConfigListener(func(*model.Config, *model.Config) { + }) + if len(cfgListeners) != 2 { + t.Fatal("should now have 2 listeners") + } + + RemoveConfigListener(id1) + if len(cfgListeners) != 1 { + t.Fatal("should've removed first listener") + } + + RemoveConfigListener(id2) + if len(cfgListeners) != 0 { + t.Fatal("should've removed both listeners") + } +} + +func TestConfigListener(t *testing.T) { + TranslationsPreInit() + EnableConfigFromEnviromentVars() + LoadConfig("config.json") + + SiteName := Cfg.TeamSettings.SiteName + defer func() { + Cfg.TeamSettings.SiteName = SiteName + SaveConfig(CfgFileName, Cfg) + }() + Cfg.TeamSettings.SiteName = "test123" + + listenerCalled := false + listener := func(oldConfig *model.Config, newConfig *model.Config) { + if listenerCalled { + t.Fatal("listener called twice") + } + + if oldConfig.TeamSettings.SiteName != "test123" { + t.Fatal("old config contains incorrect site name") + } else if newConfig.TeamSettings.SiteName != "Mattermost" { + t.Fatal("new config contains incorrect site name") + } + + listenerCalled = true + } + listenerId := AddConfigListener(listener) + defer RemoveConfigListener(listenerId) + + listener2Called := false + listener2 := func(oldConfig *model.Config, newConfig *model.Config) { + if listener2Called { + t.Fatal("listener2 called twice") + } + + listener2Called = true + } + listener2Id := AddConfigListener(listener2) + defer RemoveConfigListener(listener2Id) + + LoadConfig("config.json") + + if !listenerCalled { + t.Fatal("listener should've been called") + } else if !listener2Called { + t.Fatal("listener 2 should've been called") + } +} diff --git a/utils/license.go b/utils/license.go index d3e2c1362..c0a17bf79 100644 --- a/utils/license.go +++ b/utils/license.go @@ -114,6 +114,25 @@ func ValidateLicense(signed []byte) (bool, string) { return true, string(plaintext) } +func GetAndValidateLicenseFileFromDisk() (*model.License, []byte) { + fileName := GetLicenseFileLocation(*Cfg.ServiceSettings.LicenseFileLocation) + + if _, err := os.Stat(fileName); err != nil { + l4g.Debug("We could not find the license key in the database or on disk at %v", fileName) + return nil, nil + } + + l4g.Info("License key has not been uploaded. Loading license key from disk at %v", fileName) + licenseBytes := GetLicenseFileFromDisk(fileName) + + if success, licenseStr := ValidateLicense(licenseBytes); !success { + l4g.Error("Found license key at %v but it appears to be invalid.", fileName) + return nil, nil + } else { + return model.LicenseFromJson(strings.NewReader(licenseStr)), licenseBytes + } +} + func GetLicenseFileFromDisk(fileName string) []byte { file, err := os.Open(fileName) if err != nil { |