| diff --git a/configfile.js b/configfile.js | |
| index 59d2ad0..2c1ca8f 100644 | |
| --- a/configfile.js | |
| +++ b/configfile.js | |
| @@ -91,6 +91,31 @@ function getStubLogger () { | |
| } | |
| } | |
| +function env_var_replace(value) { | |
| + if (typeof value !== 'string') { | |
| + return value; | |
| + } | |
| + // replace $NAME with environment variable NAME (or $$ with $) | |
| + return value.replace(/(\$\$)|\$(\w+)|\$\{([^}]+)\}/g, (match, p1, p2, p3) => { | |
| + var default_value = ''; | |
| + if (p3) { | |
| + // support default values in the form ${NAME:-default} | |
| + const is_assignment = /:=/.test(p3); | |
| + const name_default = p3.split(/:[-=]/); | |
| + if (name_default.length === 2) { | |
| + p3 = name_default[0]; | |
| + default_value = name_default[1]; | |
| + if (is_assignment && p3 && !process.env[p3]) { | |
| + process.env[p3] = default_value; | |
| + } | |
| + } | |
| + } | |
| + var replacement = p1 ? '$' : (process.env[p2 || p3 || ''] || default_value); | |
| + return replacement; | |
| + }); | |
| +} | |
| +cfreader.env_var_replace = env_var_replace; | |
| + | |
| cfreader.on_watch_event = function (name, type, options, cb) { | |
| return function (fse, filename) { | |
| if (cfreader._sedation_timers[name]) { | |
| @@ -504,6 +529,7 @@ cfreader.load_ini_config = function (name, options) { | |
| pre = ''; | |
| match = regex.param.exec(line); | |
| if (match) { | |
| + match[2] = env_var_replace(match[2]); | |
| is_array_match = regex.is_array.exec(match[1]); | |
| if (is_array_match){ | |
| setter = function (key, value) { | |
| @@ -562,7 +588,7 @@ cfreader.load_flat_config = function (name, type) { | |
| if (type === 'data') { | |
| while (data.length > 0) { | |
| var match = data.match(/^([^\r\n]*)\r?\n?/); | |
| - result.push(match[1]); | |
| + result.push(env_var_replace(match[1])); | |
| data = data.slice(match[0].length); | |
| } | |
| return result; | |
| @@ -579,7 +605,7 @@ cfreader.load_flat_config = function (name, type) { | |
| } | |
| line_data = regex.line.exec(line); | |
| if (line_data) { | |
| - result.push(line_data[1].trim()); | |
| + result.push(env_var_replace(line_data[1].trim())); | |
| } | |
| }); | |
| } | |
| diff --git a/plugins/tls.js b/plugins/tls.js | |
| index 752c478..c2600f3 100644 | |
| --- a/plugins/tls.js | |
| +++ b/plugins/tls.js | |
| @@ -3,6 +3,7 @@ | |
| // see 'haraka -h tls' for help | |
| var tls_socket = require('./tls_socket'); | |
| +var { env_var_replace } = require('../configfile'); | |
| // exported so tests can override config dir | |
| exports.net_utils = require('haraka-net-utils'); | |
| @@ -67,14 +68,14 @@ exports.load_tls_ini = function () { | |
| for (let i = 0; i < config_options.length; i++) { | |
| let opt = config_options[i]; | |
| if (plugin.cfg.main[opt] === undefined) continue; | |
| - plugin.tls_opts[opt] = plugin.cfg.main[opt]; | |
| + plugin.tls_opts[opt] = env_var_replace(plugin.cfg.main[opt]); | |
| } | |
| if (plugin.cfg.inbound) { | |
| for (let i = 0; i < config_options.length; i++) { | |
| let opt = config_options[i]; | |
| if (plugin.cfg.inbound[opt] === undefined) continue; | |
| - plugin.tls_opts[opt] = plugin.cfg.inbound[opt]; | |
| + plugin.tls_opts[opt] = env_var_replace(plugin.cfg.inbound[opt]); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment