-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
_install_package.ts
399 lines (375 loc) · 15.1 KB
/
_install_package.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
/*
* 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,
Logger,
SavedObject,
SavedObjectsClientContract,
ISavedObjectsImporter,
} from '@kbn/core/server';
import { SavedObjectsErrorHelpers } from '@kbn/core/server';
import type { IAssignmentService, ITagsClient } from '@kbn/saved-objects-tagging-plugin/server';
import type { HTTPAuthorizationHeader } from '../../../../common/http_authorization_header';
import type { PackageInstallContext } from '../../../../common/types';
import { getNormalizedDataStreams } from '../../../../common/services';
import {
MAX_TIME_COMPLETE_INSTALL,
ASSETS_SAVED_OBJECT_TYPE,
PACKAGE_POLICY_SAVED_OBJECT_TYPE,
SO_SEARCH_LIMIT,
} from '../../../../common/constants';
import { PACKAGES_SAVED_OBJECT_TYPE, FLEET_INSTALL_FORMAT_VERSION } from '../../../constants';
import type {
AssetReference,
Installation,
InstallType,
InstallSource,
PackageAssetReference,
PackageVerificationResult,
IndexTemplateEntry,
} from '../../../types';
import { removeLegacyTemplates } from '../elasticsearch/template/remove_legacy';
import { isTopLevelPipeline, deletePreviousPipelines } from '../elasticsearch/ingest_pipeline';
import { installILMPolicy } from '../elasticsearch/ilm/install';
import { installKibanaAssetsAndReferences } from '../kibana/assets/install';
import { updateCurrentWriteIndices } from '../elasticsearch/template/template';
import { installTransforms } from '../elasticsearch/transform/install';
import { installMlModel } from '../elasticsearch/ml_model';
import { installIlmForDataStream } from '../elasticsearch/datastream_ilm/install';
import { saveArchiveEntriesFromAssetsMap } from '../archive/storage';
import { ConcurrentInstallOperationError } from '../../../errors';
import { appContextService, packagePolicyService } from '../..';
import { auditLoggingService } from '../../audit_logging';
import {
createInstallation,
restartInstallation,
installIndexTemplatesAndPipelines,
} from './install';
import { withPackageSpan } from './utils';
import { clearLatestFailedAttempts } from './install_errors_helpers';
// this is only exported for testing
// use a leading underscore to indicate it's not the supported path
// only the more explicit `installPackage*` functions should be used
export async function _installPackage({
savedObjectsClient,
savedObjectsImporter,
savedObjectTagAssignmentService,
savedObjectTagClient,
esClient,
logger,
installedPkg,
packageInstallContext,
installType,
installSource,
spaceId,
force,
verificationResult,
authorizationHeader,
ignoreMappingUpdateErrors,
skipDataStreamRollover,
}: {
savedObjectsClient: SavedObjectsClientContract;
savedObjectsImporter: Pick<ISavedObjectsImporter, 'import' | 'resolveImportErrors'>;
savedObjectTagAssignmentService: IAssignmentService;
savedObjectTagClient: ITagsClient;
esClient: ElasticsearchClient;
logger: Logger;
installedPkg?: SavedObject<Installation>;
packageInstallContext: PackageInstallContext;
installType: InstallType;
installSource: InstallSource;
spaceId: string;
force?: boolean;
verificationResult?: PackageVerificationResult;
authorizationHeader?: HTTPAuthorizationHeader | null;
ignoreMappingUpdateErrors?: boolean;
skipDataStreamRollover?: boolean;
}): Promise<AssetReference[]> {
const { packageInfo, paths } = packageInstallContext;
const { name: pkgName, version: pkgVersion, title: pkgTitle } = packageInfo;
try {
// if some installation already exists
if (installedPkg) {
const isStatusInstalling = installedPkg.attributes.install_status === 'installing';
const hasExceededTimeout =
Date.now() - Date.parse(installedPkg.attributes.install_started_at) <
MAX_TIME_COMPLETE_INSTALL;
logger.debug(`Package install - Install status ${installedPkg.attributes.install_status}`);
// if the installation is currently running, don't try to install
// instead, only return already installed assets
if (isStatusInstalling && hasExceededTimeout) {
// If this is a forced installation, ignore the timeout and restart the installation anyway
logger.debug(`Package install - Installation is running and has exceeded timeout`);
if (force) {
logger.debug(`Package install - Forced installation, restarting`);
await restartInstallation({
savedObjectsClient,
pkgName,
pkgVersion,
installSource,
verificationResult,
});
} else {
throw new ConcurrentInstallOperationError(
`Concurrent installation or upgrade of ${pkgName || 'unknown'}-${
pkgVersion || 'unknown'
} detected, aborting.`
);
}
} else {
// if no installation is running, or the installation has been running longer than MAX_TIME_COMPLETE_INSTALL
// (it might be stuck) update the saved object and proceed
logger.debug(
`Package install - no installation running or the installation has been running longer than ${MAX_TIME_COMPLETE_INSTALL}, restarting`
);
await restartInstallation({
savedObjectsClient,
pkgName,
pkgVersion,
installSource,
verificationResult,
});
}
} else {
logger.debug(`Package install - Create installation`);
await createInstallation({
savedObjectsClient,
packageInfo,
installSource,
spaceId,
verificationResult,
});
}
logger.debug(`Package install - Installing Kibana assets`);
const kibanaAssetPromise = withPackageSpan('Install Kibana assets', () =>
installKibanaAssetsAndReferences({
savedObjectsClient,
savedObjectsImporter,
savedObjectTagAssignmentService,
savedObjectTagClient,
pkgName,
pkgTitle,
packageInstallContext,
paths,
installedPkg,
logger,
spaceId,
assetTags: packageInfo?.asset_tags,
})
);
// Necessary to avoid async promise rejection warning
// See https://stackoverflow.com/questions/40920179/should-i-refrain-from-handling-promise-rejection-asynchronously
kibanaAssetPromise.catch(() => {});
// Use a shared array that is updated by each operation. This allows each operation to accurately update the
// installation object with it's references without requiring a refresh of the SO index on each update (faster).
let esReferences = installedPkg?.attributes.installed_es ?? [];
// the rest of the installation must happen in sequential order
// currently only the base package has an ILM policy
// at some point ILM policies can be installed/modified
// per data stream and we should then save them
const isILMPoliciesDisabled =
appContextService.getConfig()?.internal?.disableILMPolicies ?? false;
if (!isILMPoliciesDisabled) {
esReferences = await withPackageSpan('Install ILM policies', () =>
installILMPolicy(packageInstallContext, esClient, savedObjectsClient, logger, esReferences)
);
logger.debug(`Package install - Installing Data Stream ILM policies`);
({ esReferences } = await withPackageSpan('Install Data Stream ILM policies', () =>
installIlmForDataStream(
packageInstallContext,
esClient,
savedObjectsClient,
logger,
esReferences
)
));
}
// installs ml models
logger.debug(`Package install - installing ML models`);
esReferences = await withPackageSpan('Install ML models', () =>
installMlModel(packageInstallContext, esClient, savedObjectsClient, logger, esReferences)
);
let indexTemplates: IndexTemplateEntry[] = [];
if (packageInfo.type === 'integration') {
logger.debug(
`Package install - Installing index templates and pipelines, packageInfo.type ${packageInfo.type}`
);
const { installedTemplates, esReferences: templateEsReferences } =
await installIndexTemplatesAndPipelines({
installedPkg: installedPkg ? installedPkg.attributes : undefined,
packageInstallContext,
esClient,
savedObjectsClient,
logger,
esReferences,
});
esReferences = templateEsReferences;
indexTemplates = installedTemplates;
}
if (packageInfo.type === 'input' && installedPkg) {
// input packages create their data streams during package policy creation
// we must use installed_es to infer which streams exist first then
// we can install the new index templates
logger.debug(`Package install - packageInfo.type ${packageInfo.type}`);
const dataStreamNames = installedPkg.attributes.installed_es
.filter((ref) => ref.type === 'index_template')
// index templates are named {type}-{dataset}, remove everything before first hyphen
.map((ref) => ref.id.replace(/^[^-]+-/, ''));
const dataStreams = dataStreamNames.flatMap((dataStreamName) =>
getNormalizedDataStreams(packageInfo, dataStreamName)
);
if (dataStreams.length) {
logger.debug(
`Package install - installing index templates and pipelines with datastreams length ${dataStreams.length}`
);
const { installedTemplates, esReferences: templateEsReferences } =
await installIndexTemplatesAndPipelines({
installedPkg: installedPkg ? installedPkg.attributes : undefined,
packageInstallContext,
esClient,
savedObjectsClient,
logger,
esReferences,
onlyForDataStreams: dataStreams,
});
esReferences = templateEsReferences;
indexTemplates = installedTemplates;
}
}
try {
logger.debug(`Package install - Removing legacy templates`);
await removeLegacyTemplates({ packageInfo, esClient, logger });
} catch (e) {
logger.warn(`Error removing legacy templates: ${e.message}`);
}
// update current backing indices of each data stream
logger.debug(`Package install - Updating backing indices of each data stream`);
await withPackageSpan('Update write indices', () =>
updateCurrentWriteIndices(esClient, logger, indexTemplates, {
ignoreMappingUpdateErrors,
skipDataStreamRollover,
})
);
logger.debug(`Package install - Installing transforms`);
({ esReferences } = await withPackageSpan('Install transforms', () =>
installTransforms({
packageInstallContext,
esClient,
savedObjectsClient,
logger,
esReferences,
force,
authorizationHeader,
})
));
// If this is an update or retrying an update, delete the previous version's pipelines
// Top-level pipeline assets will not be removed on upgrade as of ml model package addition which requires previous
// assets to remain installed. This is a temporary solution - more robust solution tracked here https://github.com/elastic/kibana/issues/115035
if (
paths.filter((path) => isTopLevelPipeline(path)).length === 0 &&
(installType === 'update' || installType === 'reupdate') &&
installedPkg
) {
logger.debug(
`Package install - installType ${installType} Deleting previous ingest pipelines`
);
esReferences = await withPackageSpan('Delete previous ingest pipelines', () =>
deletePreviousPipelines(
esClient,
savedObjectsClient,
pkgName,
installedPkg!.attributes.version,
esReferences
)
);
}
// pipelines from a different version may have installed during a failed update
if (installType === 'rollback' && installedPkg) {
logger.debug(
`Package install - installType ${installType} Deleting previous ingest pipelines`
);
esReferences = await withPackageSpan('Delete previous ingest pipelines', () =>
deletePreviousPipelines(
esClient,
savedObjectsClient,
pkgName,
installedPkg!.attributes.install_version,
esReferences
)
);
}
const installedKibanaAssetsRefs = await kibanaAssetPromise;
logger.debug(`Package install - Updating archive entries`);
const packageAssetResults = await withPackageSpan('Update archive entries', () =>
saveArchiveEntriesFromAssetsMap({
savedObjectsClient,
assetsMap: packageInstallContext.assetsMap,
paths: packageInstallContext.paths,
packageInfo,
installSource,
})
);
const packageAssetRefs: PackageAssetReference[] = packageAssetResults.saved_objects.map(
(result) => ({
id: result.id,
type: ASSETS_SAVED_OBJECT_TYPE,
})
);
auditLoggingService.writeCustomSoAuditLog({
action: 'update',
id: pkgName,
savedObjectType: PACKAGES_SAVED_OBJECT_TYPE,
});
logger.debug(`Package install - Updating install status`);
await withPackageSpan('Update install status', () =>
savedObjectsClient.update<Installation>(PACKAGES_SAVED_OBJECT_TYPE, pkgName, {
version: pkgVersion,
install_version: pkgVersion,
install_status: 'installed',
package_assets: packageAssetRefs,
install_format_schema_version: FLEET_INSTALL_FORMAT_VERSION,
latest_install_failed_attempts: clearLatestFailedAttempts(
pkgVersion,
installedPkg?.attributes.latest_install_failed_attempts ?? []
),
})
);
// Need to refetch the installation again to retrieve all the attributes
const updatedPackage = await savedObjectsClient.get<Installation>(
PACKAGES_SAVED_OBJECT_TYPE,
pkgName
);
logger.debug(`Package install - Install status ${updatedPackage?.attributes?.install_status}`);
// If the package is flagged with the `keep_policies_up_to_date` flag, upgrade its
// associated package policies after installation
if (updatedPackage.attributes.keep_policies_up_to_date) {
await withPackageSpan('Upgrade package policies', async () => {
const policyIdsToUpgrade = await packagePolicyService.listIds(savedObjectsClient, {
page: 1,
perPage: SO_SEARCH_LIMIT,
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name:${pkgName}`,
});
logger.debug(
`Package install - Package is flagged with keep_policies_up_to_date, upgrading its associated package policies ${policyIdsToUpgrade}`
);
await packagePolicyService.upgrade(savedObjectsClient, esClient, policyIdsToUpgrade.items);
});
}
logger.debug(`Package install - Installation complete`);
return [...installedKibanaAssetsRefs, ...esReferences];
} catch (err) {
if (SavedObjectsErrorHelpers.isConflictError(err)) {
throw new ConcurrentInstallOperationError(
`Concurrent installation or upgrade of ${pkgName || 'unknown'}-${
pkgVersion || 'unknown'
} detected, aborting. Original error: ${err.message}`
);
} else {
throw err;
}
}
}