Reload license from DB for all cluster app servers (#5525)
* Reload license from DB for all cluster app servers * Increase test timeout
Этот коммит содержится в:
коммит произвёл
Harrison Healey
родитель
5a3bc43668
Коммит
5e9adddb6f
2
Makefile
2
Makefile
@@ -202,7 +202,7 @@ test-server: start-docker prepare-enterprise
|
|||||||
rm -f cover.out
|
rm -f cover.out
|
||||||
echo "mode: count" > cover.out
|
echo "mode: count" > cover.out
|
||||||
|
|
||||||
$(GO) test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=650s -covermode=count -coverprofile=capi.out ./api || exit 1
|
$(GO) test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=1050s -covermode=count -coverprofile=capi.out ./api || exit 1
|
||||||
$(GO) test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=650s -covermode=count -coverprofile=capi4.out ./api4 || exit 1
|
$(GO) test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=650s -covermode=count -coverprofile=capi4.out ./api4 || exit 1
|
||||||
$(GO) test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=60s -covermode=count -coverprofile=capp.out ./app || exit 1
|
$(GO) test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=60s -covermode=count -coverprofile=capp.out ./app || exit 1
|
||||||
$(GO) test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=60s -covermode=count -coverprofile=cmodel.out ./model || exit 1
|
$(GO) test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=60s -covermode=count -coverprofile=cmodel.out ./model || exit 1
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
l4g "github.com/alecthomas/log4go"
|
l4g "github.com/alecthomas/log4go"
|
||||||
"github.com/mattermost/platform/app"
|
"github.com/mattermost/platform/app"
|
||||||
@@ -15,11 +14,6 @@ import (
|
|||||||
"github.com/mattermost/platform/utils"
|
"github.com/mattermost/platform/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
EXPIRED_LICENSE_ERROR = "api.license.add_license.expired.app_error"
|
|
||||||
INVALID_LICENSE_ERROR = "api.license.add_license.invalid.app_error"
|
|
||||||
)
|
|
||||||
|
|
||||||
func InitLicense() {
|
func InitLicense() {
|
||||||
l4g.Debug(utils.T("api.license.init.debug"))
|
l4g.Debug(utils.T("api.license.init.debug"))
|
||||||
|
|
||||||
@@ -28,26 +22,6 @@ func InitLicense() {
|
|||||||
BaseRoutes.License.Handle("/client_config", ApiAppHandler(getClientLicenceConfig)).Methods("GET")
|
BaseRoutes.License.Handle("/client_config", ApiAppHandler(getClientLicenceConfig)).Methods("GET")
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadLicense() {
|
|
||||||
licenseId := ""
|
|
||||||
if result := <-app.Srv.Store.System().Get(); result.Err == nil {
|
|
||||||
props := result.Data.(model.StringMap)
|
|
||||||
licenseId = props[model.SYSTEM_ACTIVE_LICENSE_ID]
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(licenseId) != 26 {
|
|
||||||
l4g.Info(utils.T("mattermost.load_license.find.warn"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if result := <-app.Srv.Store.License().Get(licenseId); result.Err == nil {
|
|
||||||
record := result.Data.(*model.LicenseRecord)
|
|
||||||
utils.LoadLicense([]byte(record.Bytes))
|
|
||||||
} else {
|
|
||||||
l4g.Info(utils.T("mattermost.load_license.find.warn"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func addLicense(c *Context, w http.ResponseWriter, r *http.Request) {
|
func addLicense(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
c.LogAudit("attempt")
|
c.LogAudit("attempt")
|
||||||
err := r.ParseMultipartForm(*utils.Cfg.FileSettings.MaxFileSize)
|
err := r.ParseMultipartForm(*utils.Cfg.FileSettings.MaxFileSize)
|
||||||
@@ -83,10 +57,10 @@ func addLicense(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
buf := bytes.NewBuffer(nil)
|
buf := bytes.NewBuffer(nil)
|
||||||
io.Copy(buf, file)
|
io.Copy(buf, file)
|
||||||
|
|
||||||
if license, err := SaveLicense(buf.Bytes()); err != nil {
|
if license, err := app.SaveLicense(buf.Bytes()); err != nil {
|
||||||
if err.Id == EXPIRED_LICENSE_ERROR {
|
if err.Id == model.EXPIRED_LICENSE_ERROR {
|
||||||
c.LogAudit("failed - expired or non-started license")
|
c.LogAudit("failed - expired or non-started license")
|
||||||
} else if err.Id == INVALID_LICENSE_ERROR {
|
} else if err.Id == model.INVALID_LICENSE_ERROR {
|
||||||
c.LogAudit("failed - invalid license")
|
c.LogAudit("failed - invalid license")
|
||||||
} else {
|
} else {
|
||||||
c.LogAudit("failed - unable to save license")
|
c.LogAudit("failed - unable to save license")
|
||||||
@@ -99,56 +73,10 @@ func addLicense(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveLicense(licenseBytes []byte) (*model.License, *model.AppError) {
|
|
||||||
var license *model.License
|
|
||||||
|
|
||||||
if success, licenseStr := utils.ValidateLicense(licenseBytes); success {
|
|
||||||
license = model.LicenseFromJson(strings.NewReader(licenseStr))
|
|
||||||
|
|
||||||
if result := <-app.Srv.Store.User().AnalyticsUniqueUserCount(""); result.Err != nil {
|
|
||||||
return nil, model.NewLocAppError("addLicense", "api.license.add_license.invalid_count.app_error", nil, result.Err.Error())
|
|
||||||
} else {
|
|
||||||
uniqueUserCount := result.Data.(int64)
|
|
||||||
|
|
||||||
if uniqueUserCount > int64(*license.Features.Users) {
|
|
||||||
return nil, model.NewLocAppError("addLicense", "api.license.add_license.unique_users.app_error", map[string]interface{}{"Users": *license.Features.Users, "Count": uniqueUserCount}, "")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ok := utils.SetLicense(license); !ok {
|
|
||||||
return nil, model.NewLocAppError("addLicense", EXPIRED_LICENSE_ERROR, nil, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
record := &model.LicenseRecord{}
|
|
||||||
record.Id = license.Id
|
|
||||||
record.Bytes = string(licenseBytes)
|
|
||||||
rchan := app.Srv.Store.License().Save(record)
|
|
||||||
|
|
||||||
sysVar := &model.System{}
|
|
||||||
sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID
|
|
||||||
sysVar.Value = license.Id
|
|
||||||
schan := app.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())
|
|
||||||
}
|
|
||||||
|
|
||||||
if result := <-schan; result.Err != nil {
|
|
||||||
RemoveLicense()
|
|
||||||
return nil, model.NewLocAppError("addLicense", "api.license.add_license.save_active.app_error", nil, "")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return nil, model.NewLocAppError("addLicense", INVALID_LICENSE_ERROR, nil, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
return license, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func removeLicense(c *Context, w http.ResponseWriter, r *http.Request) {
|
func removeLicense(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
c.LogAudit("")
|
c.LogAudit("")
|
||||||
|
|
||||||
if err := RemoveLicense(); err != nil {
|
if err := app.RemoveLicense(); err != nil {
|
||||||
c.Err = err
|
c.Err = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -158,21 +86,6 @@ func removeLicense(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write([]byte(model.MapToJson(rdata)))
|
w.Write([]byte(model.MapToJson(rdata)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func RemoveLicense() *model.AppError {
|
|
||||||
utils.RemoveLicense()
|
|
||||||
|
|
||||||
sysVar := &model.System{}
|
|
||||||
sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID
|
|
||||||
sysVar.Value = ""
|
|
||||||
|
|
||||||
if result := <-app.Srv.Store.System().SaveOrUpdate(sysVar); result.Err != nil {
|
|
||||||
utils.RemoveLicense()
|
|
||||||
return result.Err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getClientLicenceConfig(c *Context, w http.ResponseWriter, r *http.Request) {
|
func getClientLicenceConfig(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
useSanitizedLicense := !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM)
|
useSanitizedLicense := !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM)
|
||||||
|
|
||||||
|
|||||||
@@ -1823,17 +1823,20 @@ func TestUpdateMfa(t *testing.T) {
|
|||||||
th := Setup().InitBasic()
|
th := Setup().InitBasic()
|
||||||
Client := th.BasicClient
|
Client := th.BasicClient
|
||||||
|
|
||||||
|
isLicensed := utils.IsLicensed
|
||||||
|
license := utils.License
|
||||||
|
enableMfa := *utils.Cfg.ServiceSettings.EnableMultifactorAuthentication
|
||||||
|
defer func() {
|
||||||
|
utils.IsLicensed = isLicensed
|
||||||
|
utils.License = license
|
||||||
|
*utils.Cfg.ServiceSettings.EnableMultifactorAuthentication = enableMfa
|
||||||
|
}()
|
||||||
|
utils.IsLicensed = false
|
||||||
|
utils.License = &model.License{Features: &model.Features{}}
|
||||||
if utils.License.Features.MFA == nil {
|
if utils.License.Features.MFA == nil {
|
||||||
utils.License.Features.MFA = new(bool)
|
utils.License.Features.MFA = new(bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
enableMfa := *utils.Cfg.ServiceSettings.EnableMultifactorAuthentication
|
|
||||||
defer func() {
|
|
||||||
utils.IsLicensed = false
|
|
||||||
*utils.License.Features.MFA = false
|
|
||||||
*utils.Cfg.ServiceSettings.EnableMultifactorAuthentication = enableMfa
|
|
||||||
}()
|
|
||||||
|
|
||||||
team := model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
team := model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
||||||
rteam, _ := Client.CreateTeam(&team)
|
rteam, _ := Client.CreateTeam(&team)
|
||||||
|
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ func InvalidateAllCachesSkipSend() {
|
|||||||
store.ClearUserCaches()
|
store.ClearUserCaches()
|
||||||
store.ClearPostCaches()
|
store.ClearPostCaches()
|
||||||
store.ClearWebhookCaches()
|
store.ClearWebhookCaches()
|
||||||
|
LoadLicense()
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConfig() *model.Config {
|
func GetConfig() *model.Config {
|
||||||
|
|||||||
99
app/license.go
Обычный файл
99
app/license.go
Обычный файл
@@ -0,0 +1,99 @@
|
|||||||
|
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
l4g "github.com/alecthomas/log4go"
|
||||||
|
"github.com/mattermost/platform/model"
|
||||||
|
"github.com/mattermost/platform/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func LoadLicense() {
|
||||||
|
utils.RemoveLicense()
|
||||||
|
|
||||||
|
licenseId := ""
|
||||||
|
if result := <-Srv.Store.System().Get(); result.Err == nil {
|
||||||
|
props := result.Data.(model.StringMap)
|
||||||
|
licenseId = props[model.SYSTEM_ACTIVE_LICENSE_ID]
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(licenseId) != 26 {
|
||||||
|
l4g.Info(utils.T("mattermost.load_license.find.warn"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if result := <-Srv.Store.License().Get(licenseId); result.Err == nil {
|
||||||
|
record := result.Data.(*model.LicenseRecord)
|
||||||
|
utils.LoadLicense([]byte(record.Bytes))
|
||||||
|
} else {
|
||||||
|
l4g.Info(utils.T("mattermost.load_license.find.warn"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SaveLicense(licenseBytes []byte) (*model.License, *model.AppError) {
|
||||||
|
var license *model.License
|
||||||
|
|
||||||
|
if success, licenseStr := utils.ValidateLicense(licenseBytes); success {
|
||||||
|
license = model.LicenseFromJson(strings.NewReader(licenseStr))
|
||||||
|
|
||||||
|
if result := <-Srv.Store.User().AnalyticsUniqueUserCount(""); result.Err != nil {
|
||||||
|
return nil, model.NewLocAppError("addLicense", "api.license.add_license.invalid_count.app_error", nil, result.Err.Error())
|
||||||
|
} else {
|
||||||
|
uniqueUserCount := result.Data.(int64)
|
||||||
|
|
||||||
|
if uniqueUserCount > int64(*license.Features.Users) {
|
||||||
|
return nil, model.NewLocAppError("addLicense", "api.license.add_license.unique_users.app_error", map[string]interface{}{"Users": *license.Features.Users, "Count": uniqueUserCount}, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok := utils.SetLicense(license); !ok {
|
||||||
|
return nil, model.NewLocAppError("addLicense", model.EXPIRED_LICENSE_ERROR, nil, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
record := &model.LicenseRecord{}
|
||||||
|
record.Id = license.Id
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
|
||||||
|
if result := <-schan; result.Err != nil {
|
||||||
|
RemoveLicense()
|
||||||
|
return nil, model.NewLocAppError("addLicense", "api.license.add_license.save_active.app_error", nil, "")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return nil, model.NewLocAppError("addLicense", model.INVALID_LICENSE_ERROR, nil, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
InvalidateAllCaches()
|
||||||
|
|
||||||
|
return license, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveLicense() *model.AppError {
|
||||||
|
utils.RemoveLicense()
|
||||||
|
|
||||||
|
sysVar := &model.System{}
|
||||||
|
sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID
|
||||||
|
sysVar.Value = ""
|
||||||
|
|
||||||
|
if result := <-Srv.Store.System().SaveOrUpdate(sysVar); result.Err != nil {
|
||||||
|
utils.RemoveLicense()
|
||||||
|
return result.Err
|
||||||
|
}
|
||||||
|
|
||||||
|
InvalidateAllCaches()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/mattermost/platform/api"
|
|
||||||
"github.com/mattermost/platform/app"
|
"github.com/mattermost/platform/app"
|
||||||
"github.com/mattermost/platform/model"
|
"github.com/mattermost/platform/model"
|
||||||
"github.com/mattermost/platform/utils"
|
"github.com/mattermost/platform/utils"
|
||||||
@@ -41,6 +40,6 @@ func initDBCommandContext(configFileLocation string) {
|
|||||||
app.NewServer()
|
app.NewServer()
|
||||||
app.InitStores()
|
app.InitStores()
|
||||||
if model.BuildEnterpriseReady == "true" {
|
if model.BuildEnterpriseReady == "true" {
|
||||||
api.LoadLicense()
|
app.LoadLicense()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/mattermost/platform/api"
|
"github.com/mattermost/platform/app"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ func uploadLicenseCmdF(cmd *cobra.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := api.SaveLicense(fileBytes); err != nil {
|
if _, err := app.SaveLicense(fileBytes); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ func doLegacyCommands() {
|
|||||||
web.InitWeb()
|
web.InitWeb()
|
||||||
|
|
||||||
if model.BuildEnterpriseReady == "true" {
|
if model.BuildEnterpriseReady == "true" {
|
||||||
api.LoadLicense()
|
app.LoadLicense()
|
||||||
}
|
}
|
||||||
|
|
||||||
runCmds()
|
runCmds()
|
||||||
@@ -1002,7 +1002,7 @@ func cmdUploadLicense() {
|
|||||||
flushLogAndExit(1)
|
flushLogAndExit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := api.SaveLicense(fileBytes); err != nil {
|
if _, err := app.SaveLicense(fileBytes); err != nil {
|
||||||
l4g.Error("%v", err)
|
l4g.Error("%v", err)
|
||||||
flushLogAndExit(1)
|
flushLogAndExit(1)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ func runServer(configFileLocation string) {
|
|||||||
web.InitWeb()
|
web.InitWeb()
|
||||||
|
|
||||||
if model.BuildEnterpriseReady == "true" {
|
if model.BuildEnterpriseReady == "true" {
|
||||||
api.LoadLicense()
|
app.LoadLicense()
|
||||||
}
|
}
|
||||||
|
|
||||||
if !utils.IsLicensed && len(utils.Cfg.SqlSettings.DataSourceReplicas) > 1 {
|
if !utils.IsLicensed && len(utils.Cfg.SqlSettings.DataSourceReplicas) > 1 {
|
||||||
|
|||||||
@@ -8,6 +8,11 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
EXPIRED_LICENSE_ERROR = "api.license.add_license.expired.app_error"
|
||||||
|
INVALID_LICENSE_ERROR = "api.license.add_license.invalid.app_error"
|
||||||
|
)
|
||||||
|
|
||||||
type LicenseRecord struct {
|
type LicenseRecord struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
CreateAt int64 `json:"create_at"`
|
CreateAt int64 `json:"create_at"`
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user