Fixed backup host logic

Now forcing backup host applies to both main and background.
And background will fallback to backup if not responding.

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
Côme Chilliet 2022-12-19 12:23:13 +01:00
parent 406750552e
commit 6b7ffcd6a8
No known key found for this signature in database
GPG key ID: A3E2F658B28C760A
2 changed files with 25 additions and 27 deletions

View file

@ -598,25 +598,26 @@ class Connection extends LDAPUtility {
}
}
$forceBackupHost = ($this->configuration->ldapOverrideMainServer || $this->getFromCache('overrideMainServer'));
$hasBackupHost = (trim($this->configuration->ldapBackupHost ?? '') !== '');
$hasBackgroundHost = (trim($this->configuration->ldapBackgroundHost ?? '') !== '');
$useBackupHost = $hasBackupHost && (!\OC::$CLI || !$hasBackgroundHost);
$useBackgroundHost = (\OC::$CLI && $hasBackgroundHost);
$overrideCacheKey = ($useBackgroundHost ? 'overrideBackgroundServer' : 'overrideMainServer');
$forceBackupHost = ($this->configuration->ldapOverrideMainServer || $this->getFromCache($overrideCacheKey));
$bindStatus = false;
try {
if (!$forceBackupHost) {
$host = $this->configuration->ldapHost;
$port = $this->configuration->ldapPort;
if (\OC::$CLI && $hasBackgroundHost) {
$host = $this->configuration->ldapBackgroundHost;
$port = $this->configuration->ldapBackgroundPort;
if (!$forceBackupHost) {
try {
$host = $this->configuration->ldapHost ?? '';
$port = $this->configuration->ldapPort ?? '';
if ($useBackgroundHost) {
$host = $this->configuration->ldapBackgroundHost ?? '';
$port = $this->configuration->ldapBackgroundPort ?? '';
}
$this->doConnect($host, $port);
return $this->bind();
}
} catch (ServerNotAvailableException $e) {
if (!$useBackupHost) {
throw $e;
} catch (ServerNotAvailableException $e) {
if (!$hasBackupHost) {
throw $e;
}
}
$this->logger->warning(
'Main LDAP not reachable, connecting to backup',
@ -626,19 +627,16 @@ class Connection extends LDAPUtility {
);
}
//if LDAP server is not reachable, try the Backup (Replica!) Server
if ($useBackupHost || $forceBackupHost) {
$this->doConnect($this->configuration->ldapBackupHost,
$this->configuration->ldapBackupPort);
$this->bindResult = [];
$bindStatus = $this->bind();
$error = $this->ldap->isResource($this->ldapConnectionRes) ?
$this->ldap->errno($this->ldapConnectionRes) : -1;
if ($bindStatus && $error === 0 && !$this->getFromCache('overrideMainServer')) {
//when bind to backup server succeeded and failed to main server,
//skip contacting him until next cache refresh
$this->writeToCache('overrideMainServer', true);
}
// if LDAP server is not reachable, try the Backup (Replica!) Server
$this->doConnect($this->configuration->ldapBackupHost ?? '', $this->configuration->ldapBackupPort ?? '');
$this->bindResult = [];
$bindStatus = $this->bind();
$error = $this->ldap->isResource($this->ldapConnectionRes) ?
$this->ldap->errno($this->ldapConnectionRes) : -1;
if ($bindStatus && $error === 0 && !$forceBackupHost) {
//when bind to backup server succeeded and failed to main server,
//skip contacting him until next cache refresh
$this->writeToCache($overrideCacheKey, true);
}
return $bindStatus;

View file

@ -121,7 +121,7 @@ class ConnectionTest extends \Test\TestCase {
->willReturn(0);
// Not called often enough? Then, the fallback to the backup server is broken.
$this->connection->expects($this->exactly(4))
$this->connection->expects($this->exactly(2))
->method('getFromCache')
->with('overrideMainServer')
->will($this->onConsecutiveCalls(false, false, true, true));