diff --git a/x-pack/plugins/observability/docs/openapi/slo/bundled.json b/x-pack/plugins/observability/docs/openapi/slo/bundled.json index 559f5713e2c35..e51f3828886cf 100644 --- a/x-pack/plugins/observability/docs/openapi/slo/bundled.json +++ b/x-pack/plugins/observability/docs/openapi/slo/bundled.json @@ -160,7 +160,8 @@ "description": "The number of SLOs to return per page", "schema": { "type": "integer", - "default": 25 + "default": 25, + "maximum": 5000 }, "example": 25 }, diff --git a/x-pack/plugins/observability/docs/openapi/slo/bundled.yaml b/x-pack/plugins/observability/docs/openapi/slo/bundled.yaml index efeeb090f0156..4b0ca84bc7c52 100644 --- a/x-pack/plugins/observability/docs/openapi/slo/bundled.yaml +++ b/x-pack/plugins/observability/docs/openapi/slo/bundled.yaml @@ -99,6 +99,7 @@ paths: schema: type: integer default: 25 + maximum: 5000 example: 25 - name: sortBy in: query diff --git a/x-pack/plugins/observability/docs/openapi/slo/paths/s@{spaceid}@api@slos.yaml b/x-pack/plugins/observability/docs/openapi/slo/paths/s@{spaceid}@api@slos.yaml index 0c7559e41bb62..b606a0aac05fb 100644 --- a/x-pack/plugins/observability/docs/openapi/slo/paths/s@{spaceid}@api@slos.yaml +++ b/x-pack/plugins/observability/docs/openapi/slo/paths/s@{spaceid}@api@slos.yaml @@ -79,6 +79,7 @@ get: schema: type: integer default: 25 + maximum: 5000 example: 25 - name: sortBy in: query diff --git a/x-pack/plugins/observability/server/services/slo/find_slo.test.ts b/x-pack/plugins/observability/server/services/slo/find_slo.test.ts index c5b9d2b73d202..10436bc0fad54 100644 --- a/x-pack/plugins/observability/server/services/slo/find_slo.test.ts +++ b/x-pack/plugins/observability/server/services/slo/find_slo.test.ts @@ -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 { diff --git a/x-pack/plugins/observability/server/services/slo/find_slo.ts b/x-pack/plugins/observability/server/services/slo/find_slo.ts index b2b5bab8ee75a..cf8150db3e627 100644 --- a/x-pack/plugins/observability/server/services/slo/find_slo.ts +++ b/x-pack/plugins/observability/server/services/slo/find_slo.ts @@ -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,