* Removing some other fake apps

* More FakeApp removed

* Removing entirely FakeApp

* Fixing some tests

* Fixing get Cluster id from get plugin status

* Fixing failing tests

* Fixing tests

* Fixing test initialization for web

* Fixing InitServer for server tests

* Fixing InitServer for server tests

* Reverting go.sum and go.mod

* Removing unneded HTMLTemplates function in App layer

* Moving back some functions to its old place to easy the review

* Moving back some functions to its old place to easy the review

* Using the last struct2interface version

* Generating store layers

* Fixing merge problems

* Addressing PR comments

* Small fix

* Fixing app tests build

* Fixing tests

* fixing tests

* Fix tests

* Fixing tests

* Fixing tests

* Fixing tests

* Moving license to server struct

* Adding some fixes to the test compilation

* Fixing cluster and some jobs initialization

* Fixing some license tests compilation problems

* Fixing recursive cache invalidation

* Regenerating app layers

* Fix test compilation

Co-authored-by: mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Jesús Espino
2020-06-12 13:43:50 +02:00
коммит произвёл GitHub
родитель f3ac33e6dc
Коммит f5eab1271b
88 изменённых файлов: 973 добавлений и 1177 удалений

Просмотреть файл

@@ -15,19 +15,19 @@ import (
const requestTrialURL = "https://customers.mattermost.com/api/v1/trials"
func (a *App) LoadLicense() {
func (s *Server) LoadLicense() {
licenseId := ""
props, err := a.Srv().Store.System().Get()
props, err := s.Store.System().Get()
if err == nil {
licenseId = props[model.SYSTEM_ACTIVE_LICENSE_ID]
}
if !model.IsValidId(licenseId) {
// Lets attempt to load the file from disk since it was missing from the DB
license, licenseBytes := utils.GetAndValidateLicenseFileFromDisk(*a.Config().ServiceSettings.LicenseFileLocation)
license, licenseBytes := utils.GetAndValidateLicenseFileFromDisk(*s.Config().ServiceSettings.LicenseFileLocation)
if license != nil {
if _, err = a.SaveLicense(licenseBytes); err != nil {
if _, err = s.SaveLicense(licenseBytes); err != nil {
mlog.Info("Failed to save license key loaded from disk.", mlog.Err(err))
} else {
licenseId = license.Id
@@ -35,25 +35,25 @@ func (a *App) LoadLicense() {
}
}
record, err := a.Srv().Store.License().Get(licenseId)
record, err := s.Store.License().Get(licenseId)
if err != nil {
mlog.Info("License key from https://mattermost.com required to unlock enterprise features.")
a.SetLicense(nil)
s.SetLicense(nil)
return
}
a.ValidateAndSetLicenseBytes([]byte(record.Bytes))
s.ValidateAndSetLicenseBytes([]byte(record.Bytes))
mlog.Info("License key valid unlocking enterprise features.")
}
func (a *App) SaveLicense(licenseBytes []byte) (*model.License, *model.AppError) {
func (s *Server) SaveLicense(licenseBytes []byte) (*model.License, *model.AppError) {
success, licenseStr := utils.ValidateLicense(licenseBytes)
if !success {
return nil, model.NewAppError("addLicense", model.INVALID_LICENSE_ERROR, nil, "", http.StatusBadRequest)
}
license := model.LicenseFromJson(strings.NewReader(licenseStr))
uniqueUserCount, err := a.Srv().Store.User().Count(model.UserCountOptions{})
uniqueUserCount, err := s.Store.User().Count(model.UserCountOptions{})
if err != nil {
return nil, model.NewAppError("addLicense", "api.license.add_license.invalid_count.app_error", nil, err.Error(), http.StatusBadRequest)
}
@@ -66,7 +66,7 @@ func (a *App) SaveLicense(licenseBytes []byte) (*model.License, *model.AppError)
return nil, model.NewAppError("addLicense", model.EXPIRED_LICENSE_ERROR, nil, "", http.StatusBadRequest)
}
if ok := a.SetLicense(license); !ok {
if ok := s.SetLicense(license); !ok {
return nil, model.NewAppError("addLicense", model.EXPIRED_LICENSE_ERROR, nil, "", http.StatusBadRequest)
}
@@ -74,46 +74,41 @@ func (a *App) SaveLicense(licenseBytes []byte) (*model.License, *model.AppError)
record.Id = license.Id
record.Bytes = string(licenseBytes)
_, err = a.Srv().Store.License().Save(record)
_, err = s.Store.License().Save(record)
if err != nil {
a.RemoveLicense()
s.RemoveLicense()
return nil, model.NewAppError("addLicense", "api.license.add_license.save.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
}
sysVar := &model.System{}
sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID
sysVar.Value = license.Id
if err := a.Srv().Store.System().SaveOrUpdate(sysVar); err != nil {
a.RemoveLicense()
if err := s.Store.System().SaveOrUpdate(sysVar); err != nil {
s.RemoveLicense()
return nil, model.NewAppError("addLicense", "api.license.add_license.save_active.app_error", nil, "", http.StatusInternalServerError)
}
a.ReloadConfig()
a.InvalidateAllCaches()
s.ReloadConfig()
s.InvalidateAllCaches()
// start job server if necessary - this handles the edge case where a license file is uploaded, but the job server
// doesn't start until the server is restarted, which prevents the 'run job now' buttons in system console from
// functioning as expected
if *a.Config().JobSettings.RunJobs && a.Srv().Jobs != nil && a.Srv().Jobs.Workers != nil {
a.Srv().Jobs.StartWorkers()
if *s.Config().JobSettings.RunJobs && s.Jobs != nil && s.Jobs.Workers != nil {
s.Jobs.StartWorkers()
}
if *a.Config().JobSettings.RunScheduler && a.Srv().Jobs != nil && a.Srv().Jobs.Schedulers != nil {
a.Srv().Jobs.StartSchedulers()
if *s.Config().JobSettings.RunScheduler && s.Jobs != nil && s.Jobs.Schedulers != nil {
s.Jobs.StartSchedulers()
}
return license, nil
}
// License returns the currently active license or nil if the application is unlicensed.
func (a *App) License() *model.License {
return a.Srv().License()
}
func (a *App) SetLicense(license *model.License) bool {
oldLicense := a.Srv().licenseValue.Load()
func (s *Server) SetLicense(license *model.License) bool {
oldLicense := s.licenseValue.Load()
defer func() {
for _, listener := range a.Srv().licenseListeners {
for _, listener := range s.licenseListeners {
if oldLicense == nil {
listener(nil, license)
} else {
@@ -125,39 +120,39 @@ func (a *App) SetLicense(license *model.License) bool {
if license != nil {
license.Features.SetDefaults()
a.Srv().licenseValue.Store(license)
a.Srv().clientLicenseValue.Store(utils.GetClientLicense(license))
s.licenseValue.Store(license)
s.clientLicenseValue.Store(utils.GetClientLicense(license))
return true
}
a.Srv().licenseValue.Store((*model.License)(nil))
a.Srv().clientLicenseValue.Store(map[string]string(nil))
s.licenseValue.Store((*model.License)(nil))
s.clientLicenseValue.Store(map[string]string(nil))
return false
}
func (a *App) ValidateAndSetLicenseBytes(b []byte) {
func (s *Server) ValidateAndSetLicenseBytes(b []byte) {
if success, licenseStr := utils.ValidateLicense(b); success {
license := model.LicenseFromJson(strings.NewReader(licenseStr))
a.SetLicense(license)
s.SetLicense(license)
return
}
mlog.Warn("No valid enterprise license found")
}
func (a *App) SetClientLicense(m map[string]string) {
a.Srv().clientLicenseValue.Store(m)
func (s *Server) SetClientLicense(m map[string]string) {
s.clientLicenseValue.Store(m)
}
func (a *App) ClientLicense() map[string]string {
if clientLicense, _ := a.Srv().clientLicenseValue.Load().(map[string]string); clientLicense != nil {
func (s *Server) ClientLicense() map[string]string {
if clientLicense, _ := s.clientLicenseValue.Load().(map[string]string); clientLicense != nil {
return clientLicense
}
return map[string]string{"IsLicensed": "false"}
}
func (a *App) RemoveLicense() *model.AppError {
if license, _ := a.Srv().licenseValue.Load().(*model.License); license == nil {
func (s *Server) RemoveLicense() *model.AppError {
if license, _ := s.licenseValue.Load().(*model.License); license == nil {
return nil
}
@@ -167,14 +162,13 @@ func (a *App) RemoveLicense() *model.AppError {
sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID
sysVar.Value = ""
if err := a.Srv().Store.System().SaveOrUpdate(sysVar); err != nil {
if err := s.Store.System().SaveOrUpdate(sysVar); err != nil {
return err
}
a.SetLicense(nil)
a.ReloadConfig()
a.InvalidateAllCaches()
s.SetLicense(nil)
s.ReloadConfig()
s.InvalidateAllCaches()
return nil
}
@@ -185,24 +179,14 @@ func (s *Server) AddLicenseListener(listener func(oldLicense, newLicense *model.
return id
}
func (a *App) AddLicenseListener(listener func(oldLicense, newLicense *model.License)) string {
id := model.NewId()
a.Srv().licenseListeners[id] = listener
return id
}
func (s *Server) RemoveLicenseListener(id string) {
delete(s.licenseListeners, id)
}
func (a *App) RemoveLicenseListener(id string) {
delete(a.Srv().licenseListeners, id)
}
func (a *App) GetSanitizedClientLicense() map[string]string {
func (s *Server) GetSanitizedClientLicense() map[string]string {
sanitizedLicense := make(map[string]string)
for k, v := range a.ClientLicense() {
for k, v := range s.ClientLicense() {
sanitizedLicense[k] = v
}
@@ -219,7 +203,7 @@ func (a *App) GetSanitizedClientLicense() map[string]string {
}
// RequestTrialLicense request a trial license from the mattermost offical license server
func (a *App) RequestTrialLicense(trialRequest *model.TrialLicenseRequest) *model.AppError {
func (s *Server) RequestTrialLicense(trialRequest *model.TrialLicenseRequest) *model.AppError {
resp, err := http.Post(requestTrialURL, "application/json", bytes.NewBuffer([]byte(trialRequest.ToJson())))
if err != nil {
return model.NewAppError("RequestTrialLicense", "api.license.request_trial_license.app_error", nil, err.Error(), http.StatusBadRequest)
@@ -227,12 +211,12 @@ func (a *App) RequestTrialLicense(trialRequest *model.TrialLicenseRequest) *mode
defer resp.Body.Close()
licenseResponse := model.MapFromJson(resp.Body)
if _, err := a.SaveLicense([]byte(licenseResponse["license"])); err != nil {
if _, err := s.SaveLicense([]byte(licenseResponse["license"])); err != nil {
return err
}
a.ReloadConfig()
a.InvalidateAllCaches()
s.ReloadConfig()
s.InvalidateAllCaches()
return nil
}