use of org.apache.directory.ldap.client.api.LdapConnection in project Singularity by HubSpot.
the class SingularityLDAPDatastore method getUser.
@Override
public Optional<SingularityUser> getUser(String user) {
if (configuration.isStripUserEmailDomain()) {
user = user.split("@")[0];
}
if (ldapCache.isPresent()) {
Optional<SingularityUser> cachedResult = ldapCache.get().getIfPresent(user);
if (cachedResult != null) {
return cachedResult;
}
}
final Set<String> groups = new HashSet<>();
try {
final LdapConnection connection = connectionPool.getConnection();
try {
checkState(connection.isConnected(), "not connected");
checkState(connection.isAuthenticated(), "not authenticated");
connection.bind();
final long startTime = System.currentTimeMillis();
try {
final EntryCursor userCursor = connection.search(configuration.getUserBaseDN(), String.format(configuration.getUserFilter(), user), SearchScope.ONELEVEL, configuration.getUserNameAttribute(), configuration.getUserEmailAttribute());
if (!userCursor.next()) {
if (ldapCache.isPresent()) {
ldapCache.get().put(user, Optional.empty());
}
return Optional.empty();
}
final Entry userEntry = userCursor.get();
// get group info
final EntryCursor cursor = connection.search(configuration.getGroupBaseDN(), String.format(configuration.getGroupFilter(), user), configuration.getGroupSearchScope(), configuration.getGroupNameAttribute());
while (cursor.next()) {
groups.add(cursor.get().get(configuration.getGroupNameAttribute()).getString());
}
Optional<SingularityUser> result = Optional.of(new SingularityUser(user, com.google.common.base.Optional.fromNullable(Strings.emptyToNull(userEntry.get(configuration.getUserNameAttribute()).getString())), com.google.common.base.Optional.fromNullable(Strings.emptyToNull(userEntry.get(configuration.getUserEmailAttribute()).getString())), groups));
if (ldapCache.isPresent()) {
ldapCache.get().put(user, result);
}
return result;
} finally {
LOG.trace("Loaded {}'s user data in {}", user, JavaUtils.duration(startTime));
connection.unBind();
}
} finally {
connectionPool.releaseConnection(connection);
}
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
use of org.apache.directory.ldap.client.api.LdapConnection in project knox by apache.
the class SimpleLdapServerTest method testBind.
@Test
public void testBind() throws LdapException, IOException {
LdapConnection connection;
connection = new LdapNetworkConnection("localhost", port);
try {
connection.bind("uid=guest,ou=people,dc=hadoop,dc=apache,dc=org", "guest-password");
} finally {
connection.close();
}
connection = new LdapNetworkConnection("localhost", port);
try {
connection.bind("uid=nobody,ou=people,dc=hadoop,dc=apache,dc=org", "guest-password");
fail("Expected LdapAuthenticationException");
} catch (LdapAuthenticationException e) {
// Expected
} finally {
connection.close();
}
connection = new LdapNetworkConnection("localhost", port);
try {
connection.bind("uid=guest,ou=people,dc=hadoop,dc=apache,dc=org", "wrong-password");
fail("Expected LdapAuthenticationException");
} catch (LdapAuthenticationException e) {
// Expected
} finally {
connection.close();
}
}
use of org.apache.directory.ldap.client.api.LdapConnection in project ldapchai by ldapchai.
the class ApacheLdapProviderImpl method init.
@Override
public void init(final ChaiConfiguration chaiConfig, final ChaiProviderFactory providerFactory) throws ChaiUnavailableException {
this.chaiConfig = chaiConfig;
super.init(chaiConfig, providerFactory);
// grab the first URL from the list.
currentLdapUrl = chaiConfig.bindURLsAsList().get(0);
final URI ldapURL = URI.create(currentLdapUrl);
final LdapConnectionConfig ldapConnectionConfig = new LdapConnectionConfig();
ldapConnectionConfig.setLdapHost(ldapURL.getHost());
ldapConnectionConfig.setLdapPort(ldapURL.getPort());
if (ldapURL.getScheme().equalsIgnoreCase("ldaps")) {
ldapConnectionConfig.setUseSsl(true);
final boolean usePromiscuousSSL = Boolean.parseBoolean(chaiConfig.getSetting(ChaiSetting.PROMISCUOUS_SSL));
if (usePromiscuousSSL) {
try {
final PromiscuousTrustManager promiscuousTrustManager = new PromiscuousTrustManager();
ldapConnectionConfig.setTrustManagers(promiscuousTrustManager);
} catch (Exception e) {
LOGGER.error("error creating promiscuous ssl ldap socket factory: " + e.getMessage());
}
} else if (chaiConfig.getTrustManager() != null) {
try {
final X509TrustManager[] trustManager = chaiConfig.getTrustManager();
ldapConnectionConfig.setTrustManagers(trustManager);
} catch (Exception e) {
LOGGER.error("error creating configured ssl ldap socket factory: " + e.getMessage());
}
}
}
final LdapConnection newConnection;
try {
newConnection = new LdapNetworkConnection(ldapConnectionConfig);
newConnection.connect();
final String bindPassword = chaiConfig.getSetting(ChaiSetting.BIND_PASSWORD);
final String bindDN = chaiConfig.getSetting(ChaiSetting.BIND_DN);
newConnection.bind(bindDN, bindPassword);
} catch (LdapException e) {
final String message = e.getMessage();
if (message.contains("Cannot connect on the server")) {
throw new ChaiUnavailableException(message, ChaiError.COMMUNICATION, false, false);
}
throw ChaiUnavailableException.forErrorMessage(message);
} catch (Exception e) {
e.printStackTrace();
final String message = e.getMessage();
throw new ChaiUnavailableException(message, ChaiError.UNKNOWN, false, false);
}
connection = newConnection;
}
use of org.apache.directory.ldap.client.api.LdapConnection in project wildfly-camel by wildfly-extras.
the class LdapIntegrationTest method getWiredConnection.
private LdapConnection getWiredConnection(int port) throws Exception {
LdapConnection connection = new LdapNetworkConnection(InetAddress.getLocalHost().getHostName(), port);
connection.bind(ServerDNConstants.ADMIN_SYSTEM_DN, "secret");
return connection;
}
use of org.apache.directory.ldap.client.api.LdapConnection in project jackrabbit-oak by apache.
the class UnboundLookupConnectionValidatorTest method testValidateNotConnected.
@Test
public void testValidateNotConnected() {
LdapConnection connection = Mockito.mock(LdapConnection.class);
when(connection.isConnected()).thenReturn(false);
assertFalse(validator.validate(connection));
}
Aggregations