-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathpackage_policy.ts
538 lines (476 loc) · 16.4 KB
/
package_policy.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
/*
* 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 { KibanaRequest, RequestHandlerContext, SavedObjectsClientContract } from 'src/core/server';
import uuid from 'uuid';
import { AuthenticatedUser } from '../../../security/server';
import {
DeletePackagePoliciesResponse,
PackagePolicyInput,
NewPackagePolicyInput,
PackagePolicyInputStream,
PackageInfo,
ListWithKuery,
packageToPackagePolicy,
isPackageLimited,
doesAgentPolicyAlreadyIncludePackage,
} from '../../common';
import { PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '../constants';
import {
NewPackagePolicy,
UpdatePackagePolicy,
PackagePolicy,
PackagePolicySOAttributes,
RegistryPackage,
CallESAsCurrentUser,
NewPackagePolicySchema,
UpdatePackagePolicySchema,
} from '../types';
import { agentPolicyService } from './agent_policy';
import { outputService } from './output';
import * as Registry from './epm/registry';
import { getPackageInfo, getInstallation, ensureInstalledPackage } from './epm/packages';
import { getAssetsData } from './epm/packages/assets';
import { compileTemplate } from './epm/agent/agent';
import { normalizeKuery } from './saved_object';
import { appContextService } from '.';
import { ExternalCallback } from '..';
const SAVED_OBJECT_TYPE = PACKAGE_POLICY_SAVED_OBJECT_TYPE;
function getDataset(st: string) {
return st.split('.')[1];
}
class PackagePolicyService {
public async create(
soClient: SavedObjectsClientContract,
callCluster: CallESAsCurrentUser,
packagePolicy: NewPackagePolicy,
options?: { id?: string; user?: AuthenticatedUser; bumpRevision?: boolean }
): Promise<PackagePolicy> {
// Check that its agent policy does not have a package policy with the same name
const parentAgentPolicy = await agentPolicyService.get(soClient, packagePolicy.policy_id);
if (!parentAgentPolicy) {
throw new Error('Agent policy not found');
} else {
if (
(parentAgentPolicy.package_policies as PackagePolicy[]).find(
(siblingPackagePolicy) => siblingPackagePolicy.name === packagePolicy.name
)
) {
throw new Error('There is already a package with the same name on this agent policy');
}
}
// Add ids to stream
const packagePolicyId = options?.id || uuid.v4();
let inputs: PackagePolicyInput[] = packagePolicy.inputs.map((input) =>
assignStreamIdToInput(packagePolicyId, input)
);
// Make sure the associated package is installed
if (packagePolicy.package?.name) {
const [, pkgInfo] = await Promise.all([
ensureInstalledPackage({
savedObjectsClient: soClient,
pkgName: packagePolicy.package.name,
callCluster,
}),
getPackageInfo({
savedObjectsClient: soClient,
pkgName: packagePolicy.package.name,
pkgVersion: packagePolicy.package.version,
}),
]);
// Check if it is a limited package, and if so, check that the corresponding agent policy does not
// already contain a package policy for this package
if (isPackageLimited(pkgInfo)) {
const agentPolicy = await agentPolicyService.get(soClient, packagePolicy.policy_id, true);
if (agentPolicy && doesAgentPolicyAlreadyIncludePackage(agentPolicy, pkgInfo.name)) {
throw new Error(
`Unable to create package policy. Package '${pkgInfo.name}' already exists on this agent policy.`
);
}
}
inputs = await this.compilePackagePolicyInputs(pkgInfo, inputs);
}
const isoDate = new Date().toISOString();
const newSo = await soClient.create<PackagePolicySOAttributes>(
SAVED_OBJECT_TYPE,
{
...packagePolicy,
inputs,
revision: 1,
created_at: isoDate,
created_by: options?.user?.username ?? 'system',
updated_at: isoDate,
updated_by: options?.user?.username ?? 'system',
},
{ ...options, id: packagePolicyId }
);
// Assign it to the given agent policy
await agentPolicyService.assignPackagePolicies(soClient, packagePolicy.policy_id, [newSo.id], {
user: options?.user,
bumpRevision: options?.bumpRevision ?? true,
});
return {
id: newSo.id,
version: newSo.version,
...newSo.attributes,
};
}
public async bulkCreate(
soClient: SavedObjectsClientContract,
packagePolicies: NewPackagePolicy[],
agentPolicyId: string,
options?: { user?: AuthenticatedUser; bumpRevision?: boolean }
): Promise<PackagePolicy[]> {
const isoDate = new Date().toISOString();
// eslint-disable-next-line @typescript-eslint/naming-convention
const { saved_objects } = await soClient.bulkCreate<PackagePolicySOAttributes>(
packagePolicies.map((packagePolicy) => {
const packagePolicyId = uuid.v4();
const inputs = packagePolicy.inputs.map((input) =>
assignStreamIdToInput(packagePolicyId, input)
);
return {
type: SAVED_OBJECT_TYPE,
id: packagePolicyId,
attributes: {
...packagePolicy,
inputs,
policy_id: agentPolicyId,
revision: 1,
created_at: isoDate,
created_by: options?.user?.username ?? 'system',
updated_at: isoDate,
updated_by: options?.user?.username ?? 'system',
},
};
})
);
// Filter out invalid SOs
const newSos = saved_objects.filter((so) => !so.error && so.attributes);
// Assign it to the given agent policy
await agentPolicyService.assignPackagePolicies(
soClient,
agentPolicyId,
newSos.map((newSo) => newSo.id),
{
user: options?.user,
bumpRevision: options?.bumpRevision ?? true,
}
);
return newSos.map((newSo) => ({
id: newSo.id,
version: newSo.version,
...newSo.attributes,
}));
}
public async get(
soClient: SavedObjectsClientContract,
id: string
): Promise<PackagePolicy | null> {
const packagePolicySO = await soClient.get<PackagePolicySOAttributes>(SAVED_OBJECT_TYPE, id);
if (!packagePolicySO) {
return null;
}
if (packagePolicySO.error) {
throw new Error(packagePolicySO.error.message);
}
return {
id: packagePolicySO.id,
version: packagePolicySO.version,
...packagePolicySO.attributes,
};
}
public async getByIDs(
soClient: SavedObjectsClientContract,
ids: string[]
): Promise<PackagePolicy[] | null> {
const packagePolicySO = await soClient.bulkGet<PackagePolicySOAttributes>(
ids.map((id) => ({
id,
type: SAVED_OBJECT_TYPE,
}))
);
if (!packagePolicySO) {
return null;
}
return packagePolicySO.saved_objects.map((so) => ({
id: so.id,
version: so.version,
...so.attributes,
}));
}
public async list(
soClient: SavedObjectsClientContract,
options: ListWithKuery
): Promise<{ items: PackagePolicy[]; total: number; page: number; perPage: number }> {
const { page = 1, perPage = 20, sortField = 'updated_at', sortOrder = 'desc', kuery } = options;
const packagePolicies = await soClient.find<PackagePolicySOAttributes>({
type: SAVED_OBJECT_TYPE,
sortField,
sortOrder,
page,
perPage,
filter: kuery ? normalizeKuery(SAVED_OBJECT_TYPE, kuery) : undefined,
});
return {
items: packagePolicies.saved_objects.map((packagePolicySO) => ({
id: packagePolicySO.id,
version: packagePolicySO.version,
...packagePolicySO.attributes,
})),
total: packagePolicies.total,
page,
perPage,
};
}
public async update(
soClient: SavedObjectsClientContract,
id: string,
packagePolicy: UpdatePackagePolicy,
options?: { user?: AuthenticatedUser }
): Promise<PackagePolicy> {
const oldPackagePolicy = await this.get(soClient, id);
const { version, ...restOfPackagePolicy } = packagePolicy;
if (!oldPackagePolicy) {
throw new Error('Package policy not found');
}
// Check that its agent policy does not have a package policy with the same name
const parentAgentPolicy = await agentPolicyService.get(soClient, packagePolicy.policy_id);
if (!parentAgentPolicy) {
throw new Error('Agent policy not found');
} else {
if (
(parentAgentPolicy.package_policies as PackagePolicy[]).find(
(siblingPackagePolicy) =>
siblingPackagePolicy.id !== id && siblingPackagePolicy.name === packagePolicy.name
)
) {
throw new Error('There is already a package with the same name on this agent policy');
}
}
let inputs = await restOfPackagePolicy.inputs.map((input) =>
assignStreamIdToInput(oldPackagePolicy.id, input)
);
if (packagePolicy.package?.name) {
const pkgInfo = await getPackageInfo({
savedObjectsClient: soClient,
pkgName: packagePolicy.package.name,
pkgVersion: packagePolicy.package.version,
});
inputs = await this.compilePackagePolicyInputs(pkgInfo, inputs);
}
await soClient.update<PackagePolicySOAttributes>(
SAVED_OBJECT_TYPE,
id,
{
...restOfPackagePolicy,
inputs,
revision: oldPackagePolicy.revision + 1,
updated_at: new Date().toISOString(),
updated_by: options?.user?.username ?? 'system',
},
{
version,
}
);
// Bump revision of associated agent policy
await agentPolicyService.bumpRevision(soClient, packagePolicy.policy_id, {
user: options?.user,
});
return (await this.get(soClient, id)) as PackagePolicy;
}
public async delete(
soClient: SavedObjectsClientContract,
ids: string[],
options?: { user?: AuthenticatedUser; skipUnassignFromAgentPolicies?: boolean }
): Promise<DeletePackagePoliciesResponse> {
const result: DeletePackagePoliciesResponse = [];
for (const id of ids) {
try {
const packagePolicy = await this.get(soClient, id);
if (!packagePolicy) {
throw new Error('Package policy not found');
}
if (!options?.skipUnassignFromAgentPolicies) {
await agentPolicyService.unassignPackagePolicies(
soClient,
packagePolicy.policy_id,
[packagePolicy.id],
{
user: options?.user,
}
);
}
await soClient.delete(SAVED_OBJECT_TYPE, id);
result.push({
id,
name: packagePolicy.name,
success: true,
});
} catch (e) {
result.push({
id,
success: false,
});
}
}
return result;
}
public async buildPackagePolicyFromPackage(
soClient: SavedObjectsClientContract,
pkgName: string
): Promise<NewPackagePolicy | undefined> {
const pkgInstall = await getInstallation({ savedObjectsClient: soClient, pkgName });
if (pkgInstall) {
const [pkgInfo, defaultOutputId] = await Promise.all([
getPackageInfo({
savedObjectsClient: soClient,
pkgName: pkgInstall.name,
pkgVersion: pkgInstall.version,
}),
outputService.getDefaultOutputId(soClient),
]);
if (pkgInfo) {
if (!defaultOutputId) {
throw new Error('Default output is not set');
}
return packageToPackagePolicy(pkgInfo, '', defaultOutputId);
}
}
}
public async compilePackagePolicyInputs(
pkgInfo: PackageInfo,
inputs: PackagePolicyInput[]
): Promise<PackagePolicyInput[]> {
const registryPkgInfo = await Registry.fetchInfo(pkgInfo.name, pkgInfo.version);
const inputsPromises = inputs.map(async (input) => {
const compiledInput = await _compilePackagePolicyInput(registryPkgInfo, pkgInfo, input);
const compiledStreams = await _compilePackageStreams(registryPkgInfo, pkgInfo, input);
return {
...input,
compiled_input: compiledInput,
streams: compiledStreams,
};
});
return Promise.all(inputsPromises);
}
public async runExternalCallbacks(
externalCallbackType: ExternalCallback[0],
newPackagePolicy: NewPackagePolicy,
context: RequestHandlerContext,
request: KibanaRequest
): Promise<NewPackagePolicy> {
let newData = newPackagePolicy;
const externalCallbacks = appContextService.getExternalCallbacks(externalCallbackType);
if (externalCallbacks && externalCallbacks.size > 0) {
let updatedNewData: NewPackagePolicy = newData;
for (const callback of externalCallbacks) {
const result = await callback(updatedNewData, context, request);
if (externalCallbackType === 'packagePolicyCreate') {
updatedNewData = NewPackagePolicySchema.validate(result);
} else if (externalCallbackType === 'packagePolicyUpdate') {
updatedNewData = UpdatePackagePolicySchema.validate(result);
}
}
newData = updatedNewData;
}
return newData;
}
}
function assignStreamIdToInput(packagePolicyId: string, input: NewPackagePolicyInput) {
return {
...input,
streams: input.streams.map((stream) => {
return { ...stream, id: `${input.type}-${stream.data_stream.dataset}-${packagePolicyId}` };
}),
};
}
async function _compilePackagePolicyInput(
registryPkgInfo: RegistryPackage,
pkgInfo: PackageInfo,
input: PackagePolicyInput
) {
if (!input.enabled || !pkgInfo.policy_templates?.[0].inputs) {
return undefined;
}
const packageInputs = pkgInfo.policy_templates[0].inputs;
const packageInput = packageInputs.find((pkgInput) => pkgInput.type === input.type);
if (!packageInput) {
throw new Error(`Input template not found, unable to find input type ${input.type}`);
}
if (!packageInput.template_path) {
return undefined;
}
const [pkgInputTemplate] = await getAssetsData(registryPkgInfo, (path: string) =>
path.endsWith(`/agent/input/${packageInput.template_path!}`)
);
if (!pkgInputTemplate || !pkgInputTemplate.buffer) {
throw new Error(`Unable to load input template at /agent/input/${packageInput.template_path!}`);
}
return compileTemplate(
// Populate template variables from input vars
Object.assign({}, input.vars),
pkgInputTemplate.buffer.toString()
);
}
async function _compilePackageStreams(
registryPkgInfo: RegistryPackage,
pkgInfo: PackageInfo,
input: PackagePolicyInput
) {
const streamsPromises = input.streams.map((stream) =>
_compilePackageStream(registryPkgInfo, pkgInfo, input, stream)
);
return await Promise.all(streamsPromises);
}
async function _compilePackageStream(
registryPkgInfo: RegistryPackage,
pkgInfo: PackageInfo,
input: PackagePolicyInput,
stream: PackagePolicyInputStream
) {
if (!stream.enabled) {
return { ...stream, compiled_stream: undefined };
}
const datasetPath = getDataset(stream.data_stream.dataset);
const packageDataStreams = pkgInfo.data_streams;
if (!packageDataStreams) {
throw new Error('Stream template not found, no data streams');
}
const packageDataStream = packageDataStreams.find(
(pkgDataStream) => pkgDataStream.dataset === stream.data_stream.dataset
);
if (!packageDataStream) {
throw new Error(`Stream template not found, unable to find dataset ${datasetPath}`);
}
const streamFromPkg = (packageDataStream.streams || []).find(
(pkgStream) => pkgStream.input === input.type
);
if (!streamFromPkg) {
throw new Error(`Stream template not found, unable to find stream for input ${input.type}`);
}
if (!streamFromPkg.template_path) {
throw new Error(`Stream template path not found for dataset ${datasetPath}`);
}
const [pkgStreamTemplate] = await getAssetsData(
registryPkgInfo,
(path: string) => path.endsWith(streamFromPkg.template_path),
datasetPath
);
if (!pkgStreamTemplate || !pkgStreamTemplate.buffer) {
throw new Error(
`Unable to load stream template ${streamFromPkg.template_path} for dataset ${datasetPath}`
);
}
const yaml = compileTemplate(
// Populate template variables from input vars and stream vars
Object.assign({}, input.vars, stream.vars),
pkgStreamTemplate.buffer.toString()
);
stream.compiled_stream = yaml;
return { ...stream };
}
export type PackagePolicyServiceInterface = PackagePolicyService;
export const packagePolicyService = new PackagePolicyService();