import { APP_CLUSTER_PRIVILEGES , APP_INDEX_PRIVILEGES } from '../../../common/constants' ; import { Privileges } from '../../../common/types/privileges' ; import { RouteDependencies } from '../../types' ; import { addBasePath } from '../index' ; export function registerPrivilegesRoute ( { router , license } ) { router . get ( { path : addBasePath ( 'privileges' ) , validate : { } } , license . guardApiRoute ( async ( ctx , req , res ) => { const privilegesResult = { hasAllPrivileges : true , missingPrivileges : { cluster : [ ] , index : [ ] , } , } ; if ( license . getStatus ( ) . isSecurityEnabled === false ) { return res . ok ( { body : privilegesResult } ) ; } const { body : { has_all_requested : hasAllPrivileges , cluster } , } = await ctx . core . elasticsearch . client . asCurrentUser . security . hasPrivileges ( { body : { cluster : APP_CLUSTER_PRIVILEGES , } , } ) ; privilegesResult . missingPrivileges . cluster = extractMissingPrivileges ( cluster ) ; privilegesResult . hasAllPrivileges = hasAllPrivileges ; const { body : { indices } , } = await ctx . core . elasticsearch . client . asCurrentUser . security . getUserPrivileges ( ) ; const oneIndexWithAllPrivileges = indices . find ( ( { privileges } ) => { if ( privileges . includes ( 'all' ) ) { return true ; } const indexHasAllPrivileges = APP_INDEX_PRIVILEGES . every ( ( privilege ) => privileges . includes ( privilege ) ) ; return indexHasAllPrivileges ; } ) ; if ( ! oneIndexWithAllPrivileges ) { privilegesResult . missingPrivileges . index = [ ... APP_INDEX_PRIVILEGES ] ; } return res . ok ( { body : privilegesResult } ) ; } ) ) ; } const extractMissingPrivileges = ( privilegesObject = { } ) : string [ ] => Object . keys ( privilegesObject ) . reduce ( ( privileges , privilegeName ) : string [ ] => { if ( ! privilegesObject [ privilegeName ] ) { privileges . push ( privilegeName ) ; } return privileges ; } , [ ] ) ;