Skip to content

Commit

Permalink
[Fleet] Allow to send SETTINGS action (#83707)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Nov 20, 2020
1 parent a2598ea commit c1a263c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/types/models/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -34,7 +33,6 @@ describe('test actions handlers schema', () => {
expect(() => {
NewAgentActionSchema.validate({
data: 'data',
sent_at: '2020-03-14T19:45:02.620Z',
});
}).toThrowError();
});
Expand All @@ -55,7 +53,6 @@ describe('test actions handlers', () => {
action: {
type: 'POLICY_CHANGE',
data: 'data',
sent_at: '2020-03-14T19:45:02.620Z',
},
},
params: {
Expand Down
34 changes: 23 additions & 11 deletions x-pack/plugins/fleet/server/types/models/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]),
}),
}),
]);
39 changes: 35 additions & 4 deletions x-pack/test/fleet_api_integration/apis/agents/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,51 @@ 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`)
.set('kbn-xsrf', 'xx')
.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]/
);
});

Expand All @@ -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);
Expand Down

0 comments on commit c1a263c

Please sign in to comment.