From 57f1cb39f8f01992d355547036b7a1e4dc3600b3 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Fri, 20 Nov 2020 09:21:24 -0500 Subject: [PATCH] [Fleet] Allow to send SETTINGS action (#83707) --- .../fleet/common/types/models/agent.ts | 1 + .../routes/agent/actions_handlers.test.ts | 3 -- .../fleet/server/types/models/agent.ts | 34 ++++++++++------ .../apis/agents/actions.ts | 39 +++++++++++++++++-- 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/x-pack/plugins/fleet/common/types/models/agent.ts b/x-pack/plugins/fleet/common/types/models/agent.ts index 303eeea6e510ce..872b389d248a36 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/routes/agent/actions_handlers.test.ts b/x-pack/plugins/fleet/server/routes/agent/actions_handlers.test.ts index 4574bcc64d4cec..2f08846642985b 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(); }); @@ -55,7 +53,6 @@ describe('test actions handlers', () => { action: { type: 'POLICY_CHANGE', data: 'data', - sent_at: '2020-03-14T19:45:02.620Z', }, }, params: { diff --git a/x-pack/plugins/fleet/server/types/models/agent.ts b/x-pack/plugins/fleet/server/types/models/agent.ts index 98ed793604954b..619c21d8bf5d94 100644 --- a/x-pack/plugins/fleet/server/types/models/agent.ts +++ b/x-pack/plugins/fleet/server/types/models/agent.ts @@ -62,14 +62,26 @@ 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.maybe(schema.any()), + ack_data: schema.maybe(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 01f69328388dbc..d97ac6f7daa6e9 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);