[EDR Workflows][Osquery][Cypress] Fallback for KibanaStatus error response when fetching kibana version (#218240)

This PR fixes an issue that can cause test execution to fail when `await
kbnClient.status.get()` doesn't return the Kibana version. The fallback
now uses the version from `package.json` in that case.

Example failure:
https://buildkite.com/elastic/kibana-on-merge/builds/66520#0196344e-f27e-4ab8-9bf7-f041b94c665d/268-3998

---------

Co-authored-by: Patryk Kopyciński <contact@patrykkopycinski.com>
Co-authored-by: Tiago Costa <tiago.costa@elastic.co>
This commit is contained in:
Konrad Szwarc 2025-04-16 10:43:10 +02:00 committed by GitHub
parent 1d430d4d35
commit ce10318ef3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 6 deletions

View file

@ -41,7 +41,8 @@ export class AgentManager extends Manager {
this.log.info(chalk.bold('Setting up Agent'));
const artifact = `docker.elastic.co/elastic-agent/elastic-agent:${await getLatestAvailableAgentVersion(
this.kbnClient
this.kbnClient,
this.log
)}`;
this.log.indent(4, () => this.log.info(`Image: ${artifact}`));
const containerName = generateRandomString(12);

View file

@ -26,7 +26,7 @@ export class FleetManager extends Manager {
}
public async setup(): Promise<void> {
const version = await getLatestAvailableAgentVersion(this.kbnClient);
const version = await getLatestAvailableAgentVersion(this.kbnClient, this.log);
this.fleetServer = await startFleetServer({
kbnClient: this.kbnClient,
logger: this.log,

View file

@ -9,6 +9,8 @@ import axios from 'axios';
import semver from 'semver';
import { map } from 'lodash';
import { PackagePolicy, CreatePackagePolicyResponse, API_VERSIONS } from '@kbn/fleet-plugin/common';
// @ts-expect-error we have to check types with "allowJs: false" for now, causing this import to fail
import { kibanaPackageJson } from '@kbn/repo-info';
import { KbnClient } from '@kbn/test';
import {
GetEnrollmentAPIKeysResponse,
@ -140,10 +142,23 @@ const isValidArtifactVersion = (version: string) => !!version.match(/^\d+\.\d+\.
* Returns the Agent version that is available for install (will check `artifacts-api.elastic.co/v1/versions`)
* that is equal to or less than `maxVersion`.
* @param kbnClient
* @param log
*/
export const getLatestAvailableAgentVersion = async (kbnClient: KbnClient): Promise<string> => {
const kbnStatus = await kbnClient.status.get();
export const getLatestAvailableAgentVersion = async (
kbnClient: KbnClient,
log: ToolingLog
): Promise<string> => {
let currentVersion: string;
try {
const kbnStatus = await kbnClient.status.get();
currentVersion = kbnStatus.version.number;
} catch {
log.warning(chalk.bold('Failed to get Kibana version, using package.json version'));
currentVersion = kibanaPackageJson.version;
}
const agentVersions = await pRetry(
async () => {
const response = await axios.get('https://artifacts-api.elastic.co/v1/versions');
@ -157,9 +172,15 @@ export const getLatestAvailableAgentVersion = async (kbnClient: KbnClient): Prom
}
).catch(() => null);
if (!agentVersions) {
log.warning(
chalk.bold('Failed to get agent versions from artifacts-api, using package.json version')
);
}
const version = agentVersions
? semver.maxSatisfying(agentVersions, `<=${kbnStatus.version.number}`)
: kbnStatus.version.number;
? semver.maxSatisfying(agentVersions, `<=${currentVersion}`)
: currentVersion;
return `${version}-SNAPSHOT`;
};