use management default page and pageSize

fixes elastic/security-team/issues/3871
This commit is contained in:
Ashokaditya 2022-06-17 15:10:22 +02:00
parent 9331a272d1
commit 751e3145e5
4 changed files with 28 additions and 12 deletions

View file

@ -68,9 +68,18 @@ describe('actions schemas', () => {
}).not.toThrow();
});
it('should not work without allowed page and pageSize params', () => {
it('should not work without allowed page params', () => {
expect(() => {
EndpointActionListRequestSchema.query.validate({ pageSize: 101 });
EndpointActionListRequestSchema.query.validate({ page: -1 });
}).toThrow();
});
it('should not work without allowed pageSize params', () => {
expect(() => {
EndpointActionListRequestSchema.query.validate({ pageSize: 100001 });
}).toThrow();
expect(() => {
EndpointActionListRequestSchema.query.validate({ pageSize: 0 });
}).toThrow();
});

View file

@ -6,6 +6,7 @@
*/
import { schema, TypeOf } from '@kbn/config-schema';
import { ENDPOINT_DEFAULT_PAGE, ENDPOINT_DEFAULT_PAGE_SIZE } from '../constants';
const BaseActionRequestSchema = {
/** A list of endpoint IDs whose hosts will be isolated (Fleet Agent IDs will be retrieved for these) */
@ -73,8 +74,10 @@ export const EndpointActionListRequestSchema = {
schema.arrayOf(schema.string({ minLength: 1 }), { minSize: 1, maxSize: 50 })
),
commands: schema.maybe(schema.arrayOf(schema.string({ minLength: 1 }), { minSize: 1 })),
page: schema.maybe(schema.number({ defaultValue: 1, min: 1 })),
pageSize: schema.maybe(schema.number({ defaultValue: 10, min: 1, max: 100 })),
page: schema.maybe(schema.number({ defaultValue: ENDPOINT_DEFAULT_PAGE, min: 0 })),
pageSize: schema.maybe(
schema.number({ defaultValue: ENDPOINT_DEFAULT_PAGE_SIZE, min: 1, max: 10000 })
),
startDate: schema.maybe(schema.string()), // date ISO strings or moment date
endDate: schema.maybe(schema.string()), // date ISO strings or moment date
userIds: schema.maybe(schema.arrayOf(schema.string({ minLength: 1 }), { minSize: 1 })),

View file

@ -43,7 +43,7 @@ describe('When using `getActionList()', () => {
it('should return expected output', async () => {
await expect(getActionList({ esClient, logger, page: 1, pageSize: 10 })).resolves.toEqual({
page: 0,
page: 1,
pageSize: 10,
commands: undefined,
userIds: undefined,
@ -67,7 +67,7 @@ describe('When using `getActionList()', () => {
'@timestamp': '2022-04-30T16:08:47.449Z',
EndpointActions: {
action_id: '123',
completed_at: '2022-04-30T16:08:47.449Z',
completed_at: '2022-04-30T10:53:59.449Z',
data: {
command: 'unisolate',
comment: '',
@ -93,7 +93,7 @@ describe('When using `getActionList()', () => {
},
action_id: '123',
agent_id: 'agent-a',
completed_at: '2022-04-30T16:08:47.449Z',
completed_at: '2022-04-30T10:53:59.449Z',
error: '',
started_at: expect.any(String),
},
@ -109,7 +109,7 @@ describe('When using `getActionList()', () => {
action_id: '123',
data: {
command: 'isolate',
comment: '5wb6pu6kh2xix5i',
comment: expect.any(String),
},
expiration: expect.any(String),
input_type: 'endpoint',
@ -226,7 +226,7 @@ describe('When using `getActionList()', () => {
elasticAgentIds: undefined,
endDate: undefined,
page: 0,
pageSize: undefined,
pageSize: 10,
startDate: undefined,
total: 0,
userIds: undefined,

View file

@ -7,6 +7,10 @@
import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { SearchTotalHits } from '@elastic/elasticsearch/lib/api/types';
import {
ENDPOINT_DEFAULT_PAGE,
ENDPOINT_DEFAULT_PAGE_SIZE,
} from '../../../../common/endpoint/constants';
import { CustomHttpRequestError } from '../../../utils/custom_http_request_error';
import type { ActionDetails, ActionListApiResponse } from '../../../../common/endpoint/types';
@ -47,8 +51,8 @@ export const getActionList = async ({
esClient: ElasticsearchClient;
logger: Logger;
}): Promise<ActionListApiResponse> => {
const size = pageSize ?? 10;
const page = (_page ?? 1) - 1;
const size = pageSize ?? ENDPOINT_DEFAULT_PAGE_SIZE;
const page = _page ?? ENDPOINT_DEFAULT_PAGE;
// # of hits to skip
const from = page * size;
@ -66,7 +70,7 @@ export const getActionList = async ({
return {
page,
pageSize,
pageSize: size,
startDate,
endDate,
elasticAgentIds,