mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Matthew Swift
08.23.2014 4ca2a9a3650344ed74e55c1bdb646dc88cf91965
Fix OPENDJ-1270: Avoid unnecessary DNS lookups when performing bind requests

* rename LDAP{ConnectionFactory|Listener}.getHostname() to getHostName()
* change LDAP{ConnectionFactory|Listener}.getAddress() to return InetSocketAddresses
* change constructors to only accept InetSocketAddresses instead of more generic SocketAddress
* ensure that LDAP{ConnectionFactory|Listener}.getAddress() returns an address which represents the exact socket address passed in during construction, except in the case where a listener is created with port 0 where we return the selected port
* modify LDAP{ConnectionFactory|Listener}.getHostName() to return whatever was provided as the host name during construction, which may have been a raw IP address. This avoids attempts to perform a reverse DNS lookup in the case where the provided host name was an IP address and, in particular, avoids DNS timeouts in environments where there is no DNS
* modify client bind processing to use LDAPConnectionFactory.getHostName() as the server name used for SASL authentication, and avoid any potential DNS delays as a result.
9 files modified
213 ■■■■ changed files
opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/LDAPConnection.java 10 ●●●●● patch | view | raw | blame | history
opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/LDAPConnectionFactoryImpl.java 10 ●●●● patch | view | raw | blame | history
opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/LDAPListenerImpl.java 20 ●●●● patch | view | raw | blame | history
opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java 46 ●●●●● patch | view | raw | blame | history
opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/LDAPConnectionFactory.java 49 ●●●●● patch | view | raw | blame | history
opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/LDAPListener.java 54 ●●●●● patch | view | raw | blame | history
opendj-ldap-sdk/src/test/java/com/forgerock/opendj/ldap/LDAPConnectionTestCase.java 6 ●●●● patch | view | raw | blame | history
opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/LDAPServer.java 10 ●●●● patch | view | raw | blame | history
opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java 8 ●●●● patch | view | raw | blame | history
opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/LDAPConnection.java
@@ -22,7 +22,7 @@
 *
 *
 *      Copyright 2010 Sun Microsystems, Inc.
 *      Portions Copyright 2011-2013 ForgeRock AS
 *      Portions Copyright 2011-2014 ForgeRock AS
 */
package com.forgerock.opendj.ldap;
@@ -32,7 +32,6 @@
import static org.forgerock.opendj.ldap.ErrorResultException.newErrorResult;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
@@ -82,6 +81,7 @@
import org.glassfish.grizzly.ssl.SSLFilter;
import com.forgerock.opendj.util.CompletedFutureResult;
import com.forgerock.opendj.util.StaticUtils;
import com.forgerock.opendj.util.Validator;
/**
@@ -253,11 +253,7 @@
        final int messageID = nextMsgID.getAndIncrement();
        final BindClient context;
        try {
            context =
                    request.createBindClient(
                            connection.getPeerAddress() instanceof InetSocketAddress
                            ? ((InetSocketAddress) connection.getPeerAddress()).getHostName()
                            : connection.getPeerAddress().toString());
            context = request.createBindClient(StaticUtils.getHostName(factory.getSocketAddress()));
        } catch (final Exception e) {
            // FIXME: I18N need to have a better error message.
            // FIXME: Is this the best result code?
opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/LDAPConnectionFactoryImpl.java
@@ -22,7 +22,7 @@
 *
 *
 *      Copyright 2010 Sun Microsystems, Inc.
 *      Portions copyright 2011-2013 ForgeRock AS
 *      Portions copyright 2011-2014 ForgeRock AS
 */
