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][Detection Engine] adds legacy siem signals telemetry #202671

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,30 @@ interface IndexAlias {
*
* @param esClient An {@link ElasticsearchClient}
* @param alias alias name used to filter results
* @param index index name used to filter results
*
* @returns an array of {@link IndexAlias} objects
*/
export const getIndexAliases = async ({
esClient,
alias,
index,
}: {
esClient: ElasticsearchClient;
alias: string;
index?: string;
}): Promise<IndexAlias[]> => {
const response = await esClient.indices.getAlias(
{
name: alias,
...(index ? { index } : {}),
},
{ meta: true }
);

return Object.keys(response.body).map((index) => ({
return Object.keys(response.body).map((indexName) => ({
alias,
index,
isWriteIndex: response.body[index].aliases[alias]?.is_write_index === true,
index: indexName,
isWriteIndex: response.body[indexName].aliases[alias]?.is_write_index === true,
}));
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type { SignalsMigrationSO } from './saved_objects_schema';
/**
* Deletes a completed migration:
* * deletes the migration SO
* * deletes the underlying task document
* * applies deletion policy to the relevant index
*
* @param esClient An {@link ElasticsearchClient}
Expand All @@ -40,7 +39,7 @@ export const deleteMigration = async ({
return migration;
}

const { destinationIndex, sourceIndex, taskId } = migration.attributes;
const { destinationIndex, sourceIndex } = migration.attributes;

if (isMigrationFailed(migration)) {
await applyMigrationCleanupPolicy({
Expand All @@ -57,7 +56,6 @@ export const deleteMigration = async ({
});
}

await esClient.delete({ index: '.tasks', id: taskId });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for tracking this one down 👍

await deleteMigrationSavedObject({ id: migration.id, soClient });

return migration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('finalizeMigration', () => {
signalsAlias: 'my-signals-alias',
soClient,
username: 'username',
legacySiemSignalsAlias: '.siem-signals-default',
});

expect(updateMigrationSavedObject).not.toHaveBeenCalled();
Expand All @@ -54,6 +55,7 @@ describe('finalizeMigration', () => {
signalsAlias: 'my-signals-alias',
soClient,
username: 'username',
legacySiemSignalsAlias: '.siem-signals-default',
});

expect(updateMigrationSavedObject).not.toHaveBeenCalled();
Expand All @@ -72,6 +74,7 @@ describe('finalizeMigration', () => {
signalsAlias: 'my-signals-alias',
soClient,
username: 'username',
legacySiemSignalsAlias: '.siem-signals-default',
});

expect(updateMigrationSavedObject).toHaveBeenCalledWith(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ export const finalizeMigration = async ({
signalsAlias,
soClient,
username,
legacySiemSignalsAlias,
}: {
esClient: ElasticsearchClient;
migration: SignalsMigrationSO;
signalsAlias: string;
soClient: SavedObjectsClientContract;
username: string;
legacySiemSignalsAlias: string;
}): Promise<SignalsMigrationSO> => {
if (!isMigrationPending(migration)) {
return migration;
Expand Down Expand Up @@ -86,6 +88,7 @@ export const finalizeMigration = async ({
esClient,
newIndex: destinationIndex,
oldIndex: sourceIndex,
legacySiemSignalsAlias,
});

const updatedMigration = await updateMigrationSavedObject({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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 { elasticsearchServiceMock } from '@kbn/core/server/mocks';
import { getIndexAliasPerSpace } from './get_index_alias_per_space';

describe('getIndexAliasPerSpace', () => {
let esClient: ReturnType<typeof elasticsearchServiceMock.createElasticsearchClient>;

beforeEach(() => {
esClient = elasticsearchServiceMock.createElasticsearchClient();
});

it('returns object with index alias and space', async () => {
esClient.indices.getAlias.mockResponseOnce({
'.siem-signals-default-old-one': {
aliases: {
'.siem-signals-default': {
is_write_index: false,
},
},
},
'.siem-signals-another-1-legacy': {
aliases: {
'.siem-signals-another-1': {
is_write_index: false,
},
},
},
});

const result = await getIndexAliasPerSpace({
esClient,
signalsIndex: '.siem-signals',
signalsAliasAllSpaces: '.siem-signals-*',
});

expect(result).toEqual({
'.siem-signals-another-1-legacy': {
alias: '.siem-signals-another-1',
indexName: '.siem-signals-another-1-legacy',
space: 'another-1',
},
'.siem-signals-default-old-one': {
alias: '.siem-signals-default',
indexName: '.siem-signals-default-old-one',
space: 'default',
},
});
});

it('filters out .internal.alert indices', async () => {
esClient.indices.getAlias.mockResponseOnce({
'.siem-signals-default-old-one': {
aliases: {
'.siem-signals-default': {
is_write_index: false,
},
},
},
'.internal.alerts-security.alerts-another-2-000001': {
aliases: {
'.siem-signals-another-2': {
is_write_index: false,
},
},
},
});

const result = await getIndexAliasPerSpace({
esClient,
signalsIndex: '.siem-signals',
signalsAliasAllSpaces: '.siem-signals-*',
});

expect(result).toEqual({
'.siem-signals-default-old-one': {
alias: '.siem-signals-default',
indexName: '.siem-signals-default-old-one',
space: 'default',
},
});
});
});
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 type { ElasticsearchClient } from '@kbn/core/server';

interface IndexAlias {
alias: string;
space: string;
indexName: string;
}

/**
* Retrieves index, its alias and Kibana space
*/
export const getIndexAliasPerSpace = async ({
esClient,
signalsIndex,
signalsAliasAllSpaces,
}: {
esClient: ElasticsearchClient;
signalsIndex: string;
signalsAliasAllSpaces: string;
}): Promise<Record<string, IndexAlias>> => {
const response = await esClient.indices.getAlias(
{
name: signalsAliasAllSpaces,
},
{ meta: true }
);

const indexAliasesMap = Object.keys(response.body).reduce<Record<string, IndexAlias>>(
(acc, indexName) => {
if (!indexName.startsWith('.internal.alerts-')) {
const alias = Object.keys(response.body[indexName].aliases)[0];

acc[indexName] = {
alias,
space: alias.replace(`${signalsIndex}-`, ''),
indexName,
};
}

return acc;
},
{}
);

return indexAliasesMap;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 { IndicesGetIndexTemplateResponse } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { elasticsearchServiceMock } from '@kbn/core/server/mocks';
import { getLatestIndexTemplateVersion } from './get_latest_index_template_version';

describe('getIndexAliasPerSpace', () => {
let esClient: ReturnType<typeof elasticsearchServiceMock.createElasticsearchClient>;

beforeEach(() => {
esClient = elasticsearchServiceMock.createElasticsearchClient();
});

it('returns latest index template version', async () => {
esClient.indices.getIndexTemplate.mockResponseOnce({
index_templates: [
{ index_template: { version: 77 } },
{ index_template: { version: 10 } },
{ index_template: { version: 23 } },
{ index_template: { version: 0 } },
],
} as IndicesGetIndexTemplateResponse);

const version = await getLatestIndexTemplateVersion({
esClient,
name: '.siem-signals-*',
});

expect(version).toBe(77);
});

it('returns 0 if templates empty', async () => {
esClient.indices.getIndexTemplate.mockResponseOnce({
index_templates: [],
});

const version = await getLatestIndexTemplateVersion({
esClient,
name: '.siem-signals-*',
});

expect(version).toBe(0);
});

it('returns 0 if request fails', async () => {
esClient.indices.getIndexTemplate.mockRejectedValueOnce('Failure');

const version = await getLatestIndexTemplateVersion({
esClient,
name: '.siem-signals-*',
});

expect(version).toBe(0);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 { ElasticsearchClient } from '@kbn/core/server';

/**
* Retrieves the latest version of index template
* There are can be multiple index templates across different Kibana spaces,
* so we get them all and return the latest(greatest) number
*/
export const getLatestIndexTemplateVersion = async ({
esClient,
name,
}: {
esClient: ElasticsearchClient;
name: string;
}): Promise<number> => {
let latestTemplateVersion: number;
try {
const response = await esClient.indices.getIndexTemplate({ name });
const versions = response.index_templates.map(
(template) => template.index_template.version ?? 0
);

latestTemplateVersion = versions.length ? Math.max(...versions) : 0;
} catch (e) {
latestTemplateVersion = 0;
}

return latestTemplateVersion;
};
Loading
Loading