Skip to content

Commit

Permalink
Merge branch 'master' into js-cachable-assets
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticmachine authored Apr 29, 2020
2 parents f06d044 + beb07c2 commit 2943a61
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 73 deletions.
65 changes: 34 additions & 31 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
node_modules
bower_components
/data
/optimize
/build
/target
**/*.js.snap
**/graphql/types.ts
/.es
/plugins
/build
/built_assets
/data
/html_docs
/src/plugins/data/common/es_query/kuery/ast/_generated_/**
/src/plugins/vis_type_timelion/public/_generated_/**
/src/legacy/ui/public/flot-charts
/optimize
/plugins
/test/fixtures/scenarios
/src/legacy/core_plugins/console/public/webpackShims
/x-pack/build
node_modules
target

!/.eslintrc.js

# plugin overrides
/src/core/lib/kbn_internal_native_observable
/src/legacy/core_plugins/console/public/tests/webpackShims
/src/legacy/core_plugins/console/public/webpackShims
/src/legacy/plugin_discovery/plugin_pack/__tests__/fixtures/plugins/broken
/src/legacy/ui/public/flot-charts
/src/legacy/ui/public/utils/decode_geo_hash.js
/src/plugins/data/common/es_query/kuery/ast/_generated_/**
/src/plugins/vis_type_timelion/public/_generated_/**
/src/plugins/vis_type_timelion/public/webpackShims/jquery.flot.*
/src/core/lib/kbn_internal_native_observable
/packages/*/target
/packages/eslint-config-kibana
/packages/kbn-pm/dist
/packages/kbn-plugin-generator/sao_template/template
/packages/kbn-ui-framework/dist
/packages/kbn-ui-framework/doc_site/build
/packages/kbn-ui-framework/generator-kui/*/templates/
/packages/kbn-test/src/functional_test_runner/__tests__/fixtures/
/packages/kbn-test/src/functional_test_runner/lib/config/__tests__/fixtures/
/x-pack/legacy/plugins/maps/public/vendor/**
/x-pack/coverage
/x-pack/build
/x-pack/legacy/plugins/**/__tests__/fixtures/**
/packages/kbn-interpreter/src/common/lib/grammar.js
/x-pack/legacy/plugins/apm/e2e/cypress/**/snapshots.js
/x-pack/legacy/plugins/canvas/canvas_plugin
/x-pack/legacy/plugins/canvas/canvas_plugin_src/lib/flot-charts
/x-pack/legacy/plugins/canvas/shareable_runtime/build
/x-pack/legacy/plugins/canvas/storybook
/x-pack/legacy/plugins/canvas/canvas_plugin_src/lib/flot-charts
/x-pack/legacy/plugins/infra/common/graphql/types.ts
/x-pack/legacy/plugins/infra/public/graphql/types.ts
/x-pack/legacy/plugins/infra/server/graphql/types.ts
/x-pack/legacy/plugins/apm/e2e/cypress/**/snapshots.js
/src/legacy/plugin_discovery/plugin_pack/__tests__/fixtures/plugins/broken
**/graphql/types.ts
**/*.js.snap
!/.eslintrc.js
/x-pack/legacy/plugins/maps/public/vendor/**

# package overrides
/packages/eslint-config-kibana
/packages/kbn-interpreter/src/common/lib/grammar.js
/packages/kbn-plugin-generator/sao_template/template
/packages/kbn-pm/dist
/packages/kbn-test/src/functional_test_runner/__tests__/fixtures/
/packages/kbn-test/src/functional_test_runner/lib/config/__tests__/fixtures/
/packages/kbn-ui-framework/dist
/packages/kbn-ui-framework/doc_site/build
/packages/kbn-ui-framework/generator-kui/*/templates/

