Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.0] [Fleet] Delete non existing streams in overrideInputs (#116998) #117114

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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