From ff588ff041d67c75389e3a65788ddd40b470fee8 Mon Sep 17 00:00:00 2001 From: Mark Hopkin Date: Mon, 11 Nov 2024 11:57:03 +0000 Subject: [PATCH] [Entity Analytics] [Entity Store] Fix Asset Criticality index issue when setting up entity engines concurrently (#199486) ## Summary If the Entity Engine setup API was called in parallel for the host and user engine, there was an issue where both calls attempt to create the asset criticality index causing 'resource_already_exists_exception' to be thrown. This pull request fixes this by ignoring the `resource_already_exists_exception` when creating indices in our util. To test, setup both engines concurrently: ``` (curl -H 'Content-Type: application/json' -X POST -H 'kbn-xsrf: true' -H 'elastic-api-version: 2023-10-31' http://elastic:changeme@localhost:5601/api/entity_store/engines/host/init -d '{}' & curl -H 'Content-Type: application/json' -X POST -H 'kbn-xsrf: true' -H 'elastic-api-version: 2023-10-31' http://elastic:changeme@localhost:5601/api/entity_store/engines/user/init -d '{}' & wait) ``` --- .../elasticsearch_assets/entity_index.ts | 18 +++++++----------- .../utils/create_or_update_index.ts | 11 ++++++++++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/security_solution/server/lib/entity_analytics/entity_store/elasticsearch_assets/entity_index.ts b/x-pack/plugins/security_solution/server/lib/entity_analytics/entity_store/elasticsearch_assets/entity_index.ts index 7bc139ea08adf..6fb5935618dfc 100644 --- a/x-pack/plugins/security_solution/server/lib/entity_analytics/entity_store/elasticsearch_assets/entity_index.ts +++ b/x-pack/plugins/security_solution/server/lib/entity_analytics/entity_store/elasticsearch_assets/entity_index.ts @@ -8,6 +8,7 @@ import type { ElasticsearchClient, Logger } from '@kbn/core/server'; import type { EntityType } from '../../../../../common/api/entity_analytics'; import { getEntitiesIndexName } from '../utils'; +import { createOrUpdateIndex } from '../../utils/create_or_update_index'; interface Options { entityType: EntityType; @@ -17,18 +18,13 @@ interface Options { } export const createEntityIndex = async ({ entityType, esClient, namespace, logger }: Options) => { - try { - await esClient.indices.create({ + await createOrUpdateIndex({ + esClient, + logger, + options: { index: getEntitiesIndexName(entityType, namespace), - body: {}, - }); - } catch (e) { - if (e.meta.body.error.type === 'resource_already_exists_exception') { - logger.debug(`Index for ${entityType} already exists, skipping creation.`); - } else { - throw e; - } - } + }, + }); }; export const deleteEntityIndex = ({ entityType, esClient, namespace }: Options) => diff --git a/x-pack/plugins/security_solution/server/lib/entity_analytics/utils/create_or_update_index.ts b/x-pack/plugins/security_solution/server/lib/entity_analytics/utils/create_or_update_index.ts index f9525e14ac6c4..b6e49017c95a7 100644 --- a/x-pack/plugins/security_solution/server/lib/entity_analytics/utils/create_or_update_index.ts +++ b/x-pack/plugins/security_solution/server/lib/entity_analytics/utils/create_or_update_index.ts @@ -51,7 +51,16 @@ export const createOrUpdateIndex = async ({ ); } } else { - return esClient.indices.create(options); + try { + await esClient.indices.create(options); + } catch (err) { + // If the index already exists, we can ignore the error + if (err?.meta?.body?.error?.type === 'resource_already_exists_exception') { + logger.info(`${options.index} already exists`); + } else { + throw err; + } + } } } catch (err) { const error = transformError(err);