-
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.
[8.x] [Cloud Security] Fix vulnerability detection rule creation logic (
#195291) (#195596) # Backport This will backport the following commits from `main` to `8.x`: - [[Cloud Security] Fix vulnerability detection rule creation logic (#195291)](#195291) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Jordan","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-09T13:18:33Z","message":"[Cloud Security] Fix vulnerability detection rule creation logic (#195291)","sha":"fbf3f8b8b24575bd9fdc10e05ed0e5032a1a4340","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Cloud Security","backport:prev-minor"],"title":"[Cloud Security] Fix vulnerability detection rule creation logic","number":195291,"url":"https://github.com/elastic/kibana/pull/195291","mergeCommit":{"message":"[Cloud Security] Fix vulnerability detection rule creation logic (#195291)","sha":"fbf3f8b8b24575bd9fdc10e05ed0e5032a1a4340"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/195291","number":195291,"mergeCommit":{"message":"[Cloud Security] Fix vulnerability detection rule creation logic (#195291)","sha":"fbf3f8b8b24575bd9fdc10e05ed0e5032a1a4340"}}]}] BACKPORT--> Co-authored-by: Jordan <[email protected]>
- Loading branch information
1 parent
4de542f
commit 0556cfc
Showing
6 changed files
with
142 additions
and
44 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
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
98 changes: 98 additions & 0 deletions
98
...sture/public/pages/vulnerabilities/utils/create_detection_rule_from_vulnerability.test.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,98 @@ | ||
/* | ||
* 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 { | ||
getVulnerabilityTags, | ||
getVulnerabilityRuleName, | ||
generateVulnerabilitiesRuleQuery, | ||
} from './create_detection_rule_from_vulnerability'; | ||
import { CspVulnerabilityFinding, Vulnerability } from '@kbn/cloud-security-posture-common'; | ||
import { isNativeCspFinding } from '../../../common/utils/is_native_csp_finding'; | ||
|
||
// Mocking the isNativeCspFinding function | ||
jest.mock('../../../common/utils/is_native_csp_finding', () => ({ | ||
isNativeCspFinding: jest.fn(), | ||
})); | ||
|
||
describe('CreateDetectionRuleFromVulnerability', () => { | ||
describe('getVulnerabilityTags', () => { | ||
it('should return tags with CSP_RULE_TAG and vulnerability id', () => { | ||
const mockVulnerability = { | ||
vulnerability: { id: 'CVE-2024-00001' }, | ||
observer: undefined, | ||
data_stream: undefined, | ||
} as unknown as CspVulnerabilityFinding; | ||
|
||
(isNativeCspFinding as jest.Mock).mockReturnValue(false); | ||
|
||
const tags = getVulnerabilityTags(mockVulnerability); | ||
expect(tags).toEqual(['Cloud Security', 'CVE-2024-00001']); | ||
}); | ||
|
||
it('should include vendor tag if available', () => { | ||
const mockVulnerability = { | ||
vulnerability: { id: 'CVE-2024-00002' }, | ||
observer: { vendor: 'Wiz' }, | ||
data_stream: undefined, | ||
} as unknown as CspVulnerabilityFinding; | ||
|
||
(isNativeCspFinding as jest.Mock).mockReturnValue(false); | ||
|
||
const tags = getVulnerabilityTags(mockVulnerability); | ||
expect(tags).toEqual(['Cloud Security', 'CVE-2024-00002', 'Wiz']); | ||
}); | ||
|
||
it('should include CNVM tags for native findings', () => { | ||
const mockVulnerability = { | ||
vulnerability: { id: 'CVE-2024-00003' }, | ||
observer: undefined, | ||
data_stream: undefined, | ||
} as unknown as CspVulnerabilityFinding; | ||
|
||
(isNativeCspFinding as jest.Mock).mockReturnValue(true); | ||
|
||
const tags = getVulnerabilityTags(mockVulnerability); | ||
expect(tags).toEqual([ | ||
'Cloud Security', | ||
'CNVM', | ||
'Data Source: Cloud Native Vulnerability Management', | ||
'Use Case: Vulnerability', | ||
'OS: Linux', | ||
'CVE-2024-00003', | ||
]); | ||
}); | ||
}); | ||
|
||
describe('getVulnerabilityRuleName', () => { | ||
it('should return correct rule name for a vulnerability', () => { | ||
const mockVulnerability = { | ||
id: 'CVE-2024-00004', | ||
description: '', | ||
reference: '', | ||
} as Vulnerability; | ||
|
||
const ruleName = getVulnerabilityRuleName(mockVulnerability); | ||
expect(ruleName).toEqual('Vulnerability: CVE-2024-00004'); | ||
}); | ||
}); | ||
|
||
describe('generateVulnerabilitiesRuleQuery', () => { | ||
it('should generate correct query for a vulnerability', () => { | ||
const mockVulnerability = { | ||
id: 'CVE-2024-00005', | ||
description: '', | ||
reference: '', | ||
} as Vulnerability; | ||
const currentTimestamp = new Date().toISOString(); | ||
|
||
const query = generateVulnerabilitiesRuleQuery(mockVulnerability); | ||
expect(query).toEqual( | ||
`vulnerability.id: "CVE-2024-00005" AND event.ingested >= "${currentTimestamp}"` | ||
); | ||
}); | ||
}); | ||
}); |
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