From e931881b9ad9a1f064571b9ffb23ca7ebc3b63c4 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Wed, 18 Nov 2020 14:14:09 -0500 Subject: [PATCH 1/5] [Fleet] Allow to send SETTINGS action --- .../fleet/common/types/models/agent.ts | 1 + .../fleet/server/types/models/agent.ts | 33 ++++++++++------ .../apis/agents/actions.ts | 39 +++++++++++++++++-- 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/x-pack/plugins/fleet/common/types/models/agent.ts b/x-pack/plugins/fleet/common/types/models/agent.ts index 303eeea6e510c..872b389d248a3 100644 --- a/x-pack/plugins/fleet/common/types/models/agent.ts +++ b/x-pack/plugins/fleet/common/types/models/agent.ts @@ -26,6 +26,7 @@ export type AgentActionType = | 'POLICY_CHANGE' | 'UNENROLL' | 'UPGRADE' + | 'SETTINGS' // INTERNAL* actions are mean to interupt long polling calls these actions will not be distributed to the agent | 'INTERNAL_POLICY_REASSIGN'; diff --git a/x-pack/plugins/fleet/server/types/models/agent.ts b/x-pack/plugins/fleet/server/types/models/agent.ts index 98ed793604954..4f9acf5575801 100644 --- a/x-pack/plugins/fleet/server/types/models/agent.ts +++ b/x-pack/plugins/fleet/server/types/models/agent.ts @@ -62,14 +62,25 @@ export const AgentEventSchema = schema.object({ id: schema.string(), }); -export const NewAgentActionSchema = schema.object({ - type: schema.oneOf([ - schema.literal('POLICY_CHANGE'), - schema.literal('UNENROLL'), - schema.literal('UPGRADE'), - schema.literal('INTERNAL_POLICY_REASSIGN'), - ]), - data: schema.maybe(schema.any()), - ack_data: schema.maybe(schema.any()), - sent_at: schema.maybe(schema.string()), -}); +export const NewAgentActionSchema = schema.oneOf([ + schema.object({ + type: schema.oneOf([ + schema.literal('POLICY_CHANGE'), + schema.literal('UNENROLL'), + schema.literal('UPGRADE'), + schema.literal('INTERNAL_POLICY_REASSIGN'), + ]), + data: schema.any(), + }), + schema.object({ + type: schema.oneOf([schema.literal('SETTINGS')]), + data: schema.object({ + log_level: schema.oneOf([ + schema.literal('debug'), + schema.literal('info'), + schema.literal('warning'), + schema.literal('error'), + ]), + }), + }), +]); diff --git a/x-pack/test/fleet_api_integration/apis/agents/actions.ts b/x-pack/test/fleet_api_integration/apis/agents/actions.ts index 01f69328388db..d97ac6f7daa6e 100644 --- a/x-pack/test/fleet_api_integration/apis/agents/actions.ts +++ b/x-pack/test/fleet_api_integration/apis/agents/actions.ts @@ -36,6 +36,39 @@ export default function (providerContext: FtrProviderContext) { expect(apiResponse.item.data).to.eql({ data: 'action_data' }); }); + it('should return a 200 if this a valid SETTINGS action request', async () => { + const { body: apiResponse } = await supertest + .post(`/api/fleet/agents/agent1/actions`) + .set('kbn-xsrf', 'xx') + .send({ + action: { + type: 'SETTINGS', + data: { log_level: 'debug' }, + }, + }) + .expect(200); + + expect(apiResponse.item.type).to.eql('SETTINGS'); + expect(apiResponse.item.data).to.eql({ log_level: 'debug' }); + }); + + it('should return a 400 if this a invalid SETTINGS action request', async () => { + const { body: apiResponse } = await supertest + .post(`/api/fleet/agents/agent1/actions`) + .set('kbn-xsrf', 'xx') + .send({ + action: { + type: 'SETTINGS', + data: { log_level: 'thisnotavalidloglevel' }, + }, + }) + .expect(400); + + expect(apiResponse.message).to.match( + /\[request body.action\.[0-9]*\.data\.log_level]: types that failed validation/ + ); + }); + it('should return a 400 when request does not have type information', async () => { const { body: apiResponse } = await supertest .post(`/api/fleet/agents/agent1/actions`) @@ -43,12 +76,11 @@ export default function (providerContext: FtrProviderContext) { .send({ action: { data: { data: 'action_data' }, - sent_at: '2020-03-18T19:45:02.620Z', }, }) .expect(400); - expect(apiResponse.message).to.eql( - '[request body.action.type]: expected at least one defined value but got [undefined]' + expect(apiResponse.message).to.match( + /\[request body.action\.[0-9]*\.type]: expected at least one defined value but got \[undefined]/ ); }); @@ -60,7 +92,6 @@ export default function (providerContext: FtrProviderContext) { action: { type: 'POLICY_CHANGE', data: { data: 'action_data' }, - sent_at: '2020-03-18T19:45:02.620Z', }, }) .expect(404); From 94a20f6f3b459fd504662f53fe7a6ec44e9f5c68 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Wed, 18 Nov 2020 14:59:50 -0500 Subject: [PATCH 2/5] Fix unit tests --- .../plugins/fleet/server/routes/agent/actions_handlers.test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/x-pack/plugins/fleet/server/routes/agent/actions_handlers.test.ts b/x-pack/plugins/fleet/server/routes/agent/actions_handlers.test.ts index 4574bcc64d4ce..09bb1d4bbdde9 100644 --- a/x-pack/plugins/fleet/server/routes/agent/actions_handlers.test.ts +++ b/x-pack/plugins/fleet/server/routes/agent/actions_handlers.test.ts @@ -25,7 +25,6 @@ describe('test actions handlers schema', () => { NewAgentActionSchema.validate({ type: 'POLICY_CHANGE', data: 'data', - sent_at: '2020-03-14T19:45:02.620Z', }) ).toBeTruthy(); }); @@ -34,7 +33,6 @@ describe('test actions handlers schema', () => { expect(() => { NewAgentActionSchema.validate({ data: 'data', - sent_at: '2020-03-14T19:45:02.620Z', }); }).toThrowError(); }); From 94d20e51291992786ceb7b4e34d562b3c5d85c87 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Thu, 19 Nov 2020 08:47:27 -0500 Subject: [PATCH 3/5] Ack data could be sent when creating an action --- x-pack/plugins/fleet/server/types/models/agent.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/plugins/fleet/server/types/models/agent.ts b/x-pack/plugins/fleet/server/types/models/agent.ts index 4f9acf5575801..48a30eb1ca709 100644 --- a/x-pack/plugins/fleet/server/types/models/agent.ts +++ b/x-pack/plugins/fleet/server/types/models/agent.ts @@ -71,6 +71,7 @@ export const NewAgentActionSchema = schema.oneOf([ schema.literal('INTERNAL_POLICY_REASSIGN'), ]), data: schema.any(), + ack_data: schema.any(), }), schema.object({ type: schema.oneOf([schema.literal('SETTINGS')]), From d126f4e2628fd139769d073158094b8133cdb942 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Thu, 19 Nov 2020 09:12:06 -0500 Subject: [PATCH 4/5] Fix test --- .../plugins/fleet/server/routes/agent/actions_handlers.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/x-pack/plugins/fleet/server/routes/agent/actions_handlers.test.ts b/x-pack/plugins/fleet/server/routes/agent/actions_handlers.test.ts index 09bb1d4bbdde9..2f08846642985 100644 --- a/x-pack/plugins/fleet/server/routes/agent/actions_handlers.test.ts +++ b/x-pack/plugins/fleet/server/routes/agent/actions_handlers.test.ts @@ -53,7 +53,6 @@ describe('test actions handlers', () => { action: { type: 'POLICY_CHANGE', data: 'data', - sent_at: '2020-03-14T19:45:02.620Z', }, }, params: { From 8ca46cdf332c68bef81b7a7d4def54ac19f086ff Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Thu, 19 Nov 2020 09:36:49 -0500 Subject: [PATCH 5/5] Fix typos --- x-pack/plugins/fleet/server/types/models/agent.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/fleet/server/types/models/agent.ts b/x-pack/plugins/fleet/server/types/models/agent.ts index 48a30eb1ca709..619c21d8bf5d9 100644 --- a/x-pack/plugins/fleet/server/types/models/agent.ts +++ b/x-pack/plugins/fleet/server/types/models/agent.ts @@ -70,8 +70,8 @@ export const NewAgentActionSchema = schema.oneOf([ schema.literal('UPGRADE'), schema.literal('INTERNAL_POLICY_REASSIGN'), ]), - data: schema.any(), - ack_data: schema.any(), + data: schema.maybe(schema.any()), + ack_data: schema.maybe(schema.any()), }), schema.object({ type: schema.oneOf([schema.literal('SETTINGS')]),