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

[Fleet] Fix validation for agents upgrade and add warning in modal #132687

Merged
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
13 changes: 8 additions & 5 deletions x-pack/plugins/fleet/common/services/is_agent_upgradeable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ export function isAgentUpgradeable(agent: Agent, kibanaVersion: string) {
return false;
}

return isAgentVersionLessThanKibana(agentVersion, kibanaVersion);
}

export const isAgentVersionLessThanKibana = (agentVersion: string, kibanaVersion: string) => {
// make sure versions are only the number before comparison
const agentVersionNumber = semverCoerce(agentVersion);
if (!agentVersionNumber) throw new Error('agent version is invalid');
if (!agentVersionNumber) throw new Error('agent version is not valid');
const kibanaVersionNumber = semverCoerce(kibanaVersion);
if (!kibanaVersionNumber) throw new Error('kibana version is invalid');
const isAgentLessThanKibana = semverLt(agentVersionNumber, kibanaVersionNumber);
if (!kibanaVersionNumber) throw new Error('kibana version is not valid');

return isAgentLessThanKibana;
}
return semverLt(agentVersionNumber, kibanaVersionNumber);
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { FormattedMessage } from '@kbn/i18n-react';

import type { EuiComboBoxOptionOption } from '@elastic/eui';

import semverCoerce from 'semver/functions/coerce';
import semverLt from 'semver/functions/lt';

