Skip to content

Commit

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

## Summary

[Internal link](elastic/security-team#10820)
to the feature details

Part of elastic/security-team#11232

This PR covers SIEM Migrations CREATE API (route:
`/internal/siem_migrations/rules/{migration_id?}`) integration test:
* Create migration with provided ID
* Create migration without provided ID
* Create migration with rules that have resources
* Error handling ("no content") when no rules provided

Also, as part of this PR, I addressed this comment to my previous
changes
#210867 (comment)
  • Loading branch information
e40pud authored Feb 13, 2025
1 parent 4fe72b6 commit 44fdf81
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 44fdf81

Please sign in to comment.