mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[Cloud Security] Plugin Initialize - Updating logs-cloud_security_posture.findings_latest-default
index mappings (#151460)
This commit is contained in:
parent
ffb555d00b
commit
eba0dc6eb5
1 changed files with 37 additions and 1 deletions
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue