-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Security Solution][Detections] Signals Migration API #84721
Merged
Merged
Changes from 35 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
916268e
WIP: basic reindexing works, lots of edge cases and TODOs to tackle
rylnd e7abd9e
Add note
rylnd 6cc36ae
Add version metadata to signals documents
rylnd 8982776
WIP: Starting over from the ground up
rylnd 3f9cef0
Fleshing out upgrade of signals
rylnd f415f9a
Fleshing out more of the upgrade path
rylnd 5e49142
Pad the version number of our destination migration index
rylnd 5bf2897
Fleshing out more upgrade finalization
rylnd 36bf30f
Ensure that new signals are generated with an appropriate schema_version
rylnd afac4fb
Apply migration cleanup policy to obsolete signals indexes
rylnd 7fad15f
Move more logic into component functions
rylnd 3be0e86
Fix type errors
rylnd fb61f53
Refactor to make things a little more organized
rylnd d6ce594
Add some JSDoc comments around our new functions
rylnd 8719981
Adds integration tests around migration status route
rylnd fb115ed
Adds API integration tests for our signals upgrade endpoint
rylnd 3e2e765
WIP: Fleshing out finalization tests
rylnd e61dd76
Consolidate terminalogy around a migration
rylnd dd0d6c3
Implement encoding of migration details
rylnd 4f30fc0
Better transformation of errors thrown from the elasticsearch client
rylnd 00aef89
Finishing integration tests around finalization endpoint
rylnd 024bed2
Test an error case due to a reindexing failure
rylnd 5c9477f
Remove unnecessary version info from signals documents
rylnd 95e1faf
Migrate an index relative to the ACTUAL template version
rylnd bc151de
Enrich our migration_status endpoint with an is_outdated qualification
rylnd 12d1d0f
Update migration scripts
rylnd 8474920
More uniform version checking
rylnd 3a590fc
Fix signal generation unit tests
rylnd d31c13e
Support reindex options to be sent to create_migration endpoint
rylnd 095cce5
Fix signal generation integration tests
rylnd 2df6e24
Add unit tests for getMigrationStatus
rylnd 70031ad
Add a basic test for getSignalsIndicesInRange
rylnd d0c2634
Add unit test for the naming of our destination migration index
rylnd 63fb0e9
Merge branch 'master' into signals_migration
rylnd 0701382
Handle write indices in our migration logic
rylnd 3816f9f
Add original hot phase to migration cleanup policy
rylnd 1fcb1d1
Update old comment
rylnd 45a3af7
Delete task document as part of finalization
rylnd f954218
Accurately report recoverable errors on create_signals_migration route
rylnd 6762afa
Merge branch 'master' into signals_migration
rylnd 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 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
13 changes: 13 additions & 0 deletions
13
..._solution/common/detection_engine/schemas/request/create_signals_migration_schema.mock.ts
This file contains 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,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { CreateSignalsMigrationSchema } from './create_signals_migration_schema'; | ||
|
||
export const getCreateSignalsMigrationSchemaMock = ( | ||
index: string = 'signals-index' | ||
): CreateSignalsMigrationSchema => ({ | ||
index: [index], | ||
}); |
29 changes: 29 additions & 0 deletions
29
...urity_solution/common/detection_engine/schemas/request/create_signals_migration_schema.ts
This file contains 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,29 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import * as t from 'io-ts'; | ||
|
||
import { index } from '../common/schemas'; | ||
import { PositiveInteger, PositiveIntegerGreaterThanZero } from '../types'; | ||
|
||
export const signalsReindexOptions = t.partial({ | ||
requests_per_second: t.number, | ||
size: PositiveIntegerGreaterThanZero, | ||
slices: PositiveInteger, | ||
}); | ||
|
||
export type SignalsReindexOptions = t.TypeOf<typeof signalsReindexOptions>; | ||
|
||
export const createSignalsMigrationSchema = t.intersection([ | ||
t.exact( | ||
t.type({ | ||
index, | ||
}) | ||
), | ||
t.exact(signalsReindexOptions), | ||
]); | ||
|
||
export type CreateSignalsMigrationSchema = t.TypeOf<typeof createSignalsMigrationSchema>; |
12 changes: 12 additions & 0 deletions
12
...olution/common/detection_engine/schemas/request/finalize_signals_migration_schema.mock.ts
This file contains 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,12 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { FinalizeSignalsMigrationSchema } from './finalize_signals_migration_schema'; | ||
|
||
export const getFinalizeSignalsMigrationSchemaMock = (): FinalizeSignalsMigrationSchema => ({ | ||
migration_token: | ||
'eyJkZXN0aW5hdGlvbkluZGV4IjoiZGVzdGluYXRpb25JbmRleCIsInNvdXJjZUluZGV4Ijoic291cmNlSW5kZXgiLCJ0YXNrSWQiOiJteS10YXNrLWlkIn0=', | ||
}); |
19 changes: 19 additions & 0 deletions
19
...ity_solution/common/detection_engine/schemas/request/finalize_signals_migration_schema.ts
This file contains 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,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import * as t from 'io-ts'; | ||
|
||
import { NonEmptyString } from '../types'; | ||
|
||
const migrationToken = NonEmptyString; | ||
|
||
export const finalizeSignalsMigrationSchema = t.exact( | ||
t.type({ | ||
migration_token: migrationToken, | ||
}) | ||
); | ||
|
||
export type FinalizeSignalsMigrationSchema = t.TypeOf<typeof finalizeSignalsMigrationSchema>; |
17 changes: 17 additions & 0 deletions
17
.../security_solution/common/detection_engine/schemas/request/get_migration_status_schema.ts
This file contains 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,17 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import * as t from 'io-ts'; | ||
|
||
import { from } from '../common/schemas'; | ||
|
||
export const getMigrationStatusSchema = t.exact( | ||
t.type({ | ||
from, | ||
}) | ||
); | ||
|
||
export type GetMigrationStatusSchema = t.TypeOf<typeof getMigrationStatusSchema>; |
49 changes: 49 additions & 0 deletions
49
x-pack/plugins/security_solution/server/lib/detection_engine/index/get_index_aliases.ts
This file contains 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,49 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { ElasticsearchClient } from 'src/core/server'; | ||
|
||
interface AliasesResponse { | ||
[indexName: string]: { | ||
aliases: { | ||
[aliasName: string]: { | ||
is_write_index: boolean; | ||
}; | ||
}; | ||
}; | ||
} | ||
|
||
interface IndexAlias { | ||
alias: string; | ||
index: string; | ||
isWriteIndex: boolean; | ||
} | ||
|
||
/** | ||
* Retrieves all index aliases for a given alias name | ||
* | ||
* @param esClient An {@link ElasticsearchClient} | ||
* @param alias alias name used to filter results | ||
* | ||
* @returns an array of {@link IndexAlias} objects | ||
*/ | ||
export const getIndexAliases = async ({ | ||
esClient, | ||
alias, | ||
}: { | ||
esClient: ElasticsearchClient; | ||
alias: string; | ||
}): Promise<IndexAlias[]> => { | ||
const response = await esClient.indices.getAlias<AliasesResponse>({ | ||
name: alias, | ||
}); | ||
|
||
return Object.keys(response.body).map((index) => ({ | ||
alias, | ||
index, | ||
isWriteIndex: response.body[index].aliases[alias]?.is_write_index === true, | ||
})); | ||
}; |
29 changes: 29 additions & 0 deletions
29
x-pack/plugins/security_solution/server/lib/detection_engine/index/get_index_count.ts
This file contains 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,29 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { ElasticsearchClient } from 'src/core/server'; | ||
|
||
/** | ||
* Retrieves the count of documents in a given index | ||
* | ||
* @param esClient An {@link ElasticsearchClient} | ||
* @param index index whose documents will be counted | ||
* | ||
* @returns the document count | ||
*/ | ||
export const getIndexCount = async ({ | ||
esClient, | ||
index, | ||
}: { | ||
esClient: ElasticsearchClient; | ||
index: string; | ||
}): Promise<number> => { | ||
const response = await esClient.count<{ count: number }>({ | ||
index, | ||
}); | ||
|
||
return response.body.count; | ||
}; |
25 changes: 25 additions & 0 deletions
25
...ty_solution/server/lib/detection_engine/migrations/create_signals_migration_index.test.ts
This file contains 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,25 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { ElasticsearchClient } from 'src/core/server'; | ||
import { elasticsearchServiceMock } from 'src/core/server/mocks'; | ||
import { createSignalsMigrationIndex } from './create_signals_migration_index'; | ||
|
||
describe('getMigrationStatus', () => { | ||
let esClient: ElasticsearchClient; | ||
|
||
beforeEach(() => { | ||
esClient = elasticsearchServiceMock.createElasticsearchClient(); | ||
}); | ||
|
||
it('creates an index suffixed with the template version', async () => { | ||
await createSignalsMigrationIndex({ esClient, index: 'my-signals-index', version: 4 }); | ||
|
||
expect(esClient.indices.create).toHaveBeenCalledWith( | ||
expect.objectContaining({ index: 'my-signals-index-r000004' }) | ||
); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
...ecurity_solution/server/lib/detection_engine/migrations/create_signals_migration_index.ts
This file contains 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,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { ElasticsearchClient } from 'src/core/server'; | ||
|
||
/** | ||
* Creates the destination index to be used during the migration of a | ||
* given signals index. | ||
* | ||
* The destination index's name is determined by adding a suffix of | ||
* `-r${templateVersion}` to the source index name | ||
* | ||
* @param esClient An {@link ElasticsearchClient} | ||
* @param index name of the source signals index | ||
* @param version version of the current signals template/mappings | ||
* | ||
* @returns the name of the created index | ||
*/ | ||
export const createSignalsMigrationIndex = async ({ | ||
esClient, | ||
index, | ||
version, | ||
}: { | ||
esClient: ElasticsearchClient; | ||
index: string; | ||
version: number; | ||
}): Promise<string> => { | ||
const paddedVersion = `${version}`.padStart(6, '0'); | ||
const destinationIndexName = `${index}-r${paddedVersion}`; | ||
|
||
const response = await esClient.indices.create<{ index: string }>({ | ||
index: destinationIndexName, | ||
body: { | ||
settings: { | ||
index: { | ||
lifecycle: { | ||
indexing_complete: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return response.body.index; | ||
}; |
32 changes: 32 additions & 0 deletions
32
...ins/security_solution/server/lib/detection_engine/migrations/get_migration_status.mock.ts
This file contains 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,32 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { IndexMappingsResponse, MigrationStatusSearchResponse } from './types'; | ||
|
||
export const getMigrationStatusSearchResponseMock = ( | ||
indices: string[] = ['signals-index'], | ||
signalVersions: number[] = [-1] | ||
): MigrationStatusSearchResponse => ({ | ||
aggregations: { | ||
signals_indices: { | ||
buckets: indices.map((index) => ({ | ||
key: index, | ||
signal_versions: { | ||
buckets: signalVersions.map((version) => ({ | ||
key: version, | ||
doc_count: 4, | ||
})), | ||
}, | ||
})), | ||
}, | ||
}, | ||
}); | ||
|
||
export const getIndexMappingsResponseMock = ( | ||
index: string = 'signals-index' | ||
): IndexMappingsResponse => ({ | ||
[index]: { mappings: { _meta: { version: -1 } } }, | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's how we're naming our destination indexes, so e.g. migrating
.siem-signals-default-000002
to template 14 would reindex into.siem-signals-default-000002-r000014
.Note that this is additive as well, so if on a subequent upgrade the user migrated this index again, it'd be something like
.siem-signals-default-000002-r000014-r000021
etc. This allows us to track all the migrations that have been performed on a given index, but it also places a limit on the number of times they can migrate before hitting the index name limit.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the
0
padding on the version numbers? Could reduce some noise and allow them to migrate more before hitting the name length limit.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The padding was added to assist sorting these indexes by name. It's not strictly necessary and not being relied upon anywhere, so we could definitely remove it.