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] refactors lists legacy templates/ILM API calls in Serverless env. #164844

Merged
merged 3 commits into from
Aug 30, 2023
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 @@ -11,7 +11,7 @@ import { LIST_INDEX } from '@kbn/securitysolution-list-constants';

import { createListIndexResponse } from '../../../common/api';
import type { ListsPluginRouter } from '../../types';
import { buildSiemResponse, removeLegacyTemplatesIfExist } from '../utils';
import { buildSiemResponse } from '../utils';
import { getListClient } from '..';

export const createListIndexRoute = (router: ListsPluginRouter): void => {
Expand Down Expand Up @@ -43,8 +43,6 @@ export const createListIndexRoute = (router: ListsPluginRouter): void => {
await lists.setListItemTemplate();
}

await removeLegacyTemplatesIfExist(lists);

if (listDataStreamExists && listItemDataStreamExists) {
return siemResponse.error({
body: `data stream: "${lists.getListName()}" and "${lists.getListItemName()}" already exists`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { LIST_INDEX } from '@kbn/securitysolution-list-constants';
import { ListClient } from '../../services/lists/list_client';
import type { ListsPluginRouter } from '../../types';
import { deleteListIndexResponse } from '../../../common/api';
import { buildSiemResponse, removeLegacyTemplatesIfExist } from '../utils';
import { buildSiemResponse } from '../utils';
import { getListClient } from '..';

/**
Expand Down Expand Up @@ -66,11 +66,17 @@ export const deleteListIndexRoute = (router: ListsPluginRouter): void => {
// ensure data streams deleted if exist
await deleteDataStreams(lists, listDataStreamExists, listItemDataStreamExists);

// ensure indices deleted if exist and were not migrated
await deleteIndices(lists, listIndexExists, listItemIndexExists);
// we need to call this section only if any of these indices exist
// to delete indices, ILM policies and legacy templates
// ILM policies and legacy templates do not exist on serverless,
// so by checking if any of index exists we ensure it is stateful
if (listIndexExists || listItemIndexExists) {
await deleteIndices(lists, listIndexExists, listItemIndexExists);
await lists.deleteLegacyListTemplateIfExists();
await lists.deleteLegacyListItemTemplateIfExists();
}

await deleteIndexTemplates(lists);
await removeLegacyTemplatesIfExist(lists);

const [validated, errors] = validate({ acknowledged: true }, deleteListIndexResponse);
if (errors != null) {
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/lists/server/routes/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* 2.0.
*/

export * from './remove_templates_if_exist';
export * from './get_error_message_exception_list_item';
export * from './get_error_message_exception_list';
export * from './get_list_client';
Expand Down

This file was deleted.

42 changes: 42 additions & 0 deletions x-pack/plugins/lists/server/services/lists/list_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ export class ListClient {
if (await this.getListPolicyExists()) {
await this.deleteListPolicy();
}

// as migration will be called eventually for every instance of Kibana, it's more efficient to delete
// legacy index template if it exists during migration
await this.deleteLegacyListTemplateIfExists();
};

/**
Expand All @@ -353,6 +357,10 @@ export class ListClient {
if (await this.getListItemPolicyExists()) {
await this.deleteListItemPolicy();
}

// as migration will be called eventually for every instance of Kibana, it's more efficient to delete
// legacy index template if it exists during migration
await this.deleteLegacyListItemTemplateIfExists();
};

/**
Expand Down Expand Up @@ -588,6 +596,23 @@ export class ListClient {
return deleteTemplate(esClient, listName);
};

/**
* Checks if legacy lists template exists and delete it
*/
public deleteLegacyListTemplateIfExists = async (): Promise<void> => {
try {
const legacyTemplateExists = await this.getLegacyListTemplateExists();

if (legacyTemplateExists) {
await this.deleteLegacyListTemplate();
}
} catch (err) {
if (err.statusCode !== 404) {
throw err;
}
}
};

/**
* Delete the list item boot strap index for ILM policies.
* @returns The contents of the bootstrap response from Elasticsearch
Expand All @@ -598,6 +623,23 @@ export class ListClient {
return deleteTemplate(esClient, listItemName);
};

/**
* Checks if legacy list item template exists and delete it
*/
public deleteLegacyListItemTemplateIfExists = async (): Promise<void> => {
try {
const legacyTemplateListItemsExists = await this.getLegacyListItemTemplateExists();

if (legacyTemplateListItemsExists) {
await this.deleteLegacyListItemTemplate();
}
} catch (err) {
if (err.statusCode !== 404) {
throw err;
}
}
};

/**
* Given a list item id, this will delete the single list item
* @returns The list item if found, otherwise null
Expand Down