From b9754807f5fc03c088066b4c71d6e2f3d21c8441 Mon Sep 17 00:00:00 2001 From: Oliver Gupte Date: Tue, 8 Jun 2021 20:30:42 -0400 Subject: [PATCH 1/3] [APM] Improvments in the APM fleet integration (#95501) --- .../lib/fleet/register_fleet_policy_callbacks.ts | 1 + .../services/package_policies_to_agent_inputs.ts | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) 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 e4306b4c2ec98..35c7f0dfdfd73 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 @@ -115,6 +115,7 @@ export function getPackagePolicyWithAgentConfigurations( { ...firstInput, config: { + ...firstInput.config, [APM_SERVER]: { value: { ...apmServerValue, diff --git a/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.ts b/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.ts index 61d7764a832b1..8e6e7dd433823 100644 --- a/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.ts +++ b/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.ts @@ -7,6 +7,7 @@ import type { PackagePolicy, FullAgentPolicyInput, FullAgentPolicyInputStream } from '../types'; import { DEFAULT_OUTPUT } from '../constants'; +import { merge } from 'lodash'; export const storedPackagePoliciesToAgentInputs = ( packagePolicies: PackagePolicy[] @@ -31,10 +32,6 @@ export const storedPackagePoliciesToAgentInputs = ( namespace: packagePolicy.namespace || 'default', }, use_output: DEFAULT_OUTPUT.name, - ...Object.entries(input.config || {}).reduce((acc, [key, { value }]) => { - acc[key] = value; - return acc; - }, {} as { [k: string]: any }), ...(input.compiled_input || {}), ...(input.streams.length ? { @@ -56,6 +53,14 @@ export const storedPackagePoliciesToAgentInputs = ( : {}), }; + merge( + fullInput, + Object.entries(input.config || {}).reduce( + (acc, [key, { value }]) => ({ ...acc, [key]: value }), + {} + ) + ); + if (packagePolicy.package) { fullInput.meta = { package: { From 98b685189d9c509b80776353d233a502cc202f6f Mon Sep 17 00:00:00 2001 From: Oliver Gupte Date: Thu, 10 Jun 2021 11:03:40 -0400 Subject: [PATCH 2/3] added unit test and line comment --- .../package_policies_to_agent_inputs.test.ts | 77 +++++++++++++++++++ .../package_policies_to_agent_inputs.ts | 1 + 2 files changed, 78 insertions(+) diff --git a/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.test.ts b/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.test.ts index 31c47bf9dc69d..a5acd823c20fd 100644 --- a/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.test.ts +++ b/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.test.ts @@ -218,4 +218,81 @@ describe('Fleet - storedPackagePoliciesToAgentInputs', () => { }, ]); }); + + it('returns agent inputs with deeply merged config values', () => { + expect( + storedPackagePoliciesToAgentInputs([ + { + ...mockPackagePolicy, + inputs: [ + { + ...mockInput, + compiled_input: { + agent_input_template_group1_vars: { + inputVar: 'input-value', + }, + agent_input_template_group2_vars: { + inputVar3: { + testFieldGroup: { + subField1: 'subfield1', + }, + testField: 'test', + }, + }, + }, + config: { + agent_input_template_group1_vars: { + value: { + inputVar2: {}, + }, + }, + agent_input_template_group2_vars: { + value: { + inputVar3: { + testFieldGroup: { + subField2: 'subfield2', + }, + }, + inputVar4: '', + }, + }, + }, + }, + ], + }, + ]) + ).toEqual([ + { + id: 'some-uuid', + revision: 1, + name: 'mock-package-policy', + type: 'test-logs', + data_stream: { namespace: 'default' }, + use_output: 'default', + agent_input_template_group1_vars: { + inputVar: 'input-value', + inputVar2: {}, + }, + agent_input_template_group2_vars: { + inputVar3: { + testField: 'test', + testFieldGroup: { + subField1: 'subfield1', + subField2: 'subfield2', + }, + }, + inputVar4: '', + }, + streams: [ + { + id: 'test-logs-foo', + data_stream: { dataset: 'foo', type: 'logs' }, + fooKey: 'fooValue1', + fooKey2: ['fooValue2'], + }, + { id: 'test-logs-bar', data_stream: { dataset: 'bar', type: 'logs' } }, + ], + }, + ]); + }); }); diff --git a/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.ts b/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.ts index 8e6e7dd433823..96c8ac5a62e08 100644 --- a/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.ts +++ b/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.ts @@ -53,6 +53,7 @@ export const storedPackagePoliciesToAgentInputs = ( : {}), }; + // deeply merge the input.config values with the full policy input merge( fullInput, Object.entries(input.config || {}).reduce( From bc83121bb3daa8fe31e1423ef2b2c6a709d7e315 Mon Sep 17 00:00:00 2001 From: Oliver Gupte Date: Thu, 10 Jun 2021 12:12:50 -0400 Subject: [PATCH 3/3] fixes eslint issues --- .../fleet/common/services/package_policies_to_agent_inputs.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.ts b/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.ts index 96c8ac5a62e08..f262521461b98 100644 --- a/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.ts +++ b/x-pack/plugins/fleet/common/services/package_policies_to_agent_inputs.ts @@ -5,9 +5,10 @@ * 2.0. */ +import { merge } from 'lodash'; + import type { PackagePolicy, FullAgentPolicyInput, FullAgentPolicyInputStream } from '../types'; import { DEFAULT_OUTPUT } from '../constants'; -import { merge } from 'lodash'; export const storedPackagePoliciesToAgentInputs = ( packagePolicies: PackagePolicy[]