PLT-6226 Fixing races with licensing (#7213)
* PLT-6226 Fixing races with licensing * Fixing build issue * Fixing licensing issue * removing commented code
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
32265df8be
Коммит
0ab490845a
@@ -11,7 +11,7 @@ func SetDefaultRolesBasedOnConfig() {
|
||||
// Reset the roles to default to make this logic easier
|
||||
model.InitalizeRoles()
|
||||
|
||||
if IsLicensed {
|
||||
if IsLicensed() {
|
||||
switch *Cfg.TeamSettings.RestrictPublicChannelCreation {
|
||||
case model.PERMISSIONS_ALL:
|
||||
model.ROLE_TEAM_USER.Permissions = append(
|
||||
@@ -33,7 +33,7 @@ func SetDefaultRolesBasedOnConfig() {
|
||||
)
|
||||
}
|
||||
|
||||
if IsLicensed {
|
||||
if IsLicensed() {
|
||||
switch *Cfg.TeamSettings.RestrictPublicChannelManagement {
|
||||
case model.PERMISSIONS_ALL:
|
||||
model.ROLE_TEAM_USER.Permissions = append(
|
||||
@@ -65,7 +65,7 @@ func SetDefaultRolesBasedOnConfig() {
|
||||
)
|
||||
}
|
||||
|
||||
if IsLicensed {
|
||||
if IsLicensed() {
|
||||
switch *Cfg.TeamSettings.RestrictPublicChannelDeletion {
|
||||
case model.PERMISSIONS_ALL:
|
||||
model.ROLE_TEAM_USER.Permissions = append(
|
||||
@@ -97,7 +97,7 @@ func SetDefaultRolesBasedOnConfig() {
|
||||
)
|
||||
}
|
||||
|
||||
if IsLicensed {
|
||||
if IsLicensed() {
|
||||
switch *Cfg.TeamSettings.RestrictPrivateChannelCreation {
|
||||
case model.PERMISSIONS_ALL:
|
||||
model.ROLE_TEAM_USER.Permissions = append(
|
||||
@@ -119,7 +119,7 @@ func SetDefaultRolesBasedOnConfig() {
|
||||
)
|
||||
}
|
||||
|
||||
if IsLicensed {
|
||||
if IsLicensed() {
|
||||
switch *Cfg.TeamSettings.RestrictPrivateChannelManagement {
|
||||
case model.PERMISSIONS_ALL:
|
||||
model.ROLE_TEAM_USER.Permissions = append(
|
||||
@@ -151,7 +151,7 @@ func SetDefaultRolesBasedOnConfig() {
|
||||
)
|
||||
}
|
||||
|
||||
if IsLicensed {
|
||||
if IsLicensed() {
|
||||
switch *Cfg.TeamSettings.RestrictPrivateChannelDeletion {
|
||||
case model.PERMISSIONS_ALL:
|
||||
model.ROLE_TEAM_USER.Permissions = append(
|
||||
@@ -184,7 +184,7 @@ func SetDefaultRolesBasedOnConfig() {
|
||||
}
|
||||
|
||||
// Restrict permissions for Private Channel Manage Members
|
||||
if IsLicensed {
|
||||
if IsLicensed() {
|
||||
switch *Cfg.TeamSettings.RestrictPrivateChannelManageMembers {
|
||||
case model.PERMISSIONS_ALL:
|
||||
model.ROLE_CHANNEL_USER.Permissions = append(
|
||||
@@ -229,7 +229,7 @@ func SetDefaultRolesBasedOnConfig() {
|
||||
}
|
||||
|
||||
// Grant permissions for inviting and adding users to a team.
|
||||
if IsLicensed {
|
||||
if IsLicensed() {
|
||||
if *Cfg.TeamSettings.RestrictTeamInvite == model.PERMISSIONS_TEAM_ADMIN {
|
||||
model.ROLE_TEAM_ADMIN.Permissions = append(
|
||||
model.ROLE_TEAM_ADMIN.Permissions,
|
||||
@@ -251,7 +251,7 @@ func SetDefaultRolesBasedOnConfig() {
|
||||
)
|
||||
}
|
||||
|
||||
if IsLicensed {
|
||||
if IsLicensed() {
|
||||
switch *Cfg.ServiceSettings.RestrictPostDelete {
|
||||
case model.PERMISSIONS_DELETE_POST_ALL:
|
||||
model.ROLE_CHANNEL_USER.Permissions = append(
|
||||
|
||||
@@ -489,7 +489,10 @@ func getClientConfig(c *model.Config) map[string]string {
|
||||
props["DiagnosticId"] = CfgDiagnosticId
|
||||
props["DiagnosticsEnabled"] = strconv.FormatBool(*c.LogSettings.EnableDiagnostics)
|
||||
|
||||
if IsLicensed {
|
||||
if IsLicensed() {
|
||||
|
||||
License := License()
|
||||
|
||||
if *License.Features.CustomBrand {
|
||||
props["EnableCustomBrand"] = strconv.FormatBool(*c.TeamSettings.EnableCustomBrand)
|
||||
props["CustomBrandText"] = *c.TeamSettings.CustomBrandText
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -54,14 +54,14 @@ func TestValidateLicense(t *testing.T) {
|
||||
func TestClientLicenseEtag(t *testing.T) {
|
||||
etag1 := GetClientLicenseEtag(false)
|
||||
|
||||
ClientLicense["SomeFeature"] = "true"
|
||||
SetClientLicense(map[string]string{"SomeFeature": "true", "IsLicensed": "true"})
|
||||
|
||||
etag2 := GetClientLicenseEtag(false)
|
||||
if etag1 == etag2 {
|
||||
t.Fatal("etags should not match")
|
||||
}
|
||||
|
||||
ClientLicense["SomeFeature"] = "false"
|
||||
SetClientLicense(map[string]string{"SomeFeature": "true", "IsLicensed": "false"})
|
||||
|
||||
etag3 := GetClientLicenseEtag(false)
|
||||
if etag2 == etag3 {
|
||||
|
||||
@@ -15,7 +15,7 @@ func IsPasswordValid(password string) *model.AppError {
|
||||
isError := false
|
||||
min := model.PASSWORD_MINIMUM_LENGTH
|
||||
|
||||
if IsLicensed && *License.Features.PasswordRequirements {
|
||||
if IsLicensed() && *License().Features.PasswordRequirements {
|
||||
if len(password) < *Cfg.PasswordSettings.MinimumLength || len(password) > model.PASSWORD_MAXIMUM_LENGTH {
|
||||
isError = true
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user