package com.forgerock.opendj.ldap;
@@ -32,7 +32,7 @@
import static org.forgerock.opendj.ldap.ErrorResultException.*;
import java.io.IOException;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -208,7 +208,7 @@
    private final LDAPClientFilter clientFilter;
    private final FilterChain defaultFilterChain;
    private final LDAPOptions options;
    private final SocketAddress socketAddress;
    private final InetSocketAddress socketAddress;
    /**
     * Prevents the transport and timeoutChecker being released when there are
@@ -236,7 +236,7 @@
     * @param options
     *            The LDAP connection options to use when creating connections.
     */
    public LDAPConnectionFactoryImpl(final SocketAddress address, final LDAPOptions options) {
    public LDAPConnectionFactoryImpl(final InetSocketAddress address, final LDAPOptions options) {
        this.transport = DEFAULT_TRANSPORT.acquireIfNull(options.getTCPNIOTransport());
        this.socketAddress = address;
        this.options = new LDAPOptions(options);
@@ -281,7 +281,7 @@
     *
     * @return The address of the Directory Server.
     */
    public SocketAddress getSocketAddress() {
    public InetSocketAddress getSocketAddress() {
        return socketAddress;
    }
opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/LDAPListenerImpl.java
@@ -22,7 +22,7 @@
 *
 *
 *      Copyright 2010 Sun Microsystems, Inc.
 *      Portions copyright 2011-2013 ForgeRock AS
 *      Portions copyright 2011-2014 ForgeRock AS
 */
package com.forgerock.opendj.ldap;
@@ -32,7 +32,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
@@ -48,6 +48,7 @@
import org.glassfish.grizzly.nio.transport.TCPNIOTransport;
import com.forgerock.opendj.util.ReferenceCountedObject;
import com.forgerock.opendj.util.StaticUtils;
/**
 * LDAP listener implementation.
@@ -58,6 +59,7 @@
    private final ServerConnectionFactory<LDAPClientContext, Integer> connectionFactory;
    private final TCPNIOServerConnection serverConnection;
    private final AtomicBoolean isClosed = new AtomicBoolean();
    private final InetSocketAddress socketAddress;
    /**
     * Creates a new LDAP listener implementation which will listen for LDAP
@@ -74,7 +76,7 @@
     *             If an error occurred while trying to listen on the provided
     *             address.
     */
    public LDAPListenerImpl(final SocketAddress address,
    public LDAPListenerImpl(final InetSocketAddress address,
            final ServerConnectionFactory<LDAPClientContext, Integer> factory,
            final LDAPListenerOptions options) throws IOException {
        this.transport = DEFAULT_TRANSPORT.acquireIfNull(options.getTCPNIOTransport());
@@ -88,6 +90,14 @@
        final TCPNIOBindingHandler bindingHandler =
                TCPNIOBindingHandler.builder(transport.get()).processor(defaultFilterChain).build();
        this.serverConnection = bindingHandler.bind(address, options.getBacklog());
        /*
         * Get the socket address now, ensuring that the host is the same as the
         * one provided in the constructor. The port will have changed if 0 was
         * passed in.
         */
        final int port = ((InetSocketAddress) serverConnection.getLocalAddress()).getPort();
        socketAddress = new InetSocketAddress(StaticUtils.getHostName(address), port);
    }
    @Override
@@ -110,8 +120,8 @@
     *
     * @return The address that this LDAP listener is listening on.
     */
    public SocketAddress getSocketAddress() {
        return serverConnection.getLocalAddress();
    public InetSocketAddress getSocketAddress() {
        return socketAddress;
    }
    @Override
opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java
@@ -22,7 +22,7 @@
 *
 *
 *      Copyright 2009-2010 Sun Microsystems, Inc.
 *      Portions copyright 2011-2013 ForgeRock AS
 *      Portions copyright 2011-2014 ForgeRock AS
 */
package com.forgerock.opendj.util;
@@ -33,6 +33,8 @@
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
@@ -2223,6 +2225,48 @@
        return ' ';
    }
    /**
     * Returns the host name associated with the provided {@code InetSocketAddress},
     * without performing a DNS lookup.
     *
     * @param socketAddress
     *            The socket address which is expected to be an instance of
     *            {@code InetSocketAddress}.
     * @return The host name associated with the provided {@code SocketAddress},
     *         or {@code null} if it is unknown.
     */
    public static String getHostName(final InetSocketAddress socketAddress) {
        /*
         * See OPENDJ-1270 for more information about slow DNS queries.
         *
         * We must avoid calling getHostName() in the case where it is likely to
         * perform a blocking DNS query. Ideally we would call getHostString(),
         * but the method was only added in JDK7.
         */
        if (socketAddress.isUnresolved()) {
            /*
             * Usually socket addresses are resolved on creation. If the address
             * is unresolved then there must be a user provided hostname instead
             * and getHostName will not perform a reverse lookup.
             */
            return socketAddress.getHostName();
        } else {
            /*
             * Simulate getHostString() by parsing the toString()
             * representation. This assumes that the toString() representation
             * is stable, which I assume it is because it is documented.
             */
            final InetAddress address = socketAddress.getAddress();
            final String hostSlashIp = address.toString();
            final int slashPos = hostSlashIp.indexOf('/');
            if (slashPos == 0) {
                return hostSlashIp.substring(1);
            } else {
                return hostSlashIp.substring(0, slashPos);
            }
        }
    }
    // Prevent instantiation.
    private StaticUtils() {
        // No implementation required.
opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/LDAPConnectionFactory.java
@@ -22,16 +22,16 @@
 *
 *
 *      Copyright 2009-2010 Sun Microsystems, Inc.
 *      Portions copyright 2011-2013 ForgeRock AS.
 *      Portions copyright 2011-2014 ForgeRock AS.
 */
package org.forgerock.opendj.ldap;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import com.forgerock.opendj.ldap.LDAPConnectionFactoryImpl;
import com.forgerock.opendj.util.StaticUtils;
import com.forgerock.opendj.util.Validator;
/**
@@ -54,7 +54,7 @@
     * @throws NullPointerException
     *             If {@code address} was {@code null}.
     */
    public LDAPConnectionFactory(final SocketAddress address) {
    public LDAPConnectionFactory(final InetSocketAddress address) {
        this(address, new LDAPOptions());
    }
@@ -69,7 +69,7 @@
     * @throws NullPointerException
     *             If {@code address} or {@code options} was {@code null}.
     */
    public LDAPConnectionFactory(final SocketAddress address, final LDAPOptions options) {
    public LDAPConnectionFactory(final InetSocketAddress address, final LDAPOptions options) {
        Validator.ensureNotNull(address, options);
        this.impl = new LDAPConnectionFactoryImpl(address, options);
    }
@@ -106,7 +106,7 @@
     */
    public LDAPConnectionFactory(final String host, final int port, final LDAPOptions options) {
        Validator.ensureNotNull(host, options);
        final SocketAddress address = new InetSocketAddress(host, port);
        final InetSocketAddress address = new InetSocketAddress(host, port);
        this.impl = new LDAPConnectionFactoryImpl(address, options);
    }
@@ -117,13 +117,7 @@
     *         or {@code null} if it is unknown.
     */
    public InetAddress getAddress() {
        final SocketAddress socketAddress = getSocketAddress();
        if (socketAddress instanceof InetSocketAddress) {
            final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
            return inetSocketAddress.getAddress();
        } else {
            return null;
        }
        return getSocketAddress().getAddress();
    }
    @Override
@@ -143,35 +137,24 @@
    }
    /**
     * Returns the host name that this LDAP listener is listening on.
     * Returns the host name that this LDAP listener is listening on. The
     * returned host name is the same host name that was provided during
     * construction and may be an IP address. More specifically, this method
     * will not perform a reverse DNS lookup.
     *
     * @return The host name that this LDAP listener is listening on, or
     *         {@code null} if it is unknown.
     * @return The host name that this LDAP listener is listening on.
     */
    public String getHostname() {
        final SocketAddress socketAddress = getSocketAddress();
        if (socketAddress instanceof InetSocketAddress) {
            final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
            return inetSocketAddress.getHostName();
        } else {
            return null;
        }
    public String getHostName() {
        return StaticUtils.getHostName(getSocketAddress());
    }
    /**
     * Returns the port that this LDAP listener is listening on.
     *
     * @return The port that this LDAP listener is listening on, or {@code -1}
     *         if it is unknown.
     * @return The port that this LDAP listener is listening on.
     */
    public int getPort() {
        final SocketAddress socketAddress = getSocketAddress();
        if (socketAddress instanceof InetSocketAddress) {
            final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
            return inetSocketAddress.getPort();
        } else {
            return -1;
        }
        return getSocketAddress().getPort();
    }
    /**
@@ -179,7 +162,7 @@
     *
     * @return The address that this LDAP listener is listening on.
     */
    public SocketAddress getSocketAddress() {
    public InetSocketAddress getSocketAddress() {
        return impl.getSocketAddress();
    }
opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/LDAPListener.java
@@ -22,7 +22,7 @@
 *
 *
 *      Copyright 2009-2010 Sun Microsystems, Inc.
 *      Portions copyright 2012 ForgeRock AS.
 *      Portions copyright 2012-2014 ForgeRock AS.
 */
package org.forgerock.opendj.ldap;
@@ -31,9 +31,9 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import com.forgerock.opendj.ldap.LDAPListenerImpl;
import com.forgerock.opendj.util.StaticUtils;
import com.forgerock.opendj.util.Validator;
/**
@@ -137,7 +137,7 @@
            final ServerConnectionFactory<LDAPClientContext, Integer> factory,
            final LDAPListenerOptions options) throws IOException {
        Validator.ensureNotNull(factory, options);
        final SocketAddress address = new InetSocketAddress(port);
        final InetSocketAddress address = new InetSocketAddress(port);
        this.impl = new LDAPListenerImpl(address, factory, options);
    }
@@ -156,7 +156,7 @@
     * @throws NullPointerException
     *             If {@code address} or {code factory} was {@code null}.
     */
    public LDAPListener(final SocketAddress address,
    public LDAPListener(final InetSocketAddress address,
            final ServerConnectionFactory<LDAPClientContext, Integer> factory) throws IOException {
        this(address, factory, new LDAPListenerOptions());
    }
@@ -179,7 +179,7 @@
     *             If {@code address}, {code factory}, or {@code options} was
     *             {@code null}.
     */
    public LDAPListener(final SocketAddress address,
    public LDAPListener(final InetSocketAddress address,
            final ServerConnectionFactory<LDAPClientContext, Integer> factory,
            final LDAPListenerOptions options) throws IOException {
        Validator.ensureNotNull(address, factory, options);
@@ -232,7 +232,7 @@
            final ServerConnectionFactory<LDAPClientContext, Integer> factory,
            final LDAPListenerOptions options) throws IOException {
        Validator.ensureNotNull(host, factory, options);
        final SocketAddress address = new InetSocketAddress(host, port);
        final InetSocketAddress address = new InetSocketAddress(host, port);
        this.impl = new LDAPListenerImpl(address, factory, options);
    }
@@ -247,49 +247,31 @@
    /**
     * Returns the {@code InetAddress} that this LDAP listener is listening on.
     *
     * @return The {@code InetAddress} that this LDAP listener is listening on,
     *         or {@code null} if it is unknown.
     * @return The {@code InetAddress} that this LDAP listener is listening on.
     */
    public InetAddress getAddress() {
        final SocketAddress socketAddress = getSocketAddress();
        if (socketAddress instanceof InetSocketAddress) {
            final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
            return inetSocketAddress.getAddress();
        } else {
            return null;
        }
        return getSocketAddress().getAddress();
    }
    /**
     * Returns the host name that this LDAP listener is listening on.
     * Returns the host name that this LDAP listener is listening on. The
     * returned host name is the same host name that was provided during
     * construction and may be an IP address. More specifically, this method
     * will not perform a reverse DNS lookup.
     *
     * @return The host name that this LDAP listener is listening on, or
     *         {@code null} if it is unknown.
     * @return The host name that this LDAP listener is listening on.
     */
    public String getHostname() {
        final SocketAddress socketAddress = getSocketAddress();
        if (socketAddress instanceof InetSocketAddress) {
            final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
            return inetSocketAddress.getHostName();
        } else {
            return null;
        }
    public String getHostName() {
        return StaticUtils.getHostName(getSocketAddress());
    }
    /**
     * Returns the port that this LDAP listener is listening on.
     *
     * @return The port that this LDAP listener is listening on, or {@code -1}
     *         if it is unknown.
     * @return The port that this LDAP listener is listening on.
     */
    public int getPort() {
        final SocketAddress socketAddress = getSocketAddress();
        if (socketAddress instanceof InetSocketAddress) {
            final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
            return inetSocketAddress.getPort();
        } else {
            return -1;
        }
        return getSocketAddress().getPort();
    }
    /**
@@ -297,7 +279,7 @@
     *
     * @return The address that this LDAP listener is listening on.
     */
    public SocketAddress getSocketAddress() {
    public InetSocketAddress getSocketAddress() {
        return impl.getSocketAddress();
    }
opendj-ldap-sdk/src/test/java/com/forgerock/opendj/ldap/LDAPConnectionTestCase.java
@@ -21,7 +21,7 @@
 * CDDL HEADER END
 *
 *
 *      Copyright 2013 ForgeRock AS.
 *      Copyright 2013-2014 ForgeRock AS.
 */
package com.forgerock.opendj.ldap;
@@ -31,7 +31,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
import org.forgerock.opendj.ldap.Connections;
@@ -75,7 +75,7 @@
    }
    private void doTestRequestTimeout(boolean isPersistentSearch) throws Exception {
        SocketAddress address = TestCaseUtils.findFreeSocketAddress();
        InetSocketAddress address = TestCaseUtils.findFreeSocketAddress();
        /*
         * Use a mock server implementation which will ignore incoming requests
opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/LDAPServer.java
@@ -22,7 +22,7 @@
 *
 *
 *      Copyright 2010 Sun Microsystems, Inc.
 *      Portions copyright 2011-2013 ForgeRock AS
 *      Portions copyright 2011-2014 ForgeRock AS
 */
package org.forgerock.opendj.ldap;
@@ -30,7 +30,7 @@
import static com.forgerock.opendj.ldap.LDAPConstants.TYPE_AUTHENTICATION_SASL;
import java.io.IOException;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -225,8 +225,8 @@
                        final Map<String, String> props = new HashMap<String, String>();
                        props.put(Sasl.QOP, "auth-conf,auth-int,auth");
                        saslServer =
                                Sasl.createSaslServer(saslMech, "ldap", clientContext
                                        .getLocalAddress().getHostName(), props,
                                Sasl.createSaslServer(saslMech, "ldap",
                                        listener.getHostName(), props,
                                        new CallbackHandler() {
                                            public void handle(Callback[] callbacks)
                                                    throws IOException,
@@ -563,7 +563,7 @@
     *
     * @return The socket address of the server.
     */
    public synchronized SocketAddress getSocketAddress() {
    public synchronized InetSocketAddress getSocketAddress() {
        if (!isRunning) {
            throw new IllegalStateException("Server is not running");
        }
opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
@@ -22,7 +22,7 @@
 *
 *
 *      Copyright 2009-2010 Sun Microsystems, Inc.
 *      Portions copyright 2012-2013 ForgeRock AS.
 *      Portions copyright 2012-2014 ForgeRock AS.
 */
package org.forgerock.opendj.ldap;
@@ -80,14 +80,14 @@
     *
     * @return The free port.
     */
    public static SocketAddress findFreeSocketAddress() {
    public static InetSocketAddress findFreeSocketAddress() {
        try {
            ServerSocket serverLdapSocket = new ServerSocket();
            serverLdapSocket.setReuseAddress(true);
            serverLdapSocket.bind(new InetSocketAddress("127.0.0.1", 0));
            final SocketAddress address = serverLdapSocket.getLocalSocketAddress();
            serverLdapSocket.close();
            return address;
            return (InetSocketAddress) address;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
@@ -129,7 +129,7 @@
     *
     * @return The socket address of the server.
     */
    public static SocketAddress getServerSocketAddress() {
    public static InetSocketAddress getServerSocketAddress() {
        return LDAPServer.getInstance().getSocketAddress();
    }