mirror of
https://github.com/SigNoz/signoz.git
synced 2026-11-18 22:30:42 +00:00
Compare commits
3 Commits
main
...
feat/licen
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1379a077e1 | ||
|
|
e7eabcfa2c | ||
|
|
b589325b35 |
@@ -86,7 +86,7 @@ func runServer(ctx context.Context, config signoz.Config, logger *slog.Logger) e
|
||||
zeus.Config{},
|
||||
noopzeus.NewProviderFactory(),
|
||||
licensing.Config{},
|
||||
func(_ sqlstore.SQLStore, _ zeus.Zeus, _ organization.Getter, _ analytics.Analytics) factory.ProviderFactory[licensing.Licensing, licensing.Config] {
|
||||
func(_ sqlstore.SQLStore, _ zeus.Zeus, _ organization.Getter, _ analytics.Analytics, _ cache.Cache) factory.ProviderFactory[licensing.Licensing, licensing.Config] {
|
||||
return nooplicensing.NewFactory()
|
||||
},
|
||||
func(_ zeus.Zeus, _ licensing.Licensing) subscription.Subscription {
|
||||
|
||||
@@ -103,8 +103,8 @@ func runServer(ctx context.Context, config signoz.Config, logger *slog.Logger) e
|
||||
enterprisezeus.Config(),
|
||||
httpzeus.NewProviderFactory(),
|
||||
enterpriselicensing.Config(24*time.Hour, 3),
|
||||
func(sqlstore sqlstore.SQLStore, zeus zeus.Zeus, orgGetter organization.Getter, analytics analytics.Analytics) factory.ProviderFactory[licensing.Licensing, licensing.Config] {
|
||||
return httplicensing.NewProviderFactory(sqlstore, zeus, orgGetter, analytics)
|
||||
func(sqlstore sqlstore.SQLStore, zeus zeus.Zeus, orgGetter organization.Getter, analytics analytics.Analytics, cache cache.Cache) factory.ProviderFactory[licensing.Licensing, licensing.Config] {
|
||||
return httplicensing.NewProviderFactory(sqlstore, zeus, orgGetter, analytics, cache)
|
||||
},
|
||||
func(zeus zeus.Zeus, licensing licensing.Licensing) subscription.Subscription {
|
||||
return httpsubscription.New(zeus, licensing)
|
||||
|
||||
26
ee/licensing/httplicensing/cache.go
Normal file
26
ee/licensing/httplicensing/cache.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package httplicensing
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/types/licensetypes"
|
||||
)
|
||||
|
||||
const (
|
||||
activeLicenseCacheKey string = "active_license"
|
||||
activeLicenseCacheTTL time.Duration = 5 * time.Minute
|
||||
)
|
||||
|
||||
// A nil License means the organization has no active license.
|
||||
type cachedActiveLicense struct {
|
||||
License *licensetypes.License
|
||||
}
|
||||
|
||||
func (typ cachedActiveLicense) MarshalBinary() ([]byte, error) {
|
||||
return json.Marshal(typ)
|
||||
}
|
||||
|
||||
func (typ *cachedActiveLicense) UnmarshalBinary(data []byte) error {
|
||||
return json.Unmarshal(data, typ)
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/SigNoz/signoz/ee/licensing/licensingstore/sqllicensingstore"
|
||||
"github.com/SigNoz/signoz/pkg/analytics"
|
||||
"github.com/SigNoz/signoz/pkg/cache"
|
||||
"github.com/SigNoz/signoz/pkg/errors"
|
||||
"github.com/SigNoz/signoz/pkg/factory"
|
||||
"github.com/SigNoz/signoz/pkg/licensing"
|
||||
@@ -25,16 +26,17 @@ type provider struct {
|
||||
settings factory.ScopedProviderSettings
|
||||
orgGetter organization.Getter
|
||||
analytics analytics.Analytics
|
||||
cache cache.Cache
|
||||
stopChan chan struct{}
|
||||
}
|
||||
|
||||
func NewProviderFactory(store sqlstore.SQLStore, zeus zeus.Zeus, orgGetter organization.Getter, analytics analytics.Analytics) factory.ProviderFactory[licensing.Licensing, licensing.Config] {
|
||||
func NewProviderFactory(store sqlstore.SQLStore, zeus zeus.Zeus, orgGetter organization.Getter, analytics analytics.Analytics, cache cache.Cache) factory.ProviderFactory[licensing.Licensing, licensing.Config] {
|
||||
return factory.NewProviderFactory(factory.MustNewName("http"), func(ctx context.Context, providerSettings factory.ProviderSettings, config licensing.Config) (licensing.Licensing, error) {
|
||||
return New(ctx, providerSettings, config, store, zeus, orgGetter, analytics)
|
||||
return New(ctx, providerSettings, config, store, zeus, orgGetter, analytics, cache)
|
||||
})
|
||||
}
|
||||
|
||||
func New(ctx context.Context, ps factory.ProviderSettings, config licensing.Config, sqlstore sqlstore.SQLStore, zeus zeus.Zeus, orgGetter organization.Getter, analytics analytics.Analytics) (licensing.Licensing, error) {
|
||||
func New(ctx context.Context, ps factory.ProviderSettings, config licensing.Config, sqlstore sqlstore.SQLStore, zeus zeus.Zeus, orgGetter organization.Getter, analytics analytics.Analytics, cache cache.Cache) (licensing.Licensing, error) {
|
||||
settings := factory.NewScopedProviderSettings(ps, "github.com/SigNoz/signoz/ee/licensing/httplicensing")
|
||||
licensestore := sqllicensingstore.New(sqlstore)
|
||||
return &provider{
|
||||
@@ -45,6 +47,7 @@ func New(ctx context.Context, ps factory.ProviderSettings, config licensing.Conf
|
||||
orgGetter: orgGetter,
|
||||
stopChan: make(chan struct{}),
|
||||
analytics: analytics,
|
||||
cache: cache,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -109,6 +112,7 @@ func (provider *provider) Activate(ctx context.Context, organizationID valuer.UU
|
||||
return nil, err
|
||||
}
|
||||
|
||||
provider.cache.Delete(ctx, organizationID, activeLicenseCacheKey)
|
||||
return license, nil
|
||||
}
|
||||
|
||||
@@ -150,25 +154,38 @@ func (provider *provider) Delete(ctx context.Context, organizationID valuer.UUID
|
||||
return errors.WithAdditionalf(err, "license %s cannot be deleted", licenseID.StringValue())
|
||||
}
|
||||
|
||||
return provider.store.Delete(ctx, organizationID, licenseID)
|
||||
if err := provider.store.Delete(ctx, organizationID, licenseID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
provider.cache.Delete(ctx, organizationID, activeLicenseCacheKey)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Caches both an active license and no active license, but never a store error.
|
||||
func (provider *provider) GetActive(ctx context.Context, organizationID valuer.UUID) (*licensetypes.License, error) {
|
||||
storableLicenses, err := provider.store.GetAll(ctx, organizationID)
|
||||
if err != nil {
|
||||
if cached, ok := provider.getActiveFromCache(ctx, organizationID); ok {
|
||||
if cached.License == nil {
|
||||
return nil, licensetypes.ErrActiveLicenseNotFound(organizationID)
|
||||
}
|
||||
|
||||
return cached.License, nil
|
||||
}
|
||||
|
||||
activeLicense, err := provider.getActiveFromStore(ctx, organizationID)
|
||||
if err != nil && !errors.Ast(err, errors.TypeNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
activeLicense, err := licensetypes.GetActiveLicenseFromStorableLicenses(storableLicenses, organizationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if cacheErr := provider.cache.Set(ctx, organizationID, activeLicenseCacheKey, &cachedActiveLicense{License: activeLicense}, activeLicenseCacheTTL); cacheErr != nil {
|
||||
provider.settings.Logger().WarnContext(ctx, "failed to write active license to cache", errors.Attr(cacheErr), slog.String("org_id", organizationID.StringValue()))
|
||||
}
|
||||
|
||||
return activeLicense, nil
|
||||
return activeLicense, err
|
||||
}
|
||||
|
||||
func (provider *provider) Refresh(ctx context.Context, organizationID valuer.UUID) error {
|
||||
activeLicense, err := provider.GetActive(ctx, organizationID)
|
||||
activeLicense, err := provider.getActiveFromStore(ctx, organizationID)
|
||||
if err != nil {
|
||||
if errors.Ast(err, errors.TypeNotFound) {
|
||||
return nil
|
||||
@@ -187,6 +204,7 @@ func (provider *provider) Refresh(ctx context.Context, organizationID valuer.UUI
|
||||
return err
|
||||
}
|
||||
|
||||
provider.cache.Delete(ctx, organizationID, activeLicenseCacheKey)
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
@@ -203,6 +221,8 @@ func (provider *provider) Refresh(ctx context.Context, organizationID valuer.UUI
|
||||
return err
|
||||
}
|
||||
|
||||
provider.cache.Delete(ctx, organizationID, activeLicenseCacheKey)
|
||||
|
||||
stats := licensetypes.NewStatsFromLicense(activeLicense)
|
||||
provider.analytics.Send(ctx,
|
||||
analyticstypes.Track{
|
||||
@@ -249,3 +269,30 @@ func (provider *provider) Collect(ctx context.Context, orgID valuer.UUID) (map[s
|
||||
|
||||
return licensetypes.NewStatsFromLicense(activeLicense), nil
|
||||
}
|
||||
|
||||
func (provider *provider) getActiveFromCache(ctx context.Context, organizationID valuer.UUID) (*cachedActiveLicense, bool) {
|
||||
cached := new(cachedActiveLicense)
|
||||
err := provider.cache.Get(ctx, organizationID, activeLicenseCacheKey, cached)
|
||||
if err != nil {
|
||||
if !errors.Ast(err, errors.TypeNotFound) {
|
||||
provider.settings.Logger().WarnContext(ctx, "failed to read active license from cache", errors.Attr(err), slog.String("org_id", organizationID.StringValue()))
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if cached.License != nil && cached.License.IsExpired() {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return cached, true
|
||||
}
|
||||
|
||||
func (provider *provider) getActiveFromStore(ctx context.Context, organizationID valuer.UUID) (*licensetypes.License, error) {
|
||||
storableLicenses, err := provider.store.GetAll(ctx, organizationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return licensetypes.GetActiveLicenseFromStorableLicenses(storableLicenses, organizationID)
|
||||
}
|
||||
|
||||
214
ee/licensing/httplicensing/provider_test.go
Normal file
214
ee/licensing/httplicensing/provider_test.go
Normal file
@@ -0,0 +1,214 @@
|
||||
package httplicensing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/analytics/analyticstest"
|
||||
"github.com/SigNoz/signoz/pkg/cache"
|
||||
"github.com/SigNoz/signoz/pkg/cache/cachetest"
|
||||
"github.com/SigNoz/signoz/pkg/errors"
|
||||
"github.com/SigNoz/signoz/pkg/instrumentation/instrumentationtest"
|
||||
"github.com/SigNoz/signoz/pkg/licensing"
|
||||
"github.com/SigNoz/signoz/pkg/sqlstore"
|
||||
"github.com/SigNoz/signoz/pkg/sqlstore/sqlstoretest"
|
||||
"github.com/SigNoz/signoz/pkg/types/licensetypes"
|
||||
"github.com/SigNoz/signoz/pkg/valuer"
|
||||
"github.com/SigNoz/signoz/pkg/zeus"
|
||||
"github.com/SigNoz/signoz/pkg/zeus/noopzeus"
|
||||
)
|
||||
|
||||
var (
|
||||
testOrganizationID = valuer.MustNewUUID("0196f794-ff30-7bee-a5f4-ef5ad315715e")
|
||||
testLicenseID = valuer.MustNewUUID("0196360e-90cd-7a74-8313-1aa815ce2a67")
|
||||
licenseColumns = []string{"id", "created_at", "updated_at", "key", "data", "last_validated_at", "org_id"}
|
||||
)
|
||||
|
||||
const (
|
||||
selectLicensesQuery = `SELECT (.+) FROM "license"`
|
||||
updateLicenseQuery = `UPDATE "license"`
|
||||
deleteLicenseQuery = `DELETE FROM "license"`
|
||||
)
|
||||
|
||||
func newTestLicensing(t *testing.T) (licensing.Licensing, sqlmock.Sqlmock, cache.Cache) {
|
||||
t.Helper()
|
||||
|
||||
sqlStore := sqlstoretest.New(sqlstore.Config{Provider: "sqlite"}, sqlmock.QueryMatcherRegexp)
|
||||
|
||||
licenseCache, err := cachetest.New(cache.Config{Provider: "memory", Memory: cache.Memory{NumCounters: 1000, MaxCost: 1 << 20}})
|
||||
require.NoError(t, err)
|
||||
|
||||
providerSettings := instrumentationtest.New().ToProviderSettings()
|
||||
|
||||
noopZeus, err := noopzeus.New(context.Background(), providerSettings, zeus.Config{})
|
||||
require.NoError(t, err)
|
||||
|
||||
licensing, err := New(context.Background(), providerSettings, licensing.Config{PollInterval: time.Hour, FailureThreshold: 3}, sqlStore, noopZeus, nil, analyticstest.New(), licenseCache)
|
||||
require.NoError(t, err)
|
||||
|
||||
return licensing, sqlStore.Mock(), licenseCache
|
||||
}
|
||||
|
||||
func newLicenseRows(lastValidatedAt time.Time) *sqlmock.Rows {
|
||||
createdAt := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
data := `{"status":"VALID","state":"EVALUATING","platform":"SELF_HOSTED","plan":{"name":"ENTERPRISE"},"valid_from":1732146923,"valid_until":-1,"features":[],"event_queue":{}}`
|
||||
return sqlmock.
|
||||
NewRows(licenseColumns).
|
||||
AddRow(testLicenseID.StringValue(), createdAt, createdAt, "test-license-key", data, lastValidatedAt, testOrganizationID.StringValue())
|
||||
}
|
||||
|
||||
func TestGetActiveServesRepeatReadsFromCache(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
licensing, mock, _ := newTestLicensing(t)
|
||||
|
||||
mock.ExpectQuery(selectLicensesQuery).WillReturnRows(newLicenseRows(time.Now().UTC()))
|
||||
|
||||
fromStore, err := licensing.GetActive(ctx, testOrganizationID)
|
||||
require.NoError(t, err)
|
||||
|
||||
fromCache, err := licensing.GetActive(ctx, testOrganizationID)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, licensetypes.NewGettableLicenseWithKey(fromStore), licensetypes.NewGettableLicenseWithKey(fromCache))
|
||||
assert.Equal(t, fromStore.OrganizationID, fromCache.OrganizationID)
|
||||
assert.True(t, fromStore.LastValidatedAt.Equal(fromCache.LastValidatedAt))
|
||||
|
||||
fromStoreData, err := json.Marshal(fromStore.Data)
|
||||
require.NoError(t, err)
|
||||
fromCacheData, err := json.Marshal(fromCache.Data)
|
||||
require.NoError(t, err)
|
||||
assert.JSONEq(t, string(fromStoreData), string(fromCacheData))
|
||||
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestGetActiveCachesNoActiveLicense(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
licensing, mock, _ := newTestLicensing(t)
|
||||
|
||||
mock.ExpectQuery(selectLicensesQuery).WillReturnRows(sqlmock.NewRows(licenseColumns))
|
||||
|
||||
for range 2 {
|
||||
license, err := licensing.GetActive(ctx, testOrganizationID)
|
||||
require.Error(t, err)
|
||||
assert.True(t, errors.Ast(err, errors.TypeNotFound))
|
||||
assert.Nil(t, license)
|
||||
}
|
||||
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestGetActiveDoesNotCacheErrors(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
licensing, mock, _ := newTestLicensing(t)
|
||||
|
||||
createdAt := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
licenseRowWithoutStatus := sqlmock.
|
||||
NewRows(licenseColumns).
|
||||
AddRow(testLicenseID.StringValue(), createdAt, createdAt, "test-license-key", `{"plan":{"name":"ENTERPRISE"}}`, createdAt, testOrganizationID.StringValue())
|
||||
|
||||
mock.ExpectQuery(selectLicensesQuery).WillReturnRows(licenseRowWithoutStatus)
|
||||
mock.ExpectQuery(selectLicensesQuery).WillReturnRows(newLicenseRows(time.Now().UTC()))
|
||||
|
||||
_, err := licensing.GetActive(ctx, testOrganizationID)
|
||||
require.Error(t, err)
|
||||
assert.ErrorContains(t, err, "license status is missing")
|
||||
|
||||
license, err := licensing.GetActive(ctx, testOrganizationID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, testLicenseID, license.ID)
|
||||
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestGetActiveReloadsExpiredCachedLicense(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
licensing, mock, licenseCache := newTestLicensing(t)
|
||||
|
||||
expiredLicense := &licensetypes.License{
|
||||
ID: valuer.GenerateUUID(),
|
||||
ValidFrom: time.Now().Add(-2 * time.Hour).Unix(),
|
||||
ValidUntil: time.Now().Add(-time.Hour).Unix(),
|
||||
OrganizationID: testOrganizationID,
|
||||
}
|
||||
require.NoError(t, licenseCache.Set(ctx, testOrganizationID, activeLicenseCacheKey, &cachedActiveLicense{License: expiredLicense}, activeLicenseCacheTTL))
|
||||
|
||||
mock.ExpectQuery(selectLicensesQuery).WillReturnRows(newLicenseRows(time.Now().UTC()))
|
||||
|
||||
license, err := licensing.GetActive(ctx, testOrganizationID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, testLicenseID, license.ID)
|
||||
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestDeleteClearsCachedActiveLicense(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
licensing, mock, _ := newTestLicensing(t)
|
||||
|
||||
mock.ExpectQuery(selectLicensesQuery).WillReturnRows(newLicenseRows(time.Now().UTC()))
|
||||
mock.ExpectQuery(selectLicensesQuery).WillReturnRows(newLicenseRows(time.Now().UTC()))
|
||||
mock.ExpectExec(deleteLicenseQuery).WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectQuery(selectLicensesQuery).WillReturnRows(sqlmock.NewRows(licenseColumns))
|
||||
|
||||
_, err := licensing.GetActive(ctx, testOrganizationID)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, licensing.Delete(ctx, testOrganizationID, testLicenseID))
|
||||
|
||||
_, err = licensing.GetActive(ctx, testOrganizationID)
|
||||
require.Error(t, err)
|
||||
assert.True(t, errors.Ast(err, errors.TypeNotFound))
|
||||
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestRefreshClearsCachedActiveLicenseOnDowngrade(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
licensing, mock, _ := newTestLicensing(t)
|
||||
|
||||
staleLastValidatedAt := time.Now().Add(-4 * time.Hour).UTC()
|
||||
mock.ExpectQuery(selectLicensesQuery).WillReturnRows(newLicenseRows(staleLastValidatedAt))
|
||||
mock.ExpectQuery(selectLicensesQuery).WillReturnRows(newLicenseRows(staleLastValidatedAt))
|
||||
mock.ExpectExec(updateLicenseQuery).WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectQuery(selectLicensesQuery).WillReturnRows(newLicenseRows(staleLastValidatedAt))
|
||||
|
||||
_, err := licensing.GetActive(ctx, testOrganizationID)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, licensing.Refresh(ctx, testOrganizationID))
|
||||
|
||||
_, err = licensing.GetActive(ctx, testOrganizationID)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestRefreshWritesActiveLicenseFromStoreNotCache(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
licensing, mock, licenseCache := newTestLicensing(t)
|
||||
|
||||
staleLastValidatedAt := time.Now().Add(-4 * time.Hour).UTC()
|
||||
staleCachedLicense := &licensetypes.License{
|
||||
ID: testLicenseID,
|
||||
Key: "stale-cached-license-key",
|
||||
ValidFrom: 1732146923,
|
||||
ValidUntil: -1,
|
||||
LastValidatedAt: staleLastValidatedAt,
|
||||
OrganizationID: testOrganizationID,
|
||||
}
|
||||
require.NoError(t, licenseCache.Set(ctx, testOrganizationID, activeLicenseCacheKey, &cachedActiveLicense{License: staleCachedLicense}, activeLicenseCacheTTL))
|
||||
|
||||
mock.ExpectQuery(selectLicensesQuery).WillReturnRows(newLicenseRows(staleLastValidatedAt))
|
||||
mock.ExpectExec(`UPDATE "license" .*'test-license-key'`).WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
require.NoError(t, licensing.Refresh(ctx, testOrganizationID))
|
||||
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
@@ -170,7 +170,7 @@ func New(
|
||||
zeusConfig zeus.Config,
|
||||
zeusProviderFactory factory.ProviderFactory[zeus.Zeus, zeus.Config],
|
||||
licenseConfig licensing.Config,
|
||||
licenseProviderFactory func(sqlstore.SQLStore, zeus.Zeus, organization.Getter, analytics.Analytics) factory.ProviderFactory[licensing.Licensing, licensing.Config],
|
||||
licenseProviderFactory func(sqlstore.SQLStore, zeus.Zeus, organization.Getter, analytics.Analytics, cache.Cache) factory.ProviderFactory[licensing.Licensing, licensing.Config],
|
||||
subscriptionCallback func(zeus.Zeus, licensing.Licensing) subscription.Subscription,
|
||||
emailingProviderFactories factory.NamedMap[factory.ProviderFactory[emailing.Emailing, emailing.Config]],
|
||||
cacheProviderFactories factory.NamedMap[factory.ProviderFactory[cache.Cache, cache.Config]],
|
||||
@@ -410,7 +410,7 @@ func New(
|
||||
// Initialize user role store
|
||||
userRoleStore := impluser.NewUserRoleStore(sqlstore, providerSettings)
|
||||
|
||||
licensingProviderFactory := licenseProviderFactory(sqlstore, zeus, orgGetter, analytics)
|
||||
licensingProviderFactory := licenseProviderFactory(sqlstore, zeus, orgGetter, analytics, cache)
|
||||
licensing, err := licensingProviderFactory.New(
|
||||
ctx,
|
||||
providerSettings,
|
||||
|
||||
@@ -119,23 +119,27 @@ func GetActiveLicenseFromStorableLicenses(storableLicenses []*StorableLicense, o
|
||||
|
||||
if activeLicense == nil &&
|
||||
(license.ValidFrom != 0) &&
|
||||
(license.ValidUntil == -1 || license.ValidUntil > time.Now().Unix()) {
|
||||
!license.IsExpired() {
|
||||
activeLicense = license
|
||||
}
|
||||
if activeLicense != nil &&
|
||||
license.ValidFrom > activeLicense.ValidFrom &&
|
||||
(license.ValidUntil == -1 || license.ValidUntil > time.Now().Unix()) {
|
||||
!license.IsExpired() {
|
||||
activeLicense = license
|
||||
}
|
||||
}
|
||||
|
||||
if activeLicense == nil {
|
||||
return nil, errors.Newf(errors.TypeNotFound, errors.CodeNotFound, "no active license found for the organization %s", organizationID.StringValue())
|
||||
return nil, ErrActiveLicenseNotFound(organizationID)
|
||||
}
|
||||
|
||||
return activeLicense, nil
|
||||
}
|
||||
|
||||
func ErrActiveLicenseNotFound(organizationID valuer.UUID) error {
|
||||
return errors.Newf(errors.TypeNotFound, errors.CodeNotFound, "no active license found for the organization %s", organizationID.StringValue())
|
||||
}
|
||||
|
||||
func NewLicense(zeusLicense *zeustypes.License, organizationID valuer.UUID) (*License, error) {
|
||||
if zeusLicense.ID.IsZero() {
|
||||
return nil, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "license id is missing")
|
||||
@@ -243,6 +247,11 @@ func NewStatsFromLicense(license *License) map[string]any {
|
||||
}
|
||||
}
|
||||
|
||||
// IsExpired reports whether the license's validity has ended. A ValidUntil of -1 means the license never expires.
|
||||
func (license *License) IsExpired() bool {
|
||||
return license.ValidUntil != -1 && license.ValidUntil <= time.Now().Unix()
|
||||
}
|
||||
|
||||
func (license *License) UpdateFeatures(features []*Feature) {
|
||||
license.Features = features
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user