diff --git a/x-pack/plugins/apm/server/lib/fleet/create_cloud_apm_package_policy.ts b/x-pack/plugins/apm/server/lib/fleet/create_cloud_apm_package_policy.ts index c336e5dc95ba6..6726865c6c3a5 100644 --- a/x-pack/plugins/apm/server/lib/fleet/create_cloud_apm_package_policy.ts +++ b/x-pack/plugins/apm/server/lib/fleet/create_cloud_apm_package_policy.ts @@ -10,6 +10,7 @@ import { SavedObjectsClientContract, Logger, } from 'kibana/server'; +import { PackagePolicy } from '../../../../fleet/common'; import { APM_SERVER_SCHEMA_SAVED_OBJECT_TYPE, APM_SERVER_SCHEMA_SAVED_OBJECT_ID, @@ -19,6 +20,8 @@ import { APMPluginStartDependencies, } from '../../types'; import { getApmPackagePolicyDefinition } from './get_apm_package_policy_definition'; +import { Setup } from '../helpers/setup_request'; +import { mergePackagePolicyWithApm } from './merge_package_policy_with_apm'; export async function createCloudApmPackgePolicy({ cloudPluginSetup, @@ -26,13 +29,15 @@ export async function createCloudApmPackgePolicy({ savedObjectsClient, esClient, logger, + setup, }: { cloudPluginSetup: APMPluginSetupDependencies['cloud']; fleetPluginStart: NonNullable; savedObjectsClient: SavedObjectsClientContract; esClient: ElasticsearchClient; logger: Logger; -}) { + setup: Setup; +}): Promise { const { attributes } = await savedObjectsClient.get( APM_SERVER_SCHEMA_SAVED_OBJECT_TYPE, APM_SERVER_SCHEMA_SAVED_OBJECT_ID @@ -40,15 +45,21 @@ export async function createCloudApmPackgePolicy({ const apmServerSchema: Record = JSON.parse( (attributes as { schemaJson: string }).schemaJson ); + // Merges agent config and source maps with the new APM cloud package policy const apmPackagePolicyDefinition = getApmPackagePolicyDefinition({ apmServerSchema, cloudPluginSetup, }); + const mergedAPMPackagePolicy = await mergePackagePolicyWithApm({ + setup, + packagePolicy: apmPackagePolicyDefinition, + fleetPluginStart, + }); logger.info(`Fleet migration on Cloud - apmPackagePolicy create start`); const apmPackagePolicy = await fleetPluginStart.packagePolicyService.create( savedObjectsClient, esClient, - apmPackagePolicyDefinition, + mergedAPMPackagePolicy, { force: true, bumpRevision: true } ); logger.info(`Fleet migration on Cloud - apmPackagePolicy create end`); diff --git a/x-pack/plugins/apm/server/lib/fleet/merge_package_policy_with_apm.ts b/x-pack/plugins/apm/server/lib/fleet/merge_package_policy_with_apm.ts new file mode 100644 index 0000000000000..fd3e3db700fd0 --- /dev/null +++ b/x-pack/plugins/apm/server/lib/fleet/merge_package_policy_with_apm.ts @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { Setup } from '../helpers/setup_request'; +import { APMPluginStartDependencies } from '../../types'; +import { listConfigurations } from '../settings/agent_configuration/list_configurations'; +import { + getPackagePolicyWithAgentConfigurations, + PackagePolicy, +} from './register_fleet_policy_callbacks'; +import { getPackagePolicyWithSourceMap, listArtifacts } from './source_maps'; + +export async function mergePackagePolicyWithApm({ + packagePolicy, + setup, + fleetPluginStart, +}: { + packagePolicy: PackagePolicy; + setup: Setup; + fleetPluginStart: NonNullable; +}) { + const agentConfigurations = await listConfigurations({ setup }); + const artifacts = await listArtifacts({ fleetPluginStart }); + return getPackagePolicyWithAgentConfigurations( + getPackagePolicyWithSourceMap({ packagePolicy, artifacts }), + agentConfigurations + ); +} diff --git a/x-pack/plugins/apm/server/lib/fleet/register_fleet_policy_callbacks.ts b/x-pack/plugins/apm/server/lib/fleet/register_fleet_policy_callbacks.ts index c122a5c406eab..b0065b70673a5 100644 --- a/x-pack/plugins/apm/server/lib/fleet/register_fleet_policy_callbacks.ts +++ b/x-pack/plugins/apm/server/lib/fleet/register_fleet_policy_callbacks.ts @@ -6,13 +6,12 @@ */ import { APMPlugin, APMRouteHandlerResources } from '../..'; -import { listConfigurations } from '../settings/agent_configuration/list_configurations'; -import { setupRequest } from '../helpers/setup_request'; -import { APMPluginStartDependencies } from '../../types'; import { ExternalCallback } from '../../../../fleet/server'; -import { AGENT_NAME } from '../../../common/elasticsearch_fieldnames'; import { AgentConfiguration } from '../../../common/agent_configuration/configuration_types'; -import { getPackagePolicyWithSourceMap, listArtifacts } from './source_maps'; +import { AGENT_NAME } from '../../../common/elasticsearch_fieldnames'; +import { APMPluginStartDependencies } from '../../types'; +import { setupRequest } from '../helpers/setup_request'; +import { mergePackagePolicyWithApm } from './merge_package_policy_with_apm'; export async function registerFleetPolicyCallbacks({ plugins, @@ -91,12 +90,11 @@ function registerPackagePolicyExternalCallback({ logger, ruleDataClient, }); - const agentConfigurations = await listConfigurations({ setup }); - const artifacts = await listArtifacts({ fleetPluginStart }); - return getPackagePolicyWithAgentConfigurations( - getPackagePolicyWithSourceMap({ packagePolicy, artifacts }), - agentConfigurations - ); + return await mergePackagePolicyWithApm({ + setup, + fleetPluginStart, + packagePolicy, + }); }; fleetPluginStart.registerExternalCallback(callbackName, callbackFn); diff --git a/x-pack/plugins/apm/server/routes/fleet.ts b/x-pack/plugins/apm/server/routes/fleet.ts index 66843f4f0df4d..a01c9dd1579b1 100644 --- a/x-pack/plugins/apm/server/routes/fleet.ts +++ b/x-pack/plugins/apm/server/routes/fleet.ts @@ -5,26 +5,27 @@ * 2.0. */ -import { keyBy } from 'lodash'; import Boom from '@hapi/boom'; -import * as t from 'io-ts'; import { i18n } from '@kbn/i18n'; +import * as t from 'io-ts'; +import { keyBy } from 'lodash'; import { - APM_SERVER_SCHEMA_SAVED_OBJECT_TYPE, APM_SERVER_SCHEMA_SAVED_OBJECT_ID, + APM_SERVER_SCHEMA_SAVED_OBJECT_TYPE, } from '../../common/apm_saved_object_constants'; +import { createCloudApmPackgePolicy } from '../lib/fleet/create_cloud_apm_package_policy'; import { getFleetAgents } from '../lib/fleet/get_agents'; import { getApmPackgePolicies } from '../lib/fleet/get_apm_package_policies'; -import { createApmServerRoute } from './create_apm_server_route'; -import { createApmServerRouteRepository } from './create_apm_server_route_repository'; import { - getCloudAgentPolicy, getApmPackagePolicy, + getCloudAgentPolicy, } from '../lib/fleet/get_cloud_apm_package_policy'; -import { createCloudApmPackgePolicy } from '../lib/fleet/create_cloud_apm_package_policy'; import { getUnsupportedApmServerSchema } from '../lib/fleet/get_unsupported_apm_server_schema'; import { isSuperuser } from '../lib/fleet/is_superuser'; import { getInternalSavedObjectsClient } from '../lib/helpers/get_internal_saved_objects_client'; +import { setupRequest } from '../lib/helpers/setup_request'; +import { createApmServerRoute } from './create_apm_server_route'; +import { createApmServerRouteRepository } from './create_apm_server_route_repository'; const hasFleetDataRoute = createApmServerRoute({ endpoint: 'GET /api/apm/fleet/has_data', @@ -172,12 +173,15 @@ const createCloudApmPackagePolicyRoute = createApmServerRoute({ throw Boom.forbidden(CLOUD_SUPERUSER_REQUIRED_MESSAGE); } + const setup = await setupRequest(resources); + const cloudApmPackagePolicy = await createCloudApmPackgePolicy({ cloudPluginSetup, fleetPluginStart, savedObjectsClient, esClient, logger, + setup, }); return { cloudApmPackagePolicy };