Skip to content

Commit 5d3302c

Browse files
committed
Add new setting to toggle whether to use the password for sentinels
1 parent 5399f37 commit 5d3302c

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

Queue/Backend/Sentinel.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class Sentinel extends Redis
2222
{
2323
private $masterName = '';
2424

25+
/*
26+
* @var bool
27+
*/
28+
private $usePasswordForSentinelInstances = false;
29+
2530
protected function connect()
2631
{
2732
$hosts = explode(',', $this->host);
@@ -36,7 +41,7 @@ protected function connect()
3641
$configuredClient = new \Credis_Client($host, $ports[$index], $timeout = 0.5, $persistent = false);
3742
$configuredClient->forceStandalone();
3843
$configuredClient->connect();
39-
if (!empty($this->password)) {
44+
if ($this->usePasswordForSentinelInstances && !empty($this->password)) {
4045
$configuredClient->auth($this->password);
4146
}
4247
$configuredSentinel = new \Credis_Sentinel($configuredClient);
@@ -67,6 +72,11 @@ public function setSentinelMasterName($name)
6772
$this->masterName = $name;
6873
}
6974

75+
public function setUsePasswordForSentinelInstances(bool $usePasswordForSentinelInstances)
76+
{
77+
$this->usePasswordForSentinelInstances = $usePasswordForSentinelInstances;
78+
}
79+
7080
protected function evalScript($script, $keys, $args)
7181
{
7282
return $this->redis->eval($script, $keys, $args);

Queue/Factory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public static function makeBackendFromSettings(SystemSettings $settings)
7272
$redis = new Queue\Backend\Sentinel();
7373
$redis->setSentinelMasterName($masterName);
7474
$redis->setDatabase($database);
75+
if (!empty($settings->getUsePasswordForSentinelInstances())) {
76+
$redis->setUsePasswordForSentinelInstances(true);
77+
}
7578
}
7679
} elseif ($settings->isUsingClusterBackend()) {
7780
$redis = new Queue\Backend\RedisCluster();

SystemSettings.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings
6060
/** @var Setting */
6161
public $sentinelMasterName;
6262

63+
/** @var Setting */
64+
public $usePasswordForSentinelInstances;
65+
6366
public function getAvailableRedisBackendTypes()
6467
{
6568
return array(
@@ -83,6 +86,7 @@ protected function init()
8386
$this->backend = $this->createBackendSetting();
8487
$this->useWhatRedisBackendType = $this->createUseWhatRedisBackendType();
8588
$this->sentinelMasterName = $this->createSetSentinelMasterName();
89+
$this->usePasswordForSentinelInstances = $this->createUsePasswordForSentinelInstances();
8690
$this->redisHost = $this->createRedisHostSetting();
8791
$this->redisPort = $this->createRedisPortSetting();
8892
$this->redisTimeout = $this->createRedisTimeoutSetting();
@@ -114,6 +118,11 @@ public function getSentinelMasterName()
114118
return $this->sentinelMasterName->getValue();
115119
}
116120

121+
public function getUsePasswordForSentinelInstances()
122+
{
123+
return $this->usePasswordForSentinelInstances->getValue();
124+
}
125+
117126
public function isUsingUnixSocket()
118127
{
119128
return substr($this->redisHost->getValue(), 0, 1) === '/';
@@ -373,6 +382,16 @@ private function createSetSentinelMasterName()
373382
});
374383
}
375384

385+
private function createUsePasswordForSentinelInstances()
386+
{
387+
return $this->makeSetting('usePasswordForSentinelInstances', $default = false, FieldConfig::TYPE_BOOL, function (FieldConfig $field) {
388+
$field->title = Piwik::translate('QueuedTracking_UsePasswordForSentinelsTitle');
389+
$field->condition = 'backend=="redis"';
390+
$field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX;
391+
$field->inlineHelp = Piwik::translate('QueuedTracking_UsePasswordForSentinelsHelp', ['</br></br>']) . '</br>';
392+
});
393+
}
394+
376395
public function checkMatchHostsAndPorts()
377396
{
378397
$hosts = $this->redisHost->getValue();

lang/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
"MasterNameFieldTitle": "Redis Sentinel Master name",
3434
"MasterNameFieldHelp": "The sentinel master name only needs to be configured if Sentinel is enabled.",
3535
"NumHostsNotMatchNumPorts": "The number of configured hosts doesn't match the number of configured ports.",
36-
"MultipleServersOnlyConfigurableIfSentinelEnabled": "Multiple hosts or ports can be only configured when Redis Sentinel is on. The plugin README will tell you how to do so."
36+
"MultipleServersOnlyConfigurableIfSentinelEnabled": "Multiple hosts or ports can be only configured when Redis Sentinel is on. The plugin README will tell you how to do so.",
37+
"UsePasswordForSentinelsTitle": "Use password with Redis Sentinels",
38+
"UsePasswordForSentinelsHelp": "Only relevant if Sentinel is enabled.%1$sIf enabled, the Redis password will also be used when authenticating with Sentinel instances to obtain the master connection information. Otherwise, the password is only used to authenticate with the Redis master."
3739
}
3840
}

0 commit comments

Comments
 (0)