-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
license.go
378 lines (316 loc) · 12.3 KB
/
license.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/pkg/errors"
"github.com/mattermost/mattermost-server/v6/jobs"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/shared/mlog"
"github.com/mattermost/mattermost-server/v6/utils"
)
const (
requestTrialURL = "https://customers.mattermost.com/api/v1/trials"
LicenseEnv = "MM_LICENSE"
LicenseRenewalURL = "https://customers.mattermost.com/subscribe/renew"
JWTDefaultTokenExpiration = 7 * 24 * time.Hour // 7 days of expiration
)
// JWTClaims custom JWT claims with the needed information for the
// renewal process
type JWTClaims struct {
LicenseID string `json:"license_id"`
ActiveUsers int64 `json:"active_users"`
jwt.StandardClaims
}
func (s *Server) LoadLicense() {
// ENV var overrides all other sources of license.
licenseStr := os.Getenv(LicenseEnv)
if licenseStr != "" {
license, err := utils.LicenseValidator.LicenseFromBytes([]byte(licenseStr))
if err != nil {
mlog.Error("Failed to read license set in environment.", mlog.Err(err))
return
}
// skip the restrictions if license is a sanctioned trial
if !license.IsSanctionedTrial() && license.IsTrialLicense() {
canStartTrialLicense, err := s.LicenseManager.CanStartTrial()
if err != nil {
mlog.Info("Failed to validate trial eligibility.", mlog.Err(err))
return
}
if !canStartTrialLicense {
mlog.Info("Cannot start trial multiple times.")
return
}
}
if s.ValidateAndSetLicenseBytes([]byte(licenseStr)) {
mlog.Info("License key from ENV is valid, unlocking enterprise features.")
}
return
}
licenseId := ""
props, nErr := s.Store.System().Get()
if nErr == nil {
licenseId = props[model.SystemActiveLicenseId]
}
if !model.IsValidId(licenseId) {
// Lets attempt to load the file from disk since it was missing from the DB
license, licenseBytes := utils.GetAndValidateLicenseFileFromDisk(*s.Config().ServiceSettings.LicenseFileLocation)
if license != 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
}
}
}
record, nErr := s.Store.License().Get(licenseId)
if nErr != nil {
mlog.Info("License key from https://mattermost.com required to unlock enterprise features.")
s.SetLicense(nil)
return
}
s.ValidateAndSetLicenseBytes([]byte(record.Bytes))
mlog.Info("License key valid unlocking enterprise features.")
}
func (s *Server) SaveLicense(licenseBytes []byte) (*model.License, *model.AppError) {
success, licenseStr := utils.LicenseValidator.ValidateLicense(licenseBytes)
if !success {
return nil, model.NewAppError("addLicense", model.InvalidLicenseError, nil, "", http.StatusBadRequest)
}
var license model.License
if jsonErr := json.Unmarshal([]byte(licenseStr), &license); jsonErr != nil {
return nil, model.NewAppError("addLicense", "api.unmarshal_error", nil, jsonErr.Error(), http.StatusInternalServerError)
}
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)
}
if uniqueUserCount > int64(*license.Features.Users) {
return nil, model.NewAppError("addLicense", "api.license.add_license.unique_users.app_error", map[string]interface{}{"Users": *license.Features.Users, "Count": uniqueUserCount}, "", http.StatusBadRequest)
}
if license.IsExpired() {
return nil, model.NewAppError("addLicense", model.ExpiredLicenseError, nil, "", http.StatusBadRequest)
}
if ok := s.SetLicense(&license); !ok {
return nil, model.NewAppError("addLicense", model.ExpiredLicenseError, nil, "", http.StatusBadRequest)
}
record := &model.LicenseRecord{}
record.Id = license.Id
record.Bytes = string(licenseBytes)
_, nErr := s.Store.License().Save(record)
if nErr != nil {
s.RemoveLicense()
var appErr *model.AppError
switch {
case errors.As(nErr, &appErr):
return nil, appErr
default:
return nil, model.NewAppError("addLicense", "api.license.add_license.save.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
}
sysVar := &model.System{}
sysVar.Name = model.SystemActiveLicenseId
sysVar.Value = license.Id
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)
}
s.ReloadConfig()
s.InvalidateAllCaches()
// restart job server workers - 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 *s.Config().JobSettings.RunJobs && s.Jobs != nil {
if err := s.Jobs.StopWorkers(); err != nil && !errors.Is(err, jobs.ErrWorkersNotRunning) {
mlog.Warn("Stopping job server workers failed", mlog.Err(err))
}
if err := s.Jobs.InitWorkers(); err != nil {
mlog.Error("Initializing job server workers failed", mlog.Err(err))
} else if err := s.Jobs.StartWorkers(); err != nil {
mlog.Error("Starting job server workers failed", mlog.Err(err))
}
}
if *s.Config().JobSettings.RunScheduler && s.Jobs != nil {
if err := s.Jobs.StartSchedulers(); err != nil && !errors.Is(err, jobs.ErrSchedulersRunning) {
mlog.Error("Starting job server schedulers failed", mlog.Err(err))
}
}
return &license, nil
}
func (s *Server) SetLicense(license *model.License) bool {
oldLicense := s.licenseValue.Load()
defer func() {
for _, listener := range s.licenseListeners {
if oldLicense == nil {
listener(nil, license)
} else {
listener(oldLicense.(*model.License), license)
}
}
}()
if license != nil {
license.Features.SetDefaults()
s.licenseValue.Store(license)
s.clientLicenseValue.Store(utils.GetClientLicense(license))
return true
}
s.licenseValue.Store((*model.License)(nil))
s.clientLicenseValue.Store(map[string]string(nil))
return false
}
func (s *Server) ValidateAndSetLicenseBytes(b []byte) bool {
if success, licenseStr := utils.LicenseValidator.ValidateLicense(b); success {
var license model.License
if jsonErr := json.Unmarshal([]byte(licenseStr), &license); jsonErr != nil {
mlog.Warn("Failed to decode license from JSON", mlog.Err(jsonErr))
return false
}
s.SetLicense(&license)
return true
}
mlog.Warn("No valid enterprise license found")
return false
}
func (s *Server) SetClientLicense(m map[string]string) {
s.clientLicenseValue.Store(m)
}
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 (s *Server) RemoveLicense() *model.AppError {
if license, _ := s.licenseValue.Load().(*model.License); license == nil {
return nil
}
mlog.Info("Remove license.", mlog.String("id", model.SystemActiveLicenseId))
sysVar := &model.System{}
sysVar.Name = model.SystemActiveLicenseId
sysVar.Value = ""
if err := s.Store.System().SaveOrUpdate(sysVar); err != nil {
return model.NewAppError("RemoveLicense", "app.system.save.app_error", nil, err.Error(), http.StatusInternalServerError)
}
s.SetLicense(nil)
s.ReloadConfig()
s.InvalidateAllCaches()
return nil
}
func (s *Server) AddLicenseListener(listener func(oldLicense, newLicense *model.License)) string {
id := model.NewId()
s.licenseListeners[id] = listener
return id
}
func (s *Server) RemoveLicenseListener(id string) {
delete(s.licenseListeners, id)
}
func (s *Server) GetSanitizedClientLicense() map[string]string {
return utils.GetSanitizedClientLicense(s.ClientLicense())
}
// RequestTrialLicense request a trial license from the mattermost official license server
func (s *Server) RequestTrialLicense(trialRequest *model.TrialLicenseRequest) *model.AppError {
trialRequestJSON, jsonErr := json.Marshal(trialRequest)
if jsonErr != nil {
return model.NewAppError("RequestTrialLicense", "api.unmarshal_error", nil, jsonErr.Error(), http.StatusInternalServerError)
}
resp, err := http.Post(requestTrialURL, "application/json", bytes.NewBuffer(trialRequestJSON))
if err != nil {
return model.NewAppError("RequestTrialLicense", "api.license.request_trial_license.app_error", nil, err.Error(), http.StatusBadRequest)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return model.NewAppError("RequestTrialLicense", "api.license.request_trial_license.app_error", nil,
fmt.Sprintf("Unexpected HTTP status code %q returned by server", resp.Status), http.StatusInternalServerError)
}
licenseResponse := model.MapFromJSON(resp.Body)
if _, ok := licenseResponse["license"]; !ok {
return model.NewAppError("RequestTrialLicense", "api.license.request_trial_license.app_error", nil, licenseResponse["message"], http.StatusBadRequest)
}
if _, err := s.SaveLicense([]byte(licenseResponse["license"])); err != nil {
return err
}
s.ReloadConfig()
s.InvalidateAllCaches()
return nil
}
// GenerateRenewalToken returns the current active token or generate a new one if
// the current active one has expired
func (s *Server) GenerateRenewalToken(expiration time.Duration) (string, *model.AppError) {
license := s.License()
if license == nil {
// Clean renewal token if there is no license present
if _, err := s.Store.System().PermanentDeleteByName(model.SystemLicenseRenewalToken); err != nil {
mlog.Warn("error removing the renewal token", mlog.Err(err))
}
return "", model.NewAppError("GenerateRenewalToken", "app.license.generate_renewal_token.no_license", nil, "", http.StatusBadRequest)
}
if *license.Features.Cloud {
return "", model.NewAppError("GenerateRenewalToken", "app.license.generate_renewal_token.bad_license", nil, "", http.StatusBadRequest)
}
currentToken, _ := s.Store.System().GetByName(model.SystemLicenseRenewalToken)
if currentToken != nil {
tokenIsValid, err := s.renewalTokenValid(currentToken.Value, license.Customer.Email)
if err != nil {
mlog.Warn("error checking license renewal token validation", mlog.Err(err))
}
if currentToken.Value != "" && tokenIsValid {
return currentToken.Value, nil
}
}
activeUsers, err := s.Store.User().Count(model.UserCountOptions{})
if err != nil {
return "", model.NewAppError("GenerateRenewalToken", "app.license.generate_renewal_token.app_error",
nil, err.Error(), http.StatusInternalServerError)
}
expirationTime := time.Now().UTC().Add(expiration)
claims := &JWTClaims{
LicenseID: license.Id,
ActiveUsers: activeUsers,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expirationTime.Unix(),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenString, err := token.SignedString([]byte(license.Customer.Email))
if err != nil {
return "", model.NewAppError("GenerateRenewalToken", "app.license.generate_renewal_token.app_error", nil, err.Error(), http.StatusInternalServerError)
}
err = s.Store.System().SaveOrUpdate(&model.System{
Name: model.SystemLicenseRenewalToken,
Value: tokenString,
})
if err != nil {
return "", model.NewAppError("GenerateRenewalToken", "app.license.generate_renewal_token.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return tokenString, nil
}
func (s *Server) renewalTokenValid(tokenString, signingKey string) (bool, error) {
claims := &JWTClaims{}
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return []byte(signingKey), nil
})
if err != nil && !token.Valid {
return false, errors.Wrapf(err, "Error validating JWT token")
}
expirationTime := time.Unix(claims.ExpiresAt, 0)
if expirationTime.Before(time.Now().UTC()) {
return false, nil
}
return true, nil
}
// GenerateLicenseRenewalLink returns a link that points to the CWS where clients can renew license
func (s *Server) GenerateLicenseRenewalLink() (string, *model.AppError) {
renewalToken, err := s.GenerateRenewalToken(JWTDefaultTokenExpiration)
if err != nil {
return "", err
}
renewalLink := LicenseRenewalURL + "?token=" + renewalToken
return renewalLink, nil
}