Skip to content

Commit

Permalink
[Fleet] fixed undefined error when missing policy vars in template (#…
Browse files Browse the repository at this point in the history
…124215) (#124379)

* fixed undefined error when missing policy vars in template

* added validation error if input has no vars

* fixed checks

* added unit test

* fixed checks

* fixed checks

* fixed checks

* moved validation error to validatePackagePolicyConfig

(cherry picked from commit 67430f9)

Co-authored-by: Julia Bardi <[email protected]>
  • Loading branch information
kibanamachine and juliaElastic authored Feb 2, 2022
1 parent d7b67f4 commit c6519d3
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 5 deletions.
126 changes: 126 additions & 0 deletions x-pack/plugins/fleet/common/services/validate_package_policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,132 @@ describe('Fleet - validatePackagePolicy()', () => {
},
});
});

it('returns package policy validation error if input var does not exist', () => {
expect(
validatePackagePolicy(
{
description: 'Linux Metrics',
enabled: true,
inputs: [
{
enabled: true,
streams: [
{
data_stream: {
dataset: 'linux.memory',
type: 'metrics',
},
enabled: true,
},
],
type: 'linux/metrics',
vars: {
period: {
type: 'string',
value: '1s',
},
},
},
],
name: 'linux-3d13ada6-a9ae-46df-8e57-ff5050f4b671',
namespace: 'default',
output_id: '',
package: {
name: 'linux',
title: 'Linux Metrics',
version: '0.6.2',
},
policy_id: 'b25cb6e0-8347-11ec-96f9-6590c25bacf9',
},
{
...mockPackage,
name: 'linux',
policy_templates: [
{
name: 'system',
title: 'Linux kernel metrics',
description: 'Collect system metrics from Linux operating systems',
inputs: [
{
title: 'Collect system metrics from Linux instances',
vars: [
{
name: 'system.hostfs',
type: 'text',
title: 'Proc Filesystem Directory',
multi: false,
required: false,
show_user: true,
description: 'The proc filesystem base directory.',
},
],
type: 'system/metrics',
description:
'Collecting Linux entropy, Network Summary, RAID, service, socket, and users metrics',
},
{
title: 'Collect low-level system metrics from Linux instances',
vars: [],
type: 'linux/metrics',
description: 'Collecting Linux conntrack, ksm, pageinfo metrics.',
},
],
multiple: true,
},
],
data_streams: [
{
dataset: 'linux.memory',
package: 'linux',
path: 'memory',
streams: [
{
input: 'linux/metrics',
title: 'Linux memory metrics',
vars: [
{
name: 'period',
type: 'text',
title: 'Period',
multi: false,
required: true,
show_user: true,
default: '10s',
},
],
template_path: 'stream.yml.hbs',
description: 'Linux paging and memory management metrics',
},
],
title: 'Linux-only memory metrics',
release: 'experimental',
type: 'metrics',
},
],
},
safeLoad
)
).toEqual({
description: null,
inputs: {
'linux/metrics': {
streams: {
'linux.memory': {
vars: {
period: ['Period is required'],
},
},
},
vars: {
period: ['period var definition does not exist'],
},
},
},
name: null,
namespace: null,
});
});
});

describe('works for packages with multiple policy templates (aka integrations)', () => {
Expand Down
15 changes: 10 additions & 5 deletions x-pack/plugins/fleet/common/services/validate_package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const validatePackagePolicy = (
results[name] = input.enabled
? validatePackagePolicyConfig(
configEntry,
inputVarDefsByPolicyTemplateAndType[inputKey][name],
(inputVarDefsByPolicyTemplateAndType[inputKey] ?? {})[name],
name,
safeLoadYaml
)
Expand Down Expand Up @@ -210,10 +210,15 @@ export const validatePackagePolicyConfig = (
}

if (varDef === undefined) {
// eslint-disable-next-line no-console
console.debug(`No variable definition for ${varName} found`);

return null;
errors.push(
i18n.translate('xpack.fleet.packagePolicyValidation.nonExistentVarMessage', {
defaultMessage: '{varName} var definition does not exist',
values: {
varName,
},
})
);
return errors;
}

if (varDef.required) {
Expand Down

0 comments on commit c6519d3

Please sign in to comment.