Skip to content

Commit

Permalink
[APM] Use bundled version of APM integration (#141624)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Sep 26, 2022
1 parent 4ab4b24 commit c17a468
Showing 1 changed file with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import fetch from 'node-fetch';
import Semver from 'semver';
import { Logger } from '../../utils/create_logger';

export class ApmSynthtraceKibanaClient {
Expand Down Expand Up @@ -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}',
});

Expand All @@ -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',
};
}

0 comments on commit c17a468

Please sign in to comment.