import { IRouter } from 'kibana/server' ; import { ILicenseState } from '../lib' ; import { EncryptedSavedObjectsPluginSetup } from '../../../encrypted_saved_objects/server' ; import { RewriteResponseCase , verifyAccessAndContext } from './lib' ; import { AlertingRequestHandlerContext , BASE_ALERTING_API_PATH , AlertingFrameworkHealth , } from '../types' ; import { getSecurityHealth } from '../lib/get_security_health' ; const rewriteBodyRes = ( { isSufficientlySecure , hasPermanentEncryptionKey , alertingFrameworkHealth , ... rest } ) => ( { ... rest , is_sufficiently_secure : isSufficientlySecure , has_permanent_encryption_key : hasPermanentEncryptionKey , alerting_framework_health : { decryption_health : alertingFrameworkHealth . decryptionHealth , execution_health : alertingFrameworkHealth . executionHealth , read_health : alertingFrameworkHealth . readHealth , } , alerting_framework_heath : { _deprecated : 'This state property has a typo, use "alerting_framework_health" instead.' , decryption_health : alertingFrameworkHealth . decryptionHealth , execution_health : alertingFrameworkHealth . executionHealth , read_health : alertingFrameworkHealth . readHealth , } , } ) ; export const healthRoute = ( router , licenseState , encryptedSavedObjects ) => { router . get ( { path : `${ BASE_ALERTING_API_PATH }/_health` , validate : false , } , router . handleLegacyErrors ( verifyAccessAndContext ( licenseState , async function ( context , req , res ) { try { const ruleTypes = Array . from ( await context . alerting . getRulesClient ( ) . listAlertTypes ( ) ) ; if ( ruleTypes . length > 0 ) { const alertingFrameworkHealth = await context . alerting . getFrameworkHealth ( ) ; const securityHealth = await getSecurityHealth ( async ( ) => ( licenseState ? licenseState . getIsSecurityEnabled ( ) : null ) , async ( ) => encryptedSavedObjects . canEncrypt , context . alerting . areApiKeysEnabled ) ; const frameworkHealth = { ... securityHealth , alertingFrameworkHealth , } ; return res . ok ( { body : rewriteBodyRes ( frameworkHealth ) , } ) ; } else { return res . forbidden ( { body : { message : `Unauthorized to access alerting framework health` } , } ) ; } } catch ( error ) { return res . badRequest ( { body : error } ) ; } } ) ) ) ; } ;