-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Expand file tree
/
Copy pathclient.ts
More file actions
426 lines (402 loc) · 14.4 KB
/
Copy pathclient.ts
File metadata and controls
426 lines (402 loc) · 14.4 KB
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
/*
* 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 { v7 as uuidv7 } from 'uuid';
import type {
AggregationsAggregate,
QueryDslQueryContainer,
SearchTotalHits,
SortCombinations,
} from '@elastic/elasticsearch/lib/api/types';
import { withSpan } from '@kbn/apm-utils';
import type { ElasticsearchClient } from '@kbn/core/server';
import { type DataStreamDefinition, DataStreamClient } from '@kbn/data-streams';
import type { ClientCreateRequest } from '@kbn/data-streams/src/types/es_api';
import type { Logger } from '@kbn/logging';
import { changeHistoryMappings } from './mappings';
import {
DATA_STREAM_NAME,
SEPARATOR_CHAR,
ECS_VERSION,
DEFAULT_RESULT_SIZE,
DEFAULT_FIELD_AGGREGATION_SIZE,
} from './constants';
import type {
ChangeHistoryAggregateField,
ChangeHistoryDocument,
ChangeHistoryFieldBucket,
GetHistoryResult,
LogChangeHistoryOptions,
GetChangeHistoryOptions,
GetChangeHistoryByFieldResult,
GetChangeHistoryByFieldsOptions,
GetChangeHistoryByFieldsResult,
ObjectChange,
} from './types';
import { sha256, sanitizeFields } from './utils';
export { DATA_STREAM_NAME } from './constants';
type ChangeHistoryDataStreamClient = DataStreamClient<
typeof changeHistoryMappings.v1,
ChangeHistoryDocument
>;
export interface IChangeHistoryClient {
isInitialized(): boolean;
initialize(elasticsearchClient: ElasticsearchClient): Promise<void>;
log(change: ObjectChange, opts: LogChangeHistoryOptions): Promise<void>;
logBulk(changes: ObjectChange[], opts: LogChangeHistoryOptions): Promise<void>;
getHistory(
spaceId: string,
objectType: string,
objectId: string,
opts?: GetChangeHistoryOptions
): Promise<GetHistoryResult>;
getHistoryByFields(
spaceId: string,
objectType: string,
objectId: string,
fields: ChangeHistoryAggregateField[],
opts?: GetChangeHistoryByFieldsOptions
): Promise<GetChangeHistoryByFieldsResult>;
}
export class ChangeHistoryClient implements IChangeHistoryClient {
private module: string;
private dataset: string;
private kibanaVersion: string;
private logger: Logger;
private client?: ChangeHistoryDataStreamClient;
constructor({
module,
dataset,
logger,
kibanaVersion,
}: {
module: string;
dataset: string;
logger: Logger;
kibanaVersion: string;
}) {
if (module.includes(SEPARATOR_CHAR)) {
throw new Error(
`Invalid module "${module}". Should not include separator [${SEPARATOR_CHAR}]`
);
}
if (dataset.includes(SEPARATOR_CHAR)) {
throw new Error(
`Invalid dataset "${dataset}". Should not include separator [${SEPARATOR_CHAR}]`
);
}
this.module = module;
this.dataset = dataset;
this.kibanaVersion = kibanaVersion;
this.logger = logger;
}
/**
* Check if the change tracking service is initialized.
* @returns true if the change tracking service is initialized.
*/
isInitialized() {
return !!this.client;
}
/**
* Initialize the change tracking service.
* @param elasticsearchClient The privileged elasticsearch client `core.elasticsearch.client.asInternalUser`.
* @returns A promise that resolves when the change tracking service is initialized.
* @throws An error if the data stream is not initialized properly.
*/
async initialize(elasticsearchClient: ElasticsearchClient) {
const definition: DataStreamDefinition<typeof changeHistoryMappings.v1, ChangeHistoryDocument> =
{
name: DATA_STREAM_NAME,
version: 3,
hidden: true,
template: {
priority: 100,
mappings: changeHistoryMappings.v1,
lifecycle: { enabled: true },
},
};
// Enroll the data stream in DSL lifecycle with infinite retention by default.
// Cluster admins can add retention later via Index Management (stateful and serverless).
try {
this.client = await DataStreamClient.initialize({
dataStream: definition,
elasticsearchClient,
logger: this.logger,
lazyCreation: false,
});
} catch (error) {
const err = new Error(
`Unable to initialize change history data stream for: module [${this.module}] and dataset [${this.dataset}]: ${error}`,
{ cause: error }
);
this.logger.error(err);
throw err;
}
}
/**
* Log a change for a single object.
* @param change - The affected object; `change.snapshot` must be the **after** (post-change) state persisted as `object.snapshot`.
* @param opts - The options for the change.
* @returns A promise that resolves when the change is logged.
* @throws An error if the data stream is not initialized, or if an error occurs while logging the change.
*/
async log(change: ObjectChange, opts: LogChangeHistoryOptions) {
return this.logBulk([change], opts);
}
/**
* Log a bulk change for one or more objects.
* @param changes - The affected objects; each `snapshot` is the **after** (post-change) state for that object.
* @param opts - The options for the bulk change.
* @param opts.action - The action performed (`rule_create`, `rule_update`, `rule_delete`, etc.)
* @param opts.username - Current login name for the user who performed the change.
* @param opts.userProfileId - Optional user profile ID (auth realm). See Elastic User Profiles.
* @param opts.spaceId - The ID of the space that the change belongs to.
* @param opts.correlationId - Optional correlation ID for the bulk change.
* @param opts.data - Optional data to merge into the change history document.
* @param opts.fieldsToHash - Optional fields whose string values are replaced with a salted SHA-256 digest (high-entropy secrets only).
* @param opts.fieldsToRedact - Optional fields whose string values are replaced with a `[redacted]` placeholder (low-entropy sensitive data).
* @param opts.refresh - Optional indicator to force an ES refresh after changes (affects performance)
* @returns A promise that resolves when the bulk change is logged.
* @throws An error if the data stream is not initialized, or if an error occurs while logging the change.
*/
async logBulk(changes: ObjectChange[], opts: LogChangeHistoryOptions) {
const client = this.getInitializedClient();
const { module, dataset, kibanaVersion } = this;
const {
username,
userProfileId,
spaceId: space,
fieldsToHash,
fieldsToRedact,
correlationId,
refresh,
spanLabels,
} = opts;
const request: ClientCreateRequest<ChangeHistoryDocument> = {
refresh,
space,
documents: [],
};
const labels = correlationId ? { ...spanLabels, correlationId } : spanLabels;
await withSpan(
{ name: 'change_history.log_bulk.build_documents', type: 'app', labels },
async () => {
for (const change of changes) {
// Create document and populate
const { objectType, objectId, timestamp, sequence } = change;
const hash = sha256(JSON.stringify(change.snapshot));
const sanitized = sanitizeFields(change.snapshot, {
fieldsToHash,
fieldsToRedact,
salt: objectId,
});
const { event, metadata, tags } = opts.data ?? {};
const created = new Date().toISOString();
const document: ChangeHistoryDocument = {
'@timestamp': new Date(timestamp || created).toISOString(),
ecs: { version: ECS_VERSION },
user: { name: username, id: userProfileId },
event: {
id: uuidv7(), // <-- uuid v7 helps making 'same millisecond' event order deterministic
created,
type: event?.type ?? 'change',
reason: event?.reason,
module,
dataset,
action: opts.action,
},
object: {
id: objectId,
type: objectType,
hash,
sequence,
fields: sanitized.fields,
snapshot: sanitized.snapshot,
},
tags,
metadata,
service: { type: 'kibana', version: kibanaVersion },
span: correlationId ? { id: correlationId } : undefined,
};
// Queue operations
request.documents.push({ _id: document.event.id, ...document });
}
}
);
try {
await withSpan(
{
name: 'change_history.log_bulk.es_bulk_create',
type: 'db',
subtype: 'elasticsearch',
labels,
},
() => client.create({ ...request })
);
} catch (err) {
this.logger.error(`Error saving change history: ${err}`);
throw err;
}
}
/**
* Get the change history of an object.
* @param spaceId - The kibana space Id where this object exists
* @param objectType - The type of the object.
* @param objectId - The ID of the object.
* @param opts - The options for the history query.
* @param opts.additionalFilters - Additional filters to apply to the history query.
* @param opts.sort - The sort order for the history query.
* @param opts.from - The starting index for the history query.
* @param opts.size - The number of results to return.
* @returns The history of the object.
* @throws An error if the data stream is not initialized, or if an error occurs while getting the history.
*/
async getHistory(
spaceId: string,
objectType: string,
objectId: string,
opts?: GetChangeHistoryOptions
): Promise<GetHistoryResult> {
const client = this.getInitializedClient();
const filter = this.buildHistoryFilters(objectType, objectId, opts?.additionalFilters);
const defaultSort: SortCombinations[] = [
{ 'object.sequence': { order: 'desc', missing: 0 } }, // <-- If available, `sequence` ordering overrides timestamps.
{ '@timestamp': { order: 'desc' } },
{ 'event.id': { order: 'desc' } },
];
const history = await withSpan(
{
name: 'change_history.get_history.es_search',
type: 'db',
subtype: 'elasticsearch',
labels: opts?.spanLabels,
},
() =>
client.search({
space: spaceId,
query: { bool: { filter } },
sort: opts?.sort ?? defaultSort,
size: opts?.size ?? DEFAULT_RESULT_SIZE,
from: opts?.from,
})
);
return {
total: Number((history.hits.total as SearchTotalHits)?.value) || 0,
items: history.hits.hits.map((h) => h._source).filter((i) => !!i),
};
}
/**
* Bucket distinct values for one or more document fields in a single search.
* Builds sibling terms aggregations (descending doc count) scoped like {@link getHistory}.
*
* Pass one or more {@link ChangeHistoryAggregateField} values (e.g. `user.name`, `event.action`).
* Duplicate `fields` entries are removed while preserving first-seen order.
*/
async getHistoryByFields(
spaceId: string,
objectType: string,
objectId: string,
fields: ChangeHistoryAggregateField[],
opts?: GetChangeHistoryByFieldsOptions
): Promise<GetChangeHistoryByFieldsResult> {
const uniqueFields = [...new Set(fields)];
if (uniqueFields.length === 0) {
return { results: [] };
}
const client = this.getInitializedClient();
const bucketSize = opts?.size ?? DEFAULT_FIELD_AGGREGATION_SIZE;
const filter = this.buildHistoryFilters(objectType, objectId, opts?.additionalFilters);
const aggregations = Object.fromEntries(
uniqueFields.map((field) => [
field,
{
terms: {
field,
size: bucketSize,
order: { _count: 'desc' as const },
},
},
])
);
const response = await client.search({
space: spaceId,
query: { bool: { filter } },
aggregations,
size: 0,
});
return {
results: uniqueFields.map((field) => ({
field,
...this.parseHistoryByFieldAggregation(response.aggregations, field),
})),
};
}
private getInitializedClient(): ChangeHistoryDataStreamClient {
const client = this.client;
if (!client) {
const err = new Error(
`Change history data stream not initialized for: module [${this.module}] and dataset [${this.dataset}]`
);
this.logger.error(err);
throw err;
}
return client;
}
private buildHistoryFilters(
objectType: string,
objectId: string,
additionalFilters?: QueryDslQueryContainer[]
): QueryDslQueryContainer[] {
const filter: QueryDslQueryContainer[] = [
{ term: { 'event.module': this.module } },
{ term: { 'event.dataset': this.dataset } },
{ term: { 'object.type': objectType } },
{ term: { 'object.id': objectId } },
];
if (additionalFilters) {
filter.push(...additionalFilters);
}
return filter;
}
/**
* Soft-parses a terms aggregation keyed by field name. Unexpected shapes or non-string
* keys degrade to empty/partial buckets rather than failing the facet request.
*/
private parseHistoryByFieldAggregation(
aggregations: Record<string, AggregationsAggregate> | undefined,
field: ChangeHistoryAggregateField
): Pick<GetChangeHistoryByFieldResult, 'buckets' | 'sumOtherDocCount'> {
const candidate = aggregations?.[field] as
| {
sum_other_doc_count?: number;
buckets?: Array<{ key?: unknown; doc_count?: number }>;
}
| undefined;
if (candidate === undefined) {
return { buckets: [], sumOtherDocCount: 0 };
}
if (typeof candidate.sum_other_doc_count !== 'number' || !Array.isArray(candidate.buckets)) {
this.logger.warn(
`Unexpected aggregation shape for change history field [${field}]; returning empty buckets`
);
return { buckets: [], sumOtherDocCount: 0 };
}
const buckets: ChangeHistoryFieldBucket[] = candidate.buckets.flatMap((bucket) => {
const { key, doc_count: docCount } = bucket;
if (typeof key !== 'string' || typeof docCount !== 'number') {
this.logger.warn(
`Skipping unexpected bucket for change history field [${field}]; key type [${typeof key}]`
);
return [];
}
return [{ key, docCount }];
});
return {
buckets,
sumOtherDocCount: candidate.sum_other_doc_count,
};
}
}