Skip to content

Commit

Permalink
[Fleet] Remove escaping numeric value in agent template (#114123)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Oct 14, 2021
1 parent e22974a commit 065b42b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
14 changes: 8 additions & 6 deletions x-pack/plugins/fleet/server/services/epm/agent/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ foo: {{bar}}
some_text_field: {{should_be_text}}
multi_text_field:
{{#each multi_text}}
- {{this}}
- !!str {{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' },
should_be_text: { type: 'text', value: 'textvalue' },
multi_text: { type: 'text', value: ['1234', 'foo', 'bar'] },
};

Expand All @@ -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'],
});
});
Expand Down Expand Up @@ -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}}
Expand All @@ -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',
Expand Down
18 changes: 0 additions & 18 deletions x-pack/plugins/fleet/server/services/epm/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]) => {
Expand All @@ -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;
}
Expand Down

0 comments on commit 065b42b

Please sign in to comment.