Skip to content

Commit

Permalink
[fleet] Add escape_string and to_json helpers (elastic#135992) (elast…
Browse files Browse the repository at this point in the history
…ic#136209)

* Add escape_string and to_json helpers

* [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix'

* Add regexp for new line and fix tests

* [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix'

Co-authored-by: kibanamachine <[email protected]>
(cherry picked from commit 4a23325)

Co-authored-by: Marc Guasch <[email protected]>
  • Loading branch information
kibanamachine and marc-gr authored Jul 12, 2022
1 parent 671cd5b commit 9bcd62b
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
89 changes: 89 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 @@ -199,6 +199,95 @@ pcap: false
});
});

describe('escape_string helper', () => {
const streamTemplate = `
input: log
password: {{escape_string password}}
`;

const streamTemplateWithNewlinesAndEscapes = `
input: log
text_var: {{escape_string text_var}}
`;

it('should wrap in single quotes and escape any single quotes in the string', () => {
const vars = {
password: { type: 'password', value: "ab'c'" },
};

const output = compileTemplate(vars, streamTemplate);
expect(output).toEqual({
input: 'log',
password: "ab'c'",
});
});

it('should respect new lines and literal escapes', () => {
const vars = {
text_var: {
type: 'text',
value: `This is a text with
New lines and \\n escaped values.`,
},
};

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

describe('to_json helper', () => {
const streamTemplate = `
input: log
json_var: {{to_json json_var}}
`;

const streamTemplateWithNewYaml = `
input: log
yaml_var: {{to_json yaml_var}}
`;

it('should parse a json string into a json object', () => {
const vars = {
json_var: { type: 'text', value: `{"foo":["bar","bazz"]}` },
};

const output = compileTemplate(vars, streamTemplate);
expect(output).toEqual({
input: 'log',
json_var: {
foo: ['bar', 'bazz'],
},
});
});

it('should parse a yaml string into a json object', () => {
const vars = {
yaml_var: {
type: 'yaml',
value: `foo:
bar:
- a
- b`,
},
};

const output = compileTemplate(vars, streamTemplateWithNewYaml);
expect(output).toEqual({
input: 'log',
yaml_var: {
foo: {
bar: ['a', 'b'],
},
},
});
});
});

it('should support optional yaml values at root level', () => {
const streamTemplate = `
input: logs
Expand Down
19 changes: 19 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 @@ -104,6 +104,25 @@ function containsHelper(this: any, item: string, check: string | string[], optio
}
handlebars.registerHelper('contains', containsHelper);

// escapeStringHelper will wrap the provided string with single quotes.
// Single quoted strings in yaml need to escape single quotes by doubling them
// and to respect any incoming newline we also need to double them, otherwise
// they will be replaced with a space.
function escapeStringHelper(str: string) {
return "'" + str.replace(/\'/g, "''").replace(/\n/g, '\n\n') + "'";
}
handlebars.registerHelper('escape_string', escapeStringHelper);

// toJsonHelper will convert any object to a Json string.
function toJsonHelper(value: any) {
if (typeof value === 'string') {
// if we get a string we assume is an already serialized json
return value;
}
return JSON.stringify(value);
}
handlebars.registerHelper('to_json', toJsonHelper);

function replaceRootLevelYamlVariables(yamlVariables: { [k: string]: any }, yamlTemplate: string) {
if (Object.keys(yamlVariables).length === 0 || !yamlTemplate) {
return yamlTemplate;
Expand Down

0 comments on commit 9bcd62b

Please sign in to comment.