From e794156efb9a388dec0c8cfce44c5482f60779a4 Mon Sep 17 00:00:00 2001 From: Josh Dover <1813008+joshdover@users.noreply.github.com> Date: Thu, 6 Oct 2022 20:50:56 +0200 Subject: [PATCH] Fix unenroll outputs shape (#142895) --- .../plugins/fleet/common/types/models/agent.ts | 11 +++++++---- .../server/services/agents/unenroll.test.ts | 16 ++++++++++++---- .../services/agents/unenroll_action_runner.ts | 8 ++++++-- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/fleet/common/types/models/agent.ts b/x-pack/plugins/fleet/common/types/models/agent.ts index ea2ad78dc10cd..9e47451aa02bb 100644 --- a/x-pack/plugins/fleet/common/types/models/agent.ts +++ b/x-pack/plugins/fleet/common/types/models/agent.ts @@ -96,10 +96,13 @@ export interface Agent extends AgentBase { access_api_key?: string; // @deprecated default_api_key_history?: FleetServerAgent['default_api_key_history']; - outputs?: Array<{ - api_key_id: string; - to_retire_api_key_ids?: FleetServerAgent['default_api_key_history']; - }>; + outputs?: Record< + string, + { + api_key_id: string; + to_retire_api_key_ids?: FleetServerAgent['default_api_key_history']; + } + >; status?: AgentStatus; packages: string[]; sort?: Array; diff --git a/x-pack/plugins/fleet/server/services/agents/unenroll.test.ts b/x-pack/plugins/fleet/server/services/agents/unenroll.test.ts index 9169df19fbcfb..b1e69fa861488 100644 --- a/x-pack/plugins/fleet/server/services/agents/unenroll.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/unenroll.test.ts @@ -331,15 +331,22 @@ describe('invalidateAPIKeysForAgents', () => { id: 'defaultApiKeyHistory2', }, ], - outputs: [ - { + outputs: { + output1: { api_key_id: 'outputApiKey1', to_retire_api_key_ids: [{ id: 'outputApiKeyRetire1' }, { id: 'outputApiKeyRetire2' }], }, - { + output2: { api_key_id: 'outputApiKey2', }, - ], + output3: { + api_key_id: 'outputApiKey3', + to_retire_api_key_ids: [ + // Somes Fleet Server agents don't have an id here (probably a bug) + { retired_at: 'foo' }, + ], + }, + }, } as any, ]); @@ -353,6 +360,7 @@ describe('invalidateAPIKeysForAgents', () => { 'outputApiKeyRetire1', 'outputApiKeyRetire2', 'outputApiKey2', + 'outputApiKey3', ]); }); }); diff --git a/x-pack/plugins/fleet/server/services/agents/unenroll_action_runner.ts b/x-pack/plugins/fleet/server/services/agents/unenroll_action_runner.ts index fed5d44fe98e8..1b17e688a8a26 100644 --- a/x-pack/plugins/fleet/server/services/agents/unenroll_action_runner.ts +++ b/x-pack/plugins/fleet/server/services/agents/unenroll_action_runner.ts @@ -216,12 +216,16 @@ export async function invalidateAPIKeysForAgents(agents: Agent[]) { agent.default_api_key_history.forEach((apiKey) => keys.push(apiKey.id)); } if (agent.outputs) { - agent.outputs.forEach((output) => { + Object.values(agent.outputs).forEach((output) => { if (output.api_key_id) { keys.push(output.api_key_id); } if (output.to_retire_api_key_ids) { - output.to_retire_api_key_ids.forEach((apiKey) => keys.push(apiKey.id)); + Object.values(output.to_retire_api_key_ids).forEach((apiKey) => { + if (apiKey?.id) { + keys.push(apiKey.id); + } + }); } }); }