-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathingestionManager.ts
546 lines (514 loc) · 24.2 KB
/
ingestionManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
import type { convertableToString } from 'xml2js';
import { parseStringPromise as xmlParse } from 'xml2js';
import TurndownService from 'turndown';
import * as R from 'ramda';
import { v4 as uuidv4 } from 'uuid';
import { clearIntervalAsync, setIntervalAsync } from 'set-interval-async/fixed';
import type { SetIntervalAsyncTimer } from 'set-interval-async/fixed';
import type { Moment } from 'moment';
import { lockResource } from '../database/redis';
import conf, { booleanConf, logApp } from '../config/conf';
import { TYPE_LOCK_ERROR, UnknownError, UnsupportedError } from '../config/errors';
import { executionContext, SYSTEM_USER } from '../utils/access';
import { type GetHttpClient, getHttpClient, OpenCTIHeaders } from '../utils/http-client';
import { isEmptyField, isNotEmptyField } from '../database/utils';
import { FROM_START_STR, now, sanitizeForMomentParsing, utcDate } from '../utils/format';
import { generateStandardId } from '../schema/identifier';
import { ENTITY_TYPE_CONTAINER_REPORT } from '../schema/stixDomainObject';
import { pushToWorkerForConnector } from '../database/rabbitmq';
import { OPENCTI_SYSTEM_UUID } from '../schema/general';
import { findAllRssIngestions, patchRssIngestion } from '../modules/ingestion/ingestion-rss-domain';
import type { AuthContext } from '../types/user';
import type { BasicStoreEntityIngestionCsv, BasicStoreEntityIngestionRss, BasicStoreEntityIngestionTaxii } from '../modules/ingestion/ingestion-types';
import { findAllTaxiiIngestions, patchTaxiiIngestion } from '../modules/ingestion/ingestion-taxii-domain';
import { ConnectorType, IngestionAuthType, TaxiiVersion } from '../generated/graphql';
import { fetchCsvFromUrl, findAllCsvIngestions, patchCsvIngestion } from '../modules/ingestion/ingestion-csv-domain';
import type { CsvMapperParsed } from '../modules/internal/csvMapper/csvMapper-types';
import { findById } from '../modules/internal/csvMapper/csvMapper-domain';
import { bundleProcess } from '../parser/csv-bundler';
import { createWork, updateExpectationsNumber } from '../domain/work';
import { parseCsvMapper } from '../modules/internal/csvMapper/csvMapper-utils';
import { findById as findUserById } from '../domain/user';
import { compareHashSHA256, hashSHA256 } from '../utils/hash';
import type { StixBundle, StixObject } from '../types/stix-common';
import { patchAttribute } from '../database/middleware';
import { ENTITY_TYPE_CONNECTOR } from '../schema/internalObject';
import { connectorIdFromIngestId, queueDetails } from '../domain/connector';
// Ingestion manager responsible to cleanup old data
// Each API will start is ingestion manager.
// If the lock is free, every API as the right to take it.
const SCHEDULE_TIME = conf.get('ingestion_manager:interval') || 30000;
const INGESTION_MANAGER_KEY = conf.get('ingestion_manager:lock_key') || 'ingestion_manager_lock';
let running = false;
// region utils
const asArray = (data: unknown) => {
if (data) {
if (Array.isArray(data)) {
return data;
}
return [data];
}
return [];
};
interface UpdateInfo {
state?: any
buffering?: boolean
messages_size?: number
}
const updateBuiltInConnectorInfo = async (context: AuthContext, user_id: string | undefined, id: string, opts: UpdateInfo = {}) => {
// Patch the related connector
const csvNow = utcDate();
const connectorPatch: any = {
updated_at: csvNow.toISOString(),
connector_info: {
last_run_datetime: csvNow.toISOString(),
next_run_datetime: csvNow.add(SCHEDULE_TIME, 'milliseconds').toISOString(),
run_and_terminate: false,
buffering: opts.buffering ?? false,
queue_threshold: 0,
queue_messages_size: (opts.messages_size ?? 0) / 1000000 // In Mb
},
connector_user_id: user_id,
};
if (opts.state) {
connectorPatch.connector_state = JSON.stringify(opts.state);
}
const connectorId = connectorIdFromIngestId(id);
await patchAttribute(context, SYSTEM_USER, connectorId, ENTITY_TYPE_CONNECTOR, connectorPatch);
};
const pushBundleToConnectorQueue = async (context: AuthContext, ingestion: BasicStoreEntityIngestionTaxii
| BasicStoreEntityIngestionRss | BasicStoreEntityIngestionCsv, bundle: StixBundle) => {
// Push the bundle to absorption queue
const connector = { internal_id: connectorIdFromIngestId(ingestion.id), connector_type: ConnectorType.ExternalImport };
const workName = `run @ ${now()}`;
const work: any = await createWork(context, SYSTEM_USER, connector, workName, connector.internal_id, { receivedTime: now() });
const stixBundle = JSON.stringify(bundle);
const content = Buffer.from(stixBundle, 'utf-8').toString('base64');
if (bundle.objects.length === 1) {
// Only add explicit expectation if the worker will not split anything
await updateExpectationsNumber(context, SYSTEM_USER, work.id, bundle.objects.length);
}
await pushToWorkerForConnector(connector.internal_id, {
type: 'bundle',
applicant_id: ingestion.user_id ?? OPENCTI_SYSTEM_UUID,
content,
work_id: work.id,
update: true
});
};
// endregion
// region Rss ingestion
type Getter = (uri: string) => Promise<string>;
interface RssElement {
pubDate: { _: string }
lastBuildDate: { _: string }
updated: { _: string }
}
interface RssItem {
title: { _: string }
summary: { _: string }
description: { _: string }
link: { _: string, href?: string }
content: { _: string }
'content:encoded': { _: string }
category: { _: string } | { _: string }[]
pubDate: { _: string }
'dc:date': { _: string }
lastBuildDate: { _: string }
updated: { _: string }
}
interface DataItem {
title: string
description: string
link: string | undefined
content: string
labels: string[]
pubDate: Moment
}
const rssItemV1Convert = (turndownService: TurndownService, feed: RssElement, entry: RssItem): DataItem => {
const { updated } = feed;
return {
title: turndownService.turndown(entry.title._),
description: turndownService.turndown(entry.summary?._ ?? ''),
link: isNotEmptyField(entry.link) ? (entry.link as { href: string }).href?.trim() : '',
content: turndownService.turndown(entry.content?._ ?? ''),
labels: [], // No label in rss v1
pubDate: utcDate(sanitizeForMomentParsing(entry.updated?._ ?? updated?._ ?? FROM_START_STR)),
};
};
const rssItemV2Convert = (turndownService: TurndownService, channel: RssElement, item: RssItem): DataItem => {
const { pubDate } = channel;
return {
title: turndownService.turndown(item.title._ ?? ''),
description: turndownService.turndown(item.description?._ ?? ''),
link: isNotEmptyField(item.link) ? ((item.link as { _: string })._ ?? '').trim() : '',
content: turndownService.turndown(item['content:encoded']?._ ?? item.content?._ ?? ''),
labels: R.uniq(asArray(item.category).filter((c) => isNotEmptyField(c)).map((c) => (c as { _: string })._.trim())),
pubDate: utcDate(sanitizeForMomentParsing(item.pubDate?._ ?? item['dc:date']?._ ?? pubDate?._ ?? FROM_START_STR)),
};
};
const rssHttpGetter = (): Getter => {
return async (uri: string) => {
const fetchResponse = await fetch(uri);
return fetchResponse.text();
};
};
// RSS Title is mandatory
// A valid date is required, and after the current_state_date
const rssDataFilter = (items: DataItem[], current_state_date: Date | undefined): DataItem[] => {
return items.filter((e) => isNotEmptyField(e.title))
.filter((e) => e.pubDate.isValid())
.filter((e) => isEmptyField(current_state_date) || e.pubDate.isAfter(current_state_date))
.sort((a, b) => a.pubDate.diff(b.pubDate));
};
export const rssDataParser = async (turndownService: TurndownService, data: convertableToString, current_state_date: Date | undefined): Promise<DataItem[]> => {
const xmlData = await xmlParse(data, { explicitArray: false, trim: true, explicitCharkey: true, mergeAttrs: true });
if (xmlData?.feed) { // Atom V1
const entries = asArray(xmlData.feed.entry);
const rssItems = entries.map((entry) => rssItemV1Convert(turndownService, xmlData.feed, entry));
return rssDataFilter(rssItems, current_state_date);
}
if (xmlData?.rss) { // Atom V2
const channels = asArray(xmlData?.rss?.channel);
const rssItems = channels.map((channel) => asArray(channel.item).map((item) => rssItemV2Convert(turndownService, channel, item))).flat();
return rssDataFilter(rssItems, current_state_date);
}
return [];
};
const rssDataHandler = async (context: AuthContext, httpRssGet: Getter, turndownService: TurndownService, ingestion: BasicStoreEntityIngestionRss) => {
const data = await httpRssGet(ingestion.uri);
const items = await rssDataParser(turndownService, data, ingestion.current_state_date);
// Build Stix bundle from items
let lastPubDate;
if (items.length > 0) {
logApp.info(`[OPENCTI-MODULE] Rss ingestion execution for ${items.length} items`);
const reports = items.map((item) => {
const report: any = {
type: 'report',
name: item.title,
labels: item.labels,
description: item.description,
created_by_ref: ingestion.created_by_ref,
object_marking_refs: ingestion.object_marking_refs,
report_types: ingestion.report_types,
published: item.pubDate.toISOString(),
};
report.id = generateStandardId(ENTITY_TYPE_CONTAINER_REPORT, report);
if (item.link) {
report.external_references = [{
source_name: item.title,
description: `${ingestion.name} ${item.title}. Retrieved ${item.pubDate.toISOString()}.`,
url: item.link
}];
}
return report;
});
const bundle: StixBundle = { type: 'bundle', spec_version: '2.1', id: `bundle--${uuidv4()}`, objects: reports };
// Push the bundle to absorption queue
await pushBundleToConnectorQueue(context, ingestion, bundle);
// Update the state
lastPubDate = R.last(items)?.pubDate;
await patchRssIngestion(context, SYSTEM_USER, ingestion.internal_id, { current_state_date: lastPubDate });
// Patch the related connector
const state = { current_state_date: lastPubDate };
await updateBuiltInConnectorInfo(context, ingestion.user_id, ingestion.id, { state });
} else {
await updateBuiltInConnectorInfo(context, ingestion.user_id, ingestion.id);
}
};
const rssExecutor = async (context: AuthContext, turndownService: TurndownService) => {
const httpGet = rssHttpGetter();
const filters = {
mode: 'and',
filters: [{ key: 'ingestion_running', values: [true] }],
filterGroups: [],
};
const opts = { filters, connectionFormat: false, noFiltersChecking: true };
const ingestions = await findAllRssIngestions(context, SYSTEM_USER, opts);
const ingestionPromises = [];
for (let i = 0; i < ingestions.length; i += 1) {
const ingestion = ingestions[i];
// If ingestion have remaining messages in the queue, dont fetch any new data
const { messages_number, messages_size } = await queueDetails(connectorIdFromIngestId(ingestion.id));
if (messages_number === 0) {
const ingestionPromise = rssDataHandler(context, httpGet, turndownService, ingestion)
.catch((e) => {
logApp.error(`[OPENCTI-MODULE] INGESTION - Error with rss handler ${ingestion.name}`);
logApp.error(e, { name: ingestion.name, context: 'RSS ingestion execution' });
});
ingestionPromises.push(ingestionPromise);
} else {
// Update the state
const ingestionPromise = updateBuiltInConnectorInfo(context, ingestion.user_id, ingestion.id, { buffering: true, messages_size });
ingestionPromises.push(ingestionPromise);
}
}
return Promise.all(ingestionPromises);
};
// endregion
// region Taxii ingestion
export interface TaxiiResponseData {
data: { more: boolean | undefined, next: string | undefined, objects: StixObject[] },
addedLastHeader: string | undefined | null
}
/**
* Compute HTTP GET parameters to send to taxii server.
*
* @see https://docs.oasis-open.org/cti/taxii/v2.1/os/taxii-v2.1-os.html#_Toc31107519
* // If the more property is set to true and the next property is populated
* // then the client can paginate through the remaining records
* // using the next URL parameter along with the same original query options.
* // If the more property is set to true and the next property is empty
* // then the client may paginate through the remaining records by using the added_after URL parameter with the
* // date/time value from the X-TAXII-Date-Added-Last header along with the same original query options.
* @param ingestion
*/
export const prepareTaxiiGetParam = (ingestion: BasicStoreEntityIngestionTaxii) => {
const next = ingestion.current_state_cursor;
const added_after = ingestion.added_after_start;
return { next, added_after };
};
const taxiiHttpGet = async (ingestion: BasicStoreEntityIngestionTaxii): Promise<TaxiiResponseData> => {
const octiHeaders = new OpenCTIHeaders();
octiHeaders.Accept = 'application/taxii+json;version=2.1';
if (ingestion.authentication_type === IngestionAuthType.Basic) {
const auth = Buffer.from(ingestion.authentication_value, 'utf-8').toString('base64');
octiHeaders.Authorization = `Basic ${auth}`;
}
if (ingestion.authentication_type === IngestionAuthType.Bearer) {
octiHeaders.Authorization = `Bearer ${ingestion.authentication_value}`;
}
let certificates;
if (ingestion.authentication_type === IngestionAuthType.Certificate) {
certificates = { cert: ingestion.authentication_value.split(':')[0], key: ingestion.authentication_value.split(':')[1], ca: ingestion.authentication_value.split(':')[2] };
}
const httpClientOptions: GetHttpClient = { headers: octiHeaders, rejectUnauthorized: false, responseType: 'json', certificates };
const httpClient = getHttpClient(httpClientOptions);
const preparedUri = ingestion.uri.endsWith('/') ? ingestion.uri : `${ingestion.uri}/`;
const url = `${preparedUri}collections/${ingestion.collection}/objects/`;
const params = prepareTaxiiGetParam(ingestion);
const { data, headers } = await httpClient.get(url, { params });
logApp.info('[OPENCTI-MODULE] Taxii HTTP Get done.', {
ingestion: ingestion.name,
request: {
params,
url,
},
response: {
addedLastHeader: headers['x-taxii-date-added-last'],
addedFirstHeader: headers['x-taxii-date-added-first'],
more: data.more,
next: data.next,
}
});
return { data, addedLastHeader: headers['x-taxii-date-added-last'] };
};
type TaxiiHandlerFn = (context: AuthContext, ingestion: BasicStoreEntityIngestionTaxii) => Promise<void>;
export const processTaxiiResponse = async (context: AuthContext, ingestion: BasicStoreEntityIngestionTaxii, taxiResponse:TaxiiResponseData) => {
const { data, addedLastHeader } = taxiResponse;
if (data.objects && data.objects.length > 0) {
logApp.info(`[OPENCTI-MODULE] Taxii ingestion execution for ${data.objects.length} items, sending stix bundle to workers.`, { ingestionId: ingestion.id });
const bundle: StixBundle = { type: 'bundle', spec_version: '2.1', id: `bundle--${uuidv4()}`, objects: taxiResponse.data.objects };
// Push the bundle to absorption queue
await pushBundleToConnectorQueue(context, ingestion, bundle);
const more = data.more || false;
// Update the state
if (more && isNotEmptyField(data.next)) {
// Do not touch to added_after_start
const state = { current_state_cursor: data.next, last_execution_date: now() };
const ingestionUpdate = await patchTaxiiIngestion(context, SYSTEM_USER, ingestion.internal_id, state);
const connectorState = { current_state_cursor: ingestionUpdate.current_state_cursor, added_after_start: ingestionUpdate.added_after_start };
await updateBuiltInConnectorInfo(context, ingestion.user_id, ingestion.id, { state: connectorState });
} else {
// Reset the pagination cursor, and update date
const state = {
current_state_cursor: undefined,
added_after_start: addedLastHeader ? utcDate(addedLastHeader) : utcDate(),
last_execution_date: now()
};
const ingestionUpdate = await patchTaxiiIngestion(context, SYSTEM_USER, ingestion.internal_id, state);
const connectorState = { current_state_cursor: ingestionUpdate.current_state_cursor, added_after_start: ingestionUpdate.added_after_start };
await updateBuiltInConnectorInfo(context, ingestion.user_id, ingestion.id, { state: connectorState });
}
} else {
const ingestionUpdate = await patchTaxiiIngestion(context, SYSTEM_USER, ingestion.internal_id, { last_execution_date: now(), current_state_cursor: undefined });
const connectorState = { current_state_cursor: ingestionUpdate.current_state_cursor, added_after_start: ingestionUpdate.added_after_start };
await updateBuiltInConnectorInfo(context, ingestion.user_id, ingestion.id, { state: connectorState });
logApp.info('[OPENCTI-MODULE] Taxii ingestion - taxii server has not sent any object.', {
next: data.next,
more: data.more,
addedLastHeader,
ingestionId: ingestion.id,
ingestionName: ingestion.name
});
}
};
const taxiiV21DataHandler: TaxiiHandlerFn = async (context: AuthContext, ingestion: BasicStoreEntityIngestionTaxii) => {
const taxiResponse = await taxiiHttpGet(ingestion);
await processTaxiiResponse(context, ingestion, taxiResponse);
};
const TAXII_HANDLERS: { [k: string]: TaxiiHandlerFn } = {
[TaxiiVersion.V21]: taxiiV21DataHandler
};
const taxiiExecutor = async (context: AuthContext) => {
const filters = {
mode: 'and',
filters: [{ key: 'ingestion_running', values: [true] }],
filterGroups: [],
};
const opts = { filters, connectionFormat: false, noFiltersChecking: true };
const ingestions = await findAllTaxiiIngestions(context, SYSTEM_USER, opts);
const ingestionPromises = [];
for (let i = 0; i < ingestions.length; i += 1) {
const ingestion = ingestions[i];
// If ingestion have remaining messages in the queue, dont fetch any new data
const { messages_number, messages_size } = await queueDetails(connectorIdFromIngestId(ingestion.id));
if (messages_number === 0) {
const taxiiHandler = TAXII_HANDLERS[ingestion.version];
if (!taxiiHandler) {
throw UnsupportedError(`[OPENCTI-MODULE] Taxii version ${ingestion.version} is not yet supported`);
}
const ingestionPromise = taxiiHandler(context, ingestion)
.catch((e) => {
logApp.error(`[OPENCTI-MODULE] INGESTION - Error with taxii handler ${ingestion.name}`);
logApp.error(e, { name: ingestion.name, context: 'Taxii ingestion execution' });
});
ingestionPromises.push(ingestionPromise);
} else {
// Update the state
const ingestionPromise = updateBuiltInConnectorInfo(context, ingestion.user_id, ingestion.id, { buffering: true, messages_size });
ingestionPromises.push(ingestionPromise);
}
}
return Promise.all(ingestionPromises);
};
// endregion
// region Csv ingestion
const csvDataToObjects = async (csvBuffer: Buffer | string, ingestion: BasicStoreEntityIngestionCsv, csvMapper: CsvMapperParsed, context: AuthContext) => {
const ingestionUser = await findUserById(context, context.user ?? SYSTEM_USER, ingestion.user_id) ?? SYSTEM_USER;
const { objects } = await bundleProcess(context, ingestionUser, csvBuffer, csvMapper);
if (objects === undefined) {
logApp.error(`[OPENCTI-MODULE] INGESTION - Undefined bundle objects for csv ingest: ${ingestion.name}`);
const error = UnknownError('Undefined CSV objects', { data: csvBuffer.toString() });
logApp.error(error, { name: ingestion.name, context: 'CSV transform' });
} else {
logApp.info(`[OPENCTI-MODULE] INGESTION - CSV ingestion execution for: ${ingestion.name} with: ${objects.length} items`);
}
return objects;
};
const csvDataHandler = async (context: AuthContext, ingestion: BasicStoreEntityIngestionCsv) => {
const user = context.user ?? SYSTEM_USER;
const csvMapper = await findById(context, user, ingestion.csv_mapper_id);
const csvMapperParsed = parseCsvMapper(csvMapper);
csvMapperParsed.user_chosen_markings = ingestion.markings ?? [];
let data: Buffer | undefined;
let addedLast: string | null | undefined;
try {
const csvResponse = await fetchCsvFromUrl(csvMapperParsed, ingestion);
data = csvResponse.data;
addedLast = csvResponse.addedLast;
} catch (e: any) {
logApp.error(`[OPENCTI-MODULE] INGESTION - Error trying to fetch csv feed for: ${ingestion.name}`);
logApp.error(e, { ingestion });
throw e;
}
const hashedIncomingData = hashSHA256(data.toString());
const isUnchangedData = compareHashSHA256(data.toString(), ingestion.current_state_hash ?? '');
if (isUnchangedData) {
logApp.info(`[OPENCTI-MODULE] INGESTION - Unchanged data for csv ingest: ${ingestion.name}`);
await updateBuiltInConnectorInfo(context, ingestion.user_id, ingestion.id);
} else {
const objects = await csvDataToObjects(data, ingestion, csvMapperParsed, context);
const bundle: StixBundle = { type: 'bundle', spec_version: '2.1', id: `bundle--${uuidv4()}`, objects };
// Push the bundle to absorption queue
await pushBundleToConnectorQueue(context, ingestion, bundle);
// Update the state
const state = { current_state_hash: hashedIncomingData, added_after_start: utcDate(addedLast) };
await patchCsvIngestion(context, SYSTEM_USER, ingestion.internal_id, state);
await updateBuiltInConnectorInfo(context, ingestion.user_id, ingestion.id, { state });
}
};
const csvExecutor = async (context: AuthContext) => {
const filters = {
mode: 'and',
filters: [{ key: 'ingestion_running', values: [true] }],
filterGroups: [],
};
const opts = { filters, connectionFormat: false, noFiltersChecking: true };
const ingestions = await findAllCsvIngestions(context, SYSTEM_USER, opts);
const ingestionPromises = [];
for (let i = 0; i < ingestions.length; i += 1) {
const ingestion = ingestions[i];
// If ingestion have remaining messages in the queue, dont fetch any new data
const { messages_number, messages_size } = await queueDetails(connectorIdFromIngestId(ingestion.id));
if (messages_number === 0) {
const ingestionPromise = csvDataHandler(context, ingestion)
.catch((e) => {
logApp.error(`[OPENCTI-MODULE] INGESTION - Error with csv handler ${ingestion.name}`);
logApp.error(e, { name: ingestion.name, context: 'CSV ingestion execution' });
});
ingestionPromises.push(ingestionPromise);
} else {
// Update the state
const ingestionPromise = updateBuiltInConnectorInfo(context, ingestion.user_id, ingestion.id, { buffering: true, messages_size });
ingestionPromises.push(ingestionPromise);
}
}
return Promise.all(ingestionPromises);
};
// endregion
const ingestionHandler = async () => {
logApp.debug('[OPENCTI-MODULE] INGESTION - Running ingestion handlers');
let lock;
try {
// Lock the manager
const turndownService = new TurndownService();
lock = await lockResource([INGESTION_MANAGER_KEY], { retryCount: 0 });
running = true;
// noinspection JSUnusedLocalSymbols
const context = executionContext('ingestion_manager');
const ingestionPromises = [];
ingestionPromises.push(rssExecutor(context, turndownService));
ingestionPromises.push(taxiiExecutor(context));
ingestionPromises.push(csvExecutor(context));
await Promise.all(ingestionPromises);
} catch (e: any) {
// We dont care about failing to get the lock.
if (e.name === TYPE_LOCK_ERROR) {
logApp.info('[OPENCTI-MODULE] INGESTION - Ingestion manager already in progress by another API');
} else {
logApp.error('[OPENCTI-MODULE] INGESTION - Ingestion handlers cannot be started');
logApp.error(e, { manager: 'INGESTION_MANAGER' });
}
} finally {
running = false;
if (lock) await lock.unlock();
}
};
const initIngestionManager = () => {
let scheduler: SetIntervalAsyncTimer<[]>;
return {
start: async () => {
logApp.info('[OPENCTI-MODULE] INGESTION - Starting ingestion manager');
scheduler = setIntervalAsync(async () => {
await ingestionHandler();
}, SCHEDULE_TIME);
},
status: () => {
return {
id: 'INGESTION_MANAGER',
enable: booleanConf('ingestion_manager:enabled', false),
running,
};
},
shutdown: async () => {
logApp.info('[OPENCTI-MODULE] INGESTION - Stopping ingestion manager');
if (scheduler) {
return clearIntervalAsync(scheduler);
}
return true;
},
};
};
const ingestionManager = initIngestionManager();
export default ingestionManager;