Skip to content

Commit

Permalink
[Security Solution][Detection engine] refactors lists legacy template…
Browse files Browse the repository at this point in the history
…s/ILM API calls in Serverless env. (#164844)

## Summary

- moved deletion of legacy index template inside to
`migrateListIndexToDataStream` and `migrateListToDataStream`. That would
allow us not to rely on `410` error to tell if we are in Serverless
environment and `_template` API is blocked. Migrate to DS function is
called only in Stateful environment, as lists indices do not exist in
Serverless
- deletion of legacy index template during migration has also other
benefit: it will be called eventually for every instance of Kibana, and
be more efficient then just calling it during index creation
  • Loading branch information
vitaliidm authored Aug 30, 2023
1 parent cc728f6 commit 116c4b8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 36 deletions.
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

0 comments on commit 116c4b8

Please sign in to comment.