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
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -16,6 +17,16 @@ const (
|
||||
LICENSE_RENEWAL_LINK = "https://mattermost.com/renew/"
|
||||
)
|
||||
|
||||
var (
|
||||
trialDuration = 30*(time.Hour*24) + (time.Hour * 8) // 720 hours (30 days) + 8 hours is trial license duration
|
||||
adminTrialDuration = 30*(time.Hour*24) + (time.Hour * 23) + (time.Minute * 59) + (time.Second * 59) // 720 hours (30 days) + 23 hours, 59 mins and 59 seconds
|
||||
|
||||
// a sanctioned trial's duration is either more than the upper bound,
|
||||
// or less than the lower bound
|
||||
sanctionedTrialDurationLowerBound = 31*(time.Hour*24) + (time.Hour * 23) + (time.Minute * 59) + (time.Second * 59) // 744 hours (31 days) + 23 hours, 59 mins and 59 seconds
|
||||
sanctionedTrialDurationUpperBound = 29*(time.Hour*24) + (time.Hour * 23) + (time.Minute * 59) + (time.Second * 59) // 696 hours (29 days) + 23 hours, 59 mins and 59 seconds
|
||||
)
|
||||
|
||||
type LicenseRecord struct {
|
||||
Id string `json:"id"`
|
||||
CreateAt int64 `json:"create_at"`
|
||||
@@ -263,6 +274,17 @@ func (l *License) ToJson() string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (l *License) IsTrialLicense() bool {
|
||||
return l.IsTrial || (l.ExpiresAt-l.StartsAt) == trialDuration.Milliseconds() || (l.ExpiresAt-l.StartsAt) == adminTrialDuration.Milliseconds()
|
||||
}
|
||||
|
||||
func (l *License) IsSanctionedTrial() bool {
|
||||
duration := l.ExpiresAt - l.StartsAt
|
||||
|
||||
return l.IsTrialLicense() &&
|
||||
(duration >= sanctionedTrialDurationLowerBound.Milliseconds() || duration <= sanctionedTrialDurationUpperBound.Milliseconds())
|
||||
}
|
||||
|
||||
// NewTestLicense returns a license that expires in the future and has the given features.
|
||||
func NewTestLicense(features ...string) *License {
|
||||
ret := &License{
|
||||
|
||||
@@ -6,6 +6,7 @@ package model
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -240,3 +241,147 @@ func TestLicenseRecordPreSave(t *testing.T) {
|
||||
|
||||
assert.NotZero(t, lr.CreateAt)
|
||||
}
|
||||
|
||||
func TestLicense_IsTrialLicense(t *testing.T) {
|
||||
t.Run("detect trial license directly from the flag", func(t *testing.T) {
|
||||
license := &License{
|
||||
IsTrial: true,
|
||||
}
|
||||
assert.True(t, license.IsTrial)
|
||||
|
||||
license.IsTrial = false
|
||||
assert.False(t, license.IsTrialLicense())
|
||||
})
|
||||
|
||||
t.Run("detect trial license form duration", func(t *testing.T) {
|
||||
startDate, err := time.Parse(time.RFC822, "01 Jan 21 00:00 UTC")
|
||||
assert.NoError(t, err)
|
||||
|
||||
endDate, err := time.Parse(time.RFC822, "31 Jan 21 08:00 UTC")
|
||||
assert.NoError(t, err)
|
||||
|
||||
license := &License{
|
||||
StartsAt: startDate.UnixNano() / int64(time.Millisecond),
|
||||
ExpiresAt: endDate.UnixNano() / int64(time.Millisecond),
|
||||
}
|
||||
assert.True(t, license.IsTrialLicense())
|
||||
|
||||
endDate, err = time.Parse(time.RFC822, "01 Feb 21 08:00 UTC")
|
||||
assert.NoError(t, err)
|
||||
|
||||
license.ExpiresAt = endDate.UnixNano() / int64(time.Millisecond)
|
||||
assert.False(t, license.IsTrialLicense())
|
||||
|
||||
// 30 days + 23 hours 59 mins 59 seconds
|
||||
endDate, err = time.Parse("02 Jan 06 15:04:05 MST", "31 Jan 21 23:59:59 UTC")
|
||||
assert.NoError(t, err)
|
||||
license.ExpiresAt = endDate.UnixNano() / int64(time.Millisecond)
|
||||
assert.True(t, license.IsTrialLicense())
|
||||
})
|
||||
|
||||
t.Run("detect trial with both flag and duration", func(t *testing.T) {
|
||||
startDate, err := time.Parse(time.RFC822, "01 Jan 21 00:00 UTC")
|
||||
assert.NoError(t, err)
|
||||
|
||||
endDate, err := time.Parse(time.RFC822, "31 Jan 21 08:00 UTC")
|
||||
assert.NoError(t, err)
|
||||
|
||||
license := &License{
|
||||
IsTrial: true,
|
||||
StartsAt: startDate.UnixNano() / int64(time.Millisecond),
|
||||
ExpiresAt: endDate.UnixNano() / int64(time.Millisecond),
|
||||
}
|
||||
|
||||
assert.True(t, license.IsTrialLicense())
|
||||
license.IsTrial = false
|
||||
|
||||
// detecting trial from duration
|
||||
assert.True(t, license.IsTrialLicense())
|
||||
|
||||
endDate, _ = time.Parse(time.RFC822, "1 Feb 2021 08:00 UTC")
|
||||
license.ExpiresAt = endDate.UnixNano() / int64(time.Millisecond)
|
||||
assert.False(t, license.IsTrialLicense())
|
||||
|
||||
license.IsTrial = true
|
||||
assert.True(t, license.IsTrialLicense())
|
||||
})
|
||||
}
|
||||
|
||||
func TestLicense_IsSanctionedTrial(t *testing.T) {
|
||||
t.Run("short duration sanctioned trial", func(t *testing.T) {
|
||||
startDate, err := time.Parse(time.RFC822, "01 Jan 21 00:00 UTC")
|
||||
assert.NoError(t, err)
|
||||
|
||||
endDate, err := time.Parse(time.RFC822, "08 Jan 21 08:00 UTC")
|
||||
assert.NoError(t, err)
|
||||
|
||||
license := &License{
|
||||
IsTrial: true,
|
||||
StartsAt: startDate.UnixNano() / int64(time.Millisecond),
|
||||
ExpiresAt: endDate.UnixNano() / int64(time.Millisecond),
|
||||
}
|
||||
|
||||
assert.True(t, license.IsSanctionedTrial())
|
||||
|
||||
license.IsTrial = false
|
||||
assert.False(t, license.IsSanctionedTrial())
|
||||
})
|
||||
|
||||
t.Run("long duration sanctioned trial", func(t *testing.T) {
|
||||
startDate, err := time.Parse(time.RFC822, "01 Jan 21 00:00 UTC")
|
||||
assert.NoError(t, err)
|
||||
|
||||
endDate, err := time.Parse(time.RFC822, "02 Feb 21 08:00 UTC")
|
||||
assert.NoError(t, err)
|
||||
|
||||
license := &License{
|
||||
IsTrial: true,
|
||||
StartsAt: startDate.UnixNano() / int64(time.Millisecond),
|
||||
ExpiresAt: endDate.UnixNano() / int64(time.Millisecond),
|
||||
}
|
||||
|
||||
assert.True(t, license.IsSanctionedTrial())
|
||||
|
||||
license.IsTrial = false
|
||||
assert.False(t, license.IsSanctionedTrial())
|
||||
})
|
||||
|
||||
t.Run("invalid duration for sanctioned trial", func(t *testing.T) {
|
||||
startDate, err := time.Parse(time.RFC822, "01 Jan 21 00:00 UTC")
|
||||
assert.NoError(t, err)
|
||||
|
||||
endDate, err := time.Parse(time.RFC822, "31 Jan 21 08:00 UTC")
|
||||
assert.NoError(t, err)
|
||||
|
||||
license := &License{
|
||||
IsTrial: true,
|
||||
StartsAt: startDate.UnixNano() / int64(time.Millisecond),
|
||||
ExpiresAt: endDate.UnixNano() / int64(time.Millisecond),
|
||||
}
|
||||
|
||||
assert.False(t, license.IsSanctionedTrial())
|
||||
})
|
||||
|
||||
t.Run("boundary conditions for sanctioned trial", func(t *testing.T) {
|
||||
startDate, err := time.Parse(time.RFC822, "01 Jan 21 00:00 UTC")
|
||||
assert.NoError(t, err)
|
||||
|
||||
// 29 days + 23 hours 59 mins 59 seconds
|
||||
endDate, err := time.Parse("02 Jan 06 15:04:05 MST", "30 Jan 21 23:59:59 UTC")
|
||||
assert.NoError(t, err)
|
||||
|
||||
license := &License{
|
||||
IsTrial: true,
|
||||
StartsAt: startDate.UnixNano() / int64(time.Millisecond),
|
||||
ExpiresAt: endDate.UnixNano() / int64(time.Millisecond),
|
||||
}
|
||||
|
||||
assert.True(t, license.IsSanctionedTrial())
|
||||
|
||||
// 31 days + 23 hours 59 mins 59 seconds
|
||||
endDate, err = time.Parse("02 Jan 06 15:04:05 MST", "01 Feb 21 23:59:59 UTC")
|
||||
assert.NoError(t, err)
|
||||
license.ExpiresAt = endDate.UnixNano() / int64(time.Millisecond)
|
||||
assert.True(t, license.IsSanctionedTrial())
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user