[Fleet] Validate pagination in fleet agents API (#148002)

This commit is contained in:
Nicolas Chaulet 2022-12-22 13:19:25 -05:00 committed by GitHub
parent 8e79d783ee
commit 5b53a8e9d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 9 deletions

View file

@ -0,0 +1,27 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { GetAgentsRequestSchema } from './agent';
describe('GetAgentsRequestSchema', () => {
it('should allow pagination with less than 10000 agents', () => {
expect(() =>
GetAgentsRequestSchema.query.validate({
page: 500,
perPage: 20,
})
).not.toThrow();
});
it('should not allow pagination to go over 10000 agents', () => {
expect(() =>
GetAgentsRequestSchema.query.validate({
page: 501,
perPage: 20,
})
).toThrowError(/You cannot use page and perPage page over 10000 agents/);
});
});

View file

@ -9,18 +9,29 @@ import { schema } from '@kbn/config-schema';
import moment from 'moment';
import semverIsValid from 'semver/functions/valid';
import { SO_SEARCH_LIMIT } from '../../constants';
import { NewAgentActionSchema } from '../models';
export const GetAgentsRequestSchema = {
query: schema.object({
page: schema.number({ defaultValue: 1 }),
perPage: schema.number({ defaultValue: 20 }),
kuery: schema.maybe(schema.string()),
showInactive: schema.boolean({ defaultValue: false }),
showUpgradeable: schema.boolean({ defaultValue: false }),
sortField: schema.maybe(schema.string()),
sortOrder: schema.maybe(schema.oneOf([schema.literal('asc'), schema.literal('desc')])),
}),
query: schema.object(
{
page: schema.number({ defaultValue: 1 }),
perPage: schema.number({ defaultValue: 20 }),
kuery: schema.maybe(schema.string()),
showInactive: schema.boolean({ defaultValue: false }),
showUpgradeable: schema.boolean({ defaultValue: false }),
sortField: schema.maybe(schema.string()),
sortOrder: schema.maybe(schema.oneOf([schema.literal('asc'), schema.literal('desc')])),
},
{
validate: (request) => {
if (request.page * request.perPage > SO_SEARCH_LIMIT) {
return `You cannot use page and perPage page over ${SO_SEARCH_LIMIT} agents`;
}
},
}
),
};
export const GetOneAgentRequestSchema = {