Skip to content

Commit

Permalink
Respect force flag in /agents/bulk_upgrade. Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
John Schulz committed Mar 18, 2021
1 parent 0cbbb41 commit 5cad301
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 20 deletions.
39 changes: 20 additions & 19 deletions x-pack/plugins/fleet/server/services/agents/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,32 +83,33 @@ export async function sendUpgradeAgentsActions(
force?: boolean;
}
) {
const kibanaVersion = appContextService.getKibanaVersion();
// Filter out agents currently unenrolling, agents unenrolled, and agents not upgradeable
const agents = await getAgents(esClient, options);
// Full set of agents
const agentsGiven = await getAgents(esClient, options);

// upgradeable if they pass the version check
// Filter out agents currently unenrolling, unenrolled, or not upgradeable b/c of version check
const kibanaVersion = appContextService.getKibanaVersion();
const upgradeableAgents = options.force
? agents
: agents.filter((agent) => isAgentUpgradeable(agent, kibanaVersion));
? agentsGiven
: agentsGiven.filter((agent) => isAgentUpgradeable(agent, kibanaVersion));

// get any policy ids from upgradable agents
const policyIdsToGet = new Set(
upgradeableAgents.filter((agent) => agent.policy_id).map((agent) => agent.policy_id!)
);
if (!options.force) {
// get any policy ids from upgradable agents
const policyIdsToGet = new Set(
upgradeableAgents.filter((agent) => agent.policy_id).map((agent) => agent.policy_id!)
);

// get the agent policies for those ids
const agentPolicies = await agentPolicyService.getByIDs(soClient, Array.from(policyIdsToGet), {
fields: ['is_managed'],
});
// get the agent policies for those ids
const agentPolicies = await agentPolicyService.getByIDs(soClient, Array.from(policyIdsToGet), {
fields: ['is_managed'],
});

// throw if any of those agent policies are managed
for (const policy of agentPolicies) {
if (policy.is_managed) {
throw new IngestManagerError(`Cannot upgrade agent in managed policy ${policy.id}`);
// throw if any of those agent policies are managed
for (const policy of agentPolicies) {
if (policy.is_managed) {
throw new IngestManagerError(`Cannot upgrade agent in managed policy ${policy.id}`);
}
}
}

// Create upgrade action for each agent
const now = new Date().toISOString();
const data = {
Expand Down
54 changes: 53 additions & 1 deletion x-pack/test/fleet_api_integration/apis/agents/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ export default function (providerContext: FtrProviderContext) {
.expect(400);
});

it('enrolled in a managed policy should respond 400 to bulk upgrade and not update the agent SOs', async () => {
it('enrolled in a managed policy bulk upgrade should respond with 400 and not update the agent SOs', async () => {
// update enrolled policy to managed
await supertest.put(`/api/fleet/agent_policies/policy1`).set('kbn-xsrf', 'xxxx').send({
name: 'Test policy',
Expand Down Expand Up @@ -592,6 +592,58 @@ export default function (providerContext: FtrProviderContext) {
expect(typeof agent1data.body.item.upgrade_started_at).to.be('undefined');
expect(typeof agent2data.body.item.upgrade_started_at).to.be('undefined');
});

it('enrolled in a managed policy bulk upgrade with force flag should respond with 200 and update the agent SOs', async () => {
// update enrolled policy to managed
await supertest.put(`/api/fleet/agent_policies/policy1`).set('kbn-xsrf', 'xxxx').send({
name: 'Test policy',
namespace: 'default',
is_managed: true,
});

const kibanaVersion = await kibanaServer.version.get();
await es.update({
id: 'agent1',
refresh: 'wait_for',
index: AGENTS_INDEX,
body: {
doc: {
local_metadata: { elastic: { agent: { upgradeable: true, version: '0.0.0' } } },
},
},
});
await es.update({
id: 'agent2',
refresh: 'wait_for',
index: AGENTS_INDEX,
body: {
doc: {
local_metadata: {
elastic: {
agent: { upgradeable: true, version: semver.inc(kibanaVersion, 'patch') },
},
},
},
},
});
// attempt to upgrade agent in managed policy
const { body } = await supertest
.post(`/api/fleet/agents/bulk_upgrade`)
.set('kbn-xsrf', 'xxx')
.send({
version: kibanaVersion,
agents: ['agent1', 'agent2'],
force: true,
});
expect(body).to.eql({});

const [agent1data, agent2data] = await Promise.all([
supertest.get(`/api/fleet/agents/agent1`),
supertest.get(`/api/fleet/agents/agent2`),
]);
expect(typeof agent1data.body.item.upgrade_started_at).to.be('string');
expect(typeof agent2data.body.item.upgrade_started_at).to.be('string');
});
});
});
}

0 comments on commit 5cad301

Please sign in to comment.