Skip to content

Commit

Permalink
Fix Chart Trend by Severity Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
CohenIdo authored Feb 25, 2025
1 parent 93931f9 commit 7f2df7c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ const getFindingsScoresDocIndexingPromises = (
});
});

const getVulnStatsTrendDocIndexingPromises = (
export const getVulnStatsTrendDocIndexingPromises = (
esClient: ElasticsearchClient,
vulnStatsAggs?: VulnSeverityAggs
) => {
Expand All @@ -348,12 +348,11 @@ const getVulnStatsTrendDocIndexingPromises = (
const scoreByCloudAccount = Object.fromEntries(
vulnStatsAggs.vulnerabilities_stats_by_cloud_account.buckets.map((accountScore) => {
const cloudAccountId = accountScore.key;

return [
cloudAccountId,
{
cloudAccountId: accountScore.key,
cloudAccountName: accountScore.cloud_account_name.buckets[0].key,
cloudAccountName: accountScore.cloud_account_name.buckets[0]?.key || '',
critical: accountScore.critical.doc_count,
high: accountScore.high.doc_count,
medium: accountScore.medium.doc_count,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

import { cloneDeep } from 'lodash';
import { stateSchemaByVersion } from './task_state';
import { getVulnStatsTrendDocIndexingPromises } from './findings_stats_task';
import { elasticsearchClientMock } from '@kbn/core-elasticsearch-client-server-mocks';

const esClient = elasticsearchClientMock.createClusterClient().asScoped().asInternalUser;

describe('finding stats task state', () => {
describe('v1', () => {
Expand Down Expand Up @@ -35,5 +39,83 @@ describe('finding stats task state', () => {
const result = v1.up(state);
expect(result).not.toHaveProperty('foo');
});

it('should return undefined if vulnStatsAggs is not provided', () => {
const result = getVulnStatsTrendDocIndexingPromises(esClient);
expect(result).toBeUndefined();
expect(esClient.index).not.toHaveBeenCalled();
});

it('should correctly process vulnerability stats and call esClient.index', async () => {
const vulnStatsAggs = {
vulnerabilities_stats_by_cloud_account: {
buckets: [
{
key: 'account1',
cloud_account_name: { buckets: [{ key: 'Account One' }] },
critical: { doc_count: 5 },
high: { doc_count: 10 },
medium: { doc_count: 15 },
low: { doc_count: 20 },
},
],
},
critical: { doc_count: 50 },
high: { doc_count: 100 },
medium: { doc_count: 150 },
low: { doc_count: 200 },
};

await getVulnStatsTrendDocIndexingPromises(esClient, vulnStatsAggs);

expect(esClient.index).toHaveBeenCalledWith({
index: expect.any(String),
document: expect.objectContaining({
vulnerabilities_stats_by_cloud_account: {
account1: expect.objectContaining({
cloudAccountName: 'Account One',
critical: 5,
high: 10,
medium: 15,
low: 20,
}),
},
}),
});
});

it('should handle missing cloud account name gracefully', async () => {
const vulnStatsAggs = {
vulnerabilities_stats_by_cloud_account: {
buckets: [
{
key: 'account2',
cloud_account_name: { buckets: [] },
critical: { doc_count: 3 },
high: { doc_count: 6 },
medium: { doc_count: 9 },
low: { doc_count: 12 },
},
],
},
critical: { doc_count: 30 },
high: { doc_count: 60 },
medium: { doc_count: 90 },
low: { doc_count: 120 },
};

await getVulnStatsTrendDocIndexingPromises(esClient, vulnStatsAggs);

expect(esClient.index).toHaveBeenCalledWith({
index: expect.any(String),
document: expect.objectContaining({
vulnerabilities_stats_by_cloud_account: {
account2: expect.objectContaining({
cloudAccountName: '',
}),
},
}),
});
});
});
});

0 comments on commit 7f2df7c

Please sign in to comment.