MM-34437 Preventing infinite trial requests (#17472)
* MM-34434: Added 'is_trial' boolean to all trial license requests and to the License struct. * MM-34434: Generalized the concept of a license request. * MM-34434: Verifies JSON field of license instance is set. * MM-34434: Added missing client param. * MM-34434: Added some tests of the request trial API endpoint. * MM-34434: Removed comment. * fix broken test (#17348) * Add missing wrapped errors (#17339) * Improve document extraction and including a document extraction command (#17183) * Add extract documents content command * Adding the extraction command and making the pure go pdf library as secondary option * Improving the memory usage and docextractor interface * Enable content extraction by default in all the instances * Tiny improvement on archive indexing * Adding App interface generation and the opentracing layer * Fixing linter errors * Addressing PR review comments * Addressing PR review comments * Update en.json (#17356) Automatic Merge * adding new feature flag (#17308) Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> * Bump no_output_timeout to 2 hours (#17358) * log invalid username (#17345) Automatic Merge * MM-34434: Added missing client param. MM-34434: Added some tests of the request trial API endpoint. MM-34434: Removed comment. * MM-34434: Switched to a hard-coded true value. * MM-34434: Reverts test change. * MM-34434: Removes unnecessary field. * MM-34434: Tests that is_trial is hard-coded by TrialLicenseRequest. * MM-34434: Removed accidental commit. * MM-34434: Removes unnecessary is_trial key from JSON payload. * MM-34434: Reverts to old pointer receiver variable name. * MM-34434: Removes test. * #MM-34437 Initialized license service * ##MM-34437 Verified at all points if server is trial elligible * WIp * #MM-34437 removed unused commented code * MM-34437 make a log less severe * #MM-34437 generated einterface mocks * #MM-34437 added license on new file * #MM-34437 removed unused translation * #MM-34437 some refactoring * Update api4/license.go * Update api4/license.go * #MM-34437 made a variable name consistent * #MM-34437 Added mocks for lince validator * #M--34437 Added license validator test framework * #MM-34437 Renamed isTrial method to isTrialLicense to avoid conflict with newlya dded field * #M--34437 Allowed sales-sanctioned trials * #MM-34437 fixed trial license API tests * Added tests for add license API * #MM-34437 fixed ValidateLicense test * #MM-34437 Added util tests * #MM-34437 using NoError for checking no error * #MM-34437 using NoError for checking no error * Added dummy piblic key for testing * Fixed tests * #MM-34437 udpaetd trial license URL for testing * #MM-34437 adjusted times for licences generated through admin portal * Reverted test-only changes Co-authored-by: Martin Kraft <martin@upspin.org> Co-authored-by: Hossein <hahmadia@users.noreply.github.com> Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: Jesús Espino <jespinog@gmail.com> Co-authored-by: Amy Blais <amy_blais@hotmail.com> Co-authored-by: Ben Cooke <benkcooke@gmail.com> Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in> Co-authored-by: Max Erenberg <max.erenberg@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
6bb1cbca63
Коммит
e4aa729a0c
@@ -11,12 +11,15 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/utils"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/audit"
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
)
|
||||
|
||||
func (api *API) InitLicense() {
|
||||
api.BaseRoutes.ApiRoot.Handle("/trial-license", api.ApiSessionRequired(requestTrialLicense)).Methods("POST")
|
||||
api.BaseRoutes.ApiRoot.Handle("/trial-license/prev", api.ApiSessionRequired(getPrevTrialLicense)).Methods("GET")
|
||||
api.BaseRoutes.ApiRoot.Handle("/license", api.ApiSessionRequired(addLicense)).Methods("POST")
|
||||
api.BaseRoutes.ApiRoot.Handle("/license", api.ApiSessionRequired(removeLicense)).Methods("DELETE")
|
||||
api.BaseRoutes.ApiRoot.Handle("/license/renewal", api.ApiSessionRequired(requestRenewalLink)).Methods("GET")
|
||||
@@ -94,7 +97,28 @@ func addLicense(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
io.Copy(buf, file)
|
||||
|
||||
license, appErr := c.App.Srv().SaveLicense(buf.Bytes())
|
||||
licenseBytes := buf.Bytes()
|
||||
license, appErr := utils.LicenseValidator.LicenseFromBytes(licenseBytes)
|
||||
if appErr != nil {
|
||||
c.Err = appErr
|
||||
return
|
||||
}
|
||||
|
||||
// skip the restrictions if license is a sanctioned trial
|
||||
if !license.IsSanctionedTrial() && license.IsTrialLicense() {
|
||||
canStartTrialLicense, err := c.App.Srv().LicenseManager.CanStartTrial()
|
||||
if err != nil {
|
||||
c.Err = model.NewAppError("addLicense", "api.license.add_license.open.app_error", nil, "", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if !canStartTrialLicense {
|
||||
c.Err = model.NewAppError("addLicense", "api.license.request-trial.can-start-trial.not-allowed", nil, "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
license, appErr = c.App.Srv().SaveLicense(licenseBytes)
|
||||
if appErr != nil {
|
||||
if appErr.Id == model.EXPIRED_LICENSE_ERROR {
|
||||
c.LogAudit("failed - expired or non-started license")
|
||||
@@ -154,6 +178,17 @@ func requestTrialLicense(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
canStartTrialLicense, err := c.App.Srv().LicenseManager.CanStartTrial()
|
||||
if err != nil {
|
||||
c.Err = model.NewAppError("requestTrialLicense", "api.license.request-trial.can-start-trial.error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if !canStartTrialLicense {
|
||||
c.Err = model.NewAppError("requestTrialLicense", "api.license.request-trial.can-start-trial.not-allowed", nil, "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var trialRequest struct {
|
||||
Users int `json:"users"`
|
||||
TermsAccepted bool `json:"terms_accepted"`
|
||||
@@ -175,9 +210,9 @@ func requestTrialLicense(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
currentUser, err := c.App.GetUser(c.AppContext.Session().UserId)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
currentUser, appErr := c.App.GetUser(c.AppContext.Session().UserId)
|
||||
if appErr != nil {
|
||||
c.Err = appErr
|
||||
return
|
||||
}
|
||||
|
||||
@@ -238,3 +273,21 @@ func requestRenewalLink(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func getPrevTrialLicense(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
license, err := c.App.Srv().LicenseManager.GetPrevTrial()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var clientLicense map[string]string
|
||||
|
||||
if c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PERMISSION_READ_LICENSE_INFORMATION) {
|
||||
clientLicense = utils.GetClientLicense(license)
|
||||
} else {
|
||||
clientLicense = utils.GetSanitizedClientLicense(utils.GetClientLicense(license))
|
||||
}
|
||||
|
||||
w.Write([]byte(model.MapToJson(clientLicense)))
|
||||
}
|
||||
|
||||
@@ -4,9 +4,16 @@
|
||||
package api4
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/einterfaces/mocks"
|
||||
"github.com/mattermost/mattermost-server/v5/utils"
|
||||
mocks2 "github.com/mattermost/mattermost-server/v5/utils/mocks"
|
||||
"github.com/mattermost/mattermost-server/v5/utils/testutils"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
@@ -75,6 +82,85 @@ func TestUploadLicenseFile(t *testing.T) {
|
||||
CheckBadRequestStatus(t, resp)
|
||||
require.False(t, ok)
|
||||
})
|
||||
|
||||
t.Run("server has already gone through trial", func(t *testing.T) {
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = false })
|
||||
mockLicenseValidator := mocks2.LicenseValidatorIface{}
|
||||
defer testutils.ResetLicenseValidator()
|
||||
|
||||
//startTimestamp, err := time.Parse("2 Jan 2006 3:04 pm", "1 Jan 2021 12:00 am")
|
||||
//require.Nil(t, err)
|
||||
|
||||
userCount := 100
|
||||
mills := model.GetMillis()
|
||||
|
||||
license := model.License{
|
||||
Id: "AAAAAAAAAAAAAAAAAAAAAAAAAA",
|
||||
Features: &model.Features{
|
||||
Users: &userCount,
|
||||
},
|
||||
Customer: &model.Customer{
|
||||
Name: "Test",
|
||||
},
|
||||
StartsAt: mills + 100,
|
||||
ExpiresAt: mills + 100 + (30*(time.Hour*24) + (time.Hour * 8)).Milliseconds(),
|
||||
}
|
||||
|
||||
mockLicenseValidator.On("LicenseFromBytes", mock.Anything).Return(&license, nil).Once()
|
||||
licenseBytes, _ := json.Marshal(license)
|
||||
licenseStr := string(licenseBytes)
|
||||
|
||||
mockLicenseValidator.On("ValidateLicense", mock.Anything).Return(true, licenseStr)
|
||||
utils.LicenseValidator = &mockLicenseValidator
|
||||
|
||||
licenseManagerMock := &mocks.LicenseInterface{}
|
||||
licenseManagerMock.On("CanStartTrial").Return(false, nil).Once()
|
||||
th.App.Srv().LicenseManager = licenseManagerMock
|
||||
|
||||
ok, resp := th.SystemAdminClient.UploadLicenseFile([]byte("sadasdasdasdasdasdsa"))
|
||||
require.False(t, ok)
|
||||
require.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
||||
require.Equal(t, "api.license.request-trial.can-start-trial.not-allowed", resp.Error.Id)
|
||||
})
|
||||
|
||||
t.Run("allow uploading sanctioned trials even if server already gone through trial", func(t *testing.T) {
|
||||
mockLicenseValidator := mocks2.LicenseValidatorIface{}
|
||||
defer testutils.ResetLicenseValidator()
|
||||
|
||||
userCount := 100
|
||||
mills := model.GetMillis()
|
||||
|
||||
license := model.License{
|
||||
Id: "PPPPPPPPPPPPPPPPPPPPPPPPPP",
|
||||
Features: &model.Features{
|
||||
Users: &userCount,
|
||||
},
|
||||
Customer: &model.Customer{
|
||||
Name: "Test",
|
||||
},
|
||||
IsTrial: true,
|
||||
StartsAt: mills + 100,
|
||||
ExpiresAt: mills + 100 + (29*(time.Hour*24) + (time.Hour * 8)).Milliseconds(),
|
||||
}
|
||||
|
||||
mockLicenseValidator.On("LicenseFromBytes", mock.Anything).Return(&license, nil).Once()
|
||||
|
||||
licenseBytes, _ := json.Marshal(license)
|
||||
licenseStr := string(licenseBytes)
|
||||
|
||||
mockLicenseValidator.On("ValidateLicense", mock.Anything).Return(true, licenseStr)
|
||||
|
||||
utils.LicenseValidator = &mockLicenseValidator
|
||||
|
||||
licenseManagerMock := &mocks.LicenseInterface{}
|
||||
licenseManagerMock.On("CanStartTrial").Return(false, nil).Once()
|
||||
th.App.Srv().LicenseManager = licenseManagerMock
|
||||
|
||||
ok, resp := th.SystemAdminClient.UploadLicenseFile([]byte("sadasdasdasdasdasdsa"))
|
||||
require.False(t, ok)
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
require.Nil(t, resp.Error)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRemoveLicenseFile(t *testing.T) {
|
||||
@@ -116,6 +202,10 @@ func TestRequestTrialLicense(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
licenseManagerMock := &mocks.LicenseInterface{}
|
||||
licenseManagerMock.On("CanStartTrial").Return(true, nil)
|
||||
th.App.Srv().LicenseManager = licenseManagerMock
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SiteURL = "http://localhost:8065/" })
|
||||
|
||||
t.Run("permission denied", func(t *testing.T) {
|
||||
|
||||
Ссылка в новой задаче
Block a user