From 119a7a0d85030ea0100c3dbc2670f8170f7cef43 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Tue, 22 Jun 2021 13:36:46 -0400 Subject: [PATCH] [Agent Packages] Extend 'contains' helper to work on strings (#102786) (#102943) * Extend 'contains' helper to work on strings * remove stray import Co-authored-by: Andrew Stucki --- .../server/services/epm/agent/agent.test.ts | 29 +++++++++++++++++++ .../fleet/server/services/epm/agent/agent.ts | 5 ++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/epm/agent/agent.test.ts b/x-pack/plugins/fleet/server/services/epm/agent/agent.test.ts index bc4ffffb683589..1be0f73a347e95 100644 --- a/x-pack/plugins/fleet/server/services/epm/agent/agent.test.ts +++ b/x-pack/plugins/fleet/server/services/epm/agent/agent.test.ts @@ -129,6 +129,13 @@ processors: password: {{password}} {{#if password}} hidden_password: {{password}} +{{/if}} + `; + const streamTemplateWithString = ` +{{#if (contains ".pcap" file)}} +pcap: true +{{else}} +pcap: false {{/if}} `; @@ -168,6 +175,28 @@ hidden_password: {{password}} tags: ['foo', 'bar'], }); }); + + it('should support strings', () => { + const vars = { + file: { value: 'foo.pcap' }, + }; + + const output = compileTemplate(vars, streamTemplateWithString); + expect(output).toEqual({ + pcap: true, + }); + }); + + it('should support strings with no match', () => { + const vars = { + file: { value: 'file' }, + }; + + const output = compileTemplate(vars, streamTemplateWithString); + expect(output).toEqual({ + pcap: false, + }); + }); }); it('should support optional yaml values at root level', () => { diff --git a/x-pack/plugins/fleet/server/services/epm/agent/agent.ts b/x-pack/plugins/fleet/server/services/epm/agent/agent.ts index 84a8ab581354af..a0d14e6962a8d4 100644 --- a/x-pack/plugins/fleet/server/services/epm/agent/agent.ts +++ b/x-pack/plugins/fleet/server/services/epm/agent/agent.ts @@ -111,11 +111,12 @@ function buildTemplateVariables(variables: PackagePolicyConfigRecord, templateSt return { vars, yamlValues }; } -function containsHelper(this: any, item: string, list: string[], options: any) { - if (Array.isArray(list) && list.includes(item)) { +function containsHelper(this: any, item: string, check: string | string[], options: any) { + if ((Array.isArray(check) || typeof check === 'string') && check.includes(item)) { if (options && options.fn) { return options.fn(this); } + return true; } return ''; }