Skip to content

Commit

Permalink
[APM] APM agent config created prior to Fleet migration is not inject…
Browse files Browse the repository at this point in the history
…ed into integration policy (elastic#105504) (elastic#105656)

Co-authored-by: Cauê Marcondes <55978943+cauemarcondes@users.noreply.github.com>
  • Loading branch information
kibanamachine and cauemarcondes authored Jul 14, 2021
1 parent da718a3 commit 1b0f4fc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -19,36 +20,46 @@ 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,
fleetPluginStart,
savedObjectsClient,
esClient,
logger,
setup,
}: {
cloudPluginSetup: APMPluginSetupDependencies['cloud'];
fleetPluginStart: NonNullable<APMPluginStartDependencies['fleet']>;
savedObjectsClient: SavedObjectsClientContract;
esClient: ElasticsearchClient;
logger: Logger;
}) {
setup: Setup;
}): Promise<PackagePolicy> {
const { attributes } = await savedObjectsClient.get(
APM_SERVER_SCHEMA_SAVED_OBJECT_TYPE,
APM_SERVER_SCHEMA_SAVED_OBJECT_ID
);
const apmServerSchema: Record<string, any> = 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`);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<APMPluginStartDependencies['fleet']>;
}) {
const agentConfigurations = await listConfigurations({ setup });
const artifacts = await listArtifacts({ fleetPluginStart });
return getPackagePolicyWithAgentConfigurations(
getPackagePolicyWithSourceMap({ packagePolicy, artifacts }),
agentConfigurations
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 11 additions & 7 deletions x-pack/plugins/apm/server/routes/fleet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 };
Expand Down

0 comments on commit 1b0f4fc

Please sign in to comment.