Fix bug in handling preconfiguration input overrides (#115030)

This commit is contained in:
Josh Dover 2021-10-14 20:13:24 +02:00 committed by GitHub
parent 45a0032e73
commit ebb9e24b61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 106 additions and 3 deletions

View file

@ -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 = {

View file

@ -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