[APM] Use bundled version of APM integration (#141624)

This commit is contained in:
Søren Louv-Jansen 2022-09-26 12:48:39 +02:00 committed by GitHub
parent 4ab4b247e6
commit c17a468cf7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,7 +7,6 @@
*/
import fetch from 'node-fetch';
import Semver from 'semver';
import { Logger } from '../../utils/create_logger';
export class ApmSynthtraceKibanaClient {
@ -53,31 +52,33 @@ export class ApmSynthtraceKibanaClient {
return kibanaUrl;
});
}
async fetchLatestApmPackageVersion(currentKibanaVersion: string) {
const url = `https://epr-snapshot.elastic.co/search?package=apm&prerelease=true&all=true&kibana.version=${currentKibanaVersion}`;
const response = await fetch(url, { method: 'GET' });
const json = (await response.json()) as Array<{ version: string }>;
const packageVersions = (json ?? []).map((item) => item.version).sort(Semver.rcompare);
const validPackageVersions = packageVersions.filter((v) => Semver.valid(v));
const bestMatch = validPackageVersions[0];
if (!bestMatch) {
throw new Error(
`None of the available APM package versions matches the current Kibana version (${currentKibanaVersion}). The latest available version is ${packageVersions[0]}. This can happen if the Kibana version was recently bumped, and no matching APM package was released. Reach out to the fleet team if this persists.`
);
}
return bestMatch;
async fetchLatestApmPackageVersion(
kibanaUrl: string,
version: string,
username: string,
password: string
) {
const url = `${kibanaUrl}/api/fleet/epm/packages/apm`;
const response = await fetch(url, {
method: 'GET',
headers: kibanaHeaders(username, password),
});
const json = (await response.json()) as { item: { latestVersion: string } };
const { latestVersion } = json.item;
return latestVersion;
}
async installApmPackage(kibanaUrl: string, version: string, username: string, password: string) {
const packageVersion = await this.fetchLatestApmPackageVersion(version);
const packageVersion = await this.fetchLatestApmPackageVersion(
kibanaUrl,
version,
username,
password
);
const response = await fetch(`${kibanaUrl}/api/fleet/epm/packages/apm/${packageVersion}`, {
method: 'POST',
headers: {
Authorization: 'Basic ' + Buffer.from(username + ':' + password).toString('base64'),
Accept: 'application/json',
'Content-Type': 'application/json',
'kbn-xsrf': 'kibana',
},
headers: kibanaHeaders(username, password),
body: '{"force":true}',
});
@ -93,3 +94,12 @@ export class ApmSynthtraceKibanaClient {
} else this.logger.error(responseJson);
}
}
function kibanaHeaders(username: string, password: string) {
return {
Authorization: 'Basic ' + Buffer.from(username + ':' + password).toString('base64'),
Accept: 'application/json',
'Content-Type': 'application/json',
'kbn-xsrf': 'kibana',
};
}