diff --git a/x-pack/plugins/fleet/server/services/package_policy.test.ts b/x-pack/plugins/fleet/server/services/package_policy.test.ts index 8b5a46dc675f..0b6b3579f7b8 100644 --- a/x-pack/plugins/fleet/server/services/package_policy.test.ts +++ b/x-pack/plugins/fleet/server/services/package_policy.test.ts @@ -1273,6 +1273,105 @@ describe('Package policy service', () => { }); }); + describe('when variable is undefined in original object and policy_template is undefined', () => { + it('adds the variable definition to the resulting object', () => { + const basePackagePolicy: NewPackagePolicy = { + name: 'base-package-policy', + description: 'Base Package Policy', + namespace: 'default', + enabled: true, + policy_id: 'xxxx', + output_id: 'xxxx', + package: { + name: 'test-package', + title: 'Test Package', + version: '0.0.1', + }, + inputs: [ + { + type: 'logs', + policy_template: 'template_1', + enabled: true, + vars: { + path: { + type: 'text', + value: ['/var/log/logfile.log'], + }, + }, + streams: [], + }, + ], + }; + + const packageInfo: PackageInfo = { + name: 'test-package', + description: 'Test Package', + title: 'Test Package', + version: '0.0.1', + latestVersion: '0.0.1', + release: 'experimental', + format_version: '1.0.0', + owner: { github: 'elastic/fleet' }, + policy_templates: [ + { + name: 'template_1', + title: 'Template 1', + description: 'Template 1', + inputs: [ + { + type: 'logs', + title: 'Log', + description: 'Log Input', + vars: [ + { + name: 'path', + type: 'text', + }, + { + name: 'path_2', + type: 'text', + }, + ], + }, + ], + }, + ], + // @ts-ignore + assets: {}, + }; + + const inputsOverride: NewPackagePolicyInput[] = [ + { + type: 'logs', + enabled: true, + streams: [], + policy_template: undefined, // preconfigured input overrides don't have a policy_template + vars: { + path: { + type: 'text', + value: '/var/log/new-logfile.log', + }, + path_2: { + type: 'text', + value: '/var/log/custom.log', + }, + }, + }, + ]; + + const result = overridePackageInputs( + basePackagePolicy, + packageInfo, + // TODO: Update this type assertion when the `InputsOverride` type is updated such + // that it no longer causes unresolvable type errors when used directly + inputsOverride as InputsOverride[], + false + ); + + expect(result.inputs[0]?.vars?.path_2.value).toEqual('/var/log/custom.log'); + }); + }); + describe('when an input of the same type exists under multiple policy templates', () => { it('adds variable definitions to the proper streams', () => { const basePackagePolicy: NewPackagePolicy = { diff --git a/x-pack/plugins/fleet/server/services/package_policy.ts b/x-pack/plugins/fleet/server/services/package_policy.ts index 546e267b8402..b7772892f542 100644 --- a/x-pack/plugins/fleet/server/services/package_policy.ts +++ b/x-pack/plugins/fleet/server/services/package_policy.ts @@ -996,9 +996,13 @@ export function overridePackageInputs( ]; for (const override of inputsOverride) { - let originalInput = inputs.find( - (i) => i.type === override.type && i.policy_template === override.policy_template - ); + // Preconfiguration does not currently support multiple policy templates, so overrides will have an undefined + // policy template, so we only match on `type` in that case. + let originalInput = override.policy_template + ? inputs.find( + (i) => i.type === override.type && i.policy_template === override.policy_template + ) + : inputs.find((i) => i.type === override.type); // If there's no corresponding input on the original package policy, just // take the override value from the new package as-is. This case typically