[Fleet] Fix incorrect conversion of string to numeric values in agent YAML (#90371)
* Convert user values back to string after yaml template compilation if they were strings originally * Add better test cases and adjust patch * Fix when field is undefined * Handle array of strings too
This commit is contained in:
parent
befe41067e
commit
f4dc6d0235
|
@ -22,10 +22,23 @@ password: {{password}}
|
|||
{{#if password}}
|
||||
hidden_password: {{password}}
|
||||
{{/if}}
|
||||
{{#if optional_field}}
|
||||
optional_field: {{optional_field}}
|
||||
{{/if}}
|
||||
foo: {{bar}}
|
||||
some_text_field: {{should_be_text}}
|
||||
multi_text_field:
|
||||
{{#each multi_text}}
|
||||
- {{this}}
|
||||
{{/each}}
|
||||
`;
|
||||
const vars = {
|
||||
paths: { value: ['/usr/local/var/log/nginx/access.log'] },
|
||||
password: { type: 'password', value: '' },
|
||||
optional_field: { type: 'text', value: undefined },
|
||||
bar: { type: 'text', value: 'bar' },
|
||||
should_be_text: { type: 'text', value: '1234' },
|
||||
multi_text: { type: 'text', value: ['1234', 'foo', 'bar'] },
|
||||
};
|
||||
|
||||
const output = compileTemplate(vars, streamTemplate);
|
||||
|
@ -35,6 +48,9 @@ hidden_password: {{password}}
|
|||
exclude_files: ['.gz$'],
|
||||
processors: [{ add_locale: null }],
|
||||
password: '',
|
||||
foo: 'bar',
|
||||
some_text_field: '1234',
|
||||
multi_text_field: ['1234', 'foo', 'bar'],
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -58,6 +58,10 @@ function replaceVariablesInYaml(yamlVariables: { [k: string]: any }, yaml: any)
|
|||
return yaml;
|
||||
}
|
||||
|
||||
const maybeEscapeNumericString = (value: string) => {
|
||||
return value.length && !isNaN(+value) ? `"${value}"` : value;
|
||||
};
|
||||
|
||||
function buildTemplateVariables(variables: PackagePolicyConfigRecord, templateStr: string) {
|
||||
const yamlValues: { [k: string]: any } = {};
|
||||
const vars = Object.entries(variables).reduce((acc, [key, recordEntry]) => {
|
||||
|
@ -84,6 +88,14 @@ function buildTemplateVariables(variables: PackagePolicyConfigRecord, templateSt
|
|||
const yamlKeyPlaceholder = `##${key}##`;
|
||||
varPart[lastKeyPart] = `"${yamlKeyPlaceholder}"`;
|
||||
yamlValues[yamlKeyPlaceholder] = recordEntry.value ? safeLoad(recordEntry.value) : null;
|
||||
} else if (recordEntry.type && recordEntry.type === 'text' && recordEntry.value?.length) {
|
||||
if (Array.isArray(recordEntry.value)) {
|
||||
varPart[lastKeyPart] = recordEntry.value.map((value: string) =>
|
||||
maybeEscapeNumericString(value)
|
||||
);
|
||||
} else {
|
||||
varPart[lastKeyPart] = maybeEscapeNumericString(recordEntry.value);
|
||||
}
|
||||
} else {
|
||||
varPart[lastKeyPart] = recordEntry.value;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue