import { schema } from '@kbn/config-schema' ; import { wrapError } from '../client/error_wrapper' ; import { mlLog } from '../lib/log' ; import { capabilitiesProvider } from '../lib/capabilities' ; import { spacesUtilsProvider } from '../lib/spaces_utils' ; import { RouteInitialization , SystemRouteDeps } from '../types' ; import { getMlNodeCount } from '../lib/node_utils' ; /**
 * System routes
 */ export function systemRoutes ( { router , mlLicense , routeGuard } , { getSpaces , cloud , resolveMlCapabilities } ) { apiGroup api apiName apiDescription router . post ( { path : '/api/ml/_has_privileges' , validate : { body : schema . maybe ( schema . any ( ) ) , } , options : { tags : [ 'access:ml:canAccessML' ] , } , } , routeGuard . basicLicenseAPIGuard ( async ( { mlClient , client , request , response } ) => { try { const { asCurrentUser } = client ; let upgradeInProgress = false ; try { const { body } = await mlClient . info ( ) ; upgradeInProgress = body . upgrade_mode === true ; } catch ( error ) { if ( error . status === 403 ) { mlLog . info ( 'Unable to determine whether upgrade is being performed due to insufficient user privileges' ) ; } else { mlLog . warn ( 'Unable to determine whether upgrade is being performed' ) ; } } if ( mlLicense . isSecurityEnabled ( ) === false ) { return response . ok ( { body : { securityDisabled : true , upgradeInProgress , } , } ) ; } else { const { body } = await asCurrentUser . security . hasPrivileges ( { body : request . body } ) ; return response . ok ( { body : { ... body , upgradeInProgress , } , } ) ; } } catch ( error ) { return response . customError ( wrapError ( error ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . get ( { path : '/api/ml/ml_capabilities' , validate : false , } , routeGuard . basicLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { isMlEnabledInSpace } = spacesUtilsProvider ( getSpaces , request ) ; const mlCapabilities = await resolveMlCapabilities ( request ) ; if ( mlCapabilities === null ) { return response . customError ( wrapError ( new Error ( 'resolveMlCapabilities is not defined' ) ) ) ; } const { getCapabilities } = capabilitiesProvider ( mlClient , mlCapabilities , mlLicense , isMlEnabledInSpace ) ; return response . ok ( { body : await getCapabilities ( ) , } ) ; } catch ( error ) { return response . customError ( wrapError ( error ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . get ( { path : '/api/ml/ml_node_count' , validate : false , options : { tags : [ 'access:ml:canGetJobs' , 'access:ml:canGetDatafeeds' ] , } , } , routeGuard . basicLicenseAPIGuard ( async ( { client , response } ) => { try { return response . ok ( { body : await getMlNodeCount ( client ) , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . get ( { path : '/api/ml/info' , validate : false , options : { tags : [ 'access:ml:canAccessML' ] , } , } , routeGuard . basicLicenseAPIGuard ( async ( { mlClient , response } ) => { try { const { body } = await mlClient . info ( ) ; const cloudId = cloud && cloud . cloudId ; return response . ok ( { body : { ... body , cloudId } , } ) ; } catch ( error ) { return response . customError ( wrapError ( error ) ) ; } } ) ) ; apiGroup apiDeprecated api apiName router . post ( { path : '/api/ml/es_search' , validate : { body : schema . maybe ( schema . any ( ) ) , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , request , response } ) => { try { const { body } = await client . asCurrentUser . search ( request . body ) ; return response . ok ( { body , } ) ; } catch ( error ) { return response . customError ( wrapError ( error ) ) ; } } ) ) ; apiGroup api apiName router . post ( { path : '/api/ml/index_exists' , validate : { body : schema . object ( { indices : schema . arrayOf ( schema . string ( ) ) } ) , } , options : { tags : [ 'access:ml:canAccessML' ] , } , } , routeGuard . basicLicenseAPIGuard ( async ( { client , request , response } ) => { try { const { indices } = request . body ; const results = await Promise . all ( indices . map ( async ( index ) => client . asCurrentUser . indices . exists ( { index , } ) ) ) ; const result = indices . reduce ( ( acc , cur , i ) => { acc [ cur ] = { exists : results [ i ] . body } ; return acc ; } , { } as Record < string , { exists : boolean ; } > ) ; return response . ok ( { body : result , } ) ; } catch ( error ) { return response . customError ( wrapError ( error ) ) ; } } ) ) ; }