Replace Redis password option with Redis startup options to make it more dynamic

This commit is contained in:
cytopia 2018-08-11 13:50:21 +02:00
parent d4deff1340
commit 32592b03dc
No known key found for this signature in database
GPG Key ID: 6D56EDB8695128A2
4 changed files with 38 additions and 13 deletions

View File

@ -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];

View File

@ -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'].')');

View File

@ -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

View File

@ -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