Skip to content
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 40 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
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 Dec 4, 2020
e7abd9e
Add note
rylnd Dec 4, 2020
6cc36ae
Add version metadata to signals documents
rylnd Dec 4, 2020
8982776
WIP: Starting over from the ground up
rylnd Dec 4, 2020
3f9cef0
Fleshing out upgrade of signals
rylnd Dec 4, 2020
f415f9a
Fleshing out more of the upgrade path
rylnd Dec 4, 2020
5e49142
Pad the version number of our destination migration index
rylnd Dec 4, 2020
5bf2897
Fleshing out more upgrade finalization
rylnd Dec 4, 2020
36bf30f
Ensure that new signals are generated with an appropriate schema_version
rylnd Dec 4, 2020
afac4fb
Apply migration cleanup policy to obsolete signals indexes
rylnd Dec 4, 2020
7fad15f
Move more logic into component functions
rylnd Dec 4, 2020
3be0e86
Fix type errors
rylnd Dec 4, 2020
fb61f53
Refactor to make things a little more organized
rylnd Dec 4, 2020
d6ce594
Add some JSDoc comments around our new functions
rylnd Dec 4, 2020
8719981
Adds integration tests around migration status route
rylnd Dec 4, 2020
fb115ed
Adds API integration tests for our signals upgrade endpoint
rylnd Dec 4, 2020
3e2e765
WIP: Fleshing out finalization tests
rylnd Dec 4, 2020
e61dd76
Consolidate terminalogy around a migration
rylnd Dec 4, 2020
dd0d6c3
Implement encoding of migration details
rylnd Dec 4, 2020
4f30fc0
Better transformation of errors thrown from the elasticsearch client
rylnd Dec 4, 2020
00aef89
Finishing integration tests around finalization endpoint
rylnd Dec 4, 2020
024bed2
Test an error case due to a reindexing failure
rylnd Dec 4, 2020
5c9477f
Remove unnecessary version info from signals documents
rylnd Dec 4, 2020
95e1faf
Migrate an index relative to the ACTUAL template version
rylnd Dec 4, 2020
bc151de
Enrich our migration_status endpoint with an is_outdated qualification
rylnd Dec 5, 2020
12d1d0f
Update migration scripts
rylnd Dec 5, 2020
8474920
More uniform version checking
rylnd Dec 5, 2020
3a590fc
Fix signal generation unit tests
rylnd Dec 5, 2020
d31c13e
Support reindex options to be sent to create_migration endpoint
rylnd Dec 5, 2020
095cce5
Fix signal generation integration tests
rylnd Dec 5, 2020
2df6e24
Add unit tests for getMigrationStatus
rylnd Dec 7, 2020
70031ad
Add a basic test for getSignalsIndicesInRange
rylnd Dec 7, 2020
d0c2634
Add unit test for the naming of our destination migration index
rylnd Dec 7, 2020
63fb0e9
Merge branch 'master' into signals_migration
rylnd Dec 7, 2020
0701382
Handle write indices in our migration logic
rylnd Dec 7, 2020
3816f9f
Add original hot phase to migration cleanup policy
rylnd Dec 8, 2020
1fcb1d1
Update old comment
rylnd Dec 8, 2020
45a3af7
Delete task document as part of finalization
rylnd Dec 8, 2020
f954218
Accurately report recoverable errors on create_signals_migration route
rylnd Dec 8, 2020
6762afa
Merge branch 'master' into signals_migration
rylnd Dec 9, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions x-pack/plugins/security_solution/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,13 @@ export const TIMELINE_PREPACKAGED_URL = `${TIMELINE_URL}/_prepackaged`;
* Default signals index key for kibana.dev.yml
*/
export const SIGNALS_INDEX_KEY = 'signalsIndex';

export const DETECTION_ENGINE_SIGNALS_URL = `${DETECTION_ENGINE_URL}/signals`;
export const DETECTION_ENGINE_SIGNALS_STATUS_URL = `${DETECTION_ENGINE_SIGNALS_URL}/status`;
export const DETECTION_ENGINE_QUERY_SIGNALS_URL = `${DETECTION_ENGINE_SIGNALS_URL}/search`;
export const DETECTION_ENGINE_SIGNALS_MIGRATION_URL = `${DETECTION_ENGINE_SIGNALS_URL}/migration`;
export const DETECTION_ENGINE_SIGNALS_MIGRATION_STATUS_URL = `${DETECTION_ENGINE_SIGNALS_URL}/migration_status`;
export const DETECTION_ENGINE_SIGNALS_FINALIZE_MIGRATION_URL = `${DETECTION_ENGINE_SIGNALS_URL}/finalize_migration`;

/**
* Common naming convention for an unauthenticated user
Expand Down
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],
});
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>;
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=',
});
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>;
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>;
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,
}));
};
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;
};
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' })
);
});
});
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}`;
Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.


const response = await esClient.indices.create<{ index: string }>({
index: destinationIndexName,
body: {
settings: {
index: {
lifecycle: {
indexing_complete: true,
},
},
},
},
});

return response.body.index;
};
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 } } },
});
Loading