Added ldap host failover test, updated error handling

Error handling updated after testing, since the ldap 'starttls'
operation can throw an error, or maybe return false. The PHP docs are
quite under-documented in regards to this function.
This commit is contained in:
Dan Brown 2022-10-16 22:15:15 +01:00
parent fc4380cbc7
commit 392eef8273
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
2 changed files with 41 additions and 2 deletions

View File

@ -247,8 +247,13 @@ class LdapService
// Start and verify TLS if it's enabled
if ($this->config['start_tls']) {
$started = $this->ldap->startTls($ldapConnection);
if (!$started) {
try {
$tlsStarted = $this->ldap->startTls($ldapConnection);
} catch (ErrorException $exception) {
$tlsStarted = false;
}
if (!$tlsStarted) {
throw new LdapException('Could not start TLS connection');
}
}

View File

@ -29,6 +29,7 @@ class LdapTest extends TestCase
config()->set([
'auth.method' => 'ldap',
'auth.defaults.guard' => 'ldap',
'services.ldap.server' => 'ldap.example.com',
'services.ldap.base_dn' => 'dc=ldap,dc=local',
'services.ldap.email_attribute' => 'mail',
'services.ldap.display_name_attribute' => 'cn',
@ -581,6 +582,39 @@ class LdapTest extends TestCase
$this->checkLdapReceivesCorrectDetails('ldap.bookstack.com', 'ldap.bookstack.com', 389);
}
public function test_host_fail_over_by_using_semicolon_seperated_hosts()
{
app('config')->set([
'services.ldap.server' => 'ldap-tiger.example.com;ldap-donkey.example.com:8080',
]);
// Standard mocks
$this->commonLdapMocks(0, 1, 1, 2, 1);
$this->mockLdap->shouldReceive('searchAndGetEntries')->times(1)->andReturn(['count' => 1, 0 => [
'uid' => [$this->mockUser->name],
'cn' => [$this->mockUser->name],
'dn' => ['dc=test' . config('services.ldap.base_dn')],
]]);
$this->mockLdap->shouldReceive('connect')->once()->with('ldap-tiger.example.com', 389)->andReturn(false);
$this->mockLdap->shouldReceive('connect')->once()->with('ldap-donkey.example.com', 8080)->andReturn($this->resourceId);
$this->mockUserLogin();
}
public function test_host_fail_over_by_using_semicolon_seperated_hosts_still_throws_error()
{
app('config')->set([
'services.ldap.server' => 'ldap-tiger.example.com;ldap-donkey.example.com:8080',
]);
$this->mockLdap->shouldReceive('connect')->once()->with('ldap-tiger.example.com', 389)->andReturn(false);
$this->mockLdap->shouldReceive('connect')->once()->with('ldap-donkey.example.com', 8080)->andReturn(false);
$resp = $this->mockUserLogin();
$resp->assertStatus(500);
$resp->assertSee('Cannot connect to ldap server, Initial connection failed');
}
public function test_forgot_password_routes_inaccessible()
{
$resp = $this->get('/password/email');