2023-03-31 04:51:38 -07:00
|
|
|
import type { TEntitlement, TLicenseBlock } from '@n8n_io/license-sdk';
|
2023-01-27 05:56:56 -08:00
|
|
|
import { LicenseManager } from '@n8n_io/license-sdk';
|
|
|
|
import type { ILogger } from 'n8n-workflow';
|
2022-11-21 06:41:24 -08:00
|
|
|
import { getLogger } from './Logger';
|
|
|
|
import config from '@/config';
|
|
|
|
import * as Db from '@/Db';
|
2023-01-04 02:38:48 -08:00
|
|
|
import { LICENSE_FEATURES, N8N_VERSION, SETTINGS_LICENSE_CERT_KEY } from './constants';
|
2023-03-16 07:34:13 -07:00
|
|
|
import { Service } from 'typedi';
|
2022-11-21 06:41:24 -08:00
|
|
|
|
2023-03-31 04:51:38 -07:00
|
|
|
async function loadCertStr(): Promise<TLicenseBlock> {
|
|
|
|
// if we have an ephemeral license, we don't want to load it from the database
|
|
|
|
const ephemeralLicense = config.get('license.cert');
|
|
|
|
if (ephemeralLicense) {
|
|
|
|
return ephemeralLicense;
|
|
|
|
}
|
2022-11-21 06:41:24 -08:00
|
|
|
const databaseSettings = await Db.collections.Settings.findOne({
|
|
|
|
where: {
|
|
|
|
key: SETTINGS_LICENSE_CERT_KEY,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return databaseSettings?.value ?? '';
|
|
|
|
}
|
|
|
|
|
2023-03-31 04:51:38 -07:00
|
|
|
async function saveCertStr(value: TLicenseBlock): Promise<void> {
|
|
|
|
// if we have an ephemeral license, we don't want to save it to the database
|
|
|
|
if (config.get('license.cert')) return;
|
2022-11-21 06:41:24 -08:00
|
|
|
await Db.collections.Settings.upsert(
|
|
|
|
{
|
|
|
|
key: SETTINGS_LICENSE_CERT_KEY,
|
|
|
|
value,
|
|
|
|
loadOnStartup: false,
|
|
|
|
},
|
|
|
|
['key'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-16 07:34:13 -07:00
|
|
|
@Service()
|
2022-11-21 06:41:24 -08:00
|
|
|
export class License {
|
|
|
|
private logger: ILogger;
|
|
|
|
|
|
|
|
private manager: LicenseManager | undefined;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.logger = getLogger();
|
|
|
|
}
|
|
|
|
|
2023-01-04 02:38:48 -08:00
|
|
|
async init(instanceId: string) {
|
2022-11-21 06:41:24 -08:00
|
|
|
if (this.manager) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const server = config.getEnv('license.serverUrl');
|
|
|
|
const autoRenewEnabled = config.getEnv('license.autoRenewEnabled');
|
|
|
|
const autoRenewOffset = config.getEnv('license.autoRenewOffset');
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.manager = new LicenseManager({
|
|
|
|
server,
|
2022-11-28 08:39:34 -08:00
|
|
|
tenantId: config.getEnv('license.tenantId'),
|
2023-01-04 02:38:48 -08:00
|
|
|
productIdentifier: `n8n-${N8N_VERSION}`,
|
2022-11-21 06:41:24 -08:00
|
|
|
autoRenewEnabled,
|
|
|
|
autoRenewOffset,
|
|
|
|
logger: this.logger,
|
|
|
|
loadCertStr,
|
|
|
|
saveCertStr,
|
|
|
|
deviceFingerprint: () => instanceId,
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.manager.initialize();
|
|
|
|
} catch (e: unknown) {
|
|
|
|
if (e instanceof Error) {
|
|
|
|
this.logger.error('Could not initialize license manager sdk', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async activate(activationKey: string): Promise<void> {
|
|
|
|
if (!this.manager) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-20 01:52:01 -08:00
|
|
|
await this.manager.activate(activationKey);
|
2022-11-21 06:41:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async renew() {
|
|
|
|
if (!this.manager) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-20 01:52:01 -08:00
|
|
|
await this.manager.renew();
|
2022-11-21 06:41:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
isFeatureEnabled(feature: string): boolean {
|
|
|
|
if (!this.manager) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.manager.hasFeatureEnabled(feature);
|
|
|
|
}
|
|
|
|
|
|
|
|
isSharingEnabled() {
|
|
|
|
return this.isFeatureEnabled(LICENSE_FEATURES.SHARING);
|
|
|
|
}
|
2022-12-20 01:52:01 -08:00
|
|
|
|
2023-01-04 00:47:48 -08:00
|
|
|
isLogStreamingEnabled() {
|
|
|
|
return this.isFeatureEnabled(LICENSE_FEATURES.LOG_STREAMING);
|
|
|
|
}
|
|
|
|
|
2023-01-24 17:18:39 -08:00
|
|
|
isLdapEnabled() {
|
|
|
|
return this.isFeatureEnabled(LICENSE_FEATURES.LDAP);
|
|
|
|
}
|
|
|
|
|
2023-02-16 06:05:39 -08:00
|
|
|
isSamlEnabled() {
|
|
|
|
return this.isFeatureEnabled(LICENSE_FEATURES.SAML);
|
|
|
|
}
|
|
|
|
|
2023-03-07 09:35:52 -08:00
|
|
|
isAdvancedExecutionFiltersEnabled() {
|
|
|
|
return this.isFeatureEnabled(LICENSE_FEATURES.ADVANCED_EXECUTION_FILTERS);
|
2023-03-07 05:18:10 -08:00
|
|
|
}
|
|
|
|
|
2022-12-20 01:52:01 -08:00
|
|
|
getCurrentEntitlements() {
|
|
|
|
return this.manager?.getCurrentEntitlements() ?? [];
|
|
|
|
}
|
|
|
|
|
|
|
|
getFeatureValue(
|
|
|
|
feature: string,
|
|
|
|
requireValidCert?: boolean,
|
|
|
|
): undefined | boolean | number | string {
|
|
|
|
if (!this.manager) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.manager.getFeatureValue(feature, requireValidCert);
|
|
|
|
}
|
|
|
|
|
|
|
|
getManagementJwt(): string {
|
|
|
|
if (!this.manager) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return this.manager.getManagementJwt();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to get the main plan for a license
|
|
|
|
*/
|
|
|
|
getMainPlan(): TEntitlement | undefined {
|
|
|
|
if (!this.manager) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const entitlements = this.getCurrentEntitlements();
|
|
|
|
if (!entitlements.length) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entitlements.find(
|
|
|
|
(entitlement) =>
|
|
|
|
(entitlement.productMetadata.terms as unknown as { isMainPlan: boolean }).isMainPlan,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper functions for computed data
|
|
|
|
getTriggerLimit(): number {
|
|
|
|
return (this.getFeatureValue('quota:activeWorkflows') ?? -1) as number;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPlanName(): string {
|
|
|
|
return (this.getFeatureValue('planName') ?? 'Community') as string;
|
|
|
|
}
|
2022-11-21 06:41:24 -08:00
|
|
|
}
|