diff --git a/x-pack/plugins/fleet/common/services/validate_package_policy.test.ts b/x-pack/plugins/fleet/common/services/validate_package_policy.test.ts index 98056d6906c59..afb6a2f806f9a 100644 --- a/x-pack/plugins/fleet/common/services/validate_package_policy.test.ts +++ b/x-pack/plugins/fleet/common/services/validate_package_policy.test.ts @@ -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)', () => { diff --git a/x-pack/plugins/fleet/common/services/validate_package_policy.ts b/x-pack/plugins/fleet/common/services/validate_package_policy.ts index b6befdf8c790e..f1e28bfbe4e55 100644 --- a/x-pack/plugins/fleet/common/services/validate_package_policy.ts +++ b/x-pack/plugins/fleet/common/services/validate_package_policy.ts @@ -143,7 +143,7 @@ export const validatePackagePolicy = ( results[name] = input.enabled ? validatePackagePolicyConfig( configEntry, - inputVarDefsByPolicyTemplateAndType[inputKey][name], + (inputVarDefsByPolicyTemplateAndType[inputKey] ?? {})[name], name, safeLoadYaml ) @@ -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) {