Skip to content

Commit

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

elastic@0cbbb41 is just a rearrangement of the tests.  elastic@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 elastic#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 and kibanamachine committed Mar 23, 2021
1 parent 6576327 commit 1cb9e99
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 1cb9e99

Please sign in to comment.