Skip to content

Commit 65594d1

Browse files
[8.x] [Rules migration][Integration test] Get Prebuilt Rules APIs (#11232) (#211403) (#211427)
# Backport This will backport the following commits from `main` to `8.x`: - [[Rules migration][Integration test] Get Prebuilt Rules APIs (#11232) (#211403)](#211403) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Ievgen Sorokopud","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-02-17T12:31:30Z","message":"[Rules migration][Integration test] Get Prebuilt Rules APIs (#11232) (#211403)\n\n## Summary\r\n\r\n[Internal link](https://github.com/elastic/security-team/issues/10820)\r\nto the feature details\r\n\r\nPart of https://github.com/elastic/security-team/issues/11232\r\n\r\nThis PR covers SIEM Migrations Get prebuilt rules API (route: `GET\r\n/internal/siem_migrations/rules/{migration_id}/prebuilt_rules`)\r\nintegration test:\r\n* get all prebuilt rules matched by migration rules\r\n* return empty response when migration rules did not match prebuilt\r\nrules","sha":"0adce7a3dbdfc8d6a6bbdff3765da1d05e12c0fb","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:Threat Hunting","Team: SecuritySolution","backport:version","v8.18.0","v9.1.0","v8.19.0"],"title":"[Rules migration][Integration test] Get Prebuilt Rules APIs (#11232)","number":211403,"url":"https://github.com/elastic/kibana/pull/211403","mergeCommit":{"message":"[Rules migration][Integration test] Get Prebuilt Rules APIs (#11232) (#211403)\n\n## Summary\r\n\r\n[Internal link](https://github.com/elastic/security-team/issues/10820)\r\nto the feature details\r\n\r\nPart of https://github.com/elastic/security-team/issues/11232\r\n\r\nThis PR covers SIEM Migrations Get prebuilt rules API (route: `GET\r\n/internal/siem_migrations/rules/{migration_id}/prebuilt_rules`)\r\nintegration test:\r\n* get all prebuilt rules matched by migration rules\r\n* return empty response when migration rules did not match prebuilt\r\nrules","sha":"0adce7a3dbdfc8d6a6bbdff3765da1d05e12c0fb"}},"sourceBranch":"main","suggestedTargetBranches":["9.0","8.18","8.x"],"targetPullRequestStates":[{"branch":"9.0","label":"v9.0.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/211403","number":211403,"mergeCommit":{"message":"[Rules migration][Integration test] Get Prebuilt Rules APIs (#11232) (#211403)\n\n## Summary\r\n\r\n[Internal link](https://github.com/elastic/security-team/issues/10820)\r\nto the feature details\r\n\r\nPart of https://github.com/elastic/security-team/issues/11232\r\n\r\nThis PR covers SIEM Migrations Get prebuilt rules API (route: `GET\r\n/internal/siem_migrations/rules/{migration_id}/prebuilt_rules`)\r\nintegration test:\r\n* get all prebuilt rules matched by migration rules\r\n* return empty response when migration rules did not match prebuilt\r\nrules","sha":"0adce7a3dbdfc8d6a6bbdff3765da1d05e12c0fb"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Ievgen Sorokopud <[email protected]>
1 parent 9181d0c commit 65594d1

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import expect from 'expect';
9+
import { v4 as uuidv4 } from 'uuid';
10+
import { RuleTranslationResult } from '@kbn/security-solution-plugin/common/siem_migrations/constants';
11+
import { deleteAllRules } from '../../../../../common/utils/security_solution';
12+
import {
13+
RuleMigrationDocument,
14+
createMigrationRules,
15+
defaultElasticRule,
16+
deleteAllMigrationRules,
17+
getMigrationRuleDocuments,
18+
migrationRulesRouteHelpersFactory,
19+
} from '../../utils';
20+
import { FtrProviderContext } from '../../../../ftr_provider_context';
21+
import {
22+
createPrebuiltRuleAssetSavedObjects,
23+
createRuleAssetSavedObject,
24+
deleteAllPrebuiltRuleAssets,
25+
deleteAllTimelines,
26+
} from '../../../detections_response/utils';
27+
28+
export default ({ getService }: FtrProviderContext) => {
29+
const es = getService('es');
30+
const log = getService('log');
31+
const supertest = getService('supertest');
32+
const migrationRulesRoutes = migrationRulesRouteHelpersFactory(supertest);
33+
34+
describe('@ess @serverless @serverlessQA Get Prebuilt Rules API', () => {
35+
beforeEach(async () => {
36+
await deleteAllRules(supertest, log);
37+
await deleteAllTimelines(es, log);
38+
await deleteAllPrebuiltRuleAssets(es, log);
39+
await deleteAllMigrationRules(es);
40+
41+
// Add some prebuilt rules
42+
const ruleAssetSavedObjects = [
43+
createRuleAssetSavedObject({ rule_id: 'rule-1', version: 1 }),
44+
createRuleAssetSavedObject({ rule_id: 'rule-2', version: 1 }),
45+
createRuleAssetSavedObject({ rule_id: 'rule-3', version: 1 }),
46+
createRuleAssetSavedObject({ rule_id: 'rule-4', version: 1 }),
47+
createRuleAssetSavedObject({ rule_id: 'rule-5', version: 1 }),
48+
];
49+
await createPrebuiltRuleAssetSavedObjects(es, ruleAssetSavedObjects);
50+
});
51+
52+
it('should return all prebuilt rules matched by migration rules', async () => {
53+
const migrationId = uuidv4();
54+
55+
const overrideCallback = (index: number): Partial<RuleMigrationDocument> => {
56+
const { query_language: queryLanguage, query, ...rest } = defaultElasticRule;
57+
return {
58+
migration_id: migrationId,
59+
elastic_rule: index < 2 ? { ...rest, prebuilt_rule_id: `rule-${index + 1}` } : undefined,
60+
translation_result: index < 2 ? RuleTranslationResult.FULL : undefined,
61+
};
62+
};
63+
const migrationRuleDocuments = getMigrationRuleDocuments(4, overrideCallback);
64+
await createMigrationRules(es, migrationRuleDocuments);
65+
66+
const response = await migrationRulesRoutes.getPrebuiltRules({ migrationId });
67+
68+
const prebuiltRulesIds = Object.keys(response.body).sort();
69+
expect(prebuiltRulesIds).toEqual(['rule-1', 'rule-2']);
70+
});
71+
72+
it('should return empty response when migration rules did not match prebuilt rules', async () => {
73+
const migrationId = uuidv4();
74+
75+
const migrationRuleDocuments = getMigrationRuleDocuments(10, () => ({
76+
migration_id: migrationId,
77+
}));
78+
await createMigrationRules(es, migrationRuleDocuments);
79+
80+
const response = await migrationRulesRoutes.getPrebuiltRules({ migrationId });
81+
expect(response.body).toEqual({});
82+
});
83+
});
84+
};

x-pack/test/security_solution_api_integration/test_suites/siem_migrations/rules/trial_license_complete_tier/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { FtrProviderContext } from '../../../../ftr_provider_context';
99
export default function ({ loadTestFile }: FtrProviderContext) {
1010
describe('@ess SecuritySolution SIEM Migrations', () => {
1111
loadTestFile(require.resolve('./create'));
12+
loadTestFile(require.resolve('./get_prebuilt_rules'));
1213
loadTestFile(require.resolve('./get'));
1314
loadTestFile(require.resolve('./install'));
1415
loadTestFile(require.resolve('./stats'));

x-pack/test/security_solution_api_integration/test_suites/siem_migrations/utils/rules.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { replaceParams } from '@kbn/openapi-common/shared';
1515
import {
1616
SIEM_RULE_MIGRATIONS_ALL_STATS_PATH,
1717
SIEM_RULE_MIGRATIONS_PATH,
18+
SIEM_RULE_MIGRATIONS_PREBUILT_RULES_PATH,
1819
SIEM_RULE_MIGRATION_INSTALL_PATH,
1920
SIEM_RULE_MIGRATION_PATH,
2021
SIEM_RULE_MIGRATION_STATS_PATH,
@@ -23,6 +24,7 @@ import {
2324
import {
2425
CreateRuleMigrationResponse,
2526
GetAllStatsRuleMigrationResponse,
27+
GetRuleMigrationPrebuiltRulesResponse,
2628
GetRuleMigrationRequestQuery,
2729
GetRuleMigrationResponse,
2830
GetRuleMigrationStatsResponse,
@@ -184,5 +186,21 @@ export const migrationRulesRouteHelpersFactory = (supertest: SuperTest.Agent) =>
184186

185187
return response;
186188
},
189+
190+
getPrebuiltRules: async ({
191+
migrationId,
192+
expectStatusCode = 200,
193+
}: MigrationRequestParams): Promise<{ body: GetRuleMigrationPrebuiltRulesResponse }> => {
194+
const response = await supertest
195+
.get(replaceParams(SIEM_RULE_MIGRATIONS_PREBUILT_RULES_PATH, { migration_id: migrationId }))
196+
.set('kbn-xsrf', 'true')
197+
.set(ELASTIC_HTTP_VERSION_HEADER, API_VERSIONS.internal.v1)
198+
.set(X_ELASTIC_INTERNAL_ORIGIN_REQUEST, 'kibana')
199+
.send();
200+
201+
assertStatusCode(expectStatusCode, response);
202+
203+
return response;
204+
},
187205
};
188206
};

0 commit comments

Comments
 (0)