import { Logger } from 'src/core/server' ; import { IKibanaResponse , KibanaRequest , KibanaResponseFactory , RequestHandler , RequestHandlerContext , } from 'kibana/server' ; import { LicensingPluginSetup , LicenseType } from '../../../licensing/server' ; import type { AlertingApiRequestHandlerContext } from '../../../alerting/server' ; export interface LicenseStatus { isValid : boolean ; isSecurityEnabled : boolean ; message ? : string ; } interface SetupSettings { pluginId : string ; minimumLicenseType : LicenseType ; defaultErrorMessage : string ; } type TransformRequestHandlerContext = RequestHandlerContext & { alerting ? : AlertingApiRequestHandlerContext ; } ; export class License { private licenseStatus : LicenseStatus = { isValid : false , isSecurityEnabled : false , message : 'Invalid License' , } ; setup ( { pluginId , minimumLicenseType , defaultErrorMessage } , { licensing , logger } ) { licensing . license$ . subscribe ( ( license ) => { const { state , message } = license . check ( pluginId , minimumLicenseType ) ; const hasRequiredLicense = state === 'valid' ; const securityFeature = license . getFeature ( 'security' ) ; const isSecurityEnabled = securityFeature !== undefined && securityFeature . isAvailable === true && securityFeature . isEnabled === true ; if ( hasRequiredLicense ) { this . licenseStatus = { isValid : true , isSecurityEnabled } ; } else { this . licenseStatus = { isValid : false , isSecurityEnabled , message : message || defaultErrorMessage , } ; if ( message ) { logger . info ( message ) ; } } } ) ; } guardApiRoute < Params , Query , Body > ( handler ) { const license = this ; return function licenseCheck ( ctx , request , response ) : IKibanaResponse < any > | Promise < IKibanaResponse < any > > { const licenseStatus = license . getStatus ( ) ; if ( ! licenseStatus . isValid ) { return response . customError ( { body : { message : licenseStatus . message || '' , } , statusCode : 403 , } ) ; } return handler ( ctx , request , response ) ; } ; } getStatus ( ) { return this . licenseStatus ; } }