import { API_BASE_PATH , DEPRECATION_LOGS_INDEX } from '../../common/constants' ; import { versionCheckHandlerWrapper } from '../lib/es_version_precheck' ; import { Privileges } from '../shared_imports' ; import { RouteDependencies } from '../types' ; const extractMissingPrivileges = ( privilegesObject = { } ) : string [ ] => Object . keys ( privilegesObject ) . reduce ( ( privileges , privilegeName ) : string [ ] => { if ( Object . values ( privilegesObject [ privilegeName ] ) . some ( ( e ) => ! e ) ) { privileges . push ( privilegeName ) ; } return privileges ; } , [ ] ) ; export function registerAppRoutes ( { router , lib : { handleEsError } , config : { isSecurityEnabled } , } ) { router . get ( { path : `${ API_BASE_PATH }/privileges` , validate : false , } , versionCheckHandlerWrapper ( async ( { core : { elasticsearch : { client } , } , } , request , response ) => { const privilegesResult = { hasAllPrivileges : true , missingPrivileges : { index : [ ] , } , } ; if ( ! isSecurityEnabled ( ) ) { return response . ok ( { body : privilegesResult } ) ; } try { const { body : { has_all_requested : hasAllPrivileges , index } , } = await client . asCurrentUser . security . hasPrivileges ( { body : { index : [ { names : [ DEPRECATION_LOGS_INDEX ] , privileges : [ 'read' ] , } , ] , } , } ) ; if ( ! hasAllPrivileges ) { privilegesResult . missingPrivileges . index = extractMissingPrivileges ( index ) ; } privilegesResult . hasAllPrivileges = hasAllPrivileges ; return response . ok ( { body : privilegesResult } ) ; } catch ( error ) { return handleEsError ( { error , response } ) ; } } ) ) ; }