feat(slo): limit perPage in find api (#167185)

This commit is contained in:
Kevin Delemme 2023-09-28 09:20:33 -04:00 committed by GitHub
parent 911ed802b7
commit c2745d3c19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 1 deletions

View file

@ -160,7 +160,8 @@
"description": "The number of SLOs to return per page",
"schema": {
"type": "integer",
"default": 25
"default": 25,
"maximum": 5000
},
"example": 25
},

View file

@ -99,6 +99,7 @@ paths:
schema:
type: integer
default: 25
maximum: 5000
example: 25
- name: sortBy
in: query

View file

@ -79,6 +79,7 @@ get:
schema:
type: integer
default: 25
maximum: 5000
example: 25
- name: sortBy
in: query

View file

@ -138,6 +138,19 @@ describe('FindSLO', () => {
`);
});
});
describe('validation', () => {
it("throws an error when 'perPage > 5000'", async () => {
const slo = createSLO();
mockSummarySearchClient.search.mockResolvedValueOnce(summarySearchResult(slo));
mockRepository.findAllByIds.mockResolvedValueOnce([slo]);
await expect(findSLO.execute({ perPage: '5000' })).resolves.not.toThrow();
await expect(findSLO.execute({ perPage: '5001' })).rejects.toThrowError(
'perPage limit to 5000'
);
});
});
});
function summarySearchResult(slo: SLO): Paginated<SLOSummary> {

View file

@ -7,11 +7,13 @@
import { FindSLOParams, FindSLOResponse, findSLOResponseSchema } from '@kbn/slo-schema';
import { SLO, SLOWithSummary } from '../../domain/models';
import { IllegalArgumentError } from '../../errors';
import { SLORepository } from './slo_repository';
import { Pagination, SLOSummary, Sort, SummarySearchClient } from './summary_search_client';
const DEFAULT_PAGE = 1;
const DEFAULT_PER_PAGE = 25;
const MAX_PER_PAGE = 5000;
export class FindSLO {
constructor(
@ -52,6 +54,10 @@ function toPagination(params: FindSLOParams): Pagination {
const page = Number(params.page);
const perPage = Number(params.perPage);
if (!isNaN(perPage) && perPage > MAX_PER_PAGE) {
throw new IllegalArgumentError('perPage limit to 5000');
}
return {
page: !isNaN(page) && page >= 1 ? page : DEFAULT_PAGE,
perPage: !isNaN(perPage) && perPage >= 1 ? perPage : DEFAULT_PER_PAGE,