Skip to content

Commit b804d8a

Browse files
committed
Handle array of strings too
1 parent c1b8dd1 commit b804d8a

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

x-pack/plugins/fleet/server/services/epm/agent/agent.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@ optional_field: {{optional_field}}
2727
{{/if}}
2828
foo: {{bar}}
2929
some_text_field: {{should_be_text}}
30+
multi_text_field:
31+
{{#each multi_text}}
32+
- {{this}}
33+
{{/each}}
3034
`;
3135
const vars = {
3236
paths: { value: ['/usr/local/var/log/nginx/access.log'] },
3337
password: { type: 'password', value: '' },
3438
optional_field: { type: 'text', value: undefined },
3539
bar: { type: 'text', value: 'bar' },
3640
should_be_text: { type: 'text', value: '1234' },
41+
multi_text: { type: 'text', value: ['1234', 'foo', 'bar'] },
3742
};
3843

3944
const output = compileTemplate(vars, streamTemplate);
@@ -45,6 +50,7 @@ some_text_field: {{should_be_text}}
4550
password: '',
4651
foo: 'bar',
4752
some_text_field: '1234',
53+
multi_text_field: ['1234', 'foo', 'bar'],
4854
});
4955
});
5056

x-pack/plugins/fleet/server/services/epm/agent/agent.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ function replaceVariablesInYaml(yamlVariables: { [k: string]: any }, yaml: any)
5858
return yaml;
5959
}
6060

61+
const maybeEscapeNumericString = (value: string) => {
62+
return value.length && !isNaN(+value) ? `"${value}"` : value;
63+
};
64+
6165
function buildTemplateVariables(variables: PackagePolicyConfigRecord, templateStr: string) {
6266
const yamlValues: { [k: string]: any } = {};
6367
const vars = Object.entries(variables).reduce((acc, [key, recordEntry]) => {
@@ -84,13 +88,14 @@ function buildTemplateVariables(variables: PackagePolicyConfigRecord, templateSt
8488
const yamlKeyPlaceholder = `##${key}##`;
8589
varPart[lastKeyPart] = `"${yamlKeyPlaceholder}"`;
8690
yamlValues[yamlKeyPlaceholder] = recordEntry.value ? safeLoad(recordEntry.value) : null;
87-
} else if (
88-
recordEntry.type &&
89-
recordEntry.type === 'text' &&
90-
recordEntry.value?.length &&
91-
!isNaN(+recordEntry.value)
92-
) {
93-
varPart[lastKeyPart] = `"${recordEntry.value}"`;
91+
} else if (recordEntry.type && recordEntry.type === 'text' && recordEntry.value?.length) {
92+
if (Array.isArray(recordEntry.value)) {
93+
varPart[lastKeyPart] = recordEntry.value.map((value: string) =>
94+
maybeEscapeNumericString(value)
95+
);
96+
} else {
97+
varPart[lastKeyPart] = maybeEscapeNumericString(recordEntry.value);
98+
}
9499
} else {
95100
varPart[lastKeyPart] = recordEntry.value;
96101
}

0 commit comments

Comments
 (0)