mirror of
https://github.com/elastic/kibana.git
synced 2025-04-25 10:23:14 -04:00
[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
2 changed files with 28 additions and 0 deletions
|
@ -22,10 +22,23 @@ password: {{password}}
|
||||||
{{#if password}}
|
{{#if password}}
|
||||||
hidden_password: {{password}}
|
hidden_password: {{password}}
|
||||||
{{/if}}
|
{{/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 = {
|
const vars = {
|
||||||
paths: { value: ['/usr/local/var/log/nginx/access.log'] },
|
paths: { value: ['/usr/local/var/log/nginx/access.log'] },
|
||||||
password: { type: 'password', value: '' },
|
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);
|
const output = compileTemplate(vars, streamTemplate);
|
||||||
|
@ -35,6 +48,9 @@ hidden_password: {{password}}
|
||||||
exclude_files: ['.gz$'],
|
exclude_files: ['.gz$'],
|
||||||
processors: [{ add_locale: null }],
|
processors: [{ add_locale: null }],
|
||||||
password: '',
|
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;
|
return yaml;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const maybeEscapeNumericString = (value: string) => {
|
||||||
|
return value.length && !isNaN(+value) ? `"${value}"` : value;
|
||||||
|
};
|
||||||
|
|
||||||
function buildTemplateVariables(variables: PackagePolicyConfigRecord, templateStr: string) {
|
function buildTemplateVariables(variables: PackagePolicyConfigRecord, templateStr: string) {
|
||||||
const yamlValues: { [k: string]: any } = {};
|
const yamlValues: { [k: string]: any } = {};
|
||||||
const vars = Object.entries(variables).reduce((acc, [key, recordEntry]) => {
|
const vars = Object.entries(variables).reduce((acc, [key, recordEntry]) => {
|
||||||
|
@ -84,6 +88,14 @@ function buildTemplateVariables(variables: PackagePolicyConfigRecord, templateSt
|
||||||
const yamlKeyPlaceholder = `##${key}##`;
|
const yamlKeyPlaceholder = `##${key}##`;
|
||||||
varPart[lastKeyPart] = `"${yamlKeyPlaceholder}"`;
|
varPart[lastKeyPart] = `"${yamlKeyPlaceholder}"`;
|
||||||
yamlValues[yamlKeyPlaceholder] = recordEntry.value ? safeLoad(recordEntry.value) : null;
|
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 {
|
} else {
|
||||||
varPart[lastKeyPart] = recordEntry.value;
|
varPart[lastKeyPart] = recordEntry.value;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue