From 32592b03dc89868b08aecd293390d2e5fe5128d6 Mon Sep 17 00:00:00 2001 From: cytopia Date: Sat, 11 Aug 2018 13:50:21 +0200 Subject: [PATCH] Replace Redis password option with Redis startup options to make it more dynamic --- .devilbox/www/config.php | 22 +++++++++++++++---- .devilbox/www/include/lib/container/Redis.php | 2 +- docker-compose.yml | 11 +++++----- env-example | 16 ++++++++++++-- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/.devilbox/www/config.php b/.devilbox/www/config.php index d6579a74..19fd8ad1 100644 --- a/.devilbox/www/config.php +++ b/.devilbox/www/config.php @@ -127,12 +127,26 @@ function loadClass($class) { break; case 'Redis': + + // Check if redis is using a password + $REDIS_ROOT_PASSWORD = ''; + + $_REDIS_ARGS = loadClass('Helper')->getEnv('REDIS_ARGS'); + $_REDIS_PASS = preg_split("/--requirepass\s+/", $_REDIS_ARGS); + if (is_array($_REDIS_PASS) && count($_REDIS_PASS)) { + // In case the option is specified multiple times, use the last effective one. + $_REDIS_PASS = $_REDIS_PASS[count($_REDIS_PASS)-1]; + if (strlen($_REDIS_PASS) > 0) { + $REDIS_ROOT_PASSWORD = $_REDIS_PASS; + } + } + loadFile($class, $cnt_dir); - if(loadClass('Helper')->getEnv('REDIS_ROOT_PASSWORD') == ''){ + if ($REDIS_ROOT_PASSWORD == '') { $_LOADED_LIBS[$class] = \devilbox\Redis::getInstance($GLOBALS['REDIS_HOST_NAME']); - }else{ + } else { $_LOADED_LIBS[$class] = \devilbox\Redis::getInstance($GLOBALS['REDIS_HOST_NAME'], array( - 'pass' => loadClass('Helper')->getEnv('REDIS_ROOT_PASSWORD'), + 'pass' => $REDIS_ROOT_PASSWORD, )); } break; @@ -149,7 +163,7 @@ function loadClass($class) { // Get optional docker classes default: - // Redis + // Unknown class exit('Class does not exist: '.$class); } return $_LOADED_LIBS[$class]; diff --git a/.devilbox/www/include/lib/container/Redis.php b/.devilbox/www/include/lib/container/Redis.php index e58e000e..078bd322 100644 --- a/.devilbox/www/include/lib/container/Redis.php +++ b/.devilbox/www/include/lib/container/Redis.php @@ -44,7 +44,7 @@ class Redis extends BaseClass implements BaseInterface $this->setConnectErrno(1); //loadClass('Logger')->error($this->_connect_error); } else { - if(array_key_exists('pass', $data)){ + if (array_key_exists('pass', $data)) { $redis->auth($data['pass']); } $redis->set('devilbox-version', $GLOBALS['DEVILBOX_VERSION'].' ('.$GLOBALS['DEVILBOX_DATE'].')'); diff --git a/docker-compose.yml b/docker-compose.yml index a5156ab6..08206056 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -402,12 +402,11 @@ services: # ------------------------------------------------------------ redis: image: redis:${REDIS_SERVER:-3.2} - - # REDIS CUSTOM PASSWORD SETUP - #comment out the following 3 lines if you want to set a custom redis password, also set the redis pass variable in .env - #command: redis-server --requirepass ${REDIS_ROOT_PASSWORD} - #environment: - # - ALLOW_EMPTY_PASSWORD=yes + + # Apply custom arguments to redis startup + command: redis-server ${REDIS_ARGS:- } + environment: + - REDIS_ARGS=${REDIS_ARGS:- } ports: # [local-machine:]local-port:docker-port diff --git a/env-example b/env-example index 21ef5762..5fd1efbc 100644 --- a/env-example +++ b/env-example @@ -576,10 +576,22 @@ HOST_PORT_PGSQL=5432 ### HOST_PORT_REDIS=6379 + ### -### Comment out to set the redis custom password also, in docker-compose.yml comment out the REDIS CUSTOM PASSWORD SETUP section +### Custom startup arguments ### -#REDIS_ROOT_PASSWORD=root +### Apply custom startup arguments to redis +### +### Example: Password protection +### Add password protection to the Redis server by specifying it should +### require a password. +### Note: Do not add quotes or spaces to the password +### +### REDIS_ARGS=--requirepass my-redis-root-password +### +### +REDIS_ARGS= +#REDIS_ARGS=--requirepass my-redis-root-password