-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
index.ts
150 lines (139 loc) · 4.71 KB
/
index.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
/*
* 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 { ALERT_RULE_CONSUMER, ALERT_RULE_TYPE_ID, SPACE_IDS } from '@kbn/rule-data-utils';
import { map, mergeMap, catchError } from 'rxjs/operators';
import { from } from 'rxjs';
import {
AlertingAuthorizationEntity,
AlertingAuthorizationFilterType,
PluginStartContract as AlertingPluginStartContract,
} from '../../../../alerting/server';
import {
ISearchStrategy,
PluginStart,
SearchStrategyDependencies,
shimHitsTotal,
} from '../../../../../../src/plugins/data/server';
import {
TimelineFactoryQueryTypes,
TimelineStrategyResponseType,
TimelineStrategyRequestType,
EntityType,
} from '../../../common/search_strategy/timeline';
import { timelineFactory } from './factory';
import { TimelineFactory } from './factory/types';
import {
ENHANCED_ES_SEARCH_STRATEGY,
ISearchOptions,
} from '../../../../../../src/plugins/data/common';
export const timelineSearchStrategyProvider = <T extends TimelineFactoryQueryTypes>(
data: PluginStart,
alerting: AlertingPluginStartContract
): ISearchStrategy<TimelineStrategyRequestType<T>, TimelineStrategyResponseType<T>> => {
const esAsInternal = data.search.searchAsInternalUser;
const es = data.search.getSearchStrategy(ENHANCED_ES_SEARCH_STRATEGY);
return {
search: (request, options, deps) => {
const factoryQueryType = request.factoryQueryType;
const entityType = request.entityType;
if (factoryQueryType == null) {
throw new Error('factoryQueryType is required');
}
const queryFactory: TimelineFactory<T> = timelineFactory[factoryQueryType];
if (entityType != null && entityType === EntityType.ALERTS) {
return timelineAlertsSearchStrategy({
es: esAsInternal,
request,
options,
deps,
queryFactory,
alerting,
});
} else {
return timelineSearchStrategy({ es, request, options, deps, queryFactory });
}
},
cancel: async (id, options, deps) => {
if (es.cancel) {
return es.cancel(id, options, deps);
}
},
};
};
const timelineSearchStrategy = <T extends TimelineFactoryQueryTypes>({
es,
request,
options,
deps,
queryFactory,
}: {
es: ISearchStrategy;
request: TimelineStrategyRequestType<T>;
options: ISearchOptions;
deps: SearchStrategyDependencies;
queryFactory: TimelineFactory<T>;
}) => {
const dsl = queryFactory.buildDsl(request);
return es.search({ ...request, params: dsl }, options, deps).pipe(
map((response) => {
return {
...response,
rawResponse: shimHitsTotal(response.rawResponse, options),
};
}),
mergeMap((esSearchRes) => queryFactory.parse(request, esSearchRes))
);
};
const timelineAlertsSearchStrategy = <T extends TimelineFactoryQueryTypes>({
es,
request,
options,
deps,
queryFactory,
alerting,
}: {
es: ISearchStrategy;
request: TimelineStrategyRequestType<T>;
options: ISearchOptions;
deps: SearchStrategyDependencies;
alerting: AlertingPluginStartContract;
queryFactory: TimelineFactory<T>;
}) => {
// Based on what solution alerts you want to see, figures out what corresponding
// index to query (ex: siem --> .alerts-security.alerts)
const indices = request.defaultIndex ?? request.indexType;
const requestWithAlertsIndices = { ...request, defaultIndex: indices, indexName: indices };
// Note: Alerts RBAC are built off of the alerting's authorization class, which
// is why we are pulling from alerting, not ther alertsClient here
const alertingAuthorizationClient = alerting.getAlertingAuthorizationWithRequest(deps.request);
const getAuthFilter = async () =>
alertingAuthorizationClient.getFindAuthorizationFilter(AlertingAuthorizationEntity.Alert, {
type: AlertingAuthorizationFilterType.ESDSL,
// Not passing in values, these are the paths for these fields
fieldNames: {
consumer: ALERT_RULE_CONSUMER,
ruleTypeId: ALERT_RULE_TYPE_ID,
spaceIds: SPACE_IDS,
},
});
return from(getAuthFilter()).pipe(
mergeMap(({ filter }) => {
const dsl = queryFactory.buildDsl({ ...requestWithAlertsIndices, authFilter: filter });
return es.search({ ...requestWithAlertsIndices, params: dsl }, options, deps);
}),
map((response) => {
return {
...response,
rawResponse: shimHitsTotal(response.rawResponse, options),
};
}),
mergeMap((esSearchRes) => queryFactory.parse(requestWithAlertsIndices, esSearchRes)),
catchError((err) => {
throw err;
})
);
};