[8.16] Fix Chart Trend by Severity Bug (#212357)

# Backport

This will backport the following commits from `main` to `8.16`:
- [Fix Chart Trend by Severity
Bug](https://github.com/elastic/kibana/pull/211735)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Ido
Cohen","email":"90558359+CohenIdo@users.noreply.github.com"},"sourceCommit":{"committedDate":"2025-02-25T11:24:50Z","message":"Fix
Chart Trend by Severity
Bug","sha":"7f2df7cd427506486ac8f7a34498d0448c431992","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Cloud
Security","backport:prev-major","v8.16.0","v8.17.0","v8.18.0","v9.1.0"],"title":"Fix
Chart Trend by Severity
Bug","number":211735,"url":"https://github.com/elastic/kibana/pull/211735","mergeCommit":{"message":"Fix
Chart Trend by Severity
Bug","sha":"7f2df7cd427506486ac8f7a34498d0448c431992"}},"sourceBranch":"main","suggestedTargetBranches":["9.0","8.16","8.17","8.18"],"targetPullRequestStates":[{"branch":"9.0","label":"v9.0.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.16","label":"v8.16.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.17","label":"v8.17.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.18","label":"v8.18.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/211735","number":211735,"mergeCommit":{"message":"Fix
Chart Trend by Severity
Bug","sha":"7f2df7cd427506486ac8f7a34498d0448c431992"}}]}] BACKPORT-->

Co-authored-by: Ido Cohen <90558359+CohenIdo@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2025-02-26 04:08:34 +11:00 committed by GitHub
parent 90c24f5dd1
commit 5ec44bd3ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 84 additions and 3 deletions

View file

@ -339,7 +339,7 @@ const getFindingsScoresDocIndexingPromises = (
});
});
const getVulnStatsTrendDocIndexingPromises = (
export const getVulnStatsTrendDocIndexingPromises = (
esClient: ElasticsearchClient,
vulnStatsAggs?: VulnSeverityAggs
) => {
@ -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,

View file

@ -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', () => {
@ -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: '',
}),
},
}),
});
});
});
});