-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathpreconfiguration.ts
435 lines (395 loc) · 15.6 KB
/
preconfiguration.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
/*
* 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 type { ElasticsearchClient, SavedObjectsClientContract } from 'src/core/server';
import { i18n } from '@kbn/i18n';
import { groupBy, omit, pick, isEqual } from 'lodash';
import { safeDump } from 'js-yaml';
import type {
NewPackagePolicy,
AgentPolicy,
Installation,
Output,
PreconfiguredAgentPolicy,
PreconfiguredPackage,
PreconfigurationError,
PreconfiguredOutput,
PackagePolicy,
} from '../../common';
import { SO_SEARCH_LIMIT, normalizeHostsForAgents } from '../../common';
import {
PRECONFIGURATION_DELETION_RECORD_SAVED_OBJECT_TYPE,
PRECONFIGURATION_LATEST_KEYWORD,
} from '../constants';
import { escapeSearchQueryPhrase } from './saved_object';
import { pkgToPkgKey } from './epm/registry';
import { getInstallation, getPackageInfo } from './epm/packages';
import { ensurePackagesCompletedInstall } from './epm/packages/install';
import { bulkInstallPackages } from './epm/packages/bulk_install_packages';
import { agentPolicyService, addPackageToAgentPolicy } from './agent_policy';
import type { InputsOverride } from './package_policy';
import { preconfigurePackageInputs, packagePolicyService } from './package_policy';
import { appContextService } from './app_context';
import type { UpgradeManagedPackagePoliciesResult } from './managed_package_policies';
import { upgradeManagedPackagePolicies } from './managed_package_policies';
import { outputService } from './output';
interface PreconfigurationResult {
policies: Array<{ id: string; updated_at: string }>;
packages: string[];
nonFatalErrors: Array<PreconfigurationError | UpgradeManagedPackagePoliciesResult>;
}
function isPreconfiguredOutputDifferentFromCurrent(
existingOutput: Output,
preconfiguredOutput: Partial<Output>
): boolean {
return (
existingOutput.is_default !== preconfiguredOutput.is_default ||
existingOutput.is_default_monitoring !== preconfiguredOutput.is_default_monitoring ||
existingOutput.name !== preconfiguredOutput.name ||
existingOutput.type !== preconfiguredOutput.type ||
(preconfiguredOutput.hosts &&
!isEqual(
existingOutput.hosts?.map(normalizeHostsForAgents),
preconfiguredOutput.hosts.map(normalizeHostsForAgents)
)) ||
existingOutput.ca_sha256 !== preconfiguredOutput.ca_sha256 ||
existingOutput.config_yaml !== preconfiguredOutput.config_yaml
);
}
export async function ensurePreconfiguredOutputs(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
outputs: PreconfiguredOutput[]
) {
const logger = appContextService.getLogger();
if (outputs.length === 0) {
return;
}
const existingOutputs = await outputService.bulkGet(
soClient,
outputs.map(({ id }) => id),
{ ignoreNotFound: true }
);
await Promise.all(
outputs.map(async (output) => {
const existingOutput = existingOutputs.find((o) => o.id === output.id);
const { id, config, ...outputData } = output;
const configYaml = config ? safeDump(config) : undefined;
const data = {
...outputData,
config_yaml: configYaml,
is_preconfigured: true,
};
if (!data.hosts || data.hosts.length === 0) {
data.hosts = outputService.getDefaultESHosts();
}
const isCreate = !existingOutput;
const isUpdateWithNewData =
existingOutput && isPreconfiguredOutputDifferentFromCurrent(existingOutput, data);
if (isCreate) {
logger.debug(`Creating output ${output.id}`);
await outputService.create(soClient, data, { id, fromPreconfiguration: true });
} else if (isUpdateWithNewData) {
logger.debug(`Updating output ${output.id}`);
await outputService.update(soClient, id, data, { fromPreconfiguration: true });
// Bump revision of all policies using that output
if (outputData.is_default || outputData.is_default_monitoring) {
await agentPolicyService.bumpAllAgentPolicies(soClient, esClient);
} else {
await agentPolicyService.bumpAllAgentPoliciesForOutput(soClient, esClient, id);
}
}
})
);
}
export async function cleanPreconfiguredOutputs(
soClient: SavedObjectsClientContract,
outputs: PreconfiguredOutput[]
) {
const existingPreconfiguredOutput = (await outputService.list(soClient)).items.filter(
(o) => o.is_preconfigured === true
);
const logger = appContextService.getLogger();
for (const output of existingPreconfiguredOutput) {
if (!outputs.find(({ id }) => output.id === id)) {
logger.info(`Deleting preconfigured output ${output.id}`);
await outputService.delete(soClient, output.id, { fromPreconfiguration: true });
}
}
}
export async function ensurePreconfiguredPackagesAndPolicies(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
policies: PreconfiguredAgentPolicy[] = [],
packages: PreconfiguredPackage[] = [],
defaultOutput: Output
): Promise<PreconfigurationResult> {
const logger = appContextService.getLogger();
// Validate configured packages to ensure there are no version conflicts
const packageNames = groupBy(packages, (pkg) => pkg.name);
const duplicatePackages = Object.entries(packageNames).filter(
([, versions]) => versions.length > 1
);
if (duplicatePackages.length) {
// List duplicate packages as a comma-separated list of <package-name>:<semver>
// If there are multiple packages with duplicate versions, separate them with semicolons, e.g
// package-a:1.0.0, package-a:2.0.0; package-b:1.0.0, package-b:2.0.0
const duplicateList = duplicatePackages
.map(([, versions]) => versions.map((v) => pkgToPkgKey(v)).join(', '))
.join('; ');
throw new Error(
i18n.translate('xpack.fleet.preconfiguration.duplicatePackageError', {
defaultMessage: 'Duplicate packages specified in configuration: {duplicateList}',
values: {
duplicateList,
},
})
);
}
// Preinstall packages specified in Kibana config
const preconfiguredPackages = await bulkInstallPackages({
savedObjectsClient: soClient,
esClient,
packagesToInstall: packages.map((pkg) =>
pkg.version === PRECONFIGURATION_LATEST_KEYWORD ? pkg.name : pkg
),
force: true, // Always force outdated packages to be installed if a later version isn't installed
});
const fulfilledPackages = [];
const rejectedPackages: PreconfigurationError[] = [];
for (let i = 0; i < preconfiguredPackages.length; i++) {
const packageResult = preconfiguredPackages[i];
if ('error' in packageResult) {
logger.warn(
`Failed installing package [${packages[i].name}] due to error: [${packageResult.error}]`
);
rejectedPackages.push({
package: { name: packages[i].name, version: packages[i].version },
error: packageResult.error,
});
} else {
fulfilledPackages.push(packageResult);
}
}
// Keeping this outside of the Promise.all because it introduces a race condition.
// If one of the required packages fails to install/upgrade it might get stuck in the installing state.
// On the next call to the /setup API, if there is a upgrade available for one of the required packages a race condition
// will occur between upgrading the package and reinstalling the previously failed package.
// By moving this outside of the Promise.all, the upgrade will occur first, and then we'll attempt to reinstall any
// packages that are stuck in the installing state.
await ensurePackagesCompletedInstall(soClient, esClient);
// Create policies specified in Kibana config
const preconfiguredPolicies = await Promise.allSettled(
policies.map(async (preconfiguredAgentPolicy) => {
if (preconfiguredAgentPolicy.id) {
// Check to see if a preconfigured policy with the same preconfiguration id was already deleted by the user
const preconfigurationId = preconfiguredAgentPolicy.id.toString();
const searchParams = {
searchFields: ['id'],
search: escapeSearchQueryPhrase(preconfigurationId),
};
const deletionRecords = await soClient.find({
type: PRECONFIGURATION_DELETION_RECORD_SAVED_OBJECT_TYPE,
...searchParams,
});
const wasDeleted = deletionRecords.total > 0;
if (wasDeleted) {
return { created: false, deleted: preconfigurationId };
}
} else if (
!preconfiguredAgentPolicy.is_default &&
!preconfiguredAgentPolicy.is_default_fleet_server
) {
throw new Error(
i18n.translate('xpack.fleet.preconfiguration.missingIDError', {
defaultMessage:
'{agentPolicyName} is missing an `id` field. `id` is required, except for policies marked is_default or is_default_fleet_server.',
values: { agentPolicyName: preconfiguredAgentPolicy.name },
})
);
}
const { created, policy } = await agentPolicyService.ensurePreconfiguredAgentPolicy(
soClient,
esClient,
omit(preconfiguredAgentPolicy, 'is_managed') // Don't add `is_managed` until the policy has been fully configured
);
if (!created) {
if (!policy?.is_managed) return { created, policy };
const { hasChanged, fields } = comparePreconfiguredPolicyToCurrent(
preconfiguredAgentPolicy,
policy
);
if (hasChanged) {
const updatedPolicy = await agentPolicyService.update(
soClient,
esClient,
String(preconfiguredAgentPolicy.id),
fields
);
return { created, policy: updatedPolicy };
}
return { created, policy };
}
return {
created,
policy,
shouldAddIsManagedFlag: preconfiguredAgentPolicy.is_managed,
};
})
);
const fulfilledPolicies = [];
const rejectedPolicies: PreconfigurationError[] = [];
for (let i = 0; i < preconfiguredPolicies.length; i++) {
const policyResult = preconfiguredPolicies[i];
if (policyResult.status === 'rejected') {
rejectedPolicies.push({
error: policyResult.reason as Error,
agentPolicy: { name: policies[i].name },
});
continue;
}
fulfilledPolicies.push(policyResult.value);
const { created, policy, shouldAddIsManagedFlag } = policyResult.value;
if (created || policies[i].is_managed) {
const preconfiguredAgentPolicy = policies[i];
const { package_policies: packagePolicies } = preconfiguredAgentPolicy;
const agentPolicyWithPackagePolicies = await agentPolicyService.get(
soClient,
policy!.id,
true
);
const installedPackagePolicies = await Promise.all(
packagePolicies.map(async ({ package: pkg, name, ...newPackagePolicy }) => {
const installedPackage = await getInstallation({
savedObjectsClient: soClient,
pkgName: pkg.name,
});
if (!installedPackage) {
const rejectedPackage = rejectedPackages.find((rp) => rp.package?.name === pkg.name);
if (rejectedPackage) {
throw new Error(
i18n.translate('xpack.fleet.preconfiguration.packageRejectedError', {
defaultMessage: `[{agentPolicyName}] could not be added. [{pkgName}] could not be installed due to error: [{errorMessage}]`,
values: {
agentPolicyName: preconfiguredAgentPolicy.name,
pkgName: pkg.name,
errorMessage: rejectedPackage.error.toString(),
},
})
);
}
throw new Error(
i18n.translate('xpack.fleet.preconfiguration.packageMissingError', {
defaultMessage:
'[{agentPolicyName}] could not be added. [{pkgName}] is not installed, add [{pkgName}] to [{packagesConfigValue}] or remove it from [{packagePolicyName}].',
values: {
agentPolicyName: preconfiguredAgentPolicy.name,
packagePolicyName: name,
pkgName: pkg.name,
packagesConfigValue: 'xpack.fleet.packages',
},
})
);
}
return { name, installedPackage, ...newPackagePolicy };
})
);
const packagePoliciesToAdd = installedPackagePolicies.filter((installablePackagePolicy) => {
return !(agentPolicyWithPackagePolicies?.package_policies as PackagePolicy[]).some(
(packagePolicy) => packagePolicy.name === installablePackagePolicy.name
);
});
await addPreconfiguredPolicyPackages(
soClient,
esClient,
policy!,
packagePoliciesToAdd!,
defaultOutput,
!created
);
// Add the is_managed flag after configuring package policies to avoid errors
if (shouldAddIsManagedFlag) {
await agentPolicyService.update(soClient, esClient, policy!.id, { is_managed: true });
}
}
}
// Handle automatic package policy upgrades for managed packages and package with
// the `keep_policies_up_to_date` setting enabled
const allPackagePolicyIds = await packagePolicyService.listIds(soClient, {
page: 1,
perPage: SO_SEARCH_LIMIT,
});
const packagePolicyUpgradeResults = await upgradeManagedPackagePolicies(
soClient,
esClient,
allPackagePolicyIds.items
);
return {
policies: fulfilledPolicies.map((p) =>
p.policy
? {
id: p.policy.id!,
updated_at: p.policy.updated_at,
}
: {
id: p.deleted!,
updated_at: i18n.translate('xpack.fleet.preconfiguration.policyDeleted', {
defaultMessage: 'Preconfigured policy {id} was deleted; skipping creation',
values: { id: p.deleted },
}),
}
),
packages: fulfilledPackages.map((pkg) => pkgToPkgKey(pkg)),
nonFatalErrors: [...rejectedPackages, ...rejectedPolicies, ...packagePolicyUpgradeResults],
};
}
export function comparePreconfiguredPolicyToCurrent(
policyFromConfig: PreconfiguredAgentPolicy,
currentPolicy: AgentPolicy
) {
// Namespace is omitted from being compared because even for managed policies, we still
// want users to be able to pick their own namespace: https://github.com/elastic/kibana/issues/110533
const configTopLevelFields = omit(policyFromConfig, 'package_policies', 'id', 'namespace');
const currentTopLevelFields = pick(currentPolicy, ...Object.keys(configTopLevelFields));
return {
hasChanged: !isEqual(configTopLevelFields, currentTopLevelFields),
fields: configTopLevelFields,
};
}
async function addPreconfiguredPolicyPackages(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
agentPolicy: AgentPolicy,
installedPackagePolicies: Array<
Partial<Omit<NewPackagePolicy, 'inputs'>> & {
name: string;
installedPackage: Installation;
inputs?: InputsOverride[];
}
>,
defaultOutput: Output,
bumpAgentPolicyRevison = false
) {
// Add packages synchronously to avoid overwriting
for (const { installedPackage, name, description, inputs } of installedPackagePolicies) {
const packageInfo = await getPackageInfo({
savedObjectsClient: soClient,
pkgName: installedPackage.name,
pkgVersion: installedPackage.version,
});
await addPackageToAgentPolicy(
soClient,
esClient,
installedPackage,
agentPolicy,
defaultOutput,
name,
description,
(policy) => preconfigurePackageInputs(policy, packageInfo, inputs),
bumpAgentPolicyRevison
);
}
}