UNPKG

n8n

Version:

n8n Workflow Automation Tool

343 lines 15.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.License = void 0; const license_sdk_1 = require("@n8n_io/license-sdk"); const n8n_core_1 = require("n8n-core"); const typedi_1 = __importStar(require("typedi")); const Logger_1 = require("./Logger"); const config_1 = __importDefault(require("./config")); const constants_1 = require("./constants"); const settings_repository_1 = require("./databases/repositories/settings.repository"); const redis_service_1 = require("./services/redis.service"); const orchestration_service_1 = require("./services/orchestration.service"); const OnShutdown_1 = require("./decorators/OnShutdown"); const license_metrics_service_1 = require("./metrics/license-metrics.service"); let License = class License { constructor(logger, instanceSettings, orchestrationService, settingsRepository, licenseMetricsService) { this.logger = logger; this.instanceSettings = instanceSettings; this.orchestrationService = orchestrationService; this.settingsRepository = settingsRepository; this.licenseMetricsService = licenseMetricsService; this.isShuttingDown = false; } renewalEnabled(instanceType) { if (instanceType !== 'main') return false; const autoRenewEnabled = config_1.default.getEnv('license.autoRenewEnabled'); if (config_1.default.getEnv('multiMainSetup.enabled')) { return autoRenewEnabled && this.instanceSettings.isLeader; } return autoRenewEnabled; } async init(instanceType = 'main', forceRecreate = false) { if (this.manager && !forceRecreate) { this.logger.warn('License manager already initialized or shutting down'); return; } if (this.isShuttingDown) { this.logger.warn('License manager already shutting down'); return; } const isMainInstance = instanceType === 'main'; const server = config_1.default.getEnv('license.serverUrl'); const offlineMode = !isMainInstance; const autoRenewOffset = config_1.default.getEnv('license.autoRenewOffset'); const saveCertStr = isMainInstance ? async (value) => await this.saveCertStr(value) : async () => { }; const onFeatureChange = isMainInstance ? async (features) => await this.onFeatureChange(features) : async () => { }; const collectUsageMetrics = isMainInstance ? async () => await this.licenseMetricsService.collectUsageMetrics() : async () => []; const collectPassthroughData = isMainInstance ? async () => await this.licenseMetricsService.collectPassthroughData() : async () => ({}); const renewalEnabled = this.renewalEnabled(instanceType); try { this.manager = new license_sdk_1.LicenseManager({ server, tenantId: config_1.default.getEnv('license.tenantId'), productIdentifier: `n8n-${constants_1.N8N_VERSION}`, autoRenewEnabled: renewalEnabled, renewOnInit: renewalEnabled, autoRenewOffset, offlineMode, logger: this.logger, loadCertStr: async () => await this.loadCertStr(), saveCertStr, deviceFingerprint: () => this.instanceSettings.instanceId, collectUsageMetrics, collectPassthroughData, onFeatureChange, }); await this.manager.initialize(); } catch (e) { if (e instanceof Error) { this.logger.error('Could not initialize license manager sdk', e); } } } async loadCertStr() { var _a; const ephemeralLicense = config_1.default.get('license.cert'); if (ephemeralLicense) { return ephemeralLicense; } const databaseSettings = await this.settingsRepository.findOne({ where: { key: constants_1.SETTINGS_LICENSE_CERT_KEY, }, }); return (_a = databaseSettings === null || databaseSettings === void 0 ? void 0 : databaseSettings.value) !== null && _a !== void 0 ? _a : ''; } async onFeatureChange(_features) { if (config_1.default.getEnv('executions.mode') === 'queue' && config_1.default.getEnv('multiMainSetup.enabled')) { const isMultiMainLicensed = _features[constants_1.LICENSE_FEATURES.MULTIPLE_MAIN_INSTANCES]; this.orchestrationService.setMultiMainSetupLicensed(isMultiMainLicensed !== null && isMultiMainLicensed !== void 0 ? isMultiMainLicensed : false); if (this.orchestrationService.isMultiMainSetupEnabled && this.orchestrationService.isFollower) { this.logger.debug('[Multi-main setup] Instance is follower, skipping sending of "reloadLicense" command...'); return; } if (this.orchestrationService.isMultiMainSetupEnabled && !isMultiMainLicensed) { this.logger.debug('[Multi-main setup] License changed with no support for multi-main setup - no new followers will be allowed to init. To restore multi-main setup, please upgrade to a license that supports this feature.'); } } if (config_1.default.getEnv('executions.mode') === 'queue') { if (!this.redisPublisher) { this.logger.debug('Initializing Redis publisher for License Service'); this.redisPublisher = await typedi_1.default.get(redis_service_1.RedisService).getPubSubPublisher(); } await this.redisPublisher.publishToCommandChannel({ command: 'reloadLicense', }); } const isS3Selected = config_1.default.getEnv('binaryDataManager.mode') === 's3'; const isS3Available = config_1.default.getEnv('binaryDataManager.availableModes').includes('s3'); const isS3Licensed = _features['feat:binaryDataS3']; if (isS3Selected && isS3Available && !isS3Licensed) { this.logger.debug('License changed with no support for external storage - blocking writes on object store. To restore writes, please upgrade to a license that supports this feature.'); typedi_1.default.get(n8n_core_1.ObjectStoreService).setReadonly(true); } } async saveCertStr(value) { if (config_1.default.get('license.cert')) return; await this.settingsRepository.upsert({ key: constants_1.SETTINGS_LICENSE_CERT_KEY, value, loadOnStartup: false, }, ['key']); } async activate(activationKey) { if (!this.manager) { return; } await this.manager.activate(activationKey); } async reload() { if (!this.manager) { return; } this.logger.debug('Reloading license'); await this.manager.reload(); } async renew() { if (!this.manager) { return; } await this.manager.renew(); } async shutdown() { this.isShuttingDown = true; if (!this.manager) { return; } await this.manager.shutdown(); } isFeatureEnabled(feature) { var _a, _b; return (_b = (_a = this.manager) === null || _a === void 0 ? void 0 : _a.hasFeatureEnabled(feature)) !== null && _b !== void 0 ? _b : false; } isSharingEnabled() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.SHARING); } isLogStreamingEnabled() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.LOG_STREAMING); } isLdapEnabled() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.LDAP); } isSamlEnabled() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.SAML); } isAiAssistantEnabled() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.AI_ASSISTANT); } isAdvancedExecutionFiltersEnabled() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.ADVANCED_EXECUTION_FILTERS); } isAdvancedPermissionsLicensed() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.ADVANCED_PERMISSIONS); } isDebugInEditorLicensed() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.DEBUG_IN_EDITOR); } isBinaryDataS3Licensed() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.BINARY_DATA_S3); } isMultipleMainInstancesLicensed() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.MULTIPLE_MAIN_INSTANCES); } isVariablesEnabled() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.VARIABLES); } isSourceControlLicensed() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.SOURCE_CONTROL); } isExternalSecretsEnabled() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.EXTERNAL_SECRETS); } isWorkflowHistoryLicensed() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.WORKFLOW_HISTORY); } isAPIDisabled() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.API_DISABLED); } isWorkerViewLicensed() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.WORKER_VIEW); } isProjectRoleAdminLicensed() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.PROJECT_ROLE_ADMIN); } isProjectRoleEditorLicensed() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.PROJECT_ROLE_EDITOR); } isProjectRoleViewerLicensed() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.PROJECT_ROLE_VIEWER); } isCustomNpmRegistryEnabled() { return this.isFeatureEnabled(constants_1.LICENSE_FEATURES.COMMUNITY_NODES_CUSTOM_REGISTRY); } getCurrentEntitlements() { var _a, _b; return (_b = (_a = this.manager) === null || _a === void 0 ? void 0 : _a.getCurrentEntitlements()) !== null && _b !== void 0 ? _b : []; } getFeatureValue(feature) { var _a; return (_a = this.manager) === null || _a === void 0 ? void 0 : _a.getFeatureValue(feature); } getManagementJwt() { if (!this.manager) { return ''; } return this.manager.getManagementJwt(); } getMainPlan() { if (!this.manager) { return undefined; } const entitlements = this.getCurrentEntitlements(); if (!entitlements.length) { return undefined; } return entitlements.find((entitlement) => { var _a, _b; return (_b = (_a = entitlement.productMetadata) === null || _a === void 0 ? void 0 : _a.terms) === null || _b === void 0 ? void 0 : _b.isMainPlan; }); } getConsumerId() { var _a, _b; return (_b = (_a = this.manager) === null || _a === void 0 ? void 0 : _a.getConsumerId()) !== null && _b !== void 0 ? _b : 'unknown'; } getUsersLimit() { var _a; return (_a = this.getFeatureValue(constants_1.LICENSE_QUOTAS.USERS_LIMIT)) !== null && _a !== void 0 ? _a : constants_1.UNLIMITED_LICENSE_QUOTA; } getTriggerLimit() { var _a; return (_a = this.getFeatureValue(constants_1.LICENSE_QUOTAS.TRIGGER_LIMIT)) !== null && _a !== void 0 ? _a : constants_1.UNLIMITED_LICENSE_QUOTA; } getVariablesLimit() { var _a; return (_a = this.getFeatureValue(constants_1.LICENSE_QUOTAS.VARIABLES_LIMIT)) !== null && _a !== void 0 ? _a : constants_1.UNLIMITED_LICENSE_QUOTA; } getWorkflowHistoryPruneLimit() { var _a; return ((_a = this.getFeatureValue(constants_1.LICENSE_QUOTAS.WORKFLOW_HISTORY_PRUNE_LIMIT)) !== null && _a !== void 0 ? _a : constants_1.UNLIMITED_LICENSE_QUOTA); } getTeamProjectLimit() { var _a; return (_a = this.getFeatureValue(constants_1.LICENSE_QUOTAS.TEAM_PROJECT_LIMIT)) !== null && _a !== void 0 ? _a : 0; } getPlanName() { var _a; return (_a = this.getFeatureValue('planName')) !== null && _a !== void 0 ? _a : 'Community'; } getInfo() { if (!this.manager) { return 'n/a'; } return this.manager.toString(); } isWithinUsersLimit() { return this.getUsersLimit() === constants_1.UNLIMITED_LICENSE_QUOTA; } async reinit() { var _a; (_a = this.manager) === null || _a === void 0 ? void 0 : _a.reset(); await this.init('main', true); } }; exports.License = License; __decorate([ (0, OnShutdown_1.OnShutdown)(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], License.prototype, "shutdown", null); exports.License = License = __decorate([ (0, typedi_1.Service)(), __metadata("design:paramtypes", [Logger_1.Logger, n8n_core_1.InstanceSettings, orchestration_service_1.OrchestrationService, settings_repository_1.SettingsRepository, license_metrics_service_1.LicenseMetricsService]) ], License); //# sourceMappingURL=License.js.map