From 065b42b6617a199138897eec92c6d2e6a6888a9f Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Thu, 14 Oct 2021 15:28:43 -0400 Subject: [PATCH] [Fleet] Remove escaping numeric value in agent template (#114123) --- .../server/services/epm/agent/agent.test.ts | 14 ++++++++------ .../fleet/server/services/epm/agent/agent.ts | 18 ------------------ 2 files changed, 8 insertions(+), 24 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 1be0f73a347e95..ed5d6473760ff6 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 @@ -29,7 +29,7 @@ foo: {{bar}} some_text_field: {{should_be_text}} multi_text_field: {{#each multi_text}} - - {{this}} + - !!str {{this}} {{/each}} `; const vars = { @@ -37,7 +37,7 @@ multi_text_field: password: { type: 'password', value: '' }, optional_field: { type: 'text', value: undefined }, bar: { type: 'text', value: 'bar' }, - should_be_text: { type: 'text', value: '1234' }, + should_be_text: { type: 'text', value: 'textvalue' }, multi_text: { type: 'text', value: ['1234', 'foo', 'bar'] }, }; @@ -49,7 +49,7 @@ multi_text_field: processors: [{ add_locale: null }], password: '', foo: 'bar', - some_text_field: '1234', + some_text_field: 'textvalue', multi_text_field: ['1234', 'foo', 'bar'], }); }); @@ -217,12 +217,13 @@ input: logs }); }); - it('should escape string values when necessary', () => { + it('should suport !!str for string values', () => { const stringTemplate = ` my-package: asteriskOnly: {{asteriskOnly}} startsWithAsterisk: {{startsWithAsterisk}} - numeric: {{numeric}} + numeric_with_str: !!str {{numeric}} + numeric_without_str: {{numeric}} mixed: {{mixed}} concatenatedEnd: {{a}}{{b}} concatenatedMiddle: {{c}}{{d}} @@ -245,7 +246,8 @@ my-package: 'my-package': { asteriskOnly: '*', startsWithAsterisk: '*lala', - numeric: '100', + numeric_with_str: '100', + numeric_without_str: 100, mixed: '1s', concatenatedEnd: '/opt/package/*/logs/my.log*', concatenatedMiddle: '/opt/*/package/logs/*my.log', 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 a0d14e6962a8d4..a01643b22cf9d5 100644 --- a/x-pack/plugins/fleet/server/services/epm/agent/agent.ts +++ b/x-pack/plugins/fleet/server/services/epm/agent/agent.ts @@ -58,14 +58,6 @@ function replaceVariablesInYaml(yamlVariables: { [k: string]: any }, yaml: any) return yaml; } -const maybeEscapeString = (value: string) => { - // Numeric strings need to be quoted to stay strings. - if (value.length && !isNaN(+value)) { - return `"${value}"`; - } - return value; -}; - function buildTemplateVariables(variables: PackagePolicyConfigRecord, templateStr: string) { const yamlValues: { [k: string]: any } = {}; const vars = Object.entries(variables).reduce((acc, [key, recordEntry]) => { @@ -92,16 +84,6 @@ function buildTemplateVariables(variables: PackagePolicyConfigRecord, templateSt const yamlKeyPlaceholder = `##${key}##`; varPart[lastKeyPart] = recordEntry.value ? `"${yamlKeyPlaceholder}"` : null; yamlValues[yamlKeyPlaceholder] = recordEntry.value ? safeLoad(recordEntry.value) : null; - } else if ( - recordEntry.type && - (recordEntry.type === 'text' || recordEntry.type === 'string') && - recordEntry.value?.length - ) { - if (Array.isArray(recordEntry.value)) { - varPart[lastKeyPart] = recordEntry.value.map((value: string) => maybeEscapeString(value)); - } else { - varPart[lastKeyPart] = maybeEscapeString(recordEntry.value); - } } else { varPart[lastKeyPart] = recordEntry.value; }