Skip to content

Commit

Permalink
Addressing alert service feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Feb 11, 2021
1 parent f58b5ff commit 575d849
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
4 changes: 4 additions & 0 deletions x-pack/plugins/case/server/client/alerts/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export const get = async ({
}

const alerts = await alertsService.getAlerts({ ids, indices, scopedClusterClient });
if (!alerts) {
return [];
}

return alerts.hits.hits.map((alert) => ({
id: alert._id,
index: alert._index,
Expand Down
8 changes: 4 additions & 4 deletions x-pack/plugins/case/server/services/alerts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ describe('updateAlertsStatus', () => {
});

describe('unhappy path', () => {
it('throws an error if no valid indices are provided', async () => {
expect(async () => {
it('ignores empty indices', async () => {
expect(
await alertService.updateAlertsStatus({
ids: ['alert-id-1'],
status: CaseStatuses.closed,
indices: new Set<string>(['']),
scopedClusterClient: esClient,
});
}).rejects.toThrow();
})
).toBeUndefined();
});
});
});
Expand Down
32 changes: 21 additions & 11 deletions x-pack/plugins/case/server/services/alerts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import _ from 'lodash';

import type { PublicMethodsOf } from '@kbn/utility-types';

import { ElasticsearchClient } from 'kibana/server';
Expand Down Expand Up @@ -37,6 +39,15 @@ interface AlertsResponse {
};
}

/**
* remove empty strings from the indices, I'm not sure how likely this is but in the case that
* the document doesn't have _index set the security_solution code sets the value to an empty string
* instead
*/
function getValidIndices(indices: Set<string>): string[] {
return [...indices].filter((index) => !_.isEmpty(index));
}

export class AlertService {
constructor() {}

Expand All @@ -46,17 +57,12 @@ export class AlertService {
indices,
scopedClusterClient,
}: UpdateAlertsStatusArgs) {
/**
* remove empty strings from the indices, I'm not sure how likely this is but in the case that
* the document doesn't have _index set the security_solution code sets the value to an empty string
* instead
*/
const sanitizedIndices = [...indices].filter((index) => index !== '');
const sanitizedIndices = getValidIndices(indices);
if (sanitizedIndices.length <= 0) {
throw new Error('No valid indices found to update the alerts status');
// log that we only had invalid indices
return;
}

// The above check makes sure that esClient is defined.
const result = await scopedClusterClient.updateByQuery({
index: sanitizedIndices,
conflicts: 'abort',
Expand All @@ -77,10 +83,14 @@ export class AlertService {
scopedClusterClient,
ids,
indices,
}: GetAlertsArgs): Promise<AlertsResponse> {
// The above check makes sure that esClient is defined.
}: GetAlertsArgs): Promise<AlertsResponse | undefined> {
const index = getValidIndices(indices);
if (index.length <= 0) {
return;
}

const result = await scopedClusterClient.search<AlertsResponse>({
index: [...indices].filter((index) => index !== ''),
index,
body: {
query: {
bool: {
Expand Down

0 comments on commit 575d849

Please sign in to comment.