Skip to content

Commit

Permalink
[Fleet] Support force flag in bulk upgrade agents API (#94952)
Browse files Browse the repository at this point in the history
## Summary

0cbbb41 is just a rearrangement of the tests.  5cad301 has the real changes: 
* Bug fix: `force: true` should bypass any restrictions re: managed policies
* Refactoring towards new response shape coming as part of #90437
* Added test to confirm new behavior


### Checklist
- [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
  • Loading branch information
John Schulz authored Mar 23, 2021
1 parent f3b71bc commit c0c5fba
Show file tree
Hide file tree
Showing 3 changed files with 509 additions and 461 deletions.
23 changes: 8 additions & 15 deletions x-pack/plugins/fleet/server/routes/agent/upgrade_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,14 @@ export const postBulkAgentsUpgradeHandler: RequestHandler<
}

try {
if (Array.isArray(agents)) {
await AgentService.sendUpgradeAgentsActions(soClient, esClient, {
agentIds: agents,
sourceUri,
version,
force,
});
} else {
await AgentService.sendUpgradeAgentsActions(soClient, esClient, {
kuery: agents,
sourceUri,
version,
force,
});
}
const agentOptions = Array.isArray(agents) ? { agentIds: agents } : { kuery: agents };
const upgradeOptions = {
...agentOptions,
sourceUri,
version,
force,
};
await AgentService.sendUpgradeAgentsActions(soClient, esClient, upgradeOptions);

const body: PostBulkAgentUpgradeResponse = {};
return response.ok({ body });
Expand Down
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
Loading

0 comments on commit c0c5fba

Please sign in to comment.