Search in sources :

Example 6 with LdapConnection

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);
    }
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) Entry(org.apache.directory.api.ldap.model.entry.Entry) SingularityUser(com.hubspot.singularity.SingularityUser) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) HashSet(java.util.HashSet) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 7 with LdapConnection

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();
    }
}
Also used : LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection) Test(org.junit.Test)

Example 8 with LdapConnection

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;
}
Also used : ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException) LdapConnectionConfig(org.apache.directory.ldap.client.api.LdapConnectionConfig) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) URI(java.net.URI) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) ChaiOperationException(com.novell.ldapchai.exception.ChaiOperationException) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 9 with LdapConnection

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;
}
Also used : LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 10 with LdapConnection

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));
}
Also used : LdapConnection(org.apache.directory.ldap.client.api.LdapConnection) Test(org.junit.Test)

Aggregations

LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)178 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)164 ArrayList (java.util.ArrayList)89 FinderException (org.apache.directory.fortress.core.FinderException)73 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)63 Entry (org.apache.directory.api.ldap.model.entry.Entry)50 SearchCursor (org.apache.directory.api.ldap.model.cursor.SearchCursor)49 Modification (org.apache.directory.api.ldap.model.entry.Modification)43 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)41 UpdateException (org.apache.directory.fortress.core.UpdateException)41 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)37 LdapNoSuchObjectException (org.apache.directory.api.ldap.model.exception.LdapNoSuchObjectException)20 CreateException (org.apache.directory.fortress.core.CreateException)17 RemoveException (org.apache.directory.fortress.core.RemoveException)17 LdapNetworkConnection (org.apache.directory.ldap.client.api.LdapNetworkConnection)14 IOException (java.io.IOException)12 Permission (org.apache.directory.fortress.core.model.Permission)9 Dn (org.apache.directory.api.ldap.model.name.Dn)7 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)6 SecurityException (org.apache.directory.fortress.core.SecurityException)6