From 7cfe8ba18c34f90390c17b9f6e1b6546a09c9cdd Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Mon, 6 Jul 2020 15:09:51 -0400 Subject: [PATCH] [Ingest Manager] Fix agent version check to work with SNAPSHOT versions (#70796) (#70838) --- .../server/services/agents/enroll.test.ts | 66 +++++++++++++++++++ .../server/services/agents/enroll.ts | 28 ++++++-- 2 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 x-pack/plugins/ingest_manager/server/services/agents/enroll.test.ts diff --git a/x-pack/plugins/ingest_manager/server/services/agents/enroll.test.ts b/x-pack/plugins/ingest_manager/server/services/agents/enroll.test.ts new file mode 100644 index 0000000000000..764564cfa49f5 --- /dev/null +++ b/x-pack/plugins/ingest_manager/server/services/agents/enroll.test.ts @@ -0,0 +1,66 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { validateAgentVersion } from './enroll'; +import { appContextService } from '../app_context'; +import { IngestManagerAppContext } from '../../plugin'; + +describe('validateAgentVersion', () => { + it('should throw with agent > kibana version', () => { + appContextService.start(({ + kibanaVersion: '8.0.0', + } as unknown) as IngestManagerAppContext); + expect(() => + validateAgentVersion({ + local: { elastic: { agent: { version: '8.8.0' } } }, + userProvided: {}, + }) + ).toThrowError(/Agent version is not compatible with kibana version/); + }); + it('should work with agent < kibana version', () => { + appContextService.start(({ + kibanaVersion: '8.0.0', + } as unknown) as IngestManagerAppContext); + validateAgentVersion({ local: { elastic: { agent: { version: '7.8.0' } } }, userProvided: {} }); + }); + + it('should work with agent = kibana version', () => { + appContextService.start(({ + kibanaVersion: '8.0.0', + } as unknown) as IngestManagerAppContext); + validateAgentVersion({ local: { elastic: { agent: { version: '8.0.0' } } }, userProvided: {} }); + }); + + it('should work with SNAPSHOT version', () => { + appContextService.start(({ + kibanaVersion: '8.0.0-SNAPSHOT', + } as unknown) as IngestManagerAppContext); + validateAgentVersion({ + local: { elastic: { agent: { version: '8.0.0-SNAPSHOT' } } }, + userProvided: {}, + }); + }); + + it('should work with a agent using SNAPSHOT version', () => { + appContextService.start(({ + kibanaVersion: '7.8.0', + } as unknown) as IngestManagerAppContext); + validateAgentVersion({ + local: { elastic: { agent: { version: '7.8.0-SNAPSHOT' } } }, + userProvided: {}, + }); + }); + + it('should work with a kibana using SNAPSHOT version', () => { + appContextService.start(({ + kibanaVersion: '7.8.0-SNAPSHOT', + } as unknown) as IngestManagerAppContext); + validateAgentVersion({ + local: { elastic: { agent: { version: '7.8.0' } } }, + userProvided: {}, + }); + }); +}); diff --git a/x-pack/plugins/ingest_manager/server/services/agents/enroll.ts b/x-pack/plugins/ingest_manager/server/services/agents/enroll.ts index bf15815e6ae41..b63b1c13e4df9 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/enroll.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/enroll.ts @@ -20,11 +20,7 @@ export async function enroll( metadata?: { local: any; userProvided: any }, sharedId?: string ): Promise { - const kibanaVersion = appContextService.getKibanaVersion(); - const version: string | undefined = metadata?.local?.elastic?.agent?.version; - if (!version || semver.compare(version, kibanaVersion) === 1) { - throw Boom.badRequest('Agent version is not compatible with kibana version'); - } + validateAgentVersion(metadata); const existingAgent = sharedId ? await getAgentBySharedId(soClient, sharedId) : null; @@ -92,3 +88,25 @@ async function getAgentBySharedId(soClient: SavedObjectsClientContract, sharedId return null; } + +export function validateAgentVersion(metadata?: { local: any; userProvided: any }) { + const kibanaVersion = semver.parse(appContextService.getKibanaVersion()); + if (!kibanaVersion) { + throw Boom.badRequest('Kibana version is not set'); + } + const version = semver.parse(metadata?.local?.elastic?.agent?.version); + if (!version) { + throw Boom.badRequest('Agent version not provided in metadata.'); + } + + if (!version || !semver.lte(formatVersion(version), formatVersion(kibanaVersion))) { + throw Boom.badRequest('Agent version is not compatible with kibana version'); + } +} + +/** + * used to remove prelease from version as includePrerelease in not working as expected + */ +function formatVersion(version: semver.SemVer) { + return `${version.major}.${version.minor}.${version.patch}`; +}