-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Registering files attachment type and limit logic
- Loading branch information
1 parent
3b15d79
commit cbb58f4
Showing
13 changed files
with
401 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
x-pack/plugins/cases/server/common/limiter_checker/base_limiter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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 type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||
import type { KueryNode } from '@kbn/es-query'; | ||
import { CASE_COMMENT_SAVED_OBJECT } from '../../../common/constants'; | ||
import type { CommentRequest, CommentType } from '../../../common/api'; | ||
import type { AttachmentService } from '../../services'; | ||
import type { Limiter } from './types'; | ||
|
||
interface LimiterParams { | ||
limit: number; | ||
attachmentType: CommentType; | ||
field: string; | ||
attachmentNoun: string; | ||
filter?: KueryNode; | ||
} | ||
|
||
export abstract class BaseLimiter implements Limiter { | ||
public readonly limit: number; | ||
public readonly errorMessage: string; | ||
|
||
private readonly limitAggregation: Record<string, estypes.AggregationsAggregationContainer>; | ||
private readonly params: LimiterParams; | ||
|
||
constructor(params: LimiterParams) { | ||
this.params = params; | ||
this.limit = params.limit; | ||
this.errorMessage = makeErrorMessage(this.limit, params.attachmentNoun); | ||
|
||
this.limitAggregation = { | ||
limiter: { | ||
value_count: { | ||
field: `${CASE_COMMENT_SAVED_OBJECT}.attributes.${params.field}`, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
public async countOfItemsWithinCase( | ||
attachmentService: AttachmentService, | ||
caseId: string | ||
): Promise<number> { | ||
const itemsAttachedToCase = await attachmentService.executeCaseAggregations<{ | ||
limiter: { value: number }; | ||
}>({ | ||
caseId, | ||
aggregations: this.limitAggregation, | ||
attachmentType: this.params.attachmentType, | ||
filter: this.params.filter, | ||
}); | ||
|
||
return itemsAttachedToCase?.limiter?.value ?? 0; | ||
} | ||
|
||
public abstract countOfItemsInRequest(requests: CommentRequest[]): number; | ||
} | ||
|
||
const makeErrorMessage = (limit: number, noun: string) => { | ||
return `Case has reached the maximum allowed number (${limit}) of attached ${noun}.`; | ||
}; |
51 changes: 51 additions & 0 deletions
51
x-pack/plugins/cases/server/common/limiter_checker/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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 Boom from '@hapi/boom'; | ||
|
||
import type { CommentRequest } from '../../../common/api'; | ||
import type { AttachmentService } from '../../services'; | ||
import type { Limiter } from './types'; | ||
import { AlertLimiter } from './limiters/alerts'; | ||
import { FileLimiter } from './limiters/files'; | ||
|
||
export class LimitChecker { | ||
private readonly limiters: Limiter[] = [new AlertLimiter(), new FileLimiter()]; | ||
|
||
constructor( | ||
private readonly attachmentService: AttachmentService, | ||
private readonly caseId: string | ||
) {} | ||
|
||
public async validate(requests: CommentRequest[]) { | ||
for (const limiter of this.limiters) { | ||
const itemsWithinRequests = limiter.countOfItemsInRequest(requests); | ||
const hasItemsInRequests = itemsWithinRequests > 0; | ||
|
||
const totalAfterRequests = async () => { | ||
const itemsWithinCase = await limiter.countOfItemsWithinCase( | ||
this.attachmentService, | ||
this.caseId | ||
); | ||
|
||
return itemsWithinRequests + itemsWithinCase; | ||
}; | ||
|
||
/** | ||
* The call to totalAfterRequests is intentionally performed after checking the limit. If the number in the | ||
* requests is greater than the max then we can skip checking how many items exist within the case because it is | ||
* guaranteed to exceed. | ||
*/ | ||
if ( | ||
hasItemsInRequests && | ||
(itemsWithinRequests > limiter.limit || (await totalAfterRequests()) > limiter.limit) | ||
) { | ||
throw Boom.badRequest(limiter.errorMessage); | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
x-pack/plugins/cases/server/common/limiter_checker/limiters/alerts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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 { CommentType } from '../../../../common/api'; | ||
import type { CommentRequest, CommentRequestAlertType } from '../../../../common/api'; | ||
import { MAX_ALERTS_PER_CASE } from '../../../../common/constants'; | ||
import { isCommentRequestTypeAlert } from '../../utils'; | ||
import { BaseLimiter } from '../base_limiter'; | ||
|
||
export class AlertLimiter extends BaseLimiter { | ||
constructor() { | ||
super({ | ||
limit: MAX_ALERTS_PER_CASE, | ||
attachmentType: CommentType.alert, | ||
attachmentNoun: 'alerts', | ||
field: 'alertId', | ||
}); | ||
} | ||
|
||
public countOfItemsInRequest(requests: CommentRequest[]): number { | ||
const totalAlertsInReq = requests | ||
.filter<CommentRequestAlertType>(isCommentRequestTypeAlert) | ||
.reduce((count, attachment) => { | ||
const ids = Array.isArray(attachment.alertId) ? attachment.alertId : [attachment.alertId]; | ||
return count + ids.length; | ||
}, 0); | ||
|
||
return totalAlertsInReq; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
x-pack/plugins/cases/server/common/limiter_checker/limiters/files.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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 { buildFilter } from '../../../client/utils'; | ||
import { CommentType, FILE_ATTACHMENT_TYPE } from '../../../../common/api'; | ||
import type { CommentRequest } from '../../../../common/api'; | ||
import { CASE_COMMENT_SAVED_OBJECT, MAX_FILES_PER_CASE } from '../../../../common/constants'; | ||
import { isFileAttachmentRequest } from '../../utils'; | ||
import { BaseLimiter } from '../base_limiter'; | ||
|
||
export class FileLimiter extends BaseLimiter { | ||
constructor() { | ||
super({ | ||
limit: MAX_FILES_PER_CASE, | ||
attachmentType: CommentType.externalReference, | ||
field: 'externalReferenceAttachmentTypeId', | ||
filter: createFileFilter(), | ||
attachmentNoun: 'files', | ||
}); | ||
} | ||
|
||
public countOfItemsInRequest(requests: CommentRequest[]): number { | ||
let fileRequests = 0; | ||
|
||
for (const request of requests) { | ||
if (isFileAttachmentRequest(request)) { | ||
fileRequests++; | ||
} | ||
} | ||
|
||
return fileRequests; | ||
} | ||
} | ||
|
||
const createFileFilter = () => | ||
buildFilter({ | ||
filters: FILE_ATTACHMENT_TYPE, | ||
field: 'externalReferenceAttachmentTypeId', | ||
operator: 'or', | ||
type: CASE_COMMENT_SAVED_OBJECT, | ||
}); |
16 changes: 16 additions & 0 deletions
16
x-pack/plugins/cases/server/common/limiter_checker/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* 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 type { CommentRequest } from '../../../common/api'; | ||
import type { AttachmentService } from '../../services'; | ||
|
||
export interface Limiter { | ||
readonly limit: number; | ||
readonly errorMessage: string; | ||
countOfItemsWithinCase(attachmentService: AttachmentService, caseId: string): Promise<number>; | ||
countOfItemsInRequest: (requests: CommentRequest[]) => number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.