Skip to content

Commit

Permalink
[9.0] [Rules migration][Integration test] Create migration API (#11232)…
Browse files Browse the repository at this point in the history
… (#210999) (#211085)

# Backport

This will backport the following commits from `main` to `9.0`:
- [[Rules migration][Integration test] Create migration API (#11232)
(#210999)](#210999)

<!--- 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-13T19:20:06Z","message":"[Rules
migration][Integration test] Create migration API (#11232)
(#210999)\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 CREATE API
(route:\r\n`/internal/siem_migrations/rules/{migration_id?}`)
integration test:\r\n* Create migration with provided ID\r\n* Create
migration without provided ID\r\n* Create migration with rules that have
resources\r\n* Error handling (\"no content\") when no rules
provided\r\n\r\nAlso, as part of this PR, I addressed this comment to my
previous\r\nchanges\r\nhttps://github.com//pull/210867#discussion_r1954344990","sha":"44fdf81bbec5c053ebccfbdbb062d39de0d7358c","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] Create migration API
(#11232)","number":210999,"url":"https://github.com/elastic/kibana/pull/210999","mergeCommit":{"message":"[Rules
migration][Integration test] Create migration API (#11232)
(#210999)\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 CREATE API
(route:\r\n`/internal/siem_migrations/rules/{migration_id?}`)
integration test:\r\n* Create migration with provided ID\r\n* Create
migration without provided ID\r\n* Create migration with rules that have
resources\r\n* Error handling (\"no content\") when no rules
provided\r\n\r\nAlso, as part of this PR, I addressed this comment to my
previous\r\nchanges\r\nhttps://github.com//pull/210867#discussion_r1954344990","sha":"44fdf81bbec5c053ebccfbdbb062d39de0d7358c"}},"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/210999","number":210999,"mergeCommit":{"message":"[Rules
migration][Integration test] Create migration API (#11232)
(#210999)\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 CREATE API
(route:\r\n`/internal/siem_migrations/rules/{migration_id?}`)
integration test:\r\n* Create migration with provided ID\r\n* Create
migration without provided ID\r\n* Create migration with rules that have
resources\r\n* Error handling (\"no content\") when no rules
provided\r\n\r\nAlso, as part of this PR, I addressed this comment to my
previous\r\nchanges\r\nhttps://github.com//pull/210867#discussion_r1954344990","sha":"44fdf81bbec5c053ebccfbdbb062d39de0d7358c"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Ievgen Sorokopud <[email protected]>
  • Loading branch information
kibanamachine and e40pud authored Feb 13, 2025
1 parent 74b8e15 commit e925dd8
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ export class RuleMigrationsDataBaseClient {
return this.currentUser.profile_uid;
}
const username = this.currentUser.username;
const users = await this.esScopedClient.asCurrentUser.security.getUser({
username,
with_profile_uid: true,
});
return users[username].profile_uid;
try {
const users = await this.esScopedClient.asCurrentUser.security.getUser({
username,
with_profile_uid: true,
});
return users[username].profile_uid;
} catch (error) {
this.logger.error(`Error getting profile_uid for user ${username}: ${error}`);
return username;
}
}

protected processResponseHits<T extends object>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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 expect from 'expect';
import { v4 as uuidv4 } from 'uuid';
import { SiemMigrationStatus } from '@kbn/security-solution-plugin/common/siem_migrations/constants';
import {
defaultOriginalRule,
deleteAllMigrationRules,
migrationResourcesRouteHelpersFactory,
migrationRulesRouteHelpersFactory,
splunkRuleWithResources,
} from '../../utils';
import { FtrProviderContext } from '../../../../ftr_provider_context';

export default ({ getService }: FtrProviderContext) => {
const es = getService('es');
const supertest = getService('supertest');
const migrationRulesRoutes = migrationRulesRouteHelpersFactory(supertest);
const migrationResourcesRoutes = migrationResourcesRouteHelpersFactory(supertest);

describe('@ess @serverless @serverlessQA Create API', () => {
beforeEach(async () => {
await deleteAllMigrationRules(es);
});

it('should create migrations with provided id', async () => {
const migrationId = uuidv4();
await migrationRulesRoutes.create({ migrationId, body: [defaultOriginalRule] });

// fetch migration rule
const response = await migrationRulesRoutes.get({ migrationId });
expect(response.body.total).toEqual(1);

const migrationRule = response.body.data[0];
expect(migrationRule).toEqual(
expect.objectContaining({
migration_id: migrationId,
original_rule: defaultOriginalRule,
status: SiemMigrationStatus.PENDING,
})
);
});

it('should create migrations without provided id', async () => {
const {
body: { migration_id: migrationId },
} = await migrationRulesRoutes.create({ body: [defaultOriginalRule] });

// fetch migration rule
const response = await migrationRulesRoutes.get({ migrationId });
expect(response.body.total).toEqual(1);

const migrationRule = response.body.data[0];
expect(migrationRule).toEqual(
expect.objectContaining({
migration_id: migrationId,
original_rule: defaultOriginalRule,
status: SiemMigrationStatus.PENDING,
})
);
});

it('should create migrations with the rules that have resources', async () => {
const migrationId = uuidv4();
await migrationRulesRoutes.create({ migrationId, body: [splunkRuleWithResources] });

// fetch migration rule
const response = await migrationRulesRoutes.get({ migrationId });
expect(response.body.total).toEqual(1);

const migrationRule = response.body.data[0];
expect(migrationRule).toEqual(
expect.objectContaining({
migration_id: migrationId,
original_rule: splunkRuleWithResources,
status: SiemMigrationStatus.PENDING,
})
);

// fetch missing resources
const resourcesResponse = await migrationResourcesRoutes.getMissingResources({
migrationId,
});
expect(resourcesResponse.body).toEqual([
{ type: 'macro', name: 'summariesonly' },
{ type: 'macro', name: 'drop_dm_object_name(1)' },
{ type: 'lookup', name: 'malware_tracker' },
]);
});

it('should return no content error', async () => {
const migrationId = uuidv4();
await migrationRulesRoutes.create({ migrationId, body: [], expectStatusCode: 204 });

// fetch migration rule
const response = await migrationRulesRoutes.get({ migrationId });
expect(response.body.total).toEqual(0);
});
});
};
Loading

0 comments on commit e925dd8

Please sign in to comment.