import { APMPlugin , APMRouteHandlerResources } from '../..' ; import { PostPackagePolicyCreateCallback , PutPackagePolicyUpdateCallback , } from '../../../../fleet/server' ; import { NewPackagePolicy , UpdatePackagePolicy , } from '../../../../fleet/common' ; import { AgentConfiguration } from '../../../common/agent_configuration/configuration_types' ; import { AGENT_NAME } from '../../../common/elasticsearch_fieldnames' ; import { APMPluginStartDependencies } from '../../types' ; import { setupRequest } from '../../lib/helpers/setup_request' ; import { mergePackagePolicyWithApm } from './merge_package_policy_with_apm' ; export async function registerFleetPolicyCallbacks ( { plugins , ruleDataClient , config , logger , kibanaVersion , } ) { if ( ! plugins . fleet ) { return ; } const fleetPluginStart = await plugins . fleet . start ( ) ; registerPackagePolicyExternalCallback ( { fleetPluginStart , callbackName : 'packagePolicyCreate' , plugins , ruleDataClient , config , logger , kibanaVersion , } ) ; registerPackagePolicyExternalCallback ( { fleetPluginStart , callbackName : 'packagePolicyUpdate' , plugins , ruleDataClient , config , logger , kibanaVersion , } ) ; } type ExternalCallbackParams = Parameters < PostPackagePolicyCreateCallback > | Parameters < PutPackagePolicyUpdateCallback > ; export type PackagePolicy = NewPackagePolicy | UpdatePackagePolicy ; type Context = ExternalCallbackParams [ 1 ] ; type Request = ExternalCallbackParams [ 2 ] ; function registerPackagePolicyExternalCallback ( { fleetPluginStart , callbackName , plugins , ruleDataClient , config , logger , kibanaVersion , } ) { const callbackFn = async ( packagePolicy , context , request ) => { if ( packagePolicy . package ?. name !== 'apm' ) { return packagePolicy ; } const setup = await setupRequest ( { context : context as any , params : { query : { _inspect : false } } , core : null as any , plugins , request , config , logger , ruleDataClient , kibanaVersion , } ) ; return await mergePackagePolicyWithApm ( { setup , fleetPluginStart , packagePolicy , } ) ; } ; fleetPluginStart . registerExternalCallback ( callbackName , callbackFn ) ; } export const APM_SERVER = 'apm-server' ; export function getPackagePolicyWithAgentConfigurations ( packagePolicy , agentConfigurations ) { const [ firstInput , ... restInputs ] = packagePolicy . inputs ; const apmServerValue = firstInput ?. config ?. [ APM_SERVER ] . value ; return { ... packagePolicy , inputs : [ { ... firstInput , config : { ... firstInput . config , [ APM_SERVER ] : { value : { ... apmServerValue , agent_config : agentConfigurations . map ( ( configuration ) => ( { service : configuration . service , config : configuration . settings , etag : configuration . etag , [ AGENT_NAME ] : configuration . agent_name , } ) ) , } , } , } , } , ... restInputs , ] , } ; }