Skip to content

Commit

Permalink
[Fleet] Validate pagination in fleet agents API (elastic#148002)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Dec 22, 2022
1 parent 8e79d78 commit 5b53a8e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
27 changes: 27 additions & 0 deletions x-pack/plugins/fleet/server/types/rest_spec/agent.test.ts
Original file line number Diff line number Diff line change
@@ -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/);
});
});
29 changes: 20 additions & 9 deletions x-pack/plugins/fleet/server/types/rest_spec/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 5b53a8e

Please sign in to comment.