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] Version checks agent vs kibana #76238

Merged
merged 6 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
70 changes: 30 additions & 40 deletions x-pack/plugins/ingest_manager/server/services/agents/enroll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,52 @@
*/

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/);
expect(() => validateAgentVersion('8.8.0', '8.0.0')).toThrowError('not compatible');
});
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: {} });
validateAgentVersion('7.8.0', '8.0.0');
});

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: {} });
validateAgentVersion('8.0.0', '8.0.0');
});

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: {},
});
validateAgentVersion('8.0.0-SNAPSHOT', '8.0.0-SNAPSHOT');
});

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: {},
});
validateAgentVersion('7.8.0-SNAPSHOT', '7.8.0');
});

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: {},
});
validateAgentVersion('7.8.0', '7.8.0-SNAPSHOT');
});

it('very close versions, e.g. patch/prerelease - all combos should work', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc: @EricDavisX wdyt of these tests? They are basically validate(agent_version, kibana_version)

Any other combinations you'd like added to the tests?

validateAgentVersion('7.9.1', '7.9.2');
validateAgentVersion('7.8.1', '7.8.2');
validateAgentVersion('7.6.99', '7.6.2');
validateAgentVersion('7.6.2', '7.6.99');
validateAgentVersion('5.94.3', '5.94.1234-SNAPSHOT');
validateAgentVersion('5.94.3-SNAPSHOT', '5.94.1');
});

it('somewhat close versions, minor release is 1 or 2 versions back and is older than the stack', () => {
validateAgentVersion('7.9.1', '7.10.2');
validateAgentVersion('7.9.9', '7.11.1');
validateAgentVersion('7.6.99', '7.6.2');
validateAgentVersion('7.6.2', '7.6.99');
expect(() => validateAgentVersion('5.94.3-SNAPSHOT', '5.93.1')).toThrowError('not compatible');
expect(() => validateAgentVersion('5.94.3', '5.92.99-SNAPSHOT')).toThrowError('not compatible');
});

it('versions where Agent is a minor version or major version greater (newer) than the stack should not work', () => {
expect(() => validateAgentVersion('7.10.1', '7.9.99')).toThrowError('not compatible');
expect(() => validateAgentVersion('7.9.9', '6.11.1')).toThrowError('not compatible');
expect(() => validateAgentVersion('5.94.3', '5.92.99-SNAPSHOT')).toThrowError('not compatible');
});
});
61 changes: 44 additions & 17 deletions x-pack/plugins/ingest_manager/server/services/agents/enroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export async function enroll(
metadata?: { local: any; userProvided: any },
sharedId?: string
): Promise<Agent> {
validateAgentVersion(metadata);
const agentVersion = metadata?.local?.elastic?.agent?.version;
validateAgentVersion(agentVersion);

const existingAgent = sharedId ? await getAgentBySharedId(soClient, sharedId) : null;

Expand Down Expand Up @@ -89,24 +90,50 @@ 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.');
export function validateAgentVersion(
agentVersion: string,
kibanaVersion = appContextService.getKibanaVersion()
) {
const agentVersionParsed = semver.parse(agentVersion);
if (!agentVersionParsed) {
throw Boom.badRequest('Agent version not provided');
}

if (!version || !semver.lte(formatVersion(version), formatVersion(kibanaVersion))) {
throw Boom.badRequest('Agent version is not compatible with kibana version');
const kibanaVersionParsed = semver.parse(kibanaVersion);
if (!kibanaVersionParsed) {
throw Boom.badRequest('Kibana version is not set or provided');
}
}

/**
* 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}`;
const diff = semver.diff(agentVersion, kibanaVersion);
switch (diff) {
// section 1) very close versions, only patch release differences - all combos should work
// Agent a.b.1 < Kibana a.b.2
// Agent a.b.2 > Kibana a.b.1
case null:
case 'prerelease':
case 'prepatch':
case 'patch':
return; // OK

// section 2) somewhat close versions, Agent minor release is 1 or 2 versions back and is older than the stack:
// Agent a.9.x < Kibana a.10.x
// Agent a.9.x < Kibana a.11.x
case 'preminor':
case 'minor':
if (
agentVersionParsed.minor < kibanaVersionParsed.minor &&
kibanaVersionParsed.minor - agentVersionParsed.minor <= 2
)
return;

// section 3) versions where Agent is a minor version or major version greater (newer) than the stack should not work:
// Agent 7.10.x > Kibana 7.9.x
// Agent 8.0.x > Kibana 7.9.x
default:
if (semver.lte(agentVersionParsed, kibanaVersionParsed)) return;
else
throw Boom.badRequest(
`Agent version ${agentVersion} is not compatible with Kibana version ${kibanaVersion}`
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export default function (providerContext: FtrProviderContext) {
},
})
.expect(400);
expect(apiResponse.message).to.match(/Agent version is not compatible with kibana/);
expect(apiResponse.message).to.match(/is not compatible/);
});

it('should allow to enroll an agent with a valid enrollment token', async () => {
Expand Down