From 1666c9f0b6456cf987f80b5b4541f646e82a2969 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Mon, 9 Oct 2017 12:29:52 +0300 Subject: [PATCH] MXS-1464 Add config 'substitute_variables' With this variables set to true, if $VAR is used as a value in the configuration file, then `$VAR` will be replaced with the value of the environment variable VAR. --- .../Getting-Started/Configuration-Guide.md | 26 +++++++++++++++++++ include/maxscale/config.h | 2 ++ server/core/config.cc | 4 +-- server/core/gateway.cc | 22 ++++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/Documentation/Getting-Started/Configuration-Guide.md b/Documentation/Getting-Started/Configuration-Guide.md index 77dc1e597..7343e61c0 100644 --- a/Documentation/Getting-Started/Configuration-Guide.md +++ b/Documentation/Getting-Started/Configuration-Guide.md @@ -561,6 +561,32 @@ This will log all statements that cannot be parsed completely. This may be useful if you suspect that MariaDB MaxScale routes statements to the wrong server (e.g. to a slave instead of to a master). +#### `substitute_variables` + +Enable or disable the substitution of environment variables in the MaxScale +configuration file. If the substitution of variables is enabled and a +configuration line like +``` +some_parameter=$SOME_VALUE +``` +is encountered, then `$SOME_VALUE` will be replaced with the actual value +of the environment variable `SOME_VALUE`. Note: +* Variable substitution will be made _only_ if '$' is the first character + of the value. +* _Everything_ following '$' is interpreted as the name of the environment + variable. +* Referring to a non-existing environment variable is a fatal error. + +By default, the value of `substitute_variables` is `false`. +``` +substitute_variables=true +``` +The setting of `substitute_variables` will have an effect on all parameters +in the all other sections, irrespective of where the `[maxscale]` section +is placed in the configuration file. However, in the `[maxscale]` section, +to ensure that substitution will take place, place the +`substitute_variables=true` line first. + ### REST API Configuration The MaxScale REST API is an HTTP interface that provides JSON format data diff --git a/include/maxscale/config.h b/include/maxscale/config.h index b9f92336d..beef3986a 100644 --- a/include/maxscale/config.h +++ b/include/maxscale/config.h @@ -157,6 +157,7 @@ extern const char CN_SSL_CERT_VERIFY_DEPTH[]; extern const char CN_SSL_KEY[]; extern const char CN_SSL_VERSION[]; extern const char CN_STRIP_DB_ESC[]; +extern const char CN_SUBSTITUTE_VARIABLES[]; extern const char CN_THREADS[]; extern const char CN_THREAD_STACK_SIZE[]; extern const char CN_TYPE[]; @@ -224,6 +225,7 @@ typedef struct char admin_ssl_ca_cert[PATH_MAX]; /**< Admin SSL CA cert */ int query_retries; /**< Number of times a interrupted query is retried */ time_t query_retry_timeout; /**< Timeout for query retries */ + bool substitute_variables; /**< Should environment variables be substituted */ } MXS_CONFIG; /** diff --git a/server/core/config.cc b/server/core/config.cc index bb5af12a1..a72fd824e 100644 --- a/server/core/config.cc +++ b/server/core/config.cc @@ -135,6 +135,7 @@ const char CN_SSL_CERT_VERIFY_DEPTH[] = "ssl_cert_verify_depth"; const char CN_SSL_KEY[] = "ssl_key"; const char CN_SSL_VERSION[] = "ssl_version"; const char CN_STRIP_DB_ESC[] = "strip_db_esc"; +const char CN_SUBSTITUTE_VARIABLES[] = "substitute_variables"; const char CN_THREADS[] = "threads"; const char CN_THREAD_STACK_SIZE[] = "thread_stack_size"; const char CN_TYPE[] = "type"; @@ -457,8 +458,7 @@ void fix_section_name(char *section) * @param value The Parameter value * @return zero on error */ -static int -ini_handler(void *userdata, const char *section, const char *name, const char *value) +static int ini_handler(void *userdata, const char *section, const char *name, const char *value) { CONFIG_CONTEXT *cntxt = (CONFIG_CONTEXT *)userdata; CONFIG_CONTEXT *ptr = cntxt; diff --git a/server/core/gateway.cc b/server/core/gateway.cc index ce9bca627..c5a2c2900 100644 --- a/server/core/gateway.cc +++ b/server/core/gateway.cc @@ -2617,6 +2617,24 @@ void set_log_augmentation(const char* value) static int cnf_preparser(void* data, const char* section, const char* name, const char* value) { MXS_CONFIG* cnf = config_get_global_options(); + + if (cnf->substitute_variables) + { + if (*value == '$') + { + char* env_value = getenv(value + 1); + + if (!env_value) + { + MXS_ERROR("The environment variable %s, used as value for parameter %s " + "in section %s, does not exist.", value, name, section); + return 0; + } + + value = env_value; + } + } + char *tmp; /** These are read from the configuration file. These will not override * command line parameters but will override default values. */ @@ -2790,6 +2808,10 @@ static int cnf_preparser(void* data, const char* section, const char* name, cons cnf->log_to_shm = config_truth_value((char*)value); } } + else if (strcmp(name, CN_SUBSTITUTE_VARIABLES) == 0) + { + cnf->substitute_variables = config_truth_value(value); + } } return 1;