2022-04-28 10:57:52 +02:00
|
|
|
import fetch from 'make-fetch-happen';
|
2023-06-13 15:54:20 +02:00
|
|
|
import {
|
|
|
|
IContextFieldStore,
|
|
|
|
IEnvironmentStore,
|
|
|
|
IEventStore,
|
|
|
|
IFeatureStrategiesStore,
|
|
|
|
IFeatureToggleStore,
|
|
|
|
IGroupStore,
|
|
|
|
IProjectStore,
|
|
|
|
IRoleStore,
|
|
|
|
ISegmentStore,
|
|
|
|
IUnleashStores,
|
|
|
|
IUserStore,
|
|
|
|
} from '../types/stores';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashConfig } from '../types/option';
|
|
|
|
import version from '../util/version';
|
|
|
|
import { Logger } from '../logger';
|
2021-08-12 15:04:37 +02:00
|
|
|
import { ISettingStore } from '../types/stores/settings-store';
|
2021-11-02 15:13:46 +01:00
|
|
|
import { hoursToMilliseconds } from 'date-fns';
|
2023-06-13 15:54:20 +02:00
|
|
|
import { IStrategyStore } from 'lib/types';
|
|
|
|
import { FEATURES_EXPORTED, FEATURES_IMPORTED } from '../types';
|
|
|
|
import { IFlagResolver } from '../types';
|
2021-04-22 10:07:10 +02:00
|
|
|
|
|
|
|
export interface IVersionInfo {
|
|
|
|
oss: string;
|
|
|
|
enterprise?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IVersionHolder {
|
|
|
|
current: IVersionInfo;
|
2022-08-31 08:06:25 +02:00
|
|
|
latest: Partial<IVersionInfo>;
|
2021-04-22 10:07:10 +02:00
|
|
|
isLatest: boolean;
|
|
|
|
instanceId: string;
|
|
|
|
}
|
|
|
|
|
2022-01-23 21:26:28 +01:00
|
|
|
export interface IVersionResponse {
|
|
|
|
versions: IVersionInfo;
|
|
|
|
latest: boolean;
|
|
|
|
}
|
|
|
|
|
2023-06-13 15:54:20 +02:00
|
|
|
export interface IFeatureUsageInfo {
|
|
|
|
instanceId: string;
|
|
|
|
versionOSS: string;
|
|
|
|
versionEnterprise?: string;
|
|
|
|
users: number;
|
|
|
|
featureToggles: number;
|
|
|
|
projects: number;
|
|
|
|
contextFields: number;
|
|
|
|
roles: number;
|
|
|
|
featureExports: number;
|
|
|
|
featureImports: number;
|
|
|
|
groups: number;
|
|
|
|
environments: number;
|
|
|
|
segments: number;
|
|
|
|
strategies: number;
|
|
|
|
SAMLenabled: boolean;
|
|
|
|
OIDCenabled: boolean;
|
|
|
|
customStrategies: number;
|
|
|
|
customStrategiesInUse: number;
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
export default class VersionService {
|
|
|
|
private logger: Logger;
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
private settingStore: ISettingStore;
|
2021-04-22 10:07:10 +02:00
|
|
|
|
2023-06-13 15:54:20 +02:00
|
|
|
private strategyStore: IStrategyStore;
|
|
|
|
|
|
|
|
private userStore: IUserStore;
|
|
|
|
|
|
|
|
private featureToggleStore: IFeatureToggleStore;
|
|
|
|
|
|
|
|
private projectStore: IProjectStore;
|
|
|
|
|
|
|
|
private environmentStore: IEnvironmentStore;
|
|
|
|
|
|
|
|
private contextFieldStore: IContextFieldStore;
|
|
|
|
|
|
|
|
private groupStore: IGroupStore;
|
|
|
|
|
|
|
|
private roleStore: IRoleStore;
|
|
|
|
|
|
|
|
private segmentStore: ISegmentStore;
|
|
|
|
|
|
|
|
private eventStore: IEventStore;
|
|
|
|
|
|
|
|
private featureStrategiesStore: IFeatureStrategiesStore;
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
private current: IVersionInfo;
|
|
|
|
|
|
|
|
private latest?: IVersionInfo;
|
|
|
|
|
|
|
|
private enabled: boolean;
|
|
|
|
|
2023-06-15 15:11:58 +02:00
|
|
|
private telemetryEnabled: boolean;
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
private versionCheckUrl: string;
|
|
|
|
|
|
|
|
private instanceId?: string;
|
|
|
|
|
|
|
|
private isLatest: boolean;
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
private timer: NodeJS.Timeout;
|
|
|
|
|
2023-06-13 15:54:20 +02:00
|
|
|
private flagResolver: IFlagResolver;
|
|
|
|
|
2021-02-19 11:13:25 +01:00
|
|
|
constructor(
|
2023-06-13 15:54:20 +02:00
|
|
|
{
|
|
|
|
settingStore,
|
|
|
|
strategyStore,
|
|
|
|
userStore,
|
|
|
|
featureToggleStore,
|
|
|
|
projectStore,
|
|
|
|
environmentStore,
|
|
|
|
contextFieldStore,
|
|
|
|
groupStore,
|
|
|
|
roleStore,
|
|
|
|
segmentStore,
|
|
|
|
eventStore,
|
|
|
|
featureStrategiesStore,
|
|
|
|
}: Pick<
|
|
|
|
IUnleashStores,
|
|
|
|
| 'settingStore'
|
|
|
|
| 'strategyStore'
|
|
|
|
| 'userStore'
|
|
|
|
| 'featureToggleStore'
|
|
|
|
| 'projectStore'
|
|
|
|
| 'environmentStore'
|
|
|
|
| 'contextFieldStore'
|
|
|
|
| 'groupStore'
|
|
|
|
| 'roleStore'
|
|
|
|
| 'segmentStore'
|
|
|
|
| 'eventStore'
|
|
|
|
| 'featureStrategiesStore'
|
|
|
|
>,
|
2021-04-22 10:07:10 +02:00
|
|
|
{
|
|
|
|
getLogger,
|
|
|
|
versionCheck,
|
2021-04-23 11:05:25 +02:00
|
|
|
enterpriseVersion,
|
2023-06-13 15:54:20 +02:00
|
|
|
flagResolver,
|
2023-06-15 15:11:58 +02:00
|
|
|
telemetry,
|
2021-04-23 11:05:25 +02:00
|
|
|
}: Pick<
|
2021-08-12 15:04:37 +02:00
|
|
|
IUnleashConfig,
|
2023-06-15 15:11:58 +02:00
|
|
|
| 'getLogger'
|
|
|
|
| 'versionCheck'
|
|
|
|
| 'enterpriseVersion'
|
|
|
|
| 'flagResolver'
|
|
|
|
| 'telemetry'
|
2021-04-23 11:05:25 +02:00
|
|
|
>,
|
2021-02-19 11:13:25 +01:00
|
|
|
) {
|
|
|
|
this.logger = getLogger('lib/services/version-service.js');
|
|
|
|
this.settingStore = settingStore;
|
2023-06-13 15:54:20 +02:00
|
|
|
this.strategyStore = strategyStore;
|
|
|
|
this.userStore = userStore;
|
|
|
|
this.featureToggleStore = featureToggleStore;
|
|
|
|
this.projectStore = projectStore;
|
|
|
|
this.environmentStore = environmentStore;
|
|
|
|
this.contextFieldStore = contextFieldStore;
|
|
|
|
this.groupStore = groupStore;
|
|
|
|
this.roleStore = roleStore;
|
|
|
|
this.segmentStore = segmentStore;
|
|
|
|
this.eventStore = eventStore;
|
|
|
|
this.featureStrategiesStore = featureStrategiesStore;
|
2021-02-19 11:13:25 +01:00
|
|
|
this.current = {
|
|
|
|
oss: version,
|
2021-04-23 11:05:25 +02:00
|
|
|
enterprise: enterpriseVersion || '',
|
2021-02-19 11:13:25 +01:00
|
|
|
};
|
2021-04-22 10:07:10 +02:00
|
|
|
this.enabled = versionCheck.enable;
|
2023-06-15 15:11:58 +02:00
|
|
|
this.telemetryEnabled = telemetry;
|
2021-04-22 10:07:10 +02:00
|
|
|
this.versionCheckUrl = versionCheck.url;
|
|
|
|
this.isLatest = true;
|
2023-06-13 15:54:20 +02:00
|
|
|
this.flagResolver = flagResolver;
|
2021-03-26 14:32:27 +01:00
|
|
|
process.nextTick(() => this.setup());
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async setup(): Promise<void> {
|
2021-03-26 14:32:27 +01:00
|
|
|
await this.setInstanceId();
|
2021-04-22 10:07:10 +02:00
|
|
|
await this.checkLatestVersion();
|
2021-05-28 11:10:24 +02:00
|
|
|
this.timer = setInterval(
|
|
|
|
async () => this.checkLatestVersion(),
|
2021-11-02 15:13:46 +01:00
|
|
|
hoursToMilliseconds(48),
|
2021-05-28 11:10:24 +02:00
|
|
|
);
|
|
|
|
this.timer.unref();
|
2021-02-19 11:13:25 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async setInstanceId(): Promise<void> {
|
2021-03-15 08:46:03 +01:00
|
|
|
try {
|
2021-02-19 11:13:25 +01:00
|
|
|
const { id } = await this.settingStore.get('instanceInfo');
|
2021-02-22 14:21:23 +01:00
|
|
|
this.instanceId = id;
|
2021-03-15 08:46:03 +01:00
|
|
|
} catch (err) {
|
|
|
|
this.logger.warn('Could not find instanceInfo');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async checkLatestVersion(): Promise<void> {
|
2021-03-15 08:46:03 +01:00
|
|
|
if (this.enabled) {
|
2021-02-19 11:13:25 +01:00
|
|
|
try {
|
2023-06-13 15:54:20 +02:00
|
|
|
const versionPayload: any = {
|
|
|
|
versions: this.current,
|
|
|
|
instanceId: this.instanceId,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (
|
2023-06-15 15:11:58 +02:00
|
|
|
this.flagResolver.isEnabled(
|
|
|
|
'experimentalExtendedTelemetry',
|
|
|
|
) &&
|
|
|
|
this.telemetryEnabled
|
2023-06-13 15:54:20 +02:00
|
|
|
) {
|
|
|
|
const featureInfo = await this.getFeatureUsageInfo();
|
|
|
|
versionPayload.featureInfo = featureInfo;
|
|
|
|
}
|
2021-03-26 14:32:27 +01:00
|
|
|
const res = await fetch(this.versionCheckUrl, {
|
2021-02-19 11:13:25 +01:00
|
|
|
method: 'POST',
|
2023-06-13 15:54:20 +02:00
|
|
|
body: JSON.stringify(versionPayload),
|
2021-02-19 11:13:25 +01:00
|
|
|
headers: { 'Content-Type': 'application/json' },
|
2021-03-26 14:32:27 +01:00
|
|
|
});
|
|
|
|
if (res.ok) {
|
2022-01-23 21:26:28 +01:00
|
|
|
const data = (await res.json()) as IVersionResponse;
|
2021-03-26 14:32:27 +01:00
|
|
|
this.latest = {
|
|
|
|
oss: data.versions.oss,
|
|
|
|
enterprise: data.versions.enterprise,
|
|
|
|
};
|
|
|
|
this.isLatest = data.latest;
|
|
|
|
} else {
|
|
|
|
this.logger.info(
|
|
|
|
`Could not check newest version. Status: ${res.status}`,
|
|
|
|
);
|
|
|
|
}
|
2021-02-19 11:13:25 +01:00
|
|
|
} catch (err) {
|
|
|
|
this.logger.info('Could not check newest version', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-13 15:54:20 +02:00
|
|
|
async getFeatureUsageInfo(): Promise<IFeatureUsageInfo> {
|
|
|
|
const [
|
|
|
|
featureToggles,
|
|
|
|
users,
|
|
|
|
projects,
|
|
|
|
contextFields,
|
|
|
|
groups,
|
|
|
|
roles,
|
|
|
|
environments,
|
|
|
|
segments,
|
|
|
|
strategies,
|
|
|
|
SAMLenabled,
|
|
|
|
OIDCenabled,
|
|
|
|
featureExports,
|
|
|
|
featureImports,
|
|
|
|
] = await Promise.all([
|
|
|
|
this.featureToggleStore.count({
|
|
|
|
archived: false,
|
|
|
|
}),
|
|
|
|
this.userStore.count(),
|
|
|
|
this.projectStore.count(),
|
|
|
|
this.contextFieldStore.count(),
|
|
|
|
this.groupStore.count(),
|
|
|
|
this.roleStore.count(),
|
|
|
|
this.environmentStore.count(),
|
|
|
|
this.segmentStore.count(),
|
|
|
|
this.strategyStore.count(),
|
|
|
|
this.hasSAML(),
|
|
|
|
this.hasOIDC(),
|
|
|
|
this.eventStore.filteredCount({ type: FEATURES_EXPORTED }),
|
|
|
|
this.eventStore.filteredCount({ type: FEATURES_IMPORTED }),
|
|
|
|
]);
|
|
|
|
const versionInfo = this.getVersionInfo();
|
|
|
|
const customStrategies =
|
|
|
|
await this.strategyStore.getEditableStrategies();
|
|
|
|
const customStrategiesInUse =
|
|
|
|
await this.featureStrategiesStore.getCustomStrategiesInUseCount();
|
|
|
|
const featureInfo = {
|
|
|
|
featureToggles,
|
|
|
|
users,
|
|
|
|
projects,
|
|
|
|
contextFields,
|
|
|
|
groups,
|
|
|
|
roles,
|
|
|
|
environments,
|
|
|
|
segments,
|
|
|
|
strategies,
|
|
|
|
SAMLenabled,
|
|
|
|
OIDCenabled,
|
|
|
|
featureExports,
|
|
|
|
featureImports,
|
|
|
|
customStrategies: customStrategies.length,
|
|
|
|
customStrategiesInUse: customStrategiesInUse,
|
|
|
|
instanceId: versionInfo.instanceId,
|
|
|
|
versionOSS: versionInfo.current.oss,
|
|
|
|
versionEnterprise: versionInfo.current.enterprise,
|
|
|
|
};
|
|
|
|
return featureInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
async hasOIDC(): Promise<boolean> {
|
|
|
|
const settings = await this.settingStore.get(
|
|
|
|
'unleash.enterprise.auth.oidc',
|
|
|
|
);
|
|
|
|
|
|
|
|
return settings?.enabled || false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async hasSAML(): Promise<boolean> {
|
|
|
|
const settings = await this.settingStore.get(
|
|
|
|
'unleash.enterprise.auth.saml',
|
|
|
|
);
|
|
|
|
|
|
|
|
return settings?.enabled || false;
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
getVersionInfo(): IVersionHolder {
|
2021-02-19 11:13:25 +01:00
|
|
|
return {
|
|
|
|
current: this.current,
|
|
|
|
latest: this.latest || {},
|
2021-02-19 11:16:35 +01:00
|
|
|
isLatest: this.isLatest,
|
2021-02-22 14:21:23 +01:00
|
|
|
instanceId: this.instanceId,
|
2021-02-19 11:13:25 +01:00
|
|
|
};
|
|
|
|
}
|
2021-05-28 11:10:24 +02:00
|
|
|
|
|
|
|
destroy(): void {
|
|
|
|
clearInterval(this.timer);
|
|
|
|
this.timer = null;
|
|
|
|
}
|
2021-02-19 11:13:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = VersionService;
|