Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] [Fleet] add escapeMultilineString Handlebar helper (#195159) #195497

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions x-pack/plugins/fleet/server/services/epm/agent/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,80 @@ New lines and \\n escaped values.`,
});
});

describe('escape_multiline_string helper', () => {
it('should escape new lines', () => {
const streamTemplate = `
input: log
multiline_text: "{{escape_multiline_string multiline_text}}"
`;

const vars = {
multiline_text: {
type: 'textarea',
value: `This is a text with
New lines and \n escaped values.`,
},
};

const output = compileTemplate(vars, streamTemplate);
expect(output).toEqual({
input: 'log',
multiline_text: `This is a text with
New lines and
escaped values.`,
});
});

it('should escape single quotes', () => {
const streamTemplate = `
input: log
multiline_text: "{{escape_multiline_string multiline_text}}"
`;

const vars = {
multiline_text: {
type: 'textarea',
value: `This is a multiline text with
'escaped values.'`,
},
};

const output = compileTemplate(vars, streamTemplate);
expect(output).toEqual({
input: 'log',
multiline_text: `This is a multiline text with
''escaped values.''`,
});
});

it('should allow concatenation of multiline strings', () => {
const streamTemplate = `
input: log
multiline_text: "{{escape_multiline_string multiline_text}}{{escape_multiline_string "
This is a concatenated text
with new lines"}}"
`;

const vars = {
multiline_text: {
type: 'textarea',
value: `This is a text with
New lines and\nescaped values.`,
},
};

const output = compileTemplate(vars, streamTemplate);
expect(output).toEqual({
input: 'log',
multiline_text: `This is a text with
New lines and
escaped values.
This is a concatenated text
with new lines`,
});
});
});

describe('to_json helper', () => {
const streamTemplate = `
input: log
Expand Down
12 changes: 12 additions & 0 deletions x-pack/plugins/fleet/server/services/epm/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ function escapeStringHelper(str: string) {
}
handlebars.registerHelper('escape_string', escapeStringHelper);

/**
* escapeMultilineStringHelper will escape a multiline string by doubling the newlines
* and escaping single quotes.
* This is useful when the string is multiline and needs to be escaped in a yaml file
* without wrapping it in single quotes.
*/
function escapeMultilineStringHelper(str: string) {
if (!str) return undefined;
return str.replace(/\'/g, "''").replace(/\n/g, '\n\n');
}
handlebars.registerHelper('escape_multiline_string', escapeMultilineStringHelper);

// toJsonHelper will convert any object to a Json string.
function toJsonHelper(value: any) {
if (typeof value === 'string') {
Expand Down
Loading