import { errors } from '@elastic/elasticsearch' ; import { SecurityHasPrivilegesIndexPrivilegesCheck } from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import { RequestHandler } from 'src/core/server' ; import { API_GET_ILM_POLICY_STATUS , API_MIGRATE_ILM_POLICY_URL , ILM_POLICY_NAME , } from '../../../common/constants' ; import { IlmPolicyStatusResponse } from '../../../common/types' ; import { ReportingCore } from '../../core' ; import { IlmPolicyManager , LevelLogger as Logger } from '../../lib' ; import { deprecations } from '../../lib/deprecations' ; export const registerDeprecationsRoutes = ( reporting , logger ) => { const { router } = reporting . getPluginSetupDeps ( ) ; const authzWrapper = < P , Q , B > ( handler ) : RequestHandler < P , Q , B > => { return async ( ctx , req , res ) => { const { security } = reporting . getPluginSetupDeps ( ) ; if ( ! security ?. license . isEnabled ( ) ) { return handler ( ctx , req , res ) ; } const { core : { elasticsearch } , } = ctx ; const store = await reporting . getStore ( ) ; try { const { body } = await elasticsearch . client . asCurrentUser . security . hasPrivileges ( { body : { index : [ { privileges : [ 'manage' ] , names : [ store . getReportingIndexPattern ( ) ] , allow_restricted_indices : true , } as unknown as SecurityHasPrivilegesIndexPrivilegesCheck , ] , } , } ) ; if ( ! body . has_all_requested ) { return res . notFound ( ) ; } } catch ( e ) { logger . error ( e ) ; return res . customError ( { statusCode : e . statusCode , body : e . message } ) ; } return handler ( ctx , req , res ) ; } ; } ; router . get ( { path : API_GET_ILM_POLICY_STATUS , validate : false , } , authzWrapper ( async ( { core : { elasticsearch : { client : scopedClient } , } , } , _req , res ) => { const checkIlmMigrationStatus = ( ) => { return deprecations . checkIlmMigrationStatus ( { reportingCore : reporting , elasticsearchClient : scopedClient . asInternalUser , } ) ; } ; try { const response = { status : await checkIlmMigrationStatus ( ) , } ; return res . ok ( { body : response } ) ; } catch ( e ) { logger . error ( e ) ; return res . customError ( { statusCode : e ?. statusCode ?? 500 , body : { message : e . message } , } ) ; } } ) ) ; router . put ( { path : API_MIGRATE_ILM_POLICY_URL , validate : false } , authzWrapper ( async ( { core : { elasticsearch } } , _req , res ) => { const store = await reporting . getStore ( ) ; const { client : { asCurrentUser : client } , } = elasticsearch ; const scopedIlmPolicyManager = IlmPolicyManager . create ( { client , } ) ; try { if ( ! ( await scopedIlmPolicyManager . doesIlmPolicyExist ( ) ) ) { await scopedIlmPolicyManager . createIlmPolicy ( ) ; } } catch ( e ) { return res . customError ( { statusCode : e ?. statusCode ?? 500 , body : { message : e . message } } ) ; } const indexPattern = store . getReportingIndexPattern ( ) ; try { await client . indices . putSettings ( { index : indexPattern , body : { 'index.lifecycle' : { name : ILM_POLICY_NAME , } , } , } ) ; return res . ok ( ) ; } catch ( err ) { logger . error ( err ) ; if ( err instanceof errors . ResponseError ) { if ( err . statusCode === 404 ) { return res . ok ( ) ; } return res . customError ( { statusCode : err . statusCode ?? 500 , body : { message : err . message , name : err . name , } , } ) ; } throw err ; } } ) ) ; } ;