diff --git a/x-pack/plugins/cloud_security_posture/server/create_indices/create_indices.ts b/x-pack/plugins/cloud_security_posture/server/create_indices/create_indices.ts index 5702cc79eba8a..93ea23a4e5b1e 100644 --- a/x-pack/plugins/cloud_security_posture/server/create_indices/create_indices.ts +++ b/x-pack/plugins/cloud_security_posture/server/create_indices/create_indices.ts @@ -6,6 +6,7 @@ */ import type { ElasticsearchClient, Logger } from '@kbn/core/server'; import { errors } from '@elastic/elasticsearch'; +import type { MappingTypeMapping } from '@elastic/elasticsearch/lib/api/types'; import { BENCHMARK_SCORE_INDEX_DEFAULT_NS, BENCHMARK_SCORE_INDEX_PATTERN, @@ -107,7 +108,21 @@ const createLatestFindingsIndex = async (esClient: ElasticsearchClient, logger: composed_of, }); - await createIndexSafe(esClient, logger, LATEST_FINDINGS_INDEX_DEFAULT_NS); + const result = await createIndexSafe(esClient, logger, LATEST_FINDINGS_INDEX_DEFAULT_NS); + + if (result === 'already-exists') { + // Make sure mappings are up-to-date + const simulateResponse = await esClient.indices.simulateTemplate({ + name: LATEST_FINDINGS_INDEX_TEMPLATE_NAME, + }); + + await updateIndexSafe( + esClient, + logger, + LATEST_FINDINGS_INDEX_DEFAULT_NS, + simulateResponse.template.mappings + ); + } } catch (e) { logger.error( `Failed to upsert index template [Template: ${LATEST_FINDINGS_INDEX_TEMPLATE_NAME}]` @@ -155,11 +170,32 @@ const createIndexSafe = async (esClient: ElasticsearchClient, logger: Logger, in }); logger.info(`Created index successfully [Name: ${index}]`); + return 'success'; } else { logger.trace(`Index already exists [Name: ${index}]`); + return 'already-exists'; } } catch (e) { logger.error(`Failed to create index [Name: ${index}]`); logger.error(e); + return 'fail'; + } +}; + +const updateIndexSafe = async ( + esClient: ElasticsearchClient, + logger: Logger, + index: string, + mappings: MappingTypeMapping +) => { + try { + await esClient.indices.putMapping({ + index, + properties: mappings.properties, + }); + logger.info(`Updated index successfully [Name: ${index}]`); + } catch (e) { + logger.error(`Failed to update index [Name: ${index}]`); + logger.error(e); } };