From 0ab490845aed0ce3b58cbffd8ec35be237abda1c Mon Sep 17 00:00:00 2001 From: Corey Hulen Date: Wed, 16 Aug 2017 09:51:45 -0700 Subject: PLT-6226 Fixing races with licensing (#7213) * PLT-6226 Fixing races with licensing * Fixing build issue * Fixing licensing issue * removing commented code --- utils/license.go | 83 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 22 deletions(-) (limited to 'utils/license.go') diff --git a/utils/license.go b/utils/license.go index e28a43e29..d0abc612d 100644 --- a/utils/license.go +++ b/utils/license.go @@ -16,17 +16,16 @@ import ( "os" "strconv" "strings" + "sync/atomic" l4g "github.com/alecthomas/log4go" "github.com/mattermost/platform/model" ) -var IsLicensed bool = false -var License *model.License = &model.License{ - Features: new(model.Features), -} -var ClientLicense map[string]string = map[string]string{"IsLicensed": "false"} +var isLicensedInt32 int32 +var licenseValue atomic.Value +var clientLicenseValue atomic.Value var publicKey []byte = []byte(`-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyZmShlU8Z8HdG0IWSZ8r @@ -38,6 +37,34 @@ a0v85XL6i9ote2P+fLZ3wX9EoioHzgdgB7arOxY50QRJO7OyCqpKFKv6lRWTXuSt hwIDAQAB -----END PUBLIC KEY-----`) +func init() { + SetLicense(nil) +} + +func IsLicensed() bool { + return atomic.LoadInt32(&isLicensedInt32) == 1 +} + +func SetIsLicensed(v bool) { + if v { + atomic.StoreInt32(&isLicensedInt32, 1) + } else { + atomic.StoreInt32(&isLicensedInt32, 0) + } +} + +func License() *model.License { + return licenseValue.Load().(*model.License) +} + +func SetClientLicense(m map[string]string) { + clientLicenseValue.Store(m) +} + +func ClientLicense() map[string]string { + return clientLicenseValue.Load().(map[string]string) +} + func LoadLicense(licenseBytes []byte) { if success, licenseStr := ValidateLicense(licenseBytes); success { license := model.LicenseFromJson(strings.NewReader(licenseStr)) @@ -49,23 +76,35 @@ func LoadLicense(licenseBytes []byte) { } func SetLicense(license *model.License) bool { - license.Features.SetDefaults() - if !license.IsExpired() { - License = license - IsLicensed = true - ClientLicense = getClientLicense(license) - ClientCfg = getClientConfig(Cfg) - return true - } + if license == nil { + SetIsLicensed(false) + license = &model.License{ + Features: new(model.Features), + } + license.Features.SetDefaults() + licenseValue.Store(license) - return false + SetClientLicense(map[string]string{"IsLicensed": "false"}) + + return false + } else { + license.Features.SetDefaults() + + if !license.IsExpired() { + licenseValue.Store(license) + SetIsLicensed(true) + clientLicenseValue.Store(getClientLicense(license)) + ClientCfg = getClientConfig(Cfg) + return true + } + + return false + } } func RemoveLicense() { - License = &model.License{} - IsLicensed = false - ClientLicense = getClientLicense(License) + SetLicense(nil) ClientCfg = getClientConfig(Cfg) } @@ -162,9 +201,9 @@ func GetLicenseFileLocation(fileLocation string) string { func getClientLicense(l *model.License) map[string]string { props := make(map[string]string) - props["IsLicensed"] = strconv.FormatBool(IsLicensed) + props["IsLicensed"] = strconv.FormatBool(IsLicensed()) - if IsLicensed { + if IsLicensed() { props["Id"] = l.Id props["Users"] = strconv.Itoa(*l.Features.Users) props["LDAP"] = strconv.FormatBool(*l.Features.LDAP) @@ -195,7 +234,7 @@ func getClientLicense(l *model.License) map[string]string { func GetClientLicenseEtag(useSanitized bool) string { value := "" - lic := ClientLicense + lic := ClientLicense() if useSanitized { lic = GetSanitizedClientLicense() @@ -211,11 +250,11 @@ func GetClientLicenseEtag(useSanitized bool) string { func GetSanitizedClientLicense() map[string]string { sanitizedLicense := make(map[string]string) - for k, v := range ClientLicense { + for k, v := range ClientLicense() { sanitizedLicense[k] = v } - if IsLicensed { + if IsLicensed() { delete(sanitizedLicense, "Id") delete(sanitizedLicense, "Name") delete(sanitizedLicense, "Email") -- cgit v1.2.3-1-g7c22