PLT-6226 Fixing races with licensing (#7213)

* PLT-6226 Fixing races with licensing

* Fixing build issue

* Fixing licensing issue

* removing commented code
Этот коммит содержится в:
Corey Hulen
2017-08-16 09:51:45 -07:00
коммит произвёл Christopher Speller
родитель 32265df8be
Коммит 0ab490845a
36 изменённых файлов: 403 добавлений и 361 удалений

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

@@ -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)
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
}
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")