Skip to content

Commit

Permalink
[Fleet] Delete non existing streams in overrideInputs (#116998)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Nov 2, 2021
1 parent c7b53f1 commit 63cf1d0
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
94 changes: 94 additions & 0 deletions x-pack/plugins/fleet/server/services/package_policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,100 @@ describe('Package policy service', () => {
expect(logfileStream?.enabled).toBe(false);
});
});

describe('when a datastream is deleted from an input', () => {
it('it remove the non existing datastream', () => {
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: [
{
enabled: true,
data_stream: { dataset: 'dataset.test123', type: 'log' },
},
],
},
],
};

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',
},
],
},
],
},
],
// @ts-ignore
assets: {},
};

const inputsOverride: NewPackagePolicyInput[] = [
{
type: 'logs',
enabled: true,
streams: [],
vars: {
path: {
type: 'text',
value: '/var/log/new-logfile.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.value).toEqual(['/var/log/logfile.log']);
});
});
});
});

Expand Down
21 changes: 19 additions & 2 deletions x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ class PackagePolicyService {
pkgName: packagePolicy.package.name,
pkgVersion: packagePolicy.package.version,
});

const registryPkgInfo = await Registry.fetchInfo(pkgInfo.name, pkgInfo.version);
inputs = await this._compilePackagePolicyInputs(
registryPkgInfo,
Expand Down Expand Up @@ -1111,7 +1112,9 @@ export function overridePackageInputs(
}

if (override.vars) {
originalInput = deepMergeVars(originalInput, override) as NewPackagePolicyInput;
const indexOfInput = inputs.indexOf(originalInput);
inputs[indexOfInput] = deepMergeVars(originalInput, override) as NewPackagePolicyInput;
originalInput = inputs[indexOfInput];
}

if (override.streams) {
Expand All @@ -1130,10 +1133,24 @@ export function overridePackageInputs(
}

if (stream.vars) {
originalStream = deepMergeVars(originalStream, stream as InputsOverride);
const indexOfStream = originalInput.streams.indexOf(originalStream);
originalInput.streams[indexOfStream] = deepMergeVars(
originalStream,
stream as InputsOverride
);
originalStream = originalInput.streams[indexOfStream];
}
}
}

// Filter all stream that have been removed from the input
originalInput.streams = originalInput.streams.filter((originalStream) => {
return (
override.streams?.some(
(s) => s.data_stream.dataset === originalStream.data_stream.dataset
) ?? false
);
});
}

const resultingPackagePolicy: NewPackagePolicy = {
Expand Down

0 comments on commit 63cf1d0

Please sign in to comment.