-
Notifications
You must be signed in to change notification settings - Fork 8k
/
migrations.ts
677 lines (635 loc) · 22.2 KB
/
migrations.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { isString } from 'lodash/fp';
import {
LogMeta,
SavedObjectMigrationMap,
SavedObjectUnsanitizedDoc,
SavedObjectMigrationFn,
SavedObjectMigrationContext,
SavedObjectAttributes,
SavedObjectAttribute,
SavedObjectReference,
} from '../../../../../src/core/server';
import { RawAlert, RawAlertAction } from '../types';
import { EncryptedSavedObjectsPluginSetup } from '../../../encrypted_saved_objects/server';
import type { IsMigrationNeededPredicate } from '../../../encrypted_saved_objects/server';
const SIEM_APP_ID = 'securitySolution';
const SIEM_SERVER_APP_ID = 'siem';
export const LEGACY_LAST_MODIFIED_VERSION = 'pre-7.10.0';
interface AlertLogMeta extends LogMeta {
migrations: { alertDocument: SavedObjectUnsanitizedDoc<RawAlert> };
}
type AlertMigration = (
doc: SavedObjectUnsanitizedDoc<RawAlert>
) => SavedObjectUnsanitizedDoc<RawAlert>;
function createEsoMigration(
encryptedSavedObjects: EncryptedSavedObjectsPluginSetup,
isMigrationNeededPredicate: IsMigrationNeededPredicate<RawAlert, RawAlert>,
migrationFunc: AlertMigration
) {
return encryptedSavedObjects.createMigration<RawAlert, RawAlert>({
isMigrationNeededPredicate,
migration: migrationFunc,
shouldMigrateIfDecryptionFails: true, // shouldMigrateIfDecryptionFails flag that applies the migration to undecrypted document if decryption fails
});
}
const SUPPORT_INCIDENTS_ACTION_TYPES = ['.servicenow', '.jira', '.resilient'];
export const isAnyActionSupportIncidents = (doc: SavedObjectUnsanitizedDoc<RawAlert>): boolean =>
doc.attributes.actions.some((action) =>
SUPPORT_INCIDENTS_ACTION_TYPES.includes(action.actionTypeId)
);
export const isSecuritySolutionRule = (doc: SavedObjectUnsanitizedDoc<RawAlert>): boolean =>
doc.attributes.alertTypeId === 'siem.signals';
export function getMigrations(
encryptedSavedObjects: EncryptedSavedObjectsPluginSetup,
isPreconfigured: (connectorId: string) => boolean
): SavedObjectMigrationMap {
const migrationWhenRBACWasIntroduced = createEsoMigration(
encryptedSavedObjects,
// migrate all documents in 7.10 in order to add the "meta" RBAC field
(doc): doc is SavedObjectUnsanitizedDoc<RawAlert> => true,
pipeMigrations(
markAsLegacyAndChangeConsumer,
setAlertIdAsDefaultDedupkeyOnPagerDutyActions,
initializeExecutionStatus
)
);
const migrationAlertUpdatedAtAndNotifyWhen = createEsoMigration(
encryptedSavedObjects,
// migrate all documents in 7.11 in order to add the "updatedAt" and "notifyWhen" fields
(doc): doc is SavedObjectUnsanitizedDoc<RawAlert> => true,
pipeMigrations(setAlertUpdatedAtDate, setNotifyWhen)
);
const migrationActions7112 = createEsoMigration(
encryptedSavedObjects,
(doc): doc is SavedObjectUnsanitizedDoc<RawAlert> => isAnyActionSupportIncidents(doc),
pipeMigrations(restructureConnectorsThatSupportIncident)
);
const migrationSecurityRules713 = createEsoMigration(
encryptedSavedObjects,
(doc): doc is SavedObjectUnsanitizedDoc<RawAlert> => isSecuritySolutionRule(doc),
pipeMigrations(removeNullsFromSecurityRules)
);
const migrationSecurityRules714 = createEsoMigration(
encryptedSavedObjects,
(doc): doc is SavedObjectUnsanitizedDoc<RawAlert> => isSecuritySolutionRule(doc),
pipeMigrations(removeNullAuthorFromSecurityRules)
);
const migrationSecurityRules715 = createEsoMigration(
encryptedSavedObjects,
(doc): doc is SavedObjectUnsanitizedDoc<RawAlert> => isSecuritySolutionRule(doc),
pipeMigrations(addExceptionListsToReferences)
);
const migrateRules716 = createEsoMigration(
encryptedSavedObjects,
(doc): doc is SavedObjectUnsanitizedDoc<RawAlert> => true,
pipeMigrations(setLegacyId, getRemovePreconfiguredConnectorsFromReferencesFn(isPreconfigured))
);
const migrationRules800 = createEsoMigration(
encryptedSavedObjects,
(doc: SavedObjectUnsanitizedDoc<RawAlert>): doc is SavedObjectUnsanitizedDoc<RawAlert> => true,
(doc) => doc // no-op
);
return {
'7.10.0': executeMigrationWithErrorHandling(migrationWhenRBACWasIntroduced, '7.10.0'),
'7.11.0': executeMigrationWithErrorHandling(migrationAlertUpdatedAtAndNotifyWhen, '7.11.0'),
'7.11.2': executeMigrationWithErrorHandling(migrationActions7112, '7.11.2'),
'7.13.0': executeMigrationWithErrorHandling(migrationSecurityRules713, '7.13.0'),
'7.14.1': executeMigrationWithErrorHandling(migrationSecurityRules714, '7.14.1'),
'7.15.0': executeMigrationWithErrorHandling(migrationSecurityRules715, '7.15.0'),
'7.16.0': executeMigrationWithErrorHandling(migrateRules716, '7.16.0'),
'8.0.0': executeMigrationWithErrorHandling(migrationRules800, '8.0.0'),
};
}
function executeMigrationWithErrorHandling(
migrationFunc: SavedObjectMigrationFn<RawAlert, RawAlert>,
version: string
) {
return (doc: SavedObjectUnsanitizedDoc<RawAlert>, context: SavedObjectMigrationContext) => {
try {
return migrationFunc(doc, context);
} catch (ex) {
context.log.error<AlertLogMeta>(
`encryptedSavedObject ${version} migration failed for alert ${doc.id} with error: ${ex.message}`,
{
migrations: {
alertDocument: doc,
},
}
);
throw ex;
}
};
}
const setAlertUpdatedAtDate = (
doc: SavedObjectUnsanitizedDoc<RawAlert>
): SavedObjectUnsanitizedDoc<RawAlert> => {
const updatedAt = doc.updated_at || doc.attributes.createdAt;
return {
...doc,
attributes: {
...doc.attributes,
updatedAt,
},
};
};
const setNotifyWhen = (
doc: SavedObjectUnsanitizedDoc<RawAlert>
): SavedObjectUnsanitizedDoc<RawAlert> => {
const notifyWhen = doc.attributes.throttle ? 'onThrottleInterval' : 'onActiveAlert';
return {
...doc,
attributes: {
...doc.attributes,
notifyWhen,
},
};
};
const consumersToChange: Map<string, string> = new Map(
Object.entries({
alerting: 'alerts',
metrics: 'infrastructure',
[SIEM_APP_ID]: SIEM_SERVER_APP_ID,
})
);
function markAsLegacyAndChangeConsumer(
doc: SavedObjectUnsanitizedDoc<RawAlert>
): SavedObjectUnsanitizedDoc<RawAlert> {
const {
attributes: { consumer },
} = doc;
return {
...doc,
attributes: {
...doc.attributes,
consumer: consumersToChange.get(consumer) ?? consumer,
// mark any alert predating 7.10 as a legacy alert
meta: {
versionApiKeyLastmodified: LEGACY_LAST_MODIFIED_VERSION,
},
},
};
}
function setAlertIdAsDefaultDedupkeyOnPagerDutyActions(
doc: SavedObjectUnsanitizedDoc<RawAlert>
): SavedObjectUnsanitizedDoc<RawAlert> {
const { attributes } = doc;
return {
...doc,
attributes: {
...attributes,
...(attributes.actions
? {
actions: attributes.actions.map((action) => {
if (action.actionTypeId !== '.pagerduty' || action.params.eventAction === 'trigger') {
return action;
}
return {
...action,
params: {
...action.params,
dedupKey: action.params.dedupKey ?? '{{alertId}}',
},
};
}),
}
: {}),
},
};
}
function initializeExecutionStatus(
doc: SavedObjectUnsanitizedDoc<RawAlert>
): SavedObjectUnsanitizedDoc<RawAlert> {
const { attributes } = doc;
return {
...doc,
attributes: {
...attributes,
executionStatus: {
status: 'pending',
lastExecutionDate: new Date().toISOString(),
error: null,
},
},
};
}
function isEmptyObject(obj: {}) {
for (const attr in obj) {
if (Object.prototype.hasOwnProperty.call(obj, attr)) {
return false;
}
}
return true;
}
function restructureConnectorsThatSupportIncident(
doc: SavedObjectUnsanitizedDoc<RawAlert>
): SavedObjectUnsanitizedDoc<RawAlert> {
const { actions } = doc.attributes;
const newActions = actions.reduce((acc, action) => {
if (
['.servicenow', '.jira', '.resilient'].includes(action.actionTypeId) &&
action.params.subAction === 'pushToService'
) {
// Future developer, we needed to do that because when we created this migration
// we forget to think about user already using 7.11.0 and having an incident attribute build the right way
// IMPORTANT -> if you change this code please do the same inside of this file
// x-pack/plugins/alerting/server/saved_objects/migrations.ts
const subActionParamsIncident =
(action.params?.subActionParams as SavedObjectAttributes)?.incident ?? null;
if (subActionParamsIncident != null && !isEmptyObject(subActionParamsIncident)) {
return [...acc, action];
}
if (action.actionTypeId === '.servicenow') {
const {
title,
comments,
comment,
description,
severity,
urgency,
impact,
short_description: shortDescription,
} = action.params.subActionParams as {
title: string;
description?: string;
severity?: string;
urgency?: string;
impact?: string;
comment?: string;
comments?: Array<{ commentId: string; comment: string }>;
short_description?: string;
};
return [
...acc,
{
...action,
params: {
subAction: 'pushToService',
subActionParams: {
incident: {
short_description: shortDescription ?? title,
description,
severity,
urgency,
impact,
},
comments: [
...(comments ?? []),
...(comment != null ? [{ commentId: '1', comment }] : []),
],
},
},
},
] as RawAlertAction[];
} else if (action.actionTypeId === '.jira') {
const {
title,
comments,
description,
issueType,
priority,
labels,
parent,
summary,
} = action.params.subActionParams as {
title: string;
description: string;
issueType: string;
priority?: string;
labels?: string[];
parent?: string;
comments?: unknown[];
summary?: string;
};
return [
...acc,
{
...action,
params: {
subAction: 'pushToService',
subActionParams: {
incident: {
summary: summary ?? title,
description,
issueType,
priority,
labels,
parent,
},
comments,
},
},
},
] as RawAlertAction[];
} else if (action.actionTypeId === '.resilient') {
const { title, comments, description, incidentTypes, severityCode, name } = action.params
.subActionParams as {
title: string;
description: string;
incidentTypes?: number[];
severityCode?: number;
comments?: unknown[];
name?: string;
};
return [
...acc,
{
...action,
params: {
subAction: 'pushToService',
subActionParams: {
incident: {
name: name ?? title,
description,
incidentTypes,
severityCode,
},
comments,
},
},
},
] as RawAlertAction[];
}
}
return [...acc, action];
}, [] as RawAlertAction[]);
return {
...doc,
attributes: {
...doc.attributes,
actions: newActions,
},
};
}
function convertNullToUndefined(attribute: SavedObjectAttribute) {
return attribute != null ? attribute : undefined;
}
function removeNullsFromSecurityRules(
doc: SavedObjectUnsanitizedDoc<RawAlert>
): SavedObjectUnsanitizedDoc<RawAlert> {
const {
attributes: { params },
} = doc;
return {
...doc,
attributes: {
...doc.attributes,
params: {
...params,
buildingBlockType: convertNullToUndefined(params.buildingBlockType),
note: convertNullToUndefined(params.note),
index: convertNullToUndefined(params.index),
language: convertNullToUndefined(params.language),
license: convertNullToUndefined(params.license),
outputIndex: convertNullToUndefined(params.outputIndex),
savedId: convertNullToUndefined(params.savedId),
timelineId: convertNullToUndefined(params.timelineId),
timelineTitle: convertNullToUndefined(params.timelineTitle),
meta: convertNullToUndefined(params.meta),
query: convertNullToUndefined(params.query),
filters: convertNullToUndefined(params.filters),
riskScoreMapping: params.riskScoreMapping != null ? params.riskScoreMapping : [],
ruleNameOverride: convertNullToUndefined(params.ruleNameOverride),
severityMapping: params.severityMapping != null ? params.severityMapping : [],
threat: params.threat != null ? params.threat : [],
threshold:
params.threshold != null &&
typeof params.threshold === 'object' &&
!Array.isArray(params.threshold)
? {
field: Array.isArray(params.threshold.field)
? params.threshold.field
: params.threshold.field === '' || params.threshold.field == null
? []
: [params.threshold.field],
value: params.threshold.value,
cardinality:
params.threshold.cardinality != null ? params.threshold.cardinality : [],
}
: undefined,
timestampOverride: convertNullToUndefined(params.timestampOverride),
exceptionsList:
params.exceptionsList != null
? params.exceptionsList
: params.exceptions_list != null
? params.exceptions_list
: params.lists != null
? params.lists
: [],
threatFilters: convertNullToUndefined(params.threatFilters),
machineLearningJobId:
params.machineLearningJobId == null
? undefined
: Array.isArray(params.machineLearningJobId)
? params.machineLearningJobId
: [params.machineLearningJobId],
},
},
};
}
/**
* The author field was introduced later and was not part of the original rules. We overlooked
* the filling in the author field as an empty array in an earlier upgrade routine from
* 'removeNullsFromSecurityRules' during the 7.13.0 upgrade. Since we don't change earlier migrations,
* but rather only move forward with the "arrow of time" we are going to upgrade and fix
* it if it is missing for anyone in 7.14.0 and above release. Earlier releases if we want to fix them,
* would have to be modified as a "7.13.1", etc... if we want to fix it there.
* @param doc The document that is not migrated and contains a "null" or "undefined" author field
* @returns The document with the author field fleshed in.
*/
function removeNullAuthorFromSecurityRules(
doc: SavedObjectUnsanitizedDoc<RawAlert>
): SavedObjectUnsanitizedDoc<RawAlert> {
const {
attributes: { params },
} = doc;
return {
...doc,
attributes: {
...doc.attributes,
params: {
...params,
author: params.author != null ? params.author : [],
},
},
};
}
/**
* This migrates exception list containers to saved object references on an upgrade.
* We only migrate if we find these conditions:
* - exceptionLists are an array and not null, undefined, or malformed data.
* - The exceptionList item is an object and id is a string and not null, undefined, or malformed data
* - The existing references do not already have an exceptionItem reference already found within it.
* Some of these issues could crop up during either user manual errors of modifying things, earlier migration
* issues, etc...
* @param doc The document that might have exceptionListItems to migrate
* @returns The document migrated with saved object references
*/
function addExceptionListsToReferences(
doc: SavedObjectUnsanitizedDoc<RawAlert>
): SavedObjectUnsanitizedDoc<RawAlert> {
const {
attributes: {
params: { exceptionsList },
},
references,
} = doc;
if (!Array.isArray(exceptionsList)) {
// early return if we are not an array such as being undefined or null or malformed.
return doc;
} else {
const exceptionsToTransform = removeMalformedExceptionsList(exceptionsList);
const newReferences = exceptionsToTransform.flatMap<SavedObjectReference>(
(exceptionItem, index) => {
const existingReferenceFound = references?.find((reference) => {
return (
reference.id === exceptionItem.id &&
((reference.type === 'exception-list' && exceptionItem.namespace_type === 'single') ||
(reference.type === 'exception-list-agnostic' &&
exceptionItem.namespace_type === 'agnostic'))
);
});
if (existingReferenceFound) {
// skip if the reference already exists for some uncommon reason so we do not add an additional one.
// This enables us to be idempotent and you can run this migration multiple times and get the same output.
return [];
} else {
return [
{
name: `param:exceptionsList_${index}`,
id: String(exceptionItem.id),
type:
exceptionItem.namespace_type === 'agnostic'
? 'exception-list-agnostic'
: 'exception-list',
},
];
}
}
);
if (references == null && newReferences.length === 0) {
// Avoid adding an empty references array if the existing saved object never had one to begin with
return doc;
} else {
return { ...doc, references: [...(references ?? []), ...newReferences] };
}
}
}
/**
* This will do a flatMap reduce where we only return exceptionsLists and their items if:
* - exceptionLists are an array and not null, undefined, or malformed data.
* - The exceptionList item is an object and id is a string and not null, undefined, or malformed data
*
* Some of these issues could crop up during either user manual errors of modifying things, earlier migration
* issues, etc...
* @param exceptionsList The list of exceptions
* @returns The exception lists if they are a valid enough shape
*/
function removeMalformedExceptionsList(
exceptionsList: SavedObjectAttribute
): SavedObjectAttributes[] {
if (!Array.isArray(exceptionsList)) {
// early return if we are not an array such as being undefined or null or malformed.
return [];
} else {
return exceptionsList.flatMap((exceptionItem) => {
if (!(exceptionItem instanceof Object) || !isString(exceptionItem.id)) {
// return early if we are not an object such as being undefined or null or malformed
// or the exceptionItem.id is not a string from being malformed
return [];
} else {
return [exceptionItem];
}
});
}
}
function setLegacyId(
doc: SavedObjectUnsanitizedDoc<RawAlert>
): SavedObjectUnsanitizedDoc<RawAlert> {
const { id } = doc;
return {
...doc,
attributes: {
...doc.attributes,
legacyId: id,
},
};
}
function getRemovePreconfiguredConnectorsFromReferencesFn(
isPreconfigured: (connectorId: string) => boolean
) {
return (doc: SavedObjectUnsanitizedDoc<RawAlert>) => {
return removePreconfiguredConnectorsFromReferences(doc, isPreconfigured);
};
}
function removePreconfiguredConnectorsFromReferences(
doc: SavedObjectUnsanitizedDoc<RawAlert>,
isPreconfigured: (connectorId: string) => boolean
): SavedObjectUnsanitizedDoc<RawAlert> {
const {
attributes: { actions },
references,
} = doc;
// Look for connector references
const connectorReferences = (references ?? []).filter((ref: SavedObjectReference) =>
ref.name.startsWith('action_')
);
if (connectorReferences.length > 0) {
const restReferences = (references ?? []).filter(
(ref: SavedObjectReference) => !ref.name.startsWith('action_')
);
const updatedConnectorReferences: SavedObjectReference[] = [];
const updatedActions: RawAlert['actions'] = [];
// For each connector reference, check if connector is preconfigured
// If yes, we need to remove from the references array and update
// the corresponding action so it directly references the preconfigured connector id
connectorReferences.forEach((connectorRef: SavedObjectReference) => {
// Look for the corresponding entry in the actions array
const correspondingAction = getCorrespondingAction(actions, connectorRef.name);
if (correspondingAction) {
if (isPreconfigured(connectorRef.id)) {
updatedActions.push({
...correspondingAction,
actionRef: `preconfigured:${connectorRef.id}`,
});
} else {
updatedActions.push(correspondingAction);
updatedConnectorReferences.push(connectorRef);
}
} else {
// Couldn't find the matching action, leave as is
updatedConnectorReferences.push(connectorRef);
}
});
return {
...doc,
attributes: {
...doc.attributes,
actions: [...updatedActions],
},
references: [...updatedConnectorReferences, ...restReferences],
};
}
return doc;
}
function getCorrespondingAction(
actions: SavedObjectAttribute,
connectorRef: string
): RawAlertAction | null {
if (!Array.isArray(actions)) {
return null;
} else {
return actions.find(
(action) => (action as RawAlertAction)?.actionRef === connectorRef
) as RawAlertAction;
}
}
function pipeMigrations(...migrations: AlertMigration[]): AlertMigration {
return (doc: SavedObjectUnsanitizedDoc<RawAlert>) =>
migrations.reduce((migratedDoc, nextMigration) => nextMigration(migratedDoc), doc);
}