20 changes: 6 additions & 14 deletions x-pack/plugins/ingest_manager/server/services/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { SavedObjectsClientContract } from 'src/core/server';
import { safeLoad } from 'js-yaml';
import { AuthenticatedUser } from '../../../security/server';
import {
DeleteDatasourcesResponse,
Expand Down Expand Up @@ -239,20 +238,13 @@ async function _assignPackageStreamToStream(
throw new Error(`Stream template not found for dataset ${dataset}`);
}

// Populate template variables from input config and stream config
const data: { [k: string]: string | string[] } = {};
if (input.vars) {
for (const key of Object.keys(input.vars)) {
data[key] = input.vars[key].value;
}
}
if (stream.vars) {
for (const key of Object.keys(stream.vars)) {
data[key] = stream.vars[key].value;
}
}
const yaml = safeLoad(createStream(data, pkgStream.buffer.toString()));
const yaml = createStream(
// Populate template variables from input vars and stream vars
Object.assign({}, input.vars, stream.vars),
pkgStream.buffer.toString()
);
stream.agent_stream = yaml;

return { ...stream };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,61 @@

import { createStream } from './agent';

test('Test creating a stream from template', () => {
const streamTemplate = `
input: log
paths:
{{#each paths}}
- {{this}}
{{/each}}
exclude_files: [".gz$"]
processors:
- add_locale: ~
`;
const vars = {
paths: ['/usr/local/var/log/nginx/access.log'],
};
describe('createStream', () => {
it('should work', () => {
const streamTemplate = `
input: log
paths:
{{#each paths}}
- {{this}}
{{/each}}
exclude_files: [".gz$"]
processors:
- add_locale: ~
`;
const vars = {
paths: { value: ['/usr/local/var/log/nginx/access.log'] },
};

const output = createStream(vars, streamTemplate);
const output = createStream(vars, streamTemplate);
expect(output).toEqual({
input: 'log',
paths: ['/usr/local/var/log/nginx/access.log'],
exclude_files: ['.gz$'],
processors: [{ add_locale: null }],
});
});

expect(output).toBe(`
input: log
paths:
- /usr/local/var/log/nginx/access.log
exclude_files: [".gz$"]
processors:
- add_locale: ~
`);
it('should support yaml values', () => {
const streamTemplate = `
input: redis/metrics
metricsets: ["key"]
test: null
{{#if key.patterns}}
key.patterns: {{key.patterns}}
{{/if}}
`;
const vars = {
'key.patterns': {
type: 'yaml',
value: `
- limit: 20
pattern: '*'
`,
},
};

const output = createStream(vars, streamTemplate);
expect(output).toEqual({
input: 'redis/metrics',
metricsets: ['key'],
test: null,
'key.patterns': [
{
limit: 20,
pattern: '*',
},
],
});
});
});
69 changes: 64 additions & 5 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,12 +5,71 @@
*/

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

interface StreamVars {
[k: string]: string | string[];
function isValidKey(key: string) {
return key !== '__proto__' && key !== 'constructor' && key !== 'prototype';
}

export function createStream(vars: StreamVars, streamTemplate: string) {
const template = Handlebars.compile(streamTemplate);
return template(vars);
function replaceVariablesInYaml(yamlVariables: { [k: string]: any }, yaml: any) {
if (Object.keys(yamlVariables).length === 0 || !yaml) {
return yaml;
}

Object.entries(yaml).forEach(([key, value]: [string, any]) => {
if (typeof value === 'object') {
yaml[key] = replaceVariablesInYaml(yamlVariables, value);
}
if (typeof value === 'string' && value in yamlVariables) {
yaml[key] = yamlVariables[value];
}
});

return yaml;
}

function buildTemplateVariables(variables: DatasourceConfigRecord) {
const yamlValues: { [k: string]: any } = {};
const vars = Object.entries(variables).reduce((acc, [key, recordEntry]) => {
// support variables with . like key.patterns
const keyParts = key.split('.');
const lastKeyPart = keyParts.pop();

if (!lastKeyPart || !isValidKey(lastKeyPart)) {
throw new Error('Invalid key');
}

let varPart = acc;
for (const keyPart of keyParts) {
if (!isValidKey(keyPart)) {
throw new Error('Invalid key');
}
if (!varPart[keyPart]) {
varPart[keyPart] = {};
}
varPart = varPart[keyPart];
}

if (recordEntry.type && recordEntry.type === 'yaml') {
const yamlKeyPlaceholder = `##${key}##`;
varPart[lastKeyPart] = `"${yamlKeyPlaceholder}"`;
yamlValues[yamlKeyPlaceholder] = recordEntry.value ? safeLoad(recordEntry.value) : null;
} else {
varPart[lastKeyPart] = recordEntry.value;
}
return acc;
}, {} as { [k: string]: any });

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, {});

return replaceVariablesInYaml(yamlValues, yamlFromStream);
}

0 comments on commit 2943a61

Please sign in to comment.