-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
elasticsearch_adapter.ts
185 lines (171 loc) · 6.12 KB
/
elasticsearch_adapter.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { get, getOr, has, head, set } from 'lodash/fp';
import { FirstLastSeenHost, HostItem, HostsData, HostsEdges } from '../../graphql/types';
import { inspectStringifyObject } from '../../utils/build_query';
import { hostFieldsMap } from '../ecs_fields';
import { FrameworkAdapter, FrameworkRequest } from '../framework';
import { TermAggregation } from '../types';
import { buildHostOverviewQuery } from './query.detail_host.dsl';
import { buildHostsQuery } from './query.hosts.dsl';
import { buildLastFirstSeenHostQuery } from './query.last_first_seen_host.dsl';
import {
HostAggEsData,
HostAggEsItem,
HostBuckets,
HostOverviewRequestOptions,
HostEsData,
HostLastFirstSeenRequestOptions,
HostsAdapter,
HostsRequestOptions,
HostValue,
} from './types';
import { DEFAULT_MAX_TABLE_QUERY_SIZE } from '../../../common/constants';
export class ElasticsearchHostsAdapter implements HostsAdapter {
constructor(private readonly framework: FrameworkAdapter) {}
public async getHosts(
request: FrameworkRequest,
options: HostsRequestOptions
): Promise<HostsData> {
const dsl = buildHostsQuery(options);
if (options.pagination && options.pagination.querySize >= DEFAULT_MAX_TABLE_QUERY_SIZE) {
throw new Error(`No query size above ${DEFAULT_MAX_TABLE_QUERY_SIZE}`);
}
const response = await this.framework.callWithRequest<HostEsData, TermAggregation>(
request,
'search',
dsl
);
const { activePage, cursorStart, fakePossibleCount, querySize } = options.pagination;
const totalCount = getOr(0, 'aggregations.host_count.value', response);
const buckets: HostAggEsItem[] = getOr([], 'aggregations.host_data.buckets', response);
const hostsEdges = buckets.map((bucket) => formatHostEdgesData(options.fields, bucket));
const fakeTotalCount = fakePossibleCount <= totalCount ? fakePossibleCount : totalCount;
const edges = hostsEdges.splice(cursorStart, querySize - cursorStart);
const inspect = {
dsl: [inspectStringifyObject(dsl)],
response: [inspectStringifyObject(response)],
};
const showMorePagesIndicator = totalCount > fakeTotalCount;
return {
inspect,
edges,
totalCount,
pageInfo: {
activePage: activePage ? activePage : 0,
fakeTotalCount,
showMorePagesIndicator,
},
};
}
public async getHostOverview(
request: FrameworkRequest,
options: HostOverviewRequestOptions
): Promise<HostItem> {
const dsl = buildHostOverviewQuery(options);
const response = await this.framework.callWithRequest<HostAggEsData, TermAggregation>(
request,
'search',
dsl
);
const aggregations: HostAggEsItem = get('aggregations', response) || {};
const inspect = {
dsl: [inspectStringifyObject(dsl)],
response: [inspectStringifyObject(response)],
};
return { inspect, _id: options.hostName, ...formatHostItem(options.fields, aggregations) };
}
public async getHostFirstLastSeen(
request: FrameworkRequest,
options: HostLastFirstSeenRequestOptions
): Promise<FirstLastSeenHost> {
const dsl = buildLastFirstSeenHostQuery(options);
const response = await this.framework.callWithRequest<HostAggEsData, TermAggregation>(
request,
'search',
dsl
);
const aggregations: HostAggEsItem = get('aggregations', response) || {};
const inspect = {
dsl: [inspectStringifyObject(dsl)],
response: [inspectStringifyObject(response)],
};
return {
inspect,
firstSeen: get('firstSeen.value_as_string', aggregations),
lastSeen: get('lastSeen.value_as_string', aggregations),
};
}
}
export const formatHostEdgesData = (fields: readonly string[], bucket: HostAggEsItem): HostsEdges =>
fields.reduce<HostsEdges>(
(flattenedFields, fieldName) => {
const hostId = get('key', bucket);
flattenedFields.node._id = hostId || null;
flattenedFields.cursor.value = hostId || '';
const fieldValue = getHostFieldValue(fieldName, bucket);
if (fieldValue != null) {
return set(`node.${fieldName}`, fieldValue, flattenedFields);
}
return flattenedFields;
},
{
node: {},
cursor: {
value: '',
tiebreaker: null,
},
}
);
const formatHostItem = (fields: readonly string[], bucket: HostAggEsItem): HostItem =>
fields.reduce<HostItem>((flattenedFields, fieldName) => {
const fieldValue = getHostFieldValue(fieldName, bucket);
if (fieldValue != null) {
return set(fieldName, fieldValue, flattenedFields);
}
return flattenedFields;
}, {});
const getHostFieldValue = (fieldName: string, bucket: HostAggEsItem): string | string[] | null => {
const aggField = hostFieldsMap[fieldName]
? hostFieldsMap[fieldName].replace(/\./g, '_')
: fieldName.replace(/\./g, '_');
if (
[
'host.ip',
'host.mac',
'cloud.instance.id',
'cloud.machine.type',
'cloud.provider',
'cloud.region',
].includes(fieldName) &&
has(aggField, bucket)
) {
const data: HostBuckets = get(aggField, bucket);
return data.buckets.map((obj) => obj.key);
} else if (has(`${aggField}.buckets`, bucket)) {
return getFirstItem(get(`${aggField}`, bucket));
} else if (has(aggField, bucket)) {
const valueObj: HostValue = get(aggField, bucket);
return valueObj.value_as_string;
} else if (['host.name', 'host.os.name', 'host.os.version'].includes(fieldName)) {
switch (fieldName) {
case 'host.name':
return get('key', bucket) || null;
case 'host.os.name':
return get('os.hits.hits[0]._source.host.os.name', bucket) || null;
case 'host.os.version':
return get('os.hits.hits[0]._source.host.os.version', bucket) || null;
}
}
return null;
};
const getFirstItem = (data: HostBuckets): string | null => {
const firstItem = head(data.buckets);
if (firstItem == null) {
return null;
}
return firstItem.key;
};