mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[Fleet] Validate pagination in fleet agents API (#148002)
This commit is contained in:
parent
8e79d783ee
commit
5b53a8e9d9
2 changed files with 47 additions and 9 deletions
27
x-pack/plugins/fleet/server/types/rest_spec/agent.test.ts
Normal file
27
x-pack/plugins/fleet/server/types/rest_spec/agent.test.ts
Normal 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/);
|
||||
});
|
||||
});
|
|
@ -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 = {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue