-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
index.ts
599 lines (514 loc) · 17.8 KB
/
index.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
* 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 { URL } from 'url';
import mime from 'mime-types';
import semverGte from 'semver/functions/gte';
import type { Response } from 'node-fetch';
import type { Logger } from '@kbn/logging';
import type { ExtractedIntegrationFields } from '@kbn/fields-metadata-plugin/server';
import { splitPkgKey as split } from '../../../../common/services';
import { KibanaAssetType } from '../../../types';
import type {
AssetsGroupedByServiceByType,
CategorySummaryList,
RegistryPackage,
RegistrySearchResults,
GetCategoriesRequest,
GetPackagesRequest,
PackageVerificationResult,
ArchivePackage,
BundledPackage,
AssetsMap,
} from '../../../types';
import {
getPathParts,
setVerificationResult,
getPackageInfo,
setPackageInfo,
generatePackageInfoFromArchiveBuffer,
unpackBufferToAssetsMap,
getVerificationResult,
} from '../archive';
import { streamToBuffer, streamToString } from '../streams';
import { appContextService } from '../..';
import {
PackageNotFoundError,
RegistryResponseError,
PackageFailedVerificationError,
PackageUnsupportedMediaTypeError,
ArchiveNotFoundError,
} from '../../../errors';
import { getBundledPackageByName } from '../packages/bundled_packages';
import { resolveDataStreamFields, resolveDataStreamsMap, withPackageSpan } from '../packages/utils';
import { verifyPackageArchiveSignature } from '../packages/package_verification';
import type { ArchiveIterator } from '../../../../common/types';
import { airGappedUtils } from '../airgapped';
import { fetchUrl, getResponse, getResponseStream } from './requests';
import { getRegistryUrl } from './registry_url';
export const splitPkgKey = split;
export const pkgToPkgKey = ({ name, version }: { name: string; version: string }) =>
`${name}-${version}`;
export async function fetchList(
params?: GetPackagesRequest['query']
): Promise<RegistrySearchResults> {
if (airGappedUtils().shouldSkipRegistryRequests) {
appContextService
.getLogger()
.debug(
'fetchList: isAirGapped enabled and no registryUrl or RegistryProxyUrl configured, skipping registry requests'
);
return [];
}
const registryUrl = getRegistryUrl();
const url = new URL(`${registryUrl}/search`);
if (params) {
if (params.category) {
url.searchParams.set('category', params.category);
}
if (params.prerelease) {
url.searchParams.set('prerelease', params.prerelease.toString());
}
}
setConstraints(url);
return fetchUrl(url.toString()).then(JSON.parse);
}
export interface FetchFindLatestPackageOptions {
ignoreConstraints?: boolean;
prerelease?: boolean;
}
async function _fetchFindLatestPackage(
packageName: string,
options?: FetchFindLatestPackageOptions
): Promise<RegistryPackage | BundledPackage | null> {
return withPackageSpan(`Find latest package ${packageName}`, async () => {
const logger = appContextService.getLogger();
const { ignoreConstraints = false, prerelease = false } = options ?? {};
const bundledPackage = await getBundledPackageByName(packageName);
const registryUrl = getRegistryUrl();
const url = new URL(`${registryUrl}/search?package=${packageName}&prerelease=${prerelease}`);
if (!ignoreConstraints) {
setConstraints(url);
}
try {
if (!airGappedUtils().shouldSkipRegistryRequests) {
const res = await fetchUrl(url.toString(), 1);
const searchResults: RegistryPackage[] = JSON.parse(res);
const latestPackageFromRegistry = searchResults[0] ?? null;
if (
bundledPackage &&
semverGte(bundledPackage.version, latestPackageFromRegistry.version)
) {
return bundledPackage;
}
return latestPackageFromRegistry;
} else if (airGappedUtils().shouldSkipRegistryRequests && bundledPackage) {
return bundledPackage;
} else {
return null;
}
} catch (error) {
logger.error(
`Failed to fetch latest version of ${packageName} from registry: ${error.message}`
);
// Fall back to the bundled version of the package if it exists
if (bundledPackage) {
return bundledPackage;
}
// Otherwise, return null and allow callers to determine whether they'll consider this an error or not
return null;
}
});
}
export async function fetchFindLatestPackageOrThrow(
packageName: string,
options?: FetchFindLatestPackageOptions
) {
const latestPackage = await _fetchFindLatestPackage(packageName, options);
if (!latestPackage) {
throw new PackageNotFoundError(`[${packageName}] package not found in registry`);
}
return latestPackage;
}
export async function fetchFindLatestPackageOrUndefined(
packageName: string,
options?: FetchFindLatestPackageOptions
) {
const logger = appContextService.getLogger();
try {
const latestPackage = await _fetchFindLatestPackage(packageName, options);
if (!latestPackage) {
return undefined;
}
return latestPackage;
} catch (error) {
logger.warn(`Error fetching latest package for ${packageName}: ${error.message}`);
return undefined;
}
}
export async function fetchInfo(
pkgName: string,
pkgVersion: string
): Promise<RegistryPackage | ArchivePackage> {
const registryUrl = getRegistryUrl();
// if isAirGapped config enabled and bundled package, use the bundled version
if (airGappedUtils().shouldSkipRegistryRequests) {
const archivePackage = await getBundledArchive(pkgName, pkgVersion);
if (archivePackage) {
return archivePackage.packageInfo;
}
}
try {
// Trailing slash avoids 301 redirect / extra hop
const res = await fetchUrl(`${registryUrl}/package/${pkgName}/${pkgVersion}/`).then(JSON.parse);
return res;
} catch (err) {
if (err instanceof RegistryResponseError && err.status === 404) {
const archivePackage = await getBundledArchive(pkgName, pkgVersion);
if (archivePackage) {
return archivePackage.packageInfo;
}
throw new PackageNotFoundError(`${pkgName}@${pkgVersion} not found`);
}
throw err;
}
}
export async function getBundledArchive(
pkgName: string,
pkgVersion: string
): Promise<{ paths: string[]; packageInfo: ArchivePackage } | undefined> {
// Check bundled packages in case the exact package being requested is available on disk
const bundledPackage = await getBundledPackageByName(pkgName);
if (bundledPackage && bundledPackage.version === pkgVersion) {
const archiveBuffer = await bundledPackage.getBuffer();
const archivePackage = await generatePackageInfoFromArchiveBuffer(
archiveBuffer,
'application/zip'
);
return archivePackage;
}
}
export async function getFile(
pkgName: string,
pkgVersion: string,
relPath: string
): Promise<Response | null> {
const filePath = `/package/${pkgName}/${pkgVersion}/${relPath}`;
return fetchFile(filePath);
}
export async function fetchFile(filePath: string): Promise<Response | null> {
if (airGappedUtils().shouldSkipRegistryRequests) {
appContextService
.getLogger()
.debug(
'fetchFile: isAirGapped enabled and no registryUrl or RegistryProxyUrl configured, skipping registry requests'
);
return null;
}
const registryUrl = getRegistryUrl();
return getResponse(`${registryUrl}${filePath}`);
}
function setKibanaVersion(url: URL) {
const config = appContextService.getConfig();
const disableVersionCheck =
(config?.developer?.disableRegistryVersionCheck ?? false) ||
config?.internal?.registry?.kibanaVersionCheckEnabled === false;
if (disableVersionCheck) {
return;
}
const kibanaVersion = appContextService.getKibanaVersion().split('-')[0]; // may be x.y.z-SNAPSHOT
if (kibanaVersion) {
url.searchParams.set('kibana.version', kibanaVersion);
}
}
function setSpecVersion(url: URL) {
const specMin = appContextService.getConfig()?.internal?.registry?.spec?.min;
const specMax = appContextService.getConfig()?.internal?.registry?.spec?.max;
if (specMin) {
url.searchParams.set('spec.min', specMin);
}
if (specMax) {
url.searchParams.set('spec.max', specMax);
}
}
function setCapabilities(url: URL) {
const capabilities = appContextService.getConfig()?.internal?.registry?.capabilities;
if (capabilities && capabilities.length > 0) {
url.searchParams.set('capabilities', capabilities.join(','));
}
}
function setConstraints(url: URL) {
setKibanaVersion(url);
setCapabilities(url);
setSpecVersion(url);
}
export async function fetchCategories(
params?: GetCategoriesRequest['query']
): Promise<CategorySummaryList> {
if (airGappedUtils().shouldSkipRegistryRequests) {
appContextService
.getLogger()
.debug(
'fetchCategories: isAirGapped enabled and no registryUrl or RegistryProxyUrl configured, skipping registry requests'
);
return [];
}
const registryUrl = getRegistryUrl();
const url = new URL(`${registryUrl}/categories`);
if (params) {
if (params.prerelease) {
url.searchParams.set('prerelease', params.prerelease.toString());
}
if (params.include_policy_templates) {
url.searchParams.set('include_policy_templates', params.include_policy_templates.toString());
}
}
setConstraints(url);
return fetchUrl(url.toString()).then(JSON.parse);
}
export async function getInfo(name: string, version: string) {
return withPackageSpan('Fetch package info', async () => {
const packageInfo = await fetchInfo(name, version);
return packageInfo as RegistryPackage;
});
}
// Check that the packageInfo exists in cache
// If not, retrieve it from the archive
async function getPackageInfoFromArchiveOrCache(
name: string,
version: string,
archiveBuffer: Buffer,
archivePath: string
): Promise<ArchivePackage> {
const cachedInfo = getPackageInfo({ name, version });
if (!cachedInfo) {
const { packageInfo } = await generatePackageInfoFromArchiveBuffer(
archiveBuffer,
ensureContentType(archivePath)
);
setPackageInfo({ packageInfo, name, version });
return packageInfo;
} else {
return cachedInfo;
}
}
export async function getPackage(
name: string,
version: string,
options?: { ignoreUnverified?: boolean; useStreaming?: boolean }
): Promise<{
paths: string[];
packageInfo: ArchivePackage;
assetsMap: AssetsMap;
archiveIterator: ArchiveIterator;
verificationResult?: PackageVerificationResult;
}> {
const logger = appContextService.getLogger();
const verifyPackage = appContextService.getExperimentalFeatures().packageVerification;
let packageInfo: ArchivePackage | undefined = getPackageInfo({ name, version });
let verificationResult: PackageVerificationResult | undefined = verifyPackage
? getVerificationResult({ name, version })
: undefined;
try {
const {
archiveBuffer,
archivePath,
verificationResult: latestVerificationResult,
} = await withPackageSpan('Fetch package archive from archive buffer', () =>
fetchArchiveBuffer({
pkgName: name,
pkgVersion: version,
shouldVerify: verifyPackage,
ignoreUnverified: options?.ignoreUnverified,
})
);
if (latestVerificationResult) {
verificationResult = latestVerificationResult;
setVerificationResult({ name, version }, latestVerificationResult);
}
const contentType = ensureContentType(archivePath);
const { paths, assetsMap, archiveIterator } = await unpackBufferToAssetsMap({
archiveBuffer,
contentType,
useStreaming: options?.useStreaming,
});
if (!packageInfo) {
packageInfo = await getPackageInfoFromArchiveOrCache(
name,
version,
archiveBuffer,
archivePath
);
}
return { paths, packageInfo, assetsMap, archiveIterator, verificationResult };
} catch (error) {
logger.warn(`getPackage failed with error: ${error}`);
throw error;
}
}
export async function getPackageFieldsMetadata(
params: { packageName: string; datasetName?: string },
options: { excludedFieldsAssets?: string[] } = {}
): Promise<ExtractedIntegrationFields> {
const { packageName, datasetName } = params;
const { excludedFieldsAssets = ['ecs.yml'] } = options;
// Attempt retrieving latest package name and version
const latestPackage = await fetchFindLatestPackageOrThrow(packageName);
const { name, version } = latestPackage;
try {
// Attempt retrieving latest package
const resolvedPackage = await getPackage(name, version);
// We need to collect all the available data streams for the package.
// In case a dataset is specified from the parameter, it will load the fields only for that specific dataset.
// As a fallback case, we'll try to read the fields for all the data streams in the package.
const dataStreamsMap = resolveDataStreamsMap(resolvedPackage.packageInfo.data_streams);
const assetsMap = resolvedPackage.assetsMap;
const dataStream = datasetName ? dataStreamsMap.get(datasetName) : null;
if (dataStream) {
// Resolve a single data stream fields when the `datasetName` parameter is specified
return resolveDataStreamFields({ dataStream, assetsMap, excludedFieldsAssets });
} else {
// Resolve and merge all the integration data streams fields otherwise
return [...dataStreamsMap.values()].reduce(
(packageDataStreamsFields, currentDataStream) =>
Object.assign(
packageDataStreamsFields,
resolveDataStreamFields({
dataStream: currentDataStream,
assetsMap,
excludedFieldsAssets,
})
),
{}
);
}
} catch (error) {
appContextService.getLogger().warn(`getPackageFieldsMetadata error: ${error}`);
throw error;
}
}
export function ensureContentType(archivePath: string) {
const contentType = mime.lookup(archivePath);
if (!contentType) {
throw new PackageUnsupportedMediaTypeError(
`Unknown compression format for '${archivePath}'. Please use .zip or .gz`
);
}
return contentType;
}
export async function fetchArchiveBuffer({
pkgName,
pkgVersion,
shouldVerify,
ignoreUnverified = false,
}: {
pkgName: string;
pkgVersion: string;
shouldVerify: boolean;
ignoreUnverified?: boolean;
}): Promise<{
archiveBuffer: Buffer;
archivePath: string;
verificationResult?: PackageVerificationResult;
}> {
const logger = appContextService.getLogger();
let { download: archivePath } = await getInfo(pkgName, pkgVersion);
// Bundled packages don't have a download path when they're installed, as they're
// ArchivePackage objects - so we fake the download path here instead
if (!archivePath) {
archivePath = `/epr/${pkgName}/${pkgName}-${pkgVersion}.zip`;
}
const registryUrl = getRegistryUrl();
const archiveUrl = `${registryUrl}${archivePath}`;
try {
const archiveBuffer = await getResponseStream(archiveUrl).then(streamToBuffer);
if (!archiveBuffer) {
logger.warn(`Archive Buffer not found`);
throw new ArchiveNotFoundError('Archive Buffer not found');
}
if (shouldVerify) {
const verificationResult = await verifyPackageArchiveSignature({
pkgName,
pkgVersion,
pkgArchiveBuffer: archiveBuffer,
logger,
});
if (verificationResult.verificationStatus === 'unverified' && !ignoreUnverified) {
throw new PackageFailedVerificationError(pkgName, pkgVersion);
}
return { archiveBuffer, archivePath, verificationResult };
}
return { archiveBuffer, archivePath };
} catch (error) {
logger.warn(`fetchArchiveBuffer failed with error: ${error}`);
throw error;
}
}
export async function getPackageArchiveSignatureOrUndefined({
pkgName,
pkgVersion,
logger,
}: {
pkgName: string;
pkgVersion: string;
logger: Logger;
}): Promise<string | undefined> {
const { signature_path: signaturePath } = await getInfo(pkgName, pkgVersion);
if (!signaturePath) {
logger.debug(
`Package ${pkgName}-${pkgVersion} does not have a signature_path, verification will not be possible.`
);
return undefined;
}
try {
const res = await fetchFile(signaturePath);
if (res?.body) return streamToString(res.body);
return undefined;
} catch (e) {
logger.error(`Error retrieving package signature at '${signaturePath}' : ${e}`);
return undefined;
}
}
export function groupPathsByService(paths: string[]): AssetsGroupedByServiceByType {
const kibanaAssetTypes = Object.values<string>(KibanaAssetType);
// ASK: best way, if any, to avoid `any`?
const assets = paths.reduce((map: any, path) => {
const parts = getPathParts(path.replace(/^\/package\//, ''));
if (
(parts.service === 'kibana' && kibanaAssetTypes.includes(parts.type)) ||
parts.service === 'elasticsearch'
) {
if (!map[parts.service]) map[parts.service] = {};
if (!map[parts.service][parts.type]) map[parts.service][parts.type] = [];
map[parts.service][parts.type].push(parts);
}
return map;
}, {});
return {
kibana: assets.kibana,
elasticsearch: assets.elasticsearch,
};
}
export function getNoticePath(paths: string[]): string | undefined {
for (const path of paths) {
const parts = getPathParts(path.replace(/^\/package\//, ''));
if (parts.type === 'notice') {
const { pkgName, pkgVersion } = splitPkgKey(parts.pkgkey);
return `/package/${pkgName}/${pkgVersion}/${parts.file}`;
}
}
return undefined;
}
export function getLicensePath(paths: string[]): string | undefined {
for (const path of paths) {
const parts = getPathParts(path.replace(/^\/package\//, ''));
if (parts.type === 'license') {
const { pkgName, pkgVersion } = splitPkgKey(parts.pkgkey);
return `/package/${pkgName}/${pkgVersion}/${parts.file}`;
}
}
return undefined;
}