import uuid from 'uuid' ; import { join } from 'path' ; import { PathConfigType } from '@osd/utils' ; import { readFile , writeFile } from './fs' ; import { HttpConfigType } from '../http' ; import { Logger } from '../logging' ; const FILE_ENCODING = 'utf8' ; const FILE_NAME = 'uuid' ; /**
 * This UUID was inadvertantly shipped in the 7.6.0 distributable and should be deleted if found.
 * See https://github.com/elastic/kibana/issues/57673 for more info.
 */ export const UUID_7_6_0_BUG = `ce42b997-a913-4d58-be46-bb1937feedd6` ; export async function resolveInstanceUuid ( { pathConfig , serverConfig , logger , } ) { const uuidFilePath = join ( pathConfig . data , FILE_NAME ) ; const uuidFromFile = await readUuidFromFile ( uuidFilePath , logger ) ; const uuidFromConfig = serverConfig . uuid ; if ( uuidFromConfig ) { if ( uuidFromConfig === uuidFromFile ) { logger . debug ( `OpenSearch Dashboards instance UUID: ${ uuidFromConfig }` ) ; return uuidFromConfig ; } else { if ( uuidFromFile === undefined ) { logger . debug ( `Setting new OpenSearch Dashboards instance UUID: ${ uuidFromConfig }` ) ; } else { logger . debug ( `Updating OpenSearch Dashboards instance UUID to: ${ uuidFromConfig } (was: ${ uuidFromFile })` ) ; } await writeUuidToFile ( uuidFilePath , uuidFromConfig ) ; return uuidFromConfig ; } } if ( uuidFromFile === undefined ) { const newUuid = uuid . v4 ( ) ; logger . debug ( `Setting new OpenSearch Dashboards instance UUID: ${ newUuid }` ) ; await writeUuidToFile ( uuidFilePath , newUuid ) ; return newUuid ; } logger . debug ( `Resuming persistent OpenSearch Dashboards instance UUID: ${ uuidFromFile }` ) ; return uuidFromFile ; } async function readUuidFromFile ( filepath , logger ) { try { const content = await readFile ( filepath ) ; const decoded = content . toString ( FILE_ENCODING ) ; if ( decoded === UUID_7_6_0_BUG ) { logger . debug ( `UUID from 7.6.0 bug detected, ignoring file UUID` ) ; return undefined ; } else { return decoded ; } } catch ( e ) { if ( e . code === 'ENOENT' ) { return undefined ; } throw new Error ( 'Unable to read OpenSearch Dashboards UUID file, please check the uuid.server configuration ' + 'value in opensearch_dashboards.yml and ensure OpenSearch Dashboards has sufficient permissions to read / write to this file. ' + `Error was: ${ e . code }` ) ; } } async function writeUuidToFile ( filepath , uuidValue ) { try { return await writeFile ( filepath , uuidValue , { encoding : FILE_ENCODING } ) ; } catch ( e ) { throw new Error ( 'Unable to write OpenSearch Dashboards UUID file, please check the uuid.server configuration ' + 'value in opensearch_dashboards.yml and ensure OpenSearch Dashboards has sufficient permissions to read / write to this file. ' + `Error was: ${ e . code }` ) ; } }