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

[Ingest] Support root level yaml variables in agent stream template #66120

Merged
merged 1 commit into from
May 13, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import { createStream } from './agent';
describe('createStream', () => {
it('should work', () => {
const streamTemplate = `
input: log
paths:
{{#each paths}}
- {{this}}
{{/each}}
exclude_files: [".gz$"]
processors:
- add_locale: ~
password: {{password}}
{{#if password}}
hidden_password: {{password}}
{{/if}}
input: log
paths:
{{#each paths}}
- {{this}}
{{/each}}
exclude_files: [".gz$"]
processors:
- add_locale: ~
password: {{password}}
{{#if password}}
hidden_password: {{password}}
{{/if}}
`;
const vars = {
paths: { value: ['/usr/local/var/log/nginx/access.log'] },
Expand All @@ -39,13 +39,16 @@ describe('createStream', () => {

it('should support yaml values', () => {
const streamTemplate = `
input: redis/metrics
metricsets: ["key"]
test: null
password: {{password}}
{{#if key.patterns}}
key.patterns: {{key.patterns}}
{{/if}}
input: redis/metrics
metricsets: ["key"]
test: null
password: {{password}}
{{custom}}
custom: {{ custom }}
{{#if key.patterns}}
key.patterns: {{key.patterns}}
{{/if}}
{{ testEmpty }}
`;
const vars = {
'key.patterns': {
Expand All @@ -55,6 +58,12 @@ describe('createStream', () => {
pattern: '*'
`,
},
custom: {
type: 'yaml',
value: `
foo: bar
`,
},
password: { type: 'password', value: '' },
};

Expand All @@ -70,6 +79,8 @@ describe('createStream', () => {
},
],
password: '',
foo: 'bar',
custom: { foo: 'bar' },
});
});
});
53 changes: 34 additions & 19 deletions x-pack/plugins/ingest_manager/server/services/epm/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,32 @@
*/

import Handlebars from 'handlebars';
import { safeLoad } from 'js-yaml';
import { safeLoad, safeDump } from 'js-yaml';
import { DatasourceConfigRecord } from '../../../../common';

export function createStream(variables: DatasourceConfigRecord, streamTemplate: string) {
const { vars, yamlValues } = buildTemplateVariables(variables, streamTemplate);

const template = Handlebars.compile(streamTemplate, { noEscape: true });
let stream = template(vars);
stream = replaceRootLevelYamlVariables(yamlValues, stream);

const yamlFromStream = safeLoad(stream, {});

// Hack to keep empty string ('') values around in the end yaml because
// `safeLoad` replaces empty strings with null
const patchedYamlFromStream = Object.entries(yamlFromStream).reduce((acc, [key, value]) => {
if (value === null && typeof vars[key] === 'string' && vars[key].trim() === '') {
acc[key] = '';
} else {
acc[key] = value;
}
return acc;
}, {} as { [k: string]: any });

return replaceVariablesInYaml(yamlValues, patchedYamlFromStream);
}

function isValidKey(key: string) {
return key !== '__proto__' && key !== 'constructor' && key !== 'prototype';
}
Expand All @@ -29,7 +52,7 @@ function replaceVariablesInYaml(yamlVariables: { [k: string]: any }, yaml: any)
return yaml;
}

function buildTemplateVariables(variables: DatasourceConfigRecord) {
function buildTemplateVariables(variables: DatasourceConfigRecord, streamTemplate: string) {
const yamlValues: { [k: string]: any } = {};
const vars = Object.entries(variables).reduce((acc, [key, recordEntry]) => {
// support variables with . like key.patterns
Expand Down Expand Up @@ -64,23 +87,15 @@ function buildTemplateVariables(variables: DatasourceConfigRecord) {
return { vars, yamlValues };
}

export function createStream(variables: DatasourceConfigRecord, streamTemplate: string) {
const { vars, yamlValues } = buildTemplateVariables(variables);

const template = Handlebars.compile(streamTemplate, { noEscape: true });
const stream = template(vars);
const yamlFromStream = safeLoad(stream, {});
function replaceRootLevelYamlVariables(yamlVariables: { [k: string]: any }, yamlTemplate: string) {
if (Object.keys(yamlVariables).length === 0 || !yamlTemplate) {
return yamlTemplate;
}

// Hack to keep empty string ('') values around in the end yaml because
// `safeLoad` replaces empty strings with null
const patchedYamlFromStream = Object.entries(yamlFromStream).reduce((acc, [key, value]) => {
if (value === null && typeof vars[key] === 'string' && vars[key].trim() === '') {
acc[key] = '';
} else {
acc[key] = value;
}
return acc;
}, {} as { [k: string]: any });
let patchedTemplate = yamlTemplate;
Object.entries(yamlVariables).forEach(([key, val]) => {
patchedTemplate = patchedTemplate.replace(new RegExp(`^"${key}"`, 'gm'), safeDump(val));
});

return replaceVariablesInYaml(yamlValues, patchedYamlFromStream);
return patchedTemplate;
}