Skip to content

Commit b0bcf1a

Browse files
[8.19] [Security Solution] Fixes rule customizations diff bug on import (#228475) (#229842)
# Backport This will backport the following commits from `main` to `8.19`: - [[Security Solution] Fixes rule customizations diff bug on import (#228475)](#228475) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Davis Plumlee","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-07-29T15:39:31Z","message":"[Security Solution] Fixes rule customizations diff bug on import (#228475)\n\n**Fixes: https://github.com/elastic/kibana/issues/228399**\n**Fixes: https://github.com/elastic/kibana/issues/228401**\n**Fixes: https://github.com/elastic/kibana/issues/228413**\n\n## Summary\n\nAddresses issue with base version API route caching and not being\nproperly invalidated on rule import and adds unit tests to cover similar\ncache invalidation cases.\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios","sha":"1a1d13341b13662af30c3bcfa54641e2a8b5dae7","branchLabelMapping":{"^v9.2.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:fix","Team:Detections and Resp","Team: SecuritySolution","Team:Detection Rule Management","Feature:Prebuilt Detection Rules","backport:version","v9.2.0","v9.1.1","v8.19.1"],"title":"[Security Solution] Fixes rule customizations diff bug on import","number":228475,"url":"https://github.com/elastic/kibana/pull/228475","mergeCommit":{"message":"[Security Solution] Fixes rule customizations diff bug on import (#228475)\n\n**Fixes: https://github.com/elastic/kibana/issues/228399**\n**Fixes: https://github.com/elastic/kibana/issues/228401**\n**Fixes: https://github.com/elastic/kibana/issues/228413**\n\n## Summary\n\nAddresses issue with base version API route caching and not being\nproperly invalidated on rule import and adds unit tests to cover similar\ncache invalidation cases.\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios","sha":"1a1d13341b13662af30c3bcfa54641e2a8b5dae7"}},"sourceBranch":"main","suggestedTargetBranches":["9.1","8.19"],"targetPullRequestStates":[{"branch":"main","label":"v9.2.0","branchLabelMappingKey":"^v9.2.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/228475","number":228475,"mergeCommit":{"message":"[Security Solution] Fixes rule customizations diff bug on import (#228475)\n\n**Fixes: https://github.com/elastic/kibana/issues/228399**\n**Fixes: https://github.com/elastic/kibana/issues/228401**\n**Fixes: https://github.com/elastic/kibana/issues/228413**\n\n## Summary\n\nAddresses issue with base version API route caching and not being\nproperly invalidated on rule import and adds unit tests to cover similar\ncache invalidation cases.\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios","sha":"1a1d13341b13662af30c3bcfa54641e2a8b5dae7"}},{"branch":"9.1","label":"v9.1.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.19","label":"v8.19.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Davis Plumlee <[email protected]>
1 parent 03aef32 commit b0bcf1a

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management_ui/components/rule_import_modal/rule_import_modal.test.tsx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,18 @@ import { RuleImportModal } from './rule_import_modal';
1212
import { ReactQueryClientProvider } from '../../../../common/containers/query_client/query_client_provider';
1313
import { importRules } from '../../../rule_management/logic';
1414
import { mockImportResponse } from './test_utils';
15+
import { useInvalidateFindRulesQuery } from '../../../rule_management/api/hooks/use_find_rules_query';
16+
import { useInvalidateFetchCoverageOverviewQuery } from '../../../rule_management/api/hooks/use_fetch_coverage_overview_query';
17+
import { useInvalidateFetchRuleManagementFiltersQuery } from '../../../rule_management/api/hooks/use_fetch_rule_management_filters_query';
18+
import { useInvalidateFetchPrebuiltRuleBaseVersionQuery } from '../../../rule_management/api/hooks/prebuilt_rules/use_fetch_prebuilt_rule_base_version_query';
1519

1620
jest.mock('../../../../common/lib/kibana');
17-
21+
jest.mock('../../../rule_management/api/hooks/use_find_rules_query');
22+
jest.mock('../../../rule_management/api/hooks/use_fetch_coverage_overview_query');
23+
jest.mock('../../../rule_management/api/hooks/use_fetch_rule_management_filters_query');
24+
jest.mock(
25+
'../../../rule_management/api/hooks/prebuilt_rules/use_fetch_prebuilt_rule_base_version_query'
26+
);
1827
jest.mock('../../../../common/lib/kibana/kibana_react', () => ({
1928
useKibana: jest.fn().mockReturnValue({
2029
services: { http: { basePath: { prepend: jest.fn() } } },
@@ -26,6 +35,22 @@ jest.mock('../../../rule_management/logic', () => ({
2635
}));
2736

2837
const hideImportModal = jest.fn();
38+
39+
const mockInvalidateFindRulesQuery = jest.fn();
40+
const mockInvalidateFetchCoverageOverviewQuery = jest.fn();
41+
const mockInvalidateFetchRuleManagementFilters = jest.fn();
42+
const mockInvalidateFetchPrebuiltRuleBaseVerison = jest.fn();
43+
jest.mocked(useInvalidateFindRulesQuery).mockReturnValue(mockInvalidateFindRulesQuery);
44+
jest
45+
.mocked(useInvalidateFetchCoverageOverviewQuery)
46+
.mockReturnValue(mockInvalidateFetchCoverageOverviewQuery);
47+
jest
48+
.mocked(useInvalidateFetchRuleManagementFiltersQuery)
49+
.mockReturnValue(mockInvalidateFetchRuleManagementFilters);
50+
jest
51+
.mocked(useInvalidateFetchPrebuiltRuleBaseVersionQuery)
52+
.mockReturnValue(mockInvalidateFetchPrebuiltRuleBaseVerison);
53+
2954
const file = new File(['file'], 'rules.json', { type: 'application/x-ndjson' });
3055

3156
const mockedImportRules = importRules as jest.Mock;
@@ -35,6 +60,26 @@ describe('RuleImportModal', () => {
3560
jest.clearAllMocks();
3661
});
3762

63+
test('should invalidate all rule related queries after importing new file', async () => {
64+
mockedImportRules.mockReturnValue(mockImportResponse());
65+
66+
const { getByTestId } = render(
67+
<ReactQueryClientProvider>
68+
<RuleImportModal isImportModalVisible={true} hideImportModal={hideImportModal} />
69+
</ReactQueryClientProvider>
70+
);
71+
72+
fireEvent.change(getByTestId('rule-file-picker'), { target: { files: [file] } });
73+
fireEvent.click(getByTestId('import-data-modal-button'));
74+
75+
await waitFor(() => {
76+
expect(mockInvalidateFindRulesQuery).toHaveBeenCalled();
77+
expect(mockInvalidateFetchCoverageOverviewQuery).toHaveBeenCalled();
78+
expect(mockInvalidateFetchRuleManagementFilters).toHaveBeenCalled();
79+
expect(mockInvalidateFetchPrebuiltRuleBaseVerison).toHaveBeenCalled();
80+
});
81+
});
82+
3883
test('should uncheck the selected checkboxes after importing new file', async () => {
3984
const { getByTestId } = render(
4085
<ReactQueryClientProvider>

x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management_ui/components/rule_import_modal/rule_import_modal.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { useInvalidateFetchRuleManagementFiltersQuery } from '../../../rule_mana
2121
import { useToasts } from '../../../../common/lib/kibana';
2222
import * as i18nCommon from '../../../common/translations';
2323
import * as i18n from './translations';
24+
import { useInvalidateFetchPrebuiltRuleBaseVersionQuery } from '../../../rule_management/api/hooks/prebuilt_rules/use_fetch_prebuilt_rule_base_version_query';
2425

2526
interface RuleImportModalProps {
2627
isImportModalVisible: boolean;
@@ -44,14 +45,17 @@ export function RuleImportModal({ isImportModalVisible, hideImportModal }: RuleI
4445
const invalidateFindRulesQuery = useInvalidateFindRulesQuery();
4546
const invalidateFetchCoverageOverviewQuery = useInvalidateFetchCoverageOverviewQuery();
4647
const invalidateFetchRuleManagementFilters = useInvalidateFetchRuleManagementFiltersQuery();
48+
const invalidateFetchPrebuiltRuleBaseVerison = useInvalidateFetchPrebuiltRuleBaseVersionQuery();
4749
const invalidateRules = useCallback(() => {
4850
invalidateFindRulesQuery();
4951
invalidateFetchRuleManagementFilters();
5052
invalidateFetchCoverageOverviewQuery();
53+
invalidateFetchPrebuiltRuleBaseVerison();
5154
}, [
5255
invalidateFindRulesQuery,
5356
invalidateFetchRuleManagementFilters,
5457
invalidateFetchCoverageOverviewQuery,
58+
invalidateFetchPrebuiltRuleBaseVerison,
5559
]);
5660

5761
const handleModalClose = useCallback(() => {

0 commit comments

Comments
 (0)