Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ingest Manager] Fix agent version check to work with SNAPSHOT versions #70796

Merged
merged 2 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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: {},
});
});
});
30 changes: 25 additions & 5 deletions x-pack/plugins/ingest_manager/server/services/agents/enroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ export async function enroll(
metadata?: { local: any; userProvided: any },
sharedId?: string
): Promise<Agent> {
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;

Expand Down Expand Up @@ -92,3 +88,27 @@ async function getAgentBySharedId(soClient: SavedObjectsClientContract, sharedId

return null;
}

export function validateAgentVersion(metadata?: { local: any; userProvided: any }) {
const kibanaVersion = semver.parse(appContextService.getKibanaVersion(), {
includePrerelease: true,
});
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}`;
}