import type { Agent } from '../../../../types';
import {
sendPostAgentUpgrade,
Expand All @@ -38,7 +41,7 @@ interface Props {
agentCount: number;
}

const getVersion = (version: Array<EuiComboBoxOptionOption<string>>) => version[0].value as string;
const getVersion = (version: Array<EuiComboBoxOptionOption<string>>) => version[0]?.value as string;

export const AgentUpgradeAgentModal: React.FunctionComponent<Props> = ({
onClose,
Expand Down Expand Up @@ -169,6 +172,17 @@ export const AgentUpgradeAgentModal: React.FunctionComponent<Props> = ({
}
}

const onCreateOption = (searchValue: string) => {
const agentVersionNumber = semverCoerce(searchValue);
if (agentVersionNumber?.version && semverLt(agentVersionNumber?.version, kibanaVersion)) {
const newOption = {
label: searchValue,
value: searchValue,
};
setSelectedVersion([newOption]);
}
};

return (
<EuiConfirmModal
data-test-subj="agentUpgradeModal"
Expand Down Expand Up @@ -230,7 +244,6 @@ export const AgentUpgradeAgentModal: React.FunctionComponent<Props> = ({
/>
)}
</p>
<EuiSpacer size="m" />
<EuiFormRow
label={i18n.translate('xpack.fleet.upgradeAgents.chooseVersionLabel', {
defaultMessage: 'Upgrade version',
Expand All @@ -246,8 +259,21 @@ export const AgentUpgradeAgentModal: React.FunctionComponent<Props> = ({
onChange={(selected: Array<EuiComboBoxOptionOption<string>>) => {
setSelectedVersion(selected);
}}
onCreateOption={onCreateOption}
customOptionText="Input the desired version"
/>
</EuiFormRow>
{!isSingleAgent ? (
<>
<EuiSpacer size="m" />
<EuiCallOut
color="warning"
title={i18n.translate('xpack.fleet.upgradeAgents.warningCallout', {
defaultMessage: 'Only available for Elastic Agent versions 8.3+',
})}
/>
</>
) : null}
<EuiSpacer size="m" />
{!isSingleAgent ? (
<EuiFormRow
Expand Down Expand Up @@ -293,7 +319,7 @@ export const AgentUpgradeAgentModal: React.FunctionComponent<Props> = ({
<>
<EuiCallOut
color="danger"
title={i18n.translate('xpack.fleet.upgradeAgents.warningCallout', {
title={i18n.translate('xpack.fleet.upgradeAgents.warningCalloutErrors', {
defaultMessage:
'Error upgrading the selected {count, plural, one {agent} other {{count} agents}}',
values: { count: isSingleAgent },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { SavedObjectsClientContract, ElasticsearchClient } from '@kbn/core/server';

import { SO_SEARCH_LIMIT } from '../../common';
import { getAgentsByKuery } from '../services/agents';
import { PACKAGE_POLICY_SAVED_OBJECT_TYPE, AGENTS_PREFIX } from '../constants';

import { packagePolicyService } from '../services/package_policy';

export const getAllFleetServerAgents = async (
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient
) => {
let packagePolicyData;
try {
packagePolicyData = await packagePolicyService.list(soClient, {
perPage: SO_SEARCH_LIMIT,
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name: fleet_server`,
});
} catch (error) {
throw new Error(error.message);
}
const agentPoliciesIds = packagePolicyData?.items.map((item) => item.policy_id);

if (agentPoliciesIds.length === 0) {
return [];
}

let agentsResponse;
try {
agentsResponse = await getAgentsByKuery(esClient, {
showInactive: false,
perPage: SO_SEARCH_LIMIT,
kuery: `${AGENTS_PREFIX}.policy_id:${agentPoliciesIds.map((id) => `"${id}"`).join(' or ')}`,
});
} catch (error) {
throw new Error(error.message);
}

const { agents: fleetServerAgents } = agentsResponse;

if (fleetServerAgents.length === 0) {
return [];
}
return fleetServerAgents;
};
66 changes: 7 additions & 59 deletions x-pack/plugins/fleet/server/routes/agent/upgrade_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import type { RequestHandler } from '@kbn/core/server';
import type { TypeOf } from '@kbn/config-schema';
import type { SavedObjectsClientContract, ElasticsearchClient } from '@kbn/core/server';

import semverCoerce from 'semver/functions/coerce';
import semverGt from 'semver/functions/gt';
Expand All @@ -21,14 +20,12 @@ import type { PostAgentUpgradeRequestSchema, PostBulkAgentUpgradeRequestSchema }
import * as AgentService from '../../services/agents';
import { appContextService } from '../../services';
import { defaultIngestErrorHandler } from '../../errors';
import { SO_SEARCH_LIMIT } from '../../../common';
import { isAgentUpgradeable } from '../../../common/services';
import { getAgentById, getAgentsByKuery } from '../../services/agents';
import { PACKAGE_POLICY_SAVED_OBJECT_TYPE, AGENTS_PREFIX } from '../../constants';

import { getMaxVersion } from '../../../common/services/get_max_version';
import { getAgentById } from '../../services/agents';
import type { Agent } from '../../types';

import { packagePolicyService } from '../../services/package_policy';
import { getAllFleetServerAgents } from '../../collectors/get_all_fleet_server_agents';

export const postAgentUpgradeHandler: RequestHandler<
TypeOf<typeof PostAgentUpgradeRequestSchema.params>,
Expand Down Expand Up @@ -60,7 +57,6 @@ export const postAgentUpgradeHandler: RequestHandler<
},
});
}

if (!force && !isAgentUpgradeable(agent, kibanaVersion)) {
return response.customError({
statusCode: 400,
Expand Down Expand Up @@ -105,7 +101,8 @@ export const postBulkAgentsUpgradeHandler: RequestHandler<
try {
checkKibanaVersion(version, kibanaVersion);
checkSourceUriAllowed(sourceUri);
await checkFleetServerVersion(version, agents, soClient, esClient);
const fleetServerAgents = await getAllFleetServerAgents(soClient, esClient);
checkFleetServerVersion(version, fleetServerAgents);
} catch (err) {
return response.customError({
statusCode: 400,
Expand Down Expand Up @@ -174,57 +171,8 @@ const checkSourceUriAllowed = (sourceUri?: string) => {
}
};

// Check the installed fleet server versions
// Allow upgrading if the agents to upgrade include fleet server agents
const checkFleetServerVersion = async (
versionToUpgradeNumber: string,
agentsIds: string | string[],
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient
) => {
let packagePolicyData;
try {
packagePolicyData = await packagePolicyService.list(soClient, {
perPage: SO_SEARCH_LIMIT,
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name: fleet_server`,
});
} catch (error) {
throw new Error(error.message);
}
const agentPoliciesIds = packagePolicyData?.items.map((item) => item.policy_id);

if (agentPoliciesIds.length === 0) {
return;
}

let agentsResponse;
try {
agentsResponse = await getAgentsByKuery(esClient, {
showInactive: false,
perPage: SO_SEARCH_LIMIT,
kuery: `${AGENTS_PREFIX}.policy_id:${agentPoliciesIds.map((id) => `"${id}"`).join(' or ')}`,
});
} catch (error) {
throw new Error(error.message);
}

const { agents: fleetServerAgents } = agentsResponse;

if (fleetServerAgents.length === 0) {
return;
}
const fleetServerIds = fleetServerAgents.map((agent) => agent.id);

let hasFleetServerAgents: boolean;
if (Array.isArray(agentsIds)) {
hasFleetServerAgents = agentsIds.some((id) => fleetServerIds.includes(id));
} else {
hasFleetServerAgents = fleetServerIds.includes(agentsIds);
}
if (hasFleetServerAgents) {
return;
}

// Check the installed fleet server version
const checkFleetServerVersion = (versionToUpgradeNumber: string, fleetServerAgents: Agent[]) => {
const fleetServerVersions = fleetServerAgents.map(
(agent) => agent.local_metadata.elastic.agent.version
) as string[];
Expand Down
33 changes: 0 additions & 33 deletions x-pack/test/fleet_api_integration/apis/agents/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,39 +710,6 @@ export default function (providerContext: FtrProviderContext) {
})
.expect(400);
});
it('should respond 200 if trying to bulk upgrade to a version higher than the latest fleet server version if the agents include fleet server agents', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

should there be a new integration test added to cover the new agent version check?

Copy link
Contributor Author

@criamico criamico May 23, 2022

Choose a reason for hiding this comment

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

Actually there was an additional requirement that was removed. We originally had two cases:
1 when there is an agent that is a fleet server agent among the batch to be upgraded, allow the upgrade
2 if only "regular" elastic agents are in the batch, don't allow the upgrade

In this PR I'm simplifying the behaviour and removing the first case, so regardless of having or not fleet server agents in the batch, the upgrade is not allowed if the target version < max fleet server version. This case is already covered in the integration test, but I'm removing the test related to case 1.

I hope the explanation is clear.

const higherVersion = semver.inc(fleetServerVersion, 'patch');
await es.update({
id: 'agent1',
refresh: 'wait_for',
index: AGENTS_INDEX,
body: {
doc: {
policy_id: `agent-policy-1`,
local_metadata: { elastic: { agent: { upgradeable: true, version: '0.0.0' } } },
},
},
});
await es.update({
id: 'agent2',
refresh: 'wait_for',
index: AGENTS_INDEX,
body: {
doc: {
policy_id: `agent-policy-2`,
local_metadata: { elastic: { agent: { upgradeable: true, version: '0.0.0' } } },
},
},
});
await supertest
.post(`/api/fleet/agents/bulk_upgrade`)
.set('kbn-xsrf', 'xxx')
.send({
agents: ['agent1', 'agent2', 'agentWithFS'],
version: higherVersion,
})
.expect(200);
});

it('should throw an error if source_uri parameter is passed', async () => {
const kibanaVersion = await kibanaServer.version.get();
Expand Down