Skip to content

Commit

Permalink
feat(slo): limit perPage in find api (#167185)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdelemme authored Sep 28, 2023
1 parent 911ed80 commit c2745d3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion x-pack/plugins/observability/docs/openapi/slo/bundled.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@
"description": "The number of SLOs to return per page",
"schema": {
"type": "integer",
"default": 25
"default": 25,
"maximum": 5000
},
"example": 25
},
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/observability/docs/openapi/slo/bundled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ paths:
schema:
type: integer
default: 25
maximum: 5000
example: 25
- name: sortBy
in: query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ get:
schema:
type: integer
default: 25
maximum: 5000
example: 25
- name: sortBy
in: query
Expand Down
13 changes: 13 additions & 0 deletions x-pack/plugins/observability/server/services/slo/find_slo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/observability/server/services/slo/find_slo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c2745d3

Please sign in to comment.