Add support for environment variable injection in kibana.yml (#16988)

This commit is contained in:
Fabien Baligand 2018-04-13 00:00:29 +02:00 committed by Tyler Smalley
parent 35e01f288f
commit 977cd51f6a
4 changed files with 41 additions and 1 deletions

View file

@ -0,0 +1,5 @@
foo: 1
bar: "pre-${KBN_ENV_VAR1}-mid-${KBN_ENV_VAR2}-post"
elasticsearch:
requestHeadersWhitelist: ["${KBN_ENV_VAR1}", "${KBN_ENV_VAR2}"]

View file

@ -0,0 +1 @@
foo: "${KBN_NON_EXISTENT_ENV_VAR}"

View file

@ -1,7 +1,16 @@
import { isArray, isPlainObject, forOwn, set, transform } from 'lodash';
import { isArray, isPlainObject, forOwn, set, transform, isString } from 'lodash';
import { readFileSync as read } from 'fs';
import { safeLoad } from 'js-yaml';
function replaceEnvVarRefs(val) {
return val.replace(/\$\{(\w+)\}/g, (match, envVarName) => {
if (process.env[envVarName] !== undefined) {
return process.env[envVarName];
} else {
throw new Error(`Unknown environment variable referenced in config : ${envVarName}`);
}
});
}
export function merge(sources) {
return transform(sources, (merged, source) => {
@ -19,6 +28,10 @@ export function merge(sources) {
return;
}
if (isString(val)) {
val = replaceEnvVarRefs(val);
}
set(merged, key, val);
});
}, {});

View file

@ -28,6 +28,26 @@ describe('cli/serve/read_yaml_config', function () {
});
});
it('should inject an environment variable value when setting a value with ${ENV_VAR}', function () {
process.env.KBN_ENV_VAR1 = 'val1';
process.env.KBN_ENV_VAR2 = 'val2';
const config = readYamlConfig([ fixture('en_var_ref_config.yml') ]);
expect(config).toEqual({
foo: 1,
bar: 'pre-val1-mid-val2-post',
elasticsearch: {
requestHeadersWhitelist: ['val1', 'val2']
}
});
});
it('should thow an exception when referenced environment variable in a config value does not exist', function () {
expect(function () {
readYamlConfig([ fixture('invalid_en_var_ref_config.yml') ]);
}).toThrow();
});
describe('different cwd()', function () {
const oldCwd = process.cwd();
const newCwd = join(oldCwd, '..');
@ -55,4 +75,5 @@ describe('cli/serve/read_yaml_config', function () {
process.chdir(oldCwd);
});
});
});