2021-08-12 15:04:37 +02:00
|
|
|
import memoizee from 'memoizee';
|
|
|
|
import { ValidationError } from 'joi';
|
|
|
|
import { getAddons, IAddonProviders } from '../addons';
|
|
|
|
import * as events from '../types/events';
|
|
|
|
import { addonSchema } from './addon-schema';
|
|
|
|
import NameExistsError from '../error/name-exists-error';
|
|
|
|
import { IEventStore } from '../types/stores/event-store';
|
|
|
|
import { IFeatureToggleStore } from '../types/stores/feature-toggle-store';
|
|
|
|
import { Logger } from '../logger';
|
|
|
|
import TagTypeService from './tag-type-service';
|
|
|
|
import { IAddon, IAddonDto, IAddonStore } from '../types/stores/addon-store';
|
2022-06-22 12:49:18 +02:00
|
|
|
import { IUnleashStores, IUnleashConfig } from '../types';
|
2021-08-12 15:04:37 +02:00
|
|
|
import { IAddonDefinition } from '../types/model';
|
2021-11-02 15:13:46 +01:00
|
|
|
import { minutesToMilliseconds } from 'date-fns';
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
const SUPPORTED_EVENTS = Object.keys(events).map((k) => events[k]);
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-02-04 11:02:58 +01:00
|
|
|
const MASKED_VALUE = '*****';
|
|
|
|
|
2022-07-12 15:50:22 +02:00
|
|
|
const WILDCARD_OPTION = '*';
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
interface ISensitiveParams {
|
|
|
|
[key: string]: string[];
|
|
|
|
}
|
|
|
|
export default class AddonService {
|
|
|
|
eventStore: IEventStore;
|
|
|
|
|
|
|
|
addonStore: IAddonStore;
|
|
|
|
|
|
|
|
featureToggleStore: IFeatureToggleStore;
|
|
|
|
|
|
|
|
logger: Logger;
|
|
|
|
|
|
|
|
tagTypeService: TagTypeService;
|
|
|
|
|
|
|
|
addonProviders: IAddonProviders;
|
|
|
|
|
|
|
|
sensitiveParams: ISensitiveParams;
|
|
|
|
|
|
|
|
fetchAddonConfigs: (() => Promise<IAddon[]>) &
|
|
|
|
memoizee.Memoized<() => Promise<IAddon[]>>;
|
|
|
|
|
2021-01-19 10:42:45 +01:00
|
|
|
constructor(
|
2021-08-12 15:04:37 +02:00
|
|
|
{
|
|
|
|
addonStore,
|
|
|
|
eventStore,
|
|
|
|
featureToggleStore,
|
|
|
|
}: Pick<
|
|
|
|
IUnleashStores,
|
|
|
|
'addonStore' | 'eventStore' | 'featureToggleStore'
|
|
|
|
>,
|
|
|
|
{ getLogger, server }: Pick<IUnleashConfig, 'getLogger' | 'server'>,
|
|
|
|
tagTypeService: TagTypeService,
|
|
|
|
addons?: IAddonProviders,
|
2021-01-19 10:42:45 +01:00
|
|
|
) {
|
|
|
|
this.eventStore = eventStore;
|
|
|
|
this.addonStore = addonStore;
|
|
|
|
this.featureToggleStore = featureToggleStore;
|
2021-08-12 15:04:37 +02:00
|
|
|
this.logger = getLogger('services/addon-service.js');
|
2021-01-19 10:42:45 +01:00
|
|
|
this.tagTypeService = tagTypeService;
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
this.addonProviders =
|
|
|
|
addons ||
|
|
|
|
getAddons({
|
|
|
|
getLogger,
|
|
|
|
unleashUrl: server.unleashUrl,
|
|
|
|
});
|
2021-02-04 11:02:58 +01:00
|
|
|
this.sensitiveParams = this.loadSensitiveParams(this.addonProviders);
|
|
|
|
if (addonStore) {
|
|
|
|
this.registerEventHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Memoized private function
|
2021-09-02 10:32:44 +02:00
|
|
|
this.fetchAddonConfigs = memoizee(
|
|
|
|
async () => addonStore.getAll({ enabled: true }),
|
|
|
|
{
|
|
|
|
promise: true,
|
2021-11-02 15:13:46 +01:00
|
|
|
maxAge: minutesToMilliseconds(1),
|
2021-09-02 10:32:44 +02:00
|
|
|
},
|
|
|
|
);
|
2021-02-04 11:02:58 +01:00
|
|
|
}
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
loadSensitiveParams(addonProviders: IAddonProviders): ISensitiveParams {
|
2021-02-04 11:02:58 +01:00
|
|
|
const providerDefinitions = Object.values(addonProviders).map(
|
2021-08-12 15:04:37 +02:00
|
|
|
(p) => p.definition,
|
2021-01-19 10:42:45 +01:00
|
|
|
);
|
2021-02-04 11:02:58 +01:00
|
|
|
return providerDefinitions.reduce((obj, definition) => {
|
|
|
|
const sensitiveParams = definition.parameters
|
2021-08-12 15:04:37 +02:00
|
|
|
.filter((p) => p.sensitive)
|
|
|
|
.map((p) => p.name);
|
2021-02-04 11:02:58 +01:00
|
|
|
|
|
|
|
const o = { ...obj };
|
|
|
|
o[definition.name] = sensitiveParams;
|
|
|
|
return o;
|
|
|
|
}, {});
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
registerEventHandler(): void {
|
|
|
|
SUPPORTED_EVENTS.forEach((eventName) =>
|
2021-01-19 10:42:45 +01:00
|
|
|
this.eventStore.on(eventName, this.handleEvent(eventName)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
handleEvent(eventName: string): (IEvent) => void {
|
2021-01-19 10:42:45 +01:00
|
|
|
const { addonProviders } = this;
|
2021-08-12 15:04:37 +02:00
|
|
|
return (event) => {
|
|
|
|
this.fetchAddonConfigs().then((addonInstances) => {
|
2021-01-19 10:42:45 +01:00
|
|
|
addonInstances
|
2021-08-12 15:04:37 +02:00
|
|
|
.filter((addon) => addon.events.includes(eventName))
|
2022-07-12 14:13:25 +02:00
|
|
|
.filter(
|
|
|
|
(addon) =>
|
|
|
|
!addon.projects ||
|
|
|
|
addon.projects.length == 0 ||
|
2022-07-12 15:50:22 +02:00
|
|
|
addon.projects[0] === WILDCARD_OPTION ||
|
2022-07-12 14:13:25 +02:00
|
|
|
addon.projects.includes(event.project),
|
|
|
|
)
|
|
|
|
.filter(
|
|
|
|
(addon) =>
|
|
|
|
!addon.environments ||
|
|
|
|
addon.environments.length == 0 ||
|
2022-07-12 15:50:22 +02:00
|
|
|
addon.environments[0] === WILDCARD_OPTION ||
|
2022-07-12 14:13:25 +02:00
|
|
|
addon.environments.includes(event.environment),
|
|
|
|
)
|
2021-08-12 15:04:37 +02:00
|
|
|
.filter((addon) => addonProviders[addon.provider])
|
|
|
|
.forEach((addon) =>
|
2021-01-19 10:42:45 +01:00
|
|
|
addonProviders[addon.provider].handleEvent(
|
|
|
|
event,
|
|
|
|
addon.parameters,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-04 11:02:58 +01:00
|
|
|
// Should be used by the controller.
|
2021-08-12 15:04:37 +02:00
|
|
|
async getAddons(): Promise<IAddon[]> {
|
2021-02-04 11:02:58 +01:00
|
|
|
const addonConfigs = await this.addonStore.getAll();
|
2021-08-12 15:04:37 +02:00
|
|
|
return addonConfigs.map((a) => this.filterSensitiveFields(a));
|
2021-02-04 11:02:58 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
filterSensitiveFields(addonConfig: IAddon): IAddon {
|
2021-02-04 11:02:58 +01:00
|
|
|
const { sensitiveParams } = this;
|
|
|
|
const a = { ...addonConfig };
|
|
|
|
a.parameters = Object.keys(a.parameters).reduce((obj, paramKey) => {
|
|
|
|
const o = { ...obj };
|
|
|
|
if (sensitiveParams[a.provider].includes(paramKey)) {
|
|
|
|
o[paramKey] = MASKED_VALUE;
|
|
|
|
} else {
|
|
|
|
o[paramKey] = a.parameters[paramKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}, {});
|
|
|
|
return a;
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async getAddon(id: number): Promise<IAddon> {
|
2021-02-04 11:02:58 +01:00
|
|
|
const addonConfig = await this.addonStore.get(id);
|
|
|
|
return this.filterSensitiveFields(addonConfig);
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
getProviderDefinitions(): IAddonDefinition[] {
|
2021-01-19 10:42:45 +01:00
|
|
|
const { addonProviders } = this;
|
2021-08-12 15:04:37 +02:00
|
|
|
return Object.values(addonProviders).map((p) => p.definition);
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async addTagTypes(providerName: string): Promise<void> {
|
2021-01-19 10:42:45 +01:00
|
|
|
const provider = this.addonProviders[providerName];
|
|
|
|
if (provider) {
|
|
|
|
const tagTypes = provider.definition.tagTypes || [];
|
2021-08-12 15:04:37 +02:00
|
|
|
const createTags = tagTypes.map(async (tagType) => {
|
2021-01-19 10:42:45 +01:00
|
|
|
try {
|
|
|
|
await this.tagTypeService.validateUnique(tagType);
|
2021-08-12 15:04:37 +02:00
|
|
|
await this.tagTypeService.createTagType(
|
|
|
|
tagType,
|
|
|
|
providerName,
|
|
|
|
);
|
2021-01-19 10:42:45 +01:00
|
|
|
} catch (err) {
|
|
|
|
if (!(err instanceof NameExistsError)) {
|
|
|
|
this.logger.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-08-12 15:04:37 +02:00
|
|
|
await Promise.all(createTags);
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async createAddon(data: IAddonDto, userName: string): Promise<IAddon> {
|
2021-01-19 10:42:45 +01:00
|
|
|
const addonConfig = await addonSchema.validateAsync(data);
|
|
|
|
await this.validateKnownProvider(addonConfig);
|
2021-02-05 14:07:46 +01:00
|
|
|
await this.validateRequiredParameters(addonConfig);
|
2021-01-19 10:42:45 +01:00
|
|
|
|
|
|
|
const createdAddon = await this.addonStore.insert(addonConfig);
|
|
|
|
await this.addTagTypes(createdAddon.provider);
|
|
|
|
|
|
|
|
this.logger.info(
|
|
|
|
`User ${userName} created addon ${addonConfig.provider}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
await this.eventStore.store({
|
|
|
|
type: events.ADDON_CONFIG_CREATED,
|
|
|
|
createdBy: userName,
|
|
|
|
data: { provider: addonConfig.provider },
|
|
|
|
});
|
|
|
|
|
|
|
|
return createdAddon;
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async updateAddon(
|
|
|
|
id: number,
|
|
|
|
data: IAddonDto,
|
|
|
|
userName: string,
|
2022-06-22 12:49:18 +02:00
|
|
|
): Promise<IAddon> {
|
2021-01-19 10:42:45 +01:00
|
|
|
const addonConfig = await addonSchema.validateAsync(data);
|
2021-02-05 14:07:46 +01:00
|
|
|
await this.validateRequiredParameters(addonConfig);
|
2021-02-04 11:02:58 +01:00
|
|
|
if (this.sensitiveParams[addonConfig.provider].length > 0) {
|
|
|
|
const existingConfig = await this.addonStore.get(id);
|
|
|
|
addonConfig.parameters = Object.keys(addonConfig.parameters).reduce(
|
|
|
|
(params, key) => {
|
|
|
|
const o = { ...params };
|
|
|
|
if (addonConfig.parameters[key] === MASKED_VALUE) {
|
|
|
|
o[key] = existingConfig.parameters[key];
|
|
|
|
} else {
|
|
|
|
o[key] = addonConfig.parameters[key];
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
}
|
2022-06-22 12:49:18 +02:00
|
|
|
const result = await this.addonStore.update(id, addonConfig);
|
2021-01-19 10:42:45 +01:00
|
|
|
await this.eventStore.store({
|
|
|
|
type: events.ADDON_CONFIG_UPDATED,
|
|
|
|
createdBy: userName,
|
|
|
|
data: { id, provider: addonConfig.provider },
|
|
|
|
});
|
|
|
|
this.logger.info(`User ${userName} updated addon ${id}`);
|
2022-06-22 12:49:18 +02:00
|
|
|
return result;
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async removeAddon(id: number, userName: string): Promise<void> {
|
2021-01-19 10:42:45 +01:00
|
|
|
await this.addonStore.delete(id);
|
|
|
|
await this.eventStore.store({
|
|
|
|
type: events.ADDON_CONFIG_DELETED,
|
|
|
|
createdBy: userName,
|
|
|
|
data: { id },
|
|
|
|
});
|
|
|
|
this.logger.info(`User ${userName} removed addon ${id}`);
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async validateKnownProvider(config: Partial<IAddonDto>): Promise<boolean> {
|
2021-01-19 10:42:45 +01:00
|
|
|
const p = this.addonProviders[config.provider];
|
|
|
|
if (!p) {
|
|
|
|
throw new TypeError(`Unknown addon provider ${config.provider}`);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 14:07:46 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async validateRequiredParameters({
|
|
|
|
provider,
|
|
|
|
parameters,
|
|
|
|
}): Promise<boolean> {
|
2021-02-05 14:07:46 +01:00
|
|
|
const providerDefinition = this.addonProviders[provider].definition;
|
|
|
|
|
|
|
|
const requiredParamsMissing = providerDefinition.parameters
|
2021-08-12 15:04:37 +02:00
|
|
|
.filter((p) => p.required)
|
|
|
|
.map((p) => p.name)
|
2021-02-05 14:07:46 +01:00
|
|
|
.filter(
|
2021-08-12 15:04:37 +02:00
|
|
|
(requiredParam) =>
|
2021-02-05 14:07:46 +01:00
|
|
|
!Object.keys(parameters).includes(requiredParam),
|
|
|
|
);
|
|
|
|
if (requiredParamsMissing.length > 0) {
|
|
|
|
throw new ValidationError(
|
|
|
|
`Missing required parameters: ${requiredParamsMissing.join(
|
|
|
|
',',
|
|
|
|
)} `,
|
|
|
|
'',
|
2021-08-12 15:04:37 +02:00
|
|
|
undefined,
|
2021-02-05 14:07:46 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|