-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Siem Migrations] Adds separate migration index to store migration metadata #216164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
51b03c7
initial commit
logeekal 5ae7eeb
types: housekeeping
logeekal fc80d53
fix: types
logeekal c24ddc0
more types fixes
logeekal c1e75b1
fix: test types
logeekal d781be8
pr feedback: migration create logic
logeekal 941f0d1
pr feedback
logeekal 90e0ac7
fix: types
logeekal c6ff3c7
Merge branch 'main' into feat/migration_index
logeekal 6040a83
fix: add tests
logeekal a9e6c44
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine 60e8450
feedback: better api + types'
logeekal 43b91c5
pr feedback: fix logging
logeekal c6f9708
pr feedback: simple get api + document processing
logeekal ab69d7a
fix creation log
logeekal 2400126
remove debug log
logeekal 9d3cc9a
remove unnecessary artifacts
logeekal e532e46
Merge branch 'main' into feat/migration_index
logeekal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
110 changes: 110 additions & 0 deletions
110
...ution/server/lib/siem_migrations/rules/data/rule_migrations_data_migration_client.test.ts
This file contains hidden or 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,110 @@ | ||
| /* | ||
| * 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 type { IScopedClusterClient } from '@kbn/core/server'; | ||
| import type { SiemRuleMigrationsClientDependencies } from '../types'; | ||
| import { RuleMigrationsDataMigrationClient } from './rule_migrations_data_migration_client'; | ||
| import { elasticsearchServiceMock, loggingSystemMock } from '@kbn/core/server/mocks'; | ||
| import type { AuthenticatedUser } from '@kbn/security-plugin-types-common'; | ||
| import type IndexApi from '@elastic/elasticsearch/lib/api/api'; | ||
| import type GetApi from '@elastic/elasticsearch/lib/api/api/get'; | ||
|
|
||
| describe('RuleMigrationsDataMigrationClient', () => { | ||
| let ruleMigrationsDataMigrationClient: RuleMigrationsDataMigrationClient; | ||
| const esClient = | ||
| elasticsearchServiceMock.createCustomClusterClient() as unknown as IScopedClusterClient; | ||
|
|
||
| const logger = loggingSystemMock.createLogger(); | ||
| const indexNameProvider = jest.fn().mockReturnValue('.kibana-siem-rule-migrations'); | ||
| const currentUser = { | ||
| userName: 'testUser', | ||
| profile_uid: 'testProfileUid', | ||
| } as unknown as AuthenticatedUser; | ||
| const dependencies = {} as unknown as SiemRuleMigrationsClientDependencies; | ||
|
|
||
| beforeEach(() => { | ||
| ruleMigrationsDataMigrationClient = new RuleMigrationsDataMigrationClient( | ||
| indexNameProvider, | ||
| currentUser, | ||
| esClient, | ||
| logger, | ||
| dependencies | ||
| ); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('create', () => { | ||
| test('should create a new migration', async () => { | ||
| const index = '.kibana-siem-rule-migrations'; | ||
|
|
||
| const result = await ruleMigrationsDataMigrationClient.create(); | ||
|
|
||
| expect(result).not.toBeFalsy(); | ||
| expect(esClient.asInternalUser.create).toHaveBeenCalledWith({ | ||
| refresh: 'wait_for', | ||
| id: result, | ||
| index, | ||
| document: { | ||
| created_by: currentUser.profile_uid, | ||
| created_at: expect.any(String), | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test('should throw an error if an error occurs', async () => { | ||
| ( | ||
| esClient.asInternalUser.create as unknown as jest.MockedFn<typeof IndexApi> | ||
| ).mockRejectedValueOnce(new Error('Test error')); | ||
|
|
||
| await expect(ruleMigrationsDataMigrationClient.create()).rejects.toThrow('Test error'); | ||
|
|
||
| expect(esClient.asInternalUser.create).toHaveBeenCalled(); | ||
| expect(logger.error).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('get', () => { | ||
| test('should get a migration', async () => { | ||
| const index = '.kibana-siem-rule-migrations'; | ||
| const id = 'testId'; | ||
| const response = { | ||
| _index: index, | ||
| found: true, | ||
| _source: { | ||
| created_by: currentUser.profile_uid, | ||
| created_at: new Date().toISOString(), | ||
| }, | ||
| _id: id, | ||
| }; | ||
|
|
||
| ( | ||
| esClient.asInternalUser.get as unknown as jest.MockedFn<typeof GetApi> | ||
| ).mockResolvedValueOnce(response); | ||
|
|
||
| const result = await ruleMigrationsDataMigrationClient.get({ id }); | ||
|
|
||
| expect(result).toEqual({ | ||
| ...response._source, | ||
| id: response._id, | ||
| }); | ||
| }); | ||
| test('should throw an error if an error occurs', async () => { | ||
| const id = 'testId'; | ||
| ( | ||
| esClient.asInternalUser.get as unknown as jest.MockedFn<typeof GetApi> | ||
| ).mockRejectedValueOnce(new Error('Test error')); | ||
|
|
||
| await expect(ruleMigrationsDataMigrationClient.get({ id })).rejects.toThrow('Test error'); | ||
|
|
||
| expect(esClient.asInternalUser.get).toHaveBeenCalled(); | ||
| expect(logger.error).toHaveBeenCalledWith(`Error getting migration ${id}: Error: Test error`); | ||
| }); | ||
| }); | ||
| }); |
52 changes: 52 additions & 0 deletions
52
...y_solution/server/lib/siem_migrations/rules/data/rule_migrations_data_migration_client.ts
This file contains hidden or 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,52 @@ | ||
| /* | ||
| * 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 { v4 as uuidV4 } from 'uuid'; | ||
| import type { StoredSiemMigration } from '../types'; | ||
| import { RuleMigrationsDataBaseClient } from './rule_migrations_data_base_client'; | ||
|
|
||
| export class RuleMigrationsDataMigrationClient extends RuleMigrationsDataBaseClient { | ||
| async create(): Promise<string> { | ||
| const migrationId = uuidV4(); | ||
| const index = await this.getIndexName(); | ||
| const profileUid = await this.getProfileUid(); | ||
| const createdAt = new Date().toISOString(); | ||
|
|
||
| await this.esClient | ||
| .create({ | ||
| refresh: 'wait_for', | ||
| id: migrationId, | ||
| index, | ||
| document: { | ||
| created_by: profileUid, | ||
| created_at: createdAt, | ||
| }, | ||
| }) | ||
| .catch((error) => { | ||
| this.logger.error(`Error creating migration ${migrationId}: ${error}`); | ||
| throw error; | ||
| }); | ||
|
|
||
| return migrationId; | ||
| } | ||
|
|
||
| async get({ id }: { id: string }): Promise<StoredSiemMigration> { | ||
| const index = await this.getIndexName(); | ||
| return this.esClient | ||
| .get<StoredSiemMigration>({ | ||
| index, | ||
| id, | ||
| }) | ||
| .then((document) => { | ||
| return this.processHit(document); | ||
| }) | ||
| .catch((error) => { | ||
| this.logger.error(`Error getting migration ${id}: ${error}`); | ||
| throw error; | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.