diff --git a/x-pack/plugins/fleet/common/types/models/agent.ts b/x-pack/plugins/fleet/common/types/models/agent.ts index e561613934a04..e52edb821b1d7 100644 --- a/x-pack/plugins/fleet/common/types/models/agent.ts +++ b/x-pack/plugins/fleet/common/types/models/agent.ts @@ -95,10 +95,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); + } + }); } }); }