import { KibanaRequest } from 'kibana/server' ; import type { MlClient } from '../../lib/ml_client' ; import { mlLog } from '../../lib/log' ; import { MlCapabilities , adminMlCapabilities , MlCapabilitiesResponse , ResolveMlCapabilities , MlCapabilitiesKey , } from '../../../common/types/capabilities' ; import { upgradeCheckProvider } from './upgrade' ; import { MlLicense } from '../../../common/license' ; import { InsufficientMLCapabilities , UnknownMLCapabilitiesError , MLPrivilegesUninitialized , } from './errors' ; export function capabilitiesProvider ( mlClient , capabilities , mlLicense , isMlEnabledInSpace ) { const { isUpgradeInProgress } = upgradeCheckProvider ( mlClient ) ; async function getCapabilities ( ) { const upgradeInProgress = await isUpgradeInProgress ( ) ; const isPlatinumOrTrialLicense = mlLicense . isFullLicense ( ) ; const mlFeatureEnabledInSpace = await isMlEnabledInSpace ( ) ; if ( upgradeInProgress === true ) { disableAdminPrivileges ( capabilities ) ; } return { capabilities , upgradeInProgress , isPlatinumOrTrialLicense , mlFeatureEnabledInSpace , } ; } return { getCapabilities } ; } function disableAdminPrivileges ( capabilities ) { Object . keys ( adminMlCapabilities ) . forEach ( ( k ) => { capabilities [ k as keyof MlCapabilities ] = false ; } ) ; capabilities . canCreateAnnotation = false ; capabilities . canDeleteAnnotation = false ; } export type HasMlCapabilities = ( capabilities : MlCapabilitiesKey [ ] ) => Promise < void > ; export function hasMlCapabilitiesProvider ( resolveMlCapabilities ) { return ( request ) : HasMlCapabilities => { let mlCapabilities = null ; return async ( capabilities ) => { try { mlCapabilities = await resolveMlCapabilities ( request ) ; } catch ( e ) { mlLog . error ( e ) ; throw new UnknownMLCapabilitiesError ( `Unable to perform ML capabilities check ${ e }` ) ; } if ( mlCapabilities === null ) { throw new MLPrivilegesUninitialized ( 'ML capabilities have not been initialized' ) ; } if ( capabilities . every ( ( c ) => mlCapabilities ! [ c ] === true ) === false ) { throw new InsufficientMLCapabilities ( 'Insufficient privileges to access feature' ) ; } } ; } ; }