From 2a0cfe847349335aa89f415718b5b5a1ef34ee2d Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Mon, 16 Mar 2020 10:20:08 -0400 Subject: [PATCH 1/2] [Fleet] Add config revision to fleet agents --- .../common/types/models/agent.ts | 3 +- .../sections/fleet/agent_list_page/index.tsx | 45 +++++-- .../ingest_manager/server/saved_objects.ts | 3 +- .../server/services/agents/acks.ts | 14 +++ .../server/services/agents/checkin.test.ts | 117 ++++++++++++++++++ .../server/services/agents/checkin.ts | 35 +++++- .../server/services/agents/enroll.ts | 1 - .../server/services/agents/update.ts | 8 +- 8 files changed, 205 insertions(+), 21 deletions(-) create mode 100644 x-pack/plugins/ingest_manager/server/services/agents/checkin.test.ts diff --git a/x-pack/plugins/ingest_manager/common/types/models/agent.ts b/x-pack/plugins/ingest_manager/common/types/models/agent.ts index ad06e8d3c9c11..179cc3fc9eb55 100644 --- a/x-pack/plugins/ingest_manager/common/types/models/agent.ts +++ b/x-pack/plugins/ingest_manager/common/types/models/agent.ts @@ -56,8 +56,9 @@ interface AgentBase { access_api_key_id?: string; default_api_key?: string; config_id?: string; + config_revision?: number; + config_newest_revision?: number; last_checkin?: string; - config_updated_at?: string; actions: AgentAction[]; } diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/index.tsx b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/index.tsx index acf09dedc25f7..8c15d09f09f4e 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/index.tsx +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/index.tsx @@ -26,6 +26,7 @@ import { EuiButtonIcon, EuiContextMenuPanel, EuiContextMenuItem, + EuiIcon, } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import { FormattedMessage, FormattedRelative } from '@kbn/i18n/react'; @@ -289,6 +290,7 @@ export const AgentListPage: React.FunctionComponent<{}> = () => { }, { field: 'active', + width: '100px', name: i18n.translate('xpack.ingestManager.agentList.statusColumnTitle', { defaultMessage: 'Status', }), @@ -299,10 +301,10 @@ export const AgentListPage: React.FunctionComponent<{}> = () => { name: i18n.translate('xpack.ingestManager.agentList.configColumnTitle', { defaultMessage: 'Configuration', }), - render: (configId: string) => { + render: (configId: string, agent: Agent) => { const configName = agentConfigs.find(p => p.id === configId)?.name; return ( - + = () => { {configName || configId} - - - - - + {agent.config_revision && ( + + + + + + )} + {agent.config_revision && + agent.config_newest_revision && + agent.config_newest_revision > agent.config_revision && ( + + + +   + {true && ( + <> + + + )} + + + )} ); }, }, { field: 'local_metadata.agent_version', + width: '100px', name: i18n.translate('xpack.ingestManager.agentList.versionTitle', { defaultMessage: 'Version', }), diff --git a/x-pack/plugins/ingest_manager/server/saved_objects.ts b/x-pack/plugins/ingest_manager/server/saved_objects.ts index 860b95b58c7f7..31cf173c3e4f9 100644 --- a/x-pack/plugins/ingest_manager/server/saved_objects.ts +++ b/x-pack/plugins/ingest_manager/server/saved_objects.ts @@ -32,7 +32,8 @@ export const savedObjectMappings = { config_id: { type: 'keyword' }, last_updated: { type: 'date' }, last_checkin: { type: 'date' }, - config_updated_at: { type: 'date' }, + config_revision: { type: 'integer' }, + config_newest_revision: { type: 'integer' }, // FIXME_INGEST https://github.com/elastic/kibana/issues/56554 default_api_key: { type: 'keyword' }, updated_at: { type: 'date' }, diff --git a/x-pack/plugins/ingest_manager/server/services/agents/acks.ts b/x-pack/plugins/ingest_manager/server/services/agents/acks.ts index 98a5f69f9d2b0..cf9a47979ae8b 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/acks.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/acks.ts @@ -51,8 +51,22 @@ export async function acknowledgeAgentActions( }); if (matchedUpdatedActions.length > 0) { + const configRevision = matchedUpdatedActions.reduce((acc, action) => { + if (action.type !== 'CONFIG_CHANGE') { + return acc; + } + const data = action.data ? JSON.parse(action.data as string) : {}; + + if (data?.config?.id !== agent.config_id) { + return acc; + } + + return data?.config?.revision > acc ? data?.config?.revision : acc; + }, agent.config_revision || 0); + await soClient.update(AGENT_SAVED_OBJECT_TYPE, agent.id, { actions: matchedUpdatedActions, + config_revision: configRevision, }); } diff --git a/x-pack/plugins/ingest_manager/server/services/agents/checkin.test.ts b/x-pack/plugins/ingest_manager/server/services/agents/checkin.test.ts new file mode 100644 index 0000000000000..d3e10fcb6b63f --- /dev/null +++ b/x-pack/plugins/ingest_manager/server/services/agents/checkin.test.ts @@ -0,0 +1,117 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { shouldCreateConfigAction } from './checkin'; +import { Agent } from '../../types'; + +function getAgent(data: Partial) { + return { actions: [], ...data } as Agent; +} + +describe('Agent checkin service', () => { + describe('shouldCreateConfigAction', () => { + it('should return false if the agent do not have an assigned config', () => { + const res = shouldCreateConfigAction(getAgent({})); + + expect(res).toBeFalsy(); + }); + + it('should return true if this is agent first checkin', () => { + const res = shouldCreateConfigAction(getAgent({ config_id: 'config1' })); + + expect(res).toBeTruthy(); + }); + + it('should return false agent is already running latest revision', () => { + const res = shouldCreateConfigAction( + getAgent({ + config_id: 'config1', + last_checkin: '2018-01-02T00:00:00', + config_revision: 1, + config_newest_revision: 1, + }) + ); + + expect(res).toBeFalsy(); + }); + + it('should return false agent has already latest revision config change action', () => { + const res = shouldCreateConfigAction( + getAgent({ + config_id: 'config1', + last_checkin: '2018-01-02T00:00:00', + config_revision: 1, + config_newest_revision: 2, + actions: [ + { + id: 'action1', + type: 'CONFIG_CHANGE', + created_at: new Date().toISOString(), + data: JSON.stringify({ + config: { + id: 'config1', + revision: 2, + }, + }), + }, + ], + }) + ); + + expect(res).toBeFalsy(); + }); + + it('should return true agent has unrelated config change actions', () => { + const res = shouldCreateConfigAction( + getAgent({ + config_id: 'config1', + last_checkin: '2018-01-02T00:00:00', + config_revision: 1, + config_newest_revision: 2, + actions: [ + { + id: 'action1', + type: 'CONFIG_CHANGE', + created_at: new Date().toISOString(), + data: JSON.stringify({ + config: { + id: 'config2', + revision: 2, + }, + }), + }, + { + id: 'action1', + type: 'CONFIG_CHANGE', + created_at: new Date().toISOString(), + data: JSON.stringify({ + config: { + id: 'config1', + revision: 1, + }, + }), + }, + ], + }) + ); + + expect(res).toBeTruthy(); + }); + + it('should return true if this agent has a new revision', () => { + const res = shouldCreateConfigAction( + getAgent({ + config_id: 'config1', + last_checkin: '2018-01-02T00:00:00', + config_revision: 1, + config_newest_revision: 2, + }) + ); + + expect(res).toBeTruthy(); + }); + }); +}); diff --git a/x-pack/plugins/ingest_manager/server/services/agents/checkin.ts b/x-pack/plugins/ingest_manager/server/services/agents/checkin.ts index 0ff4af4ffe351..d80fff5d8eceb 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/checkin.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/checkin.ts @@ -37,7 +37,7 @@ export async function agentCheckin( const actions = filterActionsForCheckin(agent); // Generate new agent config if config is updated - if (isNewAgentConfig(agent) && agent.config_id) { + if (agent.config_id && shouldCreateConfigAction(agent)) { const config = await agentConfigService.getFullConfig(soClient, agent.config_id); if (config) { // Assign output API keys @@ -149,12 +149,37 @@ function isActionEvent(event: AgentEvent) { ); } -function isNewAgentConfig(agent: Agent) { +export function shouldCreateConfigAction(agent: Agent): boolean { + if (!agent.config_id) { + return false; + } + const isFirstCheckin = !agent.last_checkin; - const isConfigUpdatedSinceLastCheckin = - agent.last_checkin && agent.config_updated_at && agent.last_checkin <= agent.config_updated_at; + if (isFirstCheckin) { + return true; + } + + const isAgentConfigOutdated = + agent.config_revision && + agent.config_newest_revision && + agent.config_revision < agent.config_newest_revision; + if (!isAgentConfigOutdated) { + return false; + } + + const isActionAlreadyGenerated = !!agent.actions.find(action => { + if (!action.data || action.type !== 'CONFIG_CHANGE') { + return false; + } + + const data = JSON.parse(action.data); + + return ( + data.config.id === agent.config_id && data.config.revision === agent.config_newest_revision + ); + }); - return isFirstCheckin || isConfigUpdatedSinceLastCheckin; + return !isActionAlreadyGenerated; } function filterActionsForCheckin(agent: Agent): AgentAction[] { diff --git a/x-pack/plugins/ingest_manager/server/services/agents/enroll.ts b/x-pack/plugins/ingest_manager/server/services/agents/enroll.ts index 0f73f71817eb0..52547e9bcb0fb 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/enroll.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/enroll.ts @@ -37,7 +37,6 @@ export async function enroll( current_error_events: undefined, actions: [], access_api_key_id: undefined, - config_updated_at: undefined, last_checkin: undefined, default_api_key: undefined, }; diff --git a/x-pack/plugins/ingest_manager/server/services/agents/update.ts b/x-pack/plugins/ingest_manager/server/services/agents/update.ts index 9eabf0944bdc4..59d0ad31d1a64 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/update.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/update.ts @@ -8,14 +8,18 @@ import { SavedObjectsClientContract } from 'src/core/server'; import { listAgents } from './crud'; import { AGENT_SAVED_OBJECT_TYPE } from '../../constants'; import { unenrollAgents } from './unenroll'; +import { agentConfigService } from '../agent_config'; export async function updateAgentsForConfigId( soClient: SavedObjectsClientContract, configId: string ) { + const config = await agentConfigService.get(soClient, configId); + if (!config) { + throw new Error('Config not found'); + } let hasMore = true; let page = 1; - const now = new Date().toISOString(); while (hasMore) { const { agents } = await listAgents(soClient, { kuery: `agents.config_id:"${configId}"`, @@ -30,7 +34,7 @@ export async function updateAgentsForConfigId( const agentUpdate = agents.map(agent => ({ id: agent.id, type: AGENT_SAVED_OBJECT_TYPE, - attributes: { config_updated_at: now }, + attributes: { config_newest_revision: config.revision }, })); await soClient.bulkUpdate(agentUpdate); From a27c3cf1568cd83bf19548e028a75138e5700ee2 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Tue, 17 Mar 2020 09:48:03 -0400 Subject: [PATCH 2/2] Fix after review --- .../ingest_manager/sections/fleet/agent_list_page/index.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/index.tsx b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/index.tsx index 8c15d09f09f4e..14a579eb72598 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/index.tsx +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/index.tsx @@ -316,7 +316,7 @@ export const AgentListPage: React.FunctionComponent<{}> = () => { {agent.config_revision && ( - + = () => { agent.config_newest_revision && agent.config_newest_revision > agent.config_revision && ( - - + +   {true && ( <>