[Fleet] Remove escaping numeric value in agent template (#114123)

This commit is contained in:
Nicolas Chaulet 2021-10-14 15:28:43 -04:00 committed by GitHub
parent e22974a85f
commit 065b42b661
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 24 deletions

View file

@ -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',

View file

@ -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;
}