-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathkbn_client_saved_objects.ts
305 lines (267 loc) · 8 KB
/
kbn_client_saved_objects.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
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { chunk } from 'lodash';
import type { ToolingLog } from '@kbn/tooling-log';
import type {
SavedObjectsBulkDeleteResponse,
SavedObjectsFindResponse,
} from '@kbn/core-saved-objects-api-server';
import { KbnClientRequester, uriencode } from './kbn_client_requester';
type MigrationVersion = Record<string, string>;
interface Reference {
id: string;
name: string;
type: string;
}
interface SavedObjectResponse<Attributes extends Record<string, any>> {
attributes: Attributes;
id: string;
migrationVersion?: MigrationVersion;
references: Reference[];
type: string;
updated_at?: string;
version?: string;
}
interface FindOptions {
type: string;
space?: string;
}
interface GetOptions {
type: string;
id: string;
space?: string;
}
interface IndexOptions<Attributes> {
type: string;
attributes: Attributes;
id?: string;
overwrite?: boolean;
migrationVersion?: MigrationVersion;
references?: Reference[];
}
interface UpdateOptions<Attributes> extends IndexOptions<Attributes> {
id: string;
}
interface MigrateResponse {
success: boolean;
result: Array<{ status: string }>;
}
interface CleanOptions {
space?: string;
types: string[];
}
interface CleanApiResponse {
deleted: number;
}
interface DeleteObjectsOptions {
space?: string;
objects: Array<{
type: string;
id: string;
}>;
}
const DELETE_CHUNK_SIZE = 50;
// add types here
const STANDARD_LIST_TYPES = [
'url',
'index-pattern',
'action',
'query',
'alert',
'graph-workspace',
'tag',
'visualization',
'canvas-element',
'canvas-workpad',
'dashboard',
'search',
'lens',
'links',
'map',
// cases saved objects
'cases',
'cases-comments',
'cases-user-actions',
'cases-configure',
'cases-connector-mappings',
// synthetics based objects
'synthetics-monitor',
'uptime-dynamic-settings',
'synthetics-privates-locations',
'synthetics-private-location',
'osquery-saved-query',
'osquery-pack',
'infrastructure-ui-source',
'metrics-data-source',
'metrics-explorer-view',
'inventory-view',
'infrastructure-monitoring-log-view',
'apm-indices',
// Fleet saved object types
'ingest_manager_settings',
'ingest-outputs',
'ingest-download-sources',
'ingest-agent-policies',
'fleet-agent-policies',
'ingest-package-policies',
'fleet-package-policies',
'epm-packages',
'epm-packages-assets',
'fleet-preconfiguration-deletion-record',
'fleet-fleet-server-host',
'fleet-proxy',
'fleet-uninstall-tokens',
'fleet-space-settings',
];
/**
* SO client for FTR.
*
* @remarks: Leverage the `ftrApis` plugin under the hood.
*/
export class KbnClientSavedObjects {
constructor(private readonly log: ToolingLog, private readonly requester: KbnClientRequester) {}
/**
* Run the saved objects migration
*/
public async migrate() {
this.log.debug('Migrating saved objects');
const { data } = await this.requester.request<MigrateResponse>({
description: 'migrate saved objects',
path: uriencode`/internal/saved_objects/_migrate`,
method: 'POST',
body: {},
});
return data;
}
/**
* Get an object
*/
public async get<Attributes extends Record<string, any>>(options: GetOptions) {
this.log.debug('Getting saved object: %j', options);
const { data } = await this.requester.request<SavedObjectResponse<Attributes>>({
description: 'get saved object',
path: options.space
? uriencode`/s/${options.space}/internal/ftr/kbn_client_so/${options.type}/${options.id}`
: uriencode`/internal/ftr/kbn_client_so/${options.type}/${options.id}`,
method: 'GET',
});
return data;
}
/**
* Find saved objects
*/
public async find<Attributes extends Record<string, any>>(options: FindOptions) {
this.log.debug('Find saved objects: %j', options);
const { data } = await this.requester.request<SavedObjectsFindResponse<Attributes>>({
description: 'find saved objects',
path: options.space
? uriencode`/s/${options.space}/internal/ftr/kbn_client_so/_find?type=${options.type}`
: uriencode`/internal/ftr/kbn_client_so/_find?type=${options.type}`,
method: 'GET',
});
return data;
}
/**
* Create a saved object
*/
public async create<Attributes extends Record<string, any>>(options: IndexOptions<Attributes>) {
this.log.debug('Creating saved object: %j', options);
const { data } = await this.requester.request<SavedObjectResponse<Attributes>>({
description: 'update saved object',
path: options.id
? uriencode`/internal/ftr/kbn_client_so/${options.type}/${options.id}`
: uriencode`/internal/ftr/kbn_client_so/${options.type}`,
query: {
overwrite: options.overwrite,
},
method: 'POST',
body: {
attributes: options.attributes,
migrationVersion: options.migrationVersion,
references: options.references,
},
});
return data;
}
/**
* Update a saved object
*/
public async update<Attributes extends Record<string, any>>(options: UpdateOptions<Attributes>) {
this.log.debug('Updating saved object: %j', options);
const { data } = await this.requester.request<SavedObjectResponse<Attributes>>({
description: 'update saved object',
path: uriencode`/internal/ftr/kbn_client_so/${options.type}/${options.id}`,
query: {
overwrite: options.overwrite,
},
method: 'PUT',
body: {
attributes: options.attributes,
migrationVersion: options.migrationVersion,
references: options.references,
},
});
return data;
}
/**
* Delete an object
*/
public async delete(options: GetOptions) {
this.log.debug('Deleting saved object %s/%s', options);
const { data } = await this.requester.request({
description: 'delete saved object',
path: options.space
? uriencode`/s/${options.space}/internal/ftr/kbn_client_so/${options.type}/${options.id}`
: uriencode`/internal/ftr/kbn_client_so/${options.type}/${options.id}`,
method: 'DELETE',
});
return data;
}
public async clean(options: CleanOptions) {
this.log.debug('Cleaning all saved objects', { space: options.space });
const resp = await this.requester.request<CleanApiResponse>({
method: 'POST',
path: options.space
? uriencode`/s/${options.space}/internal/ftr/kbn_client_so/_clean`
: `/internal/ftr/kbn_client_so/_clean`,
body: {
types: options.types,
},
});
const deleted = resp.data.deleted;
this.log.success('deleted', deleted, 'objects');
}
public async cleanStandardList(options?: { space?: string }) {
const newOptions = { types: STANDARD_LIST_TYPES, space: options?.space };
await this.clean(newOptions);
}
public async bulkDelete(options: DeleteObjectsOptions) {
let deleted = 0;
let missing = 0;
const chunks = chunk(options.objects, DELETE_CHUNK_SIZE);
for (let i = 0; i < chunks.length; i++) {
const objects = chunks[i];
const { data: response } = await this.requester.request<SavedObjectsBulkDeleteResponse>({
method: 'POST',
path: options.space
? uriencode`/s/${options.space}/internal/ftr/kbn_client_so/_bulk_delete`
: uriencode`/internal/ftr/kbn_client_so/_bulk_delete`,
body: objects.map(({ type, id }) => ({ type, id })),
});
response.statuses.forEach((status) => {
if (status.success) {
deleted++;
} else if (status.error?.statusCode === 404) {
missing++;
}
});
}
return { deleted, missing };
}
}