Skip to content

Commit bb82d2c

Browse files
cauemarcondeskibanamachine
authored andcommitted
[APM] APM agent config created prior to Fleet migration is not injected into integration policy (#105504)
1 parent da718a3 commit bb82d2c

4 files changed

Lines changed: 65 additions & 20 deletions

File tree

x-pack/plugins/apm/server/lib/fleet/create_cloud_apm_package_policy.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
SavedObjectsClientContract,
1111
Logger,
1212
} from 'kibana/server';
13+
import { PackagePolicy } from '../../../../fleet/common';
1314
import {
1415
APM_SERVER_SCHEMA_SAVED_OBJECT_TYPE,
1516
APM_SERVER_SCHEMA_SAVED_OBJECT_ID,
@@ -19,36 +20,46 @@ import {
1920
APMPluginStartDependencies,
2021
} from '../../types';
2122
import { getApmPackagePolicyDefinition } from './get_apm_package_policy_definition';
23+
import { Setup } from '../helpers/setup_request';
24+
import { mergePackagePolicyWithApm } from './merge_package_policy_with_apm';
2225

2326
export async function createCloudApmPackgePolicy({
2427
cloudPluginSetup,
2528
fleetPluginStart,
2629
savedObjectsClient,
2730
esClient,
2831
logger,
32+
setup,
2933
}: {
3034
cloudPluginSetup: APMPluginSetupDependencies['cloud'];
3135
fleetPluginStart: NonNullable<APMPluginStartDependencies['fleet']>;
3236
savedObjectsClient: SavedObjectsClientContract;
3337
esClient: ElasticsearchClient;
3438
logger: Logger;
35-
}) {
39+
setup: Setup;
40+
}): Promise<PackagePolicy> {
3641
const { attributes } = await savedObjectsClient.get(
3742
APM_SERVER_SCHEMA_SAVED_OBJECT_TYPE,
3843
APM_SERVER_SCHEMA_SAVED_OBJECT_ID
3944
);
4045
const apmServerSchema: Record<string, any> = JSON.parse(
4146
(attributes as { schemaJson: string }).schemaJson
4247
);
48+
// Merges agent config and source maps with the new APM cloud package policy
4349
const apmPackagePolicyDefinition = getApmPackagePolicyDefinition({
4450
apmServerSchema,
4551
cloudPluginSetup,
4652
});
53+
const mergedAPMPackagePolicy = await mergePackagePolicyWithApm({
54+
setup,
55+
packagePolicy: apmPackagePolicyDefinition,
56+
fleetPluginStart,
57+
});
4758
logger.info(`Fleet migration on Cloud - apmPackagePolicy create start`);
4859
const apmPackagePolicy = await fleetPluginStart.packagePolicyService.create(
4960
savedObjectsClient,
5061
esClient,
51-
apmPackagePolicyDefinition,
62+
mergedAPMPackagePolicy,
5263
{ force: true, bumpRevision: true }
5364
);
5465
logger.info(`Fleet migration on Cloud - apmPackagePolicy create end`);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { Setup } from '../helpers/setup_request';
9+
import { APMPluginStartDependencies } from '../../types';
10+
import { listConfigurations } from '../settings/agent_configuration/list_configurations';
11+
import {
12+
getPackagePolicyWithAgentConfigurations,
13+
PackagePolicy,
14+
} from './register_fleet_policy_callbacks';
15+
import { getPackagePolicyWithSourceMap, listArtifacts } from './source_maps';
16+
17+
export async function mergePackagePolicyWithApm({
18+
packagePolicy,
19+
setup,
20+
fleetPluginStart,
21+
}: {
22+
packagePolicy: PackagePolicy;
23+
setup: Setup;
24+
fleetPluginStart: NonNullable<APMPluginStartDependencies['fleet']>;
25+
}) {
26+
const agentConfigurations = await listConfigurations({ setup });
27+
const artifacts = await listArtifacts({ fleetPluginStart });
28+
return getPackagePolicyWithAgentConfigurations(
29+
getPackagePolicyWithSourceMap({ packagePolicy, artifacts }),
30+
agentConfigurations
31+
);
32+
}

x-pack/plugins/apm/server/lib/fleet/register_fleet_policy_callbacks.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
*/
77

88
import { APMPlugin, APMRouteHandlerResources } from '../..';
9-
import { listConfigurations } from '../settings/agent_configuration/list_configurations';
10-
import { setupRequest } from '../helpers/setup_request';
11-
import { APMPluginStartDependencies } from '../../types';
129
import { ExternalCallback } from '../../../../fleet/server';
13-
import { AGENT_NAME } from '../../../common/elasticsearch_fieldnames';
1410
import { AgentConfiguration } from '../../../common/agent_configuration/configuration_types';
15-
import { getPackagePolicyWithSourceMap, listArtifacts } from './source_maps';
11+
import { AGENT_NAME } from '../../../common/elasticsearch_fieldnames';
12+
import { APMPluginStartDependencies } from '../../types';
13+
import { setupRequest } from '../helpers/setup_request';
14+
import { mergePackagePolicyWithApm } from './merge_package_policy_with_apm';
1615

1716
export async function registerFleetPolicyCallbacks({
1817
plugins,
@@ -91,12 +90,11 @@ function registerPackagePolicyExternalCallback({
9190
logger,
9291
ruleDataClient,
9392
});
94-
const agentConfigurations = await listConfigurations({ setup });
95-
const artifacts = await listArtifacts({ fleetPluginStart });
96-
return getPackagePolicyWithAgentConfigurations(
97-
getPackagePolicyWithSourceMap({ packagePolicy, artifacts }),
98-
agentConfigurations
99-
);
93+
return await mergePackagePolicyWithApm({
94+
setup,
95+
fleetPluginStart,
96+
packagePolicy,
97+
});
10098
};
10199

102100
fleetPluginStart.registerExternalCallback(callbackName, callbackFn);

x-pack/plugins/apm/server/routes/fleet.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@
55
* 2.0.
66
*/
77

8-
import { keyBy } from 'lodash';
98
import Boom from '@hapi/boom';
10-
import * as t from 'io-ts';
119
import { i18n } from '@kbn/i18n';
10+
import * as t from 'io-ts';
11+
import { keyBy } from 'lodash';
1212
import {
13-
APM_SERVER_SCHEMA_SAVED_OBJECT_TYPE,
1413
APM_SERVER_SCHEMA_SAVED_OBJECT_ID,
14+
APM_SERVER_SCHEMA_SAVED_OBJECT_TYPE,
1515
} from '../../common/apm_saved_object_constants';
16+
import { createCloudApmPackgePolicy } from '../lib/fleet/create_cloud_apm_package_policy';
1617
import { getFleetAgents } from '../lib/fleet/get_agents';
1718
import { getApmPackgePolicies } from '../lib/fleet/get_apm_package_policies';
18-
import { createApmServerRoute } from './create_apm_server_route';
19-
import { createApmServerRouteRepository } from './create_apm_server_route_repository';
2019
import {
21-
getCloudAgentPolicy,
2220
getApmPackagePolicy,
21+
getCloudAgentPolicy,
2322
} from '../lib/fleet/get_cloud_apm_package_policy';
24-
import { createCloudApmPackgePolicy } from '../lib/fleet/create_cloud_apm_package_policy';
2523
import { getUnsupportedApmServerSchema } from '../lib/fleet/get_unsupported_apm_server_schema';
2624
import { isSuperuser } from '../lib/fleet/is_superuser';
2725
import { getInternalSavedObjectsClient } from '../lib/helpers/get_internal_saved_objects_client';
26+
import { setupRequest } from '../lib/helpers/setup_request';
27+
import { createApmServerRoute } from './create_apm_server_route';
28+
import { createApmServerRouteRepository } from './create_apm_server_route_repository';
2829

2930
const hasFleetDataRoute = createApmServerRoute({
3031
endpoint: 'GET /api/apm/fleet/has_data',
@@ -172,12 +173,15 @@ const createCloudApmPackagePolicyRoute = createApmServerRoute({
172173
throw Boom.forbidden(CLOUD_SUPERUSER_REQUIRED_MESSAGE);
173174
}
174175

176+
const setup = await setupRequest(resources);
177+
175178
const cloudApmPackagePolicy = await createCloudApmPackgePolicy({
176179
cloudPluginSetup,
177180
fleetPluginStart,
178181
savedObjectsClient,
179182
esClient,
180183
logger,
184+
setup,
181185
});
182186

183187
return { cloudApmPackagePolicy };

0 commit comments

Comments
 (0)