Skip to content
Snippets Groups Projects
Commit c665d029 authored by Philippe Larose's avatar Philippe Larose Committed by Adrien Béraud
Browse files

ad/ldap-connector: fix directory search when ldap/ad server is down

The LDAP and the AD connector now falls back to its internal database
whenever it is not able to access its authentication server. This fixes
a null pointer exception that was otherwise raised in the SearchDirectoryServlet.

Ticket: https://redmine.savoirfairelinux.com/issues/7666
Change-Id: Ifa0df8956d14c9715730e389a1cd6ae68e6bfb48
parent 1df46520
No related branches found
No related tags found
No related merge requests found
......@@ -47,6 +47,7 @@ public class ADConnector implements AuthenticationSource {
public static ActiveDirectorySettings settings;
private final AuthenticationService authenticationService = new AuthenticationService();
private final UserProfileService userProfileService;
private DataStore dataStore;
public ADConnector(String settings, DataStore dataStore) {
Gson gson = GsonFactory.createGson();
......@@ -58,6 +59,7 @@ public class ADConnector implements AuthenticationSource {
endpoint.setHost(ADConnector.settings.getHost());
endpoints.add(endpoint);
}
this.dataStore = dataStore;
userProfileService = new UserProfileService(dataStore);
// Configure scheduler to revoke users
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
......@@ -112,15 +114,26 @@ public class ADConnector implements AuthenticationSource {
@Override
public List<UserProfile> searchUserProfiles(
String queryString, String field, Optional<Integer> page) {
return userProfileService.getUserProfile(queryString, field, false, page);
List<UserProfile> results =
userProfileService.getUserProfile(queryString, field, false, page);
// There is a possibility that the LDAP server is offline or inaccessible
// In that case, we fallback to the local database
if (results == null) {
results = dataStore.searchUserProfiles(queryString, field, page);
}
return results;
}
@Override
public UserProfile getUserProfile(String username) {
List<UserProfile> result =
List<UserProfile> results =
userProfileService.getUserProfile(username, "LOGON_NAME", true, Optional.empty());
if (result == null || result.size() != 1) return null;
return result.get(0);
if (results == null) {
return dataStore.getUserProfile(username);
}
if (results.size() != 1) return null;
return results.get(0);
}
@Override
......
......@@ -48,6 +48,7 @@ public class LDAPConnector implements AuthenticationSource {
private final AuthenticationService authenticationService;
private final UserProfileService userProfileService;
public static LDAPSettings settings;
private DataStore dataStore;
public LDAPConnector(String strSettings, DataStore dataStore) {
Gson gson = GsonFactory.createGson();
......@@ -62,6 +63,7 @@ public class LDAPConnector implements AuthenticationSource {
.connectionInitializers(bindConnectionInitializer)
.build();
authenticationService = new AuthenticationService(new DefaultConnectionFactory(connConfig));
this.dataStore = dataStore;
userProfileService =
new UserProfileService(dataStore, new DefaultConnectionFactory(connConfig));
// Configure scheduler to revoke users
......@@ -83,14 +85,25 @@ public class LDAPConnector implements AuthenticationSource {
@Override
public List<UserProfile> searchUserProfiles(
String queryString, String field, Optional<Integer> page) {
return userProfileService.getUserProfile(queryString, field, false, page);
List<UserProfile> results =
userProfileService.getUserProfile(queryString, field, false, page);
// There is a possibility that the LDAP server is offline or inaccessible
// In that case, we fallback to the local database
if (results == null) {
results = dataStore.searchUserProfiles(queryString, field, page);
}
return results;
}
@Override
public UserProfile getUserProfile(String username) {
List<UserProfile> results =
userProfileService.getUserProfile(username, "LOGON_NAME", true, Optional.empty());
if (results == null || results.size() != 1) return null;
if (results == null) {
return dataStore.getUserProfile(username);
}
if (results.size() != 1) return null;
return results.get(0);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment