-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
alerts_client.ts
692 lines (639 loc) · 21.6 KB
/
alerts_client.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*
* 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 Boom from '@hapi/boom';
import { estypes } from '@elastic/elasticsearch';
import { PublicMethodsOf } from '@kbn/utility-types';
import { Filter, buildEsQuery, EsQueryConfig } from '@kbn/es-query';
import { decodeVersion, encodeHitVersion } from '@kbn/securitysolution-es-utils';
import type {
getEsQueryConfig as getEsQueryConfigTyped,
getSafeSortIds as getSafeSortIdsTyped,
isValidFeatureId as isValidFeatureIdTyped,
STATUS_VALUES,
ValidFeatureId,
} from '@kbn/rule-data-utils';
import {
getEsQueryConfig as getEsQueryConfigNonTyped,
getSafeSortIds as getSafeSortIdsNonTyped,
isValidFeatureId as isValidFeatureIdNonTyped,
// @ts-expect-error
} from '@kbn/rule-data-utils/target_node/alerts_as_data_rbac';
import { InlineScript, QueryDslQueryContainer } from '@elastic/elasticsearch/api/types';
import { AlertTypeParams, AlertingAuthorizationFilterType } from '../../../alerting/server';
import {
ReadOperations,
AlertingAuthorization,
WriteOperations,
AlertingAuthorizationEntity,
} from '../../../alerting/server';
import { Logger, ElasticsearchClient, EcsEventOutcome } from '../../../../../src/core/server';
import { alertAuditEvent, operationAlertAuditActionMap } from './audit_events';
import { AuditLogger } from '../../../security/server';
import {
ALERT_WORKFLOW_STATUS,
ALERT_RULE_CONSUMER,
ALERT_RULE_TYPE_ID,
SPACE_IDS,
} from '../../common/technical_rule_data_field_names';
import { ParsedTechnicalFields } from '../../common/parse_technical_fields';
import { Dataset, RuleDataPluginService } from '../rule_data_plugin_service';
const getEsQueryConfig: typeof getEsQueryConfigTyped = getEsQueryConfigNonTyped;
const getSafeSortIds: typeof getSafeSortIdsTyped = getSafeSortIdsNonTyped;
const isValidFeatureId: typeof isValidFeatureIdTyped = isValidFeatureIdNonTyped;
// TODO: Fix typings https://github.com/elastic/kibana/issues/101776
type NonNullableProps<Obj extends {}, Props extends keyof Obj> = Omit<Obj, Props> &
{ [K in Props]-?: NonNullable<Obj[K]> };
type AlertType = { _index: string; _id: string } & NonNullableProps<
ParsedTechnicalFields,
typeof ALERT_RULE_TYPE_ID | typeof ALERT_RULE_CONSUMER | typeof SPACE_IDS
>;
const isValidAlert = (source?: estypes.SearchHit<ParsedTechnicalFields>): source is AlertType => {
return (
(source?._source?.[ALERT_RULE_TYPE_ID] != null &&
source?._source?.[ALERT_RULE_CONSUMER] != null &&
source?._source?.[SPACE_IDS] != null) ||
(source?.fields?.[ALERT_RULE_TYPE_ID][0] != null &&
source?.fields?.[ALERT_RULE_CONSUMER][0] != null &&
source?.fields?.[SPACE_IDS][0] != null)
);
};
export interface ConstructorOptions {
logger: Logger;
authorization: PublicMethodsOf<AlertingAuthorization>;
auditLogger?: AuditLogger;
esClient: ElasticsearchClient;
ruleDataService: RuleDataPluginService;
}
export interface UpdateOptions<Params extends AlertTypeParams> {
id: string;
status: string;
_version: string | undefined;
index: string;
}
export interface BulkUpdateOptions<Params extends AlertTypeParams> {
ids: string[] | undefined | null;
status: STATUS_VALUES;
index: string;
query: object | string | undefined | null;
}
interface GetAlertParams {
id: string;
index?: string;
}
interface SingleSearchAfterAndAudit {
id?: string | null | undefined;
query?: string | object | undefined;
aggs?: object | undefined;
index?: string;
_source?: string[] | undefined;
track_total_hits?: boolean | undefined;
size?: number | undefined;
operation: WriteOperations.Update | ReadOperations.Find | ReadOperations.Get;
lastSortIds?: Array<string | number> | undefined;
}
/**
* Provides apis to interact with alerts as data
* ensures the request is authorized to perform read / write actions
* on alerts as data.
*/
export class AlertsClient {
private readonly logger: Logger;
private readonly auditLogger?: AuditLogger;
private readonly authorization: PublicMethodsOf<AlertingAuthorization>;
private readonly esClient: ElasticsearchClient;
private readonly spaceId: string | undefined;
private readonly ruleDataService: RuleDataPluginService;
constructor(options: ConstructorOptions) {
this.logger = options.logger;
this.authorization = options.authorization;
this.esClient = options.esClient;
this.auditLogger = options.auditLogger;
// If spaceId is undefined, it means that spaces is disabled
// Otherwise, if space is enabled and not specified, it is "default"
this.spaceId = this.authorization.getSpaceId();
this.ruleDataService = options.ruleDataService;
}
private getOutcome(
operation: WriteOperations.Update | ReadOperations.Find | ReadOperations.Get
): { outcome: EcsEventOutcome } {
return {
outcome: operation === WriteOperations.Update ? 'unknown' : 'success',
};
}
private getAlertStatusFieldUpdate(
source: ParsedTechnicalFields | undefined,
status: STATUS_VALUES
) {
return source?.[ALERT_WORKFLOW_STATUS] == null
? { signal: { status } }
: { [ALERT_WORKFLOW_STATUS]: status };
}
/**
* Accepts an array of ES documents and executes ensureAuthorized for the given operation
* @param items
* @param operation
* @returns
*/
private async ensureAllAuthorized(
items: Array<{
_id: string;
// this is typed kind of crazy to fit the output of es api response to this
_source?:
| {
[ALERT_RULE_TYPE_ID]?: string | null | undefined;
[ALERT_RULE_CONSUMER]?: string | null | undefined;
}
| null
| undefined;
}>,
operation: ReadOperations.Find | ReadOperations.Get | WriteOperations.Update
) {
const { hitIds, ownersAndRuleTypeIds } = items.reduce(
(acc, hit) => ({
hitIds: [hit._id, ...acc.hitIds],
ownersAndRuleTypeIds: [
{
[ALERT_RULE_TYPE_ID]: hit?._source?.[ALERT_RULE_TYPE_ID],
[ALERT_RULE_CONSUMER]: hit?._source?.[ALERT_RULE_CONSUMER],
},
],
}),
{ hitIds: [], ownersAndRuleTypeIds: [] } as {
hitIds: string[];
ownersAndRuleTypeIds: Array<{
[ALERT_RULE_TYPE_ID]: string | null | undefined;
[ALERT_RULE_CONSUMER]: string | null | undefined;
}>;
}
);
const assertString = (hit: unknown): hit is string => hit !== null && hit !== undefined;
return Promise.all(
ownersAndRuleTypeIds.map((hit) => {
const alertOwner = hit?.[ALERT_RULE_CONSUMER];
const ruleId = hit?.[ALERT_RULE_TYPE_ID];
if (hit != null && assertString(alertOwner) && assertString(ruleId)) {
return this.authorization.ensureAuthorized({
ruleTypeId: ruleId,
consumer: alertOwner,
operation,
entity: AlertingAuthorizationEntity.Alert,
});
}
})
).catch((error) => {
for (const hitId of hitIds) {
this.auditLogger?.log(
alertAuditEvent({
action: operationAlertAuditActionMap[operation],
id: hitId,
error,
})
);
}
throw error;
});
}
/**
* This will be used as a part of the "find" api
* In the future we will add an "aggs" param
* @param param0
* @returns
*/
private async singleSearchAfterAndAudit({
id,
query,
aggs,
_source,
track_total_hits: trackTotalHits,
size,
index,
operation,
lastSortIds = [],
}: SingleSearchAfterAndAudit) {
try {
const alertSpaceId = this.spaceId;
if (alertSpaceId == null) {
const errorMessage = 'Failed to acquire spaceId from authorization client';
this.logger.error(`fetchAlertAndAudit threw an error: ${errorMessage}`);
throw Boom.failedDependency(`fetchAlertAndAudit threw an error: ${errorMessage}`);
}
const config = getEsQueryConfig();
let queryBody = {
fields: [ALERT_RULE_TYPE_ID, ALERT_RULE_CONSUMER, ALERT_WORKFLOW_STATUS, SPACE_IDS],
query: await this.buildEsQueryWithAuthz(query, id, alertSpaceId, operation, config),
aggs,
_source,
track_total_hits: trackTotalHits,
size,
sort: [
{
'@timestamp': {
order: 'asc',
unmapped_type: 'date',
},
},
],
};
if (lastSortIds.length > 0) {
queryBody = {
...queryBody,
// @ts-expect-error
search_after: lastSortIds,
};
}
const result = await this.esClient.search<ParsedTechnicalFields>({
index: index ?? '.alerts-*',
ignore_unavailable: true,
// @ts-expect-error
body: queryBody,
seq_no_primary_term: true,
});
if (!result?.body.hits.hits.every((hit) => isValidAlert(hit))) {
const errorMessage = `Invalid alert found with id of "${id}" or with query "${query}" and operation ${operation}`;
this.logger.error(errorMessage);
throw Boom.badData(errorMessage);
}
if (result?.body?.hits?.hits != null && result?.body.hits.hits.length > 0) {
await this.ensureAllAuthorized(result.body.hits.hits, operation);
result?.body.hits.hits.map((item) =>
this.auditLogger?.log(
alertAuditEvent({
action: operationAlertAuditActionMap[operation],
id: item._id,
...this.getOutcome(operation),
})
)
);
}
return result.body;
} catch (error) {
const errorMessage = `Unable to retrieve alert details for alert with id of "${id}" or with query "${query}" and operation ${operation} \nError: ${error}`;
this.logger.error(errorMessage);
throw Boom.notFound(errorMessage);
}
}
/**
* When an update by ids is requested, do a multi-get, ensure authz and audit alerts, then execute bulk update
* @param param0
* @returns
*/
private async mgetAlertsAuditOperate({
ids,
status,
indexName,
operation,
}: {
ids: string[];
status: STATUS_VALUES;
indexName: string;
operation: ReadOperations.Find | ReadOperations.Get | WriteOperations.Update;
}) {
try {
const mgetRes = await this.esClient.mget<ParsedTechnicalFields>({
index: indexName,
body: {
ids,
},
});
await this.ensureAllAuthorized(mgetRes.body.docs, operation);
for (const id of ids) {
this.auditLogger?.log(
alertAuditEvent({
action: operationAlertAuditActionMap[operation],
id,
...this.getOutcome(operation),
})
);
}
const bulkUpdateRequest = mgetRes.body.docs.flatMap((item) => {
const fieldToUpdate = this.getAlertStatusFieldUpdate(item?._source, status);
return [
{
update: {
_index: item._index,
_id: item._id,
},
},
{
doc: {
...fieldToUpdate,
},
},
];
});
const bulkUpdateResponse = await this.esClient.bulk({
refresh: 'wait_for',
body: bulkUpdateRequest,
});
return bulkUpdateResponse;
} catch (exc) {
this.logger.error(`error in mgetAlertsAuditOperate ${exc}`);
throw exc;
}
}
private async buildEsQueryWithAuthz(
query: object | string | null | undefined,
id: string | null | undefined,
alertSpaceId: string,
operation: WriteOperations.Update | ReadOperations.Get | ReadOperations.Find,
config: EsQueryConfig
) {
try {
const { filter: authzFilter } = await this.authorization.getAuthorizationFilter(
AlertingAuthorizationEntity.Alert,
{
type: AlertingAuthorizationFilterType.ESDSL,
fieldNames: { consumer: ALERT_RULE_CONSUMER, ruleTypeId: ALERT_RULE_TYPE_ID },
},
operation
);
let esQuery;
if (id != null) {
esQuery = { query: `_id:${id}`, language: 'kuery' };
} else if (typeof query === 'string') {
esQuery = { query, language: 'kuery' };
} else if (query != null && typeof query === 'object') {
esQuery = [];
}
const builtQuery = buildEsQuery(
undefined,
esQuery == null ? { query: ``, language: 'kuery' } : esQuery,
[
(authzFilter as unknown) as Filter,
({ term: { [SPACE_IDS]: alertSpaceId } } as unknown) as Filter,
],
config
);
if (query != null && typeof query === 'object') {
return {
...builtQuery,
bool: {
...builtQuery.bool,
must: [...builtQuery.bool.must, query],
},
};
}
return builtQuery;
} catch (exc) {
this.logger.error(exc);
throw Boom.expectationFailed(
`buildEsQueryWithAuthz threw an error: unable to get authorization filter \n ${exc}`
);
}
}
/**
* executes a search after to find alerts with query (+ authz filter)
* @param param0
* @returns
*/
private async queryAndAuditAllAlerts({
index,
query,
operation,
}: {
index: string;
query: object | string;
operation: WriteOperations.Update | ReadOperations.Find | ReadOperations.Get;
}) {
let lastSortIds;
let hasSortIds = true;
const alertSpaceId = this.spaceId;
if (alertSpaceId == null) {
this.logger.error('Failed to acquire spaceId from authorization client');
return;
}
const config = getEsQueryConfig();
const authorizedQuery = await this.buildEsQueryWithAuthz(
query,
null,
alertSpaceId,
operation,
config
);
while (hasSortIds) {
try {
const result = await this.singleSearchAfterAndAudit({
id: null,
query,
index,
operation,
lastSortIds,
});
if (lastSortIds != null && result?.hits.hits.length === 0) {
return { auditedAlerts: true, authorizedQuery };
}
if (result == null) {
this.logger.error('RESULT WAS EMPTY');
return { auditedAlerts: false, authorizedQuery };
}
if (result.hits.hits.length === 0) {
this.logger.error('Search resulted in no hits');
return { auditedAlerts: true, authorizedQuery };
}
lastSortIds = getSafeSortIds(result.hits.hits[result.hits.hits.length - 1]?.sort);
if (lastSortIds != null && lastSortIds.length !== 0) {
hasSortIds = true;
} else {
hasSortIds = false;
return { auditedAlerts: true, authorizedQuery };
}
} catch (error) {
const errorMessage = `queryAndAuditAllAlerts threw an error: Unable to retrieve alerts with query "${query}" and operation ${operation} \n ${error}`;
this.logger.error(errorMessage);
throw Boom.notFound(errorMessage);
}
}
}
public async get({ id, index }: GetAlertParams) {
try {
// first search for the alert by id, then use the alert info to check if user has access to it
const alert = await this.singleSearchAfterAndAudit({
id,
index,
operation: ReadOperations.Get,
});
if (alert == null || alert.hits.hits.length === 0) {
const errorMessage = `Unable to retrieve alert details for alert with id of "${id}" and operation ${ReadOperations.Get}`;
this.logger.error(errorMessage);
throw Boom.notFound(errorMessage);
}
// move away from pulling data from _source in the future
return alert.hits.hits[0]._source;
} catch (error) {
this.logger.error(`get threw an error: ${error}`);
throw error;
}
}
public async update<Params extends AlertTypeParams = never>({
id,
status,
_version,
index,
}: UpdateOptions<Params>) {
try {
const alert = await this.singleSearchAfterAndAudit({
id,
index,
operation: WriteOperations.Update,
});
if (alert == null || alert.hits.hits.length === 0) {
const errorMessage = `Unable to retrieve alert details for alert with id of "${id}" and operation ${ReadOperations.Get}`;
this.logger.error(errorMessage);
throw Boom.notFound(errorMessage);
}
const fieldToUpdate = this.getAlertStatusFieldUpdate(
alert?.hits.hits[0]._source,
status as STATUS_VALUES
);
const { body: response } = await this.esClient.update<ParsedTechnicalFields>({
...decodeVersion(_version),
id,
index,
body: {
doc: {
...fieldToUpdate,
},
},
refresh: 'wait_for',
});
return {
...response,
_version: encodeHitVersion(response),
};
} catch (error) {
this.logger.error(`update threw an error: ${error}`);
throw error;
}
}
public async bulkUpdate<Params extends AlertTypeParams = never>({
ids,
query,
index,
status,
}: BulkUpdateOptions<Params>) {
// rejects at the route level if more than 1000 id's are passed in
if (ids != null) {
return this.mgetAlertsAuditOperate({
ids,
status,
indexName: index,
operation: WriteOperations.Update,
});
} else if (query != null) {
try {
// execute search after with query + authorization filter
// audit results of that query
const fetchAndAuditResponse = await this.queryAndAuditAllAlerts({
query,
index,
operation: WriteOperations.Update,
});
if (!fetchAndAuditResponse?.auditedAlerts) {
throw Boom.forbidden('Failed to audit alerts');
}
// executes updateByQuery with query + authorization filter
// used in the queryAndAuditAllAlerts function
const result = await this.esClient.updateByQuery({
index,
conflicts: 'proceed',
refresh: true,
body: {
script: {
source: `if (ctx._source['${ALERT_WORKFLOW_STATUS}'] != null) {
ctx._source['${ALERT_WORKFLOW_STATUS}'] = '${status}'
}
if (ctx._source.signal != null && ctx._source.signal.status != null) {
ctx._source.signal.status = '${status}'
}`,
lang: 'painless',
} as InlineScript,
query: fetchAndAuditResponse.authorizedQuery as Omit<QueryDslQueryContainer, 'script'>,
},
ignore_unavailable: true,
});
return result;
} catch (err) {
this.logger.error(`bulkUpdate threw an error: ${err}`);
throw err;
}
} else {
throw Boom.badRequest('no ids or query were provided for updating');
}
}
public async find<Params extends AlertTypeParams = never>({
query,
aggs,
_source,
track_total_hits: trackTotalHits,
size,
index,
}: {
query?: object | undefined;
aggs?: object | undefined;
index: string | undefined;
track_total_hits?: boolean | undefined;
_source?: string[] | undefined;
size?: number | undefined;
}) {
try {
// first search for the alert by id, then use the alert info to check if user has access to it
const alertsSearchResponse = await this.singleSearchAfterAndAudit({
query,
aggs,
_source,
track_total_hits: trackTotalHits,
size,
index,
operation: ReadOperations.Find,
});
if (alertsSearchResponse == null) {
const errorMessage = `Unable to retrieve alert details for alert with query and operation ${ReadOperations.Find}`;
this.logger.error(errorMessage);
throw Boom.notFound(errorMessage);
}
return alertsSearchResponse;
} catch (error) {
this.logger.error(`find threw an error: ${error}`);
throw error;
}
}
public async getAuthorizedAlertsIndices(featureIds: string[]): Promise<string[] | undefined> {
try {
// ATTENTION FUTURE DEVELOPER when you are a super user the augmentedRuleTypes.authorizedRuleTypes will
// return all of the features that you can access and does not care about your featureIds
const augmentedRuleTypes = await this.authorization.getAugmentedRuleTypesWithAuthorization(
featureIds,
[ReadOperations.Find, ReadOperations.Get, WriteOperations.Update],
AlertingAuthorizationEntity.Alert
);
// As long as the user can read a minimum of one type of rule type produced by the provided feature,
// the user should be provided that features' alerts index.
// Limiting which alerts that user can read on that index will be done via the findAuthorizationFilter
const authorizedFeatures = new Set<string>();
for (const ruleType of augmentedRuleTypes.authorizedRuleTypes) {
authorizedFeatures.add(ruleType.producer);
}
const validAuthorizedFeatures = Array.from(authorizedFeatures).filter(
(feature): feature is ValidFeatureId =>
featureIds.includes(feature) && isValidFeatureId(feature)
);
const toReturn = validAuthorizedFeatures.flatMap((feature) => {
const indices = this.ruleDataService.findIndicesByFeature(feature, Dataset.alerts);
if (feature === 'siem') {
return indices.map((i) => `${i.baseName}-${this.spaceId}`);
} else {
return indices.map((i) => i.baseName);
}
});
return toReturn;
} catch (exc) {
const errMessage = `getAuthorizedAlertsIndices failed to get authorized rule types: ${exc}`;
this.logger.error(errMessage);
throw Boom.failedDependency(errMessage);
}
}
}