From 00bb4799899cbbcdf48b147639dee3cee7ea199b Mon Sep 17 00:00:00 2001 From: Corey Hulen Date: Fri, 31 Mar 2017 06:54:30 -0700 Subject: PLT-6090 adding ability to read license file from disk (#5895) * PLT-6090 adding ability to read license file from disk * Fixing unit test that fails only sometimes * Fixing test that fails randomly --- app/license.go | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) (limited to 'app/license.go') diff --git a/app/license.go b/app/license.go index 1efaf85d5..6b448b19d 100644 --- a/app/license.go +++ b/app/license.go @@ -4,6 +4,7 @@ package app import ( + "os" "strings" l4g "github.com/alecthomas/log4go" @@ -21,13 +22,36 @@ func LoadLicense() { } if len(licenseId) != 26 { - l4g.Info(utils.T("mattermost.load_license.find.warn")) - return + // Lets attempt to load the file from disk since it was missing from the DB + fileName := utils.GetLicenseFileLocation(*utils.Cfg.ServiceSettings.LicenseFileLocation) + + if _, err := os.Stat(fileName); err == nil { + l4g.Info("License key has not been uploaded. Loading license key from disk at %v", fileName) + licenseBytes := utils.GetLicenseFileFromDisk(fileName) + + if success, licenseStr := utils.ValidateLicense(licenseBytes); success { + licenseFileFromDisk := model.LicenseFromJson(strings.NewReader(licenseStr)) + licenseId = licenseFileFromDisk.Id + if _, err := SaveLicense(licenseBytes); err != nil { + l4g.Info("Failed to save license key loaded from disk err=%v", err.Error()) + return + } + } else { + l4g.Error("Found license key at %v but it appears to be invalid.", fileName) + return + } + + } else { + l4g.Info(utils.T("mattermost.load_license.find.warn")) + l4g.Debug("We could not find the license key in the database or on disk at %v", fileName) + return + } } if result := <-Srv.Store.License().Get(licenseId); result.Err == nil { record := result.Data.(*model.LicenseRecord) utils.LoadLicense([]byte(record.Bytes)) + l4g.Info("License key valid unlocking enterprise features.") } else { l4g.Info(utils.T("mattermost.load_license.find.warn")) } @@ -58,16 +82,16 @@ func SaveLicense(licenseBytes []byte) (*model.License, *model.AppError) { record.Bytes = string(licenseBytes) rchan := Srv.Store.License().Save(record) - sysVar := &model.System{} - sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID - sysVar.Value = license.Id - schan := Srv.Store.System().SaveOrUpdate(sysVar) - if result := <-rchan; result.Err != nil { RemoveLicense() return nil, model.NewLocAppError("addLicense", "api.license.add_license.save.app_error", nil, "err="+result.Err.Error()) } + sysVar := &model.System{} + sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID + sysVar.Value = license.Id + schan := Srv.Store.System().SaveOrUpdate(sysVar) + if result := <-schan; result.Err != nil { RemoveLicense() return nil, model.NewLocAppError("addLicense", "api.license.add_license.save_active.app_error", nil, "") @@ -76,10 +100,6 @@ func SaveLicense(licenseBytes []byte) (*model.License, *model.AppError) { return nil, model.NewLocAppError("addLicense", model.INVALID_LICENSE_ERROR, nil, "") } - ReloadConfig() - - InvalidateAllCaches() - return license, nil } -- cgit v1.2.3-1-g7c22