[Fleet] Allow to send SETTINGS action (#83707) (#83921)

This commit is contained in:
Nicolas Chaulet 2020-11-20 12:53:12 -05:00 committed by GitHub
parent a4c7d111c5
commit 3f90e72b1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 18 deletions

View file

@ -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';

View file

@ -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: {

View file

@ -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'),
]),
}),
}),
]);

View file

@ -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);