Ldap host failover: updated method names and split out method

This commit is contained in:
Dan Brown 2022-10-16 22:40:10 +01:00
parent 392eef8273
commit 303dbf9b01
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9

View File

@ -216,7 +216,7 @@ class LdapService
$this->ldap->setOption(null, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_NEVER);
}
$serverDetails = $this->parseEnvironmentServer($this->config['server']);
$serverDetails = $this->parseMultiServerString($this->config['server']);
$this->ldapConnection = $this->prepareServerConnection($serverDetails);
return $this->ldapConnection;
@ -225,7 +225,7 @@ class LdapService
/**
* Processes an array of received servers and returns the first working connection.
*
* @param array $serverDetails
* @param array $serverDetails array<array{host: string, port: int}>
* @return resource
* @throws LdapException
*/
@ -234,7 +234,25 @@ class LdapService
$lastException = null;
foreach ($serverDetails as $server) {
try {
$ldapConnection = $this->ldap->connect($server['host'], $server['port']);
return $this->startServerConnection($server);
} catch (LdapException $exception) {
$lastException = $exception;
}
}
throw $lastException;
}
/**
* Attempt to start a server connection from the provided details.
*
* @param array{host: string, port: int} $serverDetail
* @return resource
* @throws LdapException
*/
protected function startServerConnection(array $serverDetail)
{
$ldapConnection = $this->ldap->connect($serverDetail['host'], $serverDetail['port']);
if (!$ldapConnection) {
throw new LdapException(trans('errors.ldap_cannot_connect'));
@ -259,36 +277,28 @@ class LdapService
}
return $ldapConnection;
} catch (LdapException $exception) {
$lastException = $exception;
}
}
throw $lastException;
}
/**
* Parse environment variable with LDAP server and returns an array of recognized servers.
* If you need to use multiple addresses, separate them with a semicolon.
* Ex: 'ldap.example.com:8069;ldaps://ldap.example.com'
* Parse a potentially multi-value LDAP server host string and return an array of host/port detail pairs.
* Multiple hosts are separated with a semicolon, for example: 'ldap.example.com:8069;ldaps://ldap.example.com'
*
* @return array<array{host: string, port: int}>
*/
protected function parseEnvironmentServer(string $environmentServer): array
protected function parseMultiServerString(string $serversString): array
{
$explodedEnvironmentServer = explode(';', $environmentServer);
$result_servers = [];
$serverStringList = explode(';', $serversString);
foreach ($explodedEnvironmentServer as $serverString) {
$result_servers[] = $this->parseServerString($serverString);
}
return $result_servers;
return array_map(fn ($serverStr) => $this->parseSingleServerString($serverStr), $serverStringList);
}
/**
* Parse a LDAP server string and return the host and port for a connection.
* Parse an LDAP server string and return the host and port for a connection.
* Is flexible to formats such as 'ldap.example.com:8069' or 'ldaps://ldap.example.com'.
*
* @return array{host: string, port: int}
*/
protected function parseServerString(string $serverString): array
protected function parseSingleServerString(string $serverString): array
{
$serverNameParts = explode(':', $serverString);