Search in sources :

Example 1 with LDAPListenerConfig

use of com.unboundid.ldap.listener.LDAPListenerConfig in project ldapsdk by pingidentity.

the class LDAPDebugger method doToolProcessing.

/**
 * Performs the actual processing for this tool.  In this case, it gets a
 * connection to the directory server and uses it to perform the requested
 * search.
 *
 * @return  The result code for the processing that was performed.
 */
@Override()
@NotNull()
public ResultCode doToolProcessing() {
    // Create the proxy request handler that will be used to forward requests to
    // a remote directory.
    final ProxyRequestHandler proxyHandler;
    try {
        proxyHandler = new ProxyRequestHandler(createServerSet());
    } catch (final LDAPException le) {
        err("Unable to prepare to connect to the target server:  ", le.getMessage());
        return le.getResultCode();
    }
    // Create the log handler to use for the output.
    final Handler logHandler;
    if (outputFile.isPresent()) {
        try {
            logHandler = new FileHandler(outputFile.getValue().getAbsolutePath());
        } catch (final IOException ioe) {
            err("Unable to open the output file for writing:  ", StaticUtils.getExceptionMessage(ioe));
            return ResultCode.LOCAL_ERROR;
        }
    } else {
        logHandler = new ConsoleHandler();
    }
    StaticUtils.setLogHandlerLevel(logHandler, Level.INFO);
    logHandler.setFormatter(new MinimalLogFormatter(MinimalLogFormatter.DEFAULT_TIMESTAMP_FORMAT, false, false, true));
    // Create the debugger request handler that will be used to write the
    // debug output.
    LDAPListenerRequestHandler requestHandler = new LDAPDebuggerRequestHandler(logHandler, proxyHandler);
    // handler to accomplish that.
    if (codeLogFile.isPresent()) {
        try {
            requestHandler = new ToCodeRequestHandler(codeLogFile.getValue(), true, requestHandler);
        } catch (final Exception e) {
            err("Unable to open code log file '", codeLogFile.getValue().getAbsolutePath(), "' for writing:  ", StaticUtils.getExceptionMessage(e));
            return ResultCode.LOCAL_ERROR;
        }
    }
    // Create and start the LDAP listener.
    final LDAPListenerConfig config = new LDAPListenerConfig(listenPort.getValue(), requestHandler);
    if (listenAddress.isPresent()) {
        try {
            config.setListenAddress(LDAPConnectionOptions.DEFAULT_NAME_RESOLVER.getByName(listenAddress.getValue()));
        } catch (final Exception e) {
            err("Unable to resolve '", listenAddress.getValue(), "' as a valid address:  ", StaticUtils.getExceptionMessage(e));
            return ResultCode.PARAM_ERROR;
        }
    }
    if (listenUsingSSL.isPresent()) {
        try {
            final SSLUtil sslUtil;
            if (generateSelfSignedCertificate.isPresent()) {
                final ObjectPair<File, char[]> keyStoreInfo = SelfSignedCertificateGenerator.generateTemporarySelfSignedCertificate(getToolName(), CryptoHelper.KEY_STORE_TYPE_JKS);
                sslUtil = new SSLUtil(new KeyStoreKeyManager(keyStoreInfo.getFirst(), keyStoreInfo.getSecond(), CryptoHelper.KEY_STORE_TYPE_JKS, null, true), new TrustAllTrustManager(false));
            } else {
                sslUtil = createSSLUtil(true);
            }
            config.setServerSocketFactory(sslUtil.createSSLServerSocketFactory());
        } catch (final Exception e) {
            err("Unable to create a server socket factory to accept SSL-based " + "client connections:  ", StaticUtils.getExceptionMessage(e));
            return ResultCode.LOCAL_ERROR;
        }
    }
    listener = new LDAPListener(config);
    try {
        listener.startListening();
    } catch (final Exception e) {
        err("Unable to start listening for client connections:  ", StaticUtils.getExceptionMessage(e));
        return ResultCode.LOCAL_ERROR;
    }
    // Display a message with information about the port on which it is
    // listening for connections.
    int port = listener.getListenPort();
    while (port <= 0) {
        try {
            Thread.sleep(1L);
        } catch (final Exception e) {
            Debug.debugException(e);
            if (e instanceof InterruptedException) {
                Thread.currentThread().interrupt();
            }
        }
        port = listener.getListenPort();
    }
    if (listenUsingSSL.isPresent()) {
        out("Listening for SSL-based LDAP client connections on port ", port);
    } else {
        out("Listening for LDAP client connections on port ", port);
    }
    // Note that at this point, the listener will continue running in a
    // separate thread, so we can return from this thread without exiting the
    // program.  However, we'll want to register a shutdown hook so that we can
    // close the logger.
    shutdownListener = new LDAPDebuggerShutdownListener(listener, logHandler);
    Runtime.getRuntime().addShutdownHook(shutdownListener);
    return ResultCode.SUCCESS;
}
Also used : KeyStoreKeyManager(com.unboundid.util.ssl.KeyStoreKeyManager) MinimalLogFormatter(com.unboundid.util.MinimalLogFormatter) LDAPListenerRequestHandler(com.unboundid.ldap.listener.LDAPListenerRequestHandler) LDAPListener(com.unboundid.ldap.listener.LDAPListener) ProxyRequestHandler(com.unboundid.ldap.listener.ProxyRequestHandler) LDAPDebuggerRequestHandler(com.unboundid.ldap.listener.LDAPDebuggerRequestHandler) ToCodeRequestHandler(com.unboundid.ldap.listener.ToCodeRequestHandler) ToCodeRequestHandler(com.unboundid.ldap.listener.ToCodeRequestHandler) FileHandler(java.util.logging.FileHandler) LDAPListenerRequestHandler(com.unboundid.ldap.listener.LDAPListenerRequestHandler) ProxyRequestHandler(com.unboundid.ldap.listener.ProxyRequestHandler) ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) LDAPDebuggerRequestHandler(com.unboundid.ldap.listener.LDAPDebuggerRequestHandler) IOException(java.io.IOException) TrustAllTrustManager(com.unboundid.util.ssl.TrustAllTrustManager) ConsoleHandler(java.util.logging.ConsoleHandler) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) FileHandler(java.util.logging.FileHandler) SSLUtil(com.unboundid.util.ssl.SSLUtil) LDAPListenerConfig(com.unboundid.ldap.listener.LDAPListenerConfig) LDAPException(com.unboundid.ldap.sdk.LDAPException) File(java.io.File) NotNull(com.unboundid.util.NotNull)

Example 2 with LDAPListenerConfig

use of com.unboundid.ldap.listener.LDAPListenerConfig in project ldapsdk by pingidentity.

the class LDAPDebuggerTestCase method setUp.

/**
 * Create the listener that will be used for this class.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@BeforeClass()
public void setUp() throws Exception {
    final File debuggerLogFile = createTempFile();
    debuggerLogFile.delete();
    final File codeLogFile = createTempFile();
    codeLogFile.delete();
    final File listenerLogFile = createTempFile();
    listenerLogFile.delete();
    final AccessLogRequestHandler requestHandler = new AccessLogRequestHandler(new FileHandler(listenerLogFile.getAbsolutePath()), new TestRequestHandler());
    final LDAPListenerConfig config = new LDAPListenerConfig(0, requestHandler);
    listener = new LDAPListener(config);
    listener.startListening();
    final int listenerPort = listener.getListenPort();
    debugger = new LDAPDebugger(null, null);
    assertEquals(debugger.runTool("--hostname", "localhost", "--port", String.valueOf(listenerPort), "--outputFile", debuggerLogFile.getAbsolutePath(), "--codeLogFile", codeLogFile.getAbsolutePath()), ResultCode.SUCCESS);
    conn = new LDAPConnection("localhost", debugger.getListener().getListenPort());
    TestRequestHandler.setControls(new Control("4.3.2.1", true, new ASN1OctetString("x")), new Control("4.3.2.2", false, new ASN1OctetString("y")));
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) LDAPListenerConfig(com.unboundid.ldap.listener.LDAPListenerConfig) AuthorizationIdentityRequestControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl) Control(com.unboundid.ldap.sdk.Control) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) LDAPListener(com.unboundid.ldap.listener.LDAPListener) TestRequestHandler(com.unboundid.ldap.listener.TestRequestHandler) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) File(java.io.File) AccessLogRequestHandler(com.unboundid.ldap.listener.AccessLogRequestHandler) FileHandler(java.util.logging.FileHandler) BeforeClass(org.testng.annotations.BeforeClass)

Example 3 with LDAPListenerConfig

use of com.unboundid.ldap.listener.LDAPListenerConfig in project ldapsdk by pingidentity.

the class TestLDAPSDKPerformance method doToolProcessing.

/**
 * Performs the core set of processing for this tool.
 *
 * @return  A result code that indicates whether the processing completed
 *          successfully.
 */
@Override()
@NotNull()
public ResultCode doToolProcessing() {
    // Create the socket factory to use for accepting connections.  If the
    // --useSSL argument was provided, then create a temporary keystore and
    // generate a certificate in it.
    final ServerSocketFactory serverSocketFactory;
    if (useSSLArg.isPresent()) {
        try {
            final File keyStoreFile = File.createTempFile("test-ldap-sdk-performance-keystore-", ".jks");
            keyStoreFile.deleteOnExit();
            keyStoreFile.delete();
            final ByteArrayOutputStream out = new ByteArrayOutputStream();
            final ResultCode manageCertificatesResultCode = ManageCertificates.main(null, out, out, "generate-self-signed-certificate", "--keystore", keyStoreFile.getAbsolutePath(), "--keystore-password", keyStoreFile.getAbsolutePath(), "--keystore-type", CryptoHelper.KEY_STORE_TYPE_JKS, "--alias", "server-cert", "--subject-dn", "CN=Test LDAP SDK Performance");
            if (manageCertificatesResultCode != ResultCode.SUCCESS) {
                final String message = "ERROR:  Unable to use the " + "manage-certificates tool to generate a self-signed server " + "certificate to use for SSL communication.";
                completionMessage.compareAndSet(null, message);
                wrapErr(0, WRAP_COLUMN, message);
                err();
                wrapErr(0, WRAP_COLUMN, "The manage-certificates output was:");
                err();
                err(StaticUtils.toUTF8String(out.toByteArray()));
                return manageCertificatesResultCode;
            }
            final SSLUtil sslUtil = new SSLUtil(new KeyStoreKeyManager(keyStoreFile, keyStoreFile.getAbsolutePath().toCharArray(), CryptoHelper.KEY_STORE_TYPE_JKS, "server-cert"), new TrustAllTrustManager());
            serverSocketFactory = sslUtil.createSSLServerSocketFactory();
        } catch (final Exception e) {
            Debug.debugException(e);
            final String message = "ERROR:  Unable to initialize support for SSL " + "communication:  " + StaticUtils.getExceptionMessage(e);
            completionMessage.compareAndSet(null, message);
            wrapErr(0, WRAP_COLUMN, message);
            return ResultCode.LOCAL_ERROR;
        }
    } else {
        serverSocketFactory = ServerSocketFactory.getDefault();
    }
    // Create the search result entries to return in response to each search.
    final int numEntries = entriesPerSearchArg.getValue();
    final List<Entry> entries = new ArrayList<>(numEntries);
    for (int i = 1; i <= numEntries; i++) {
        entries.add(new Entry("uid=user." + i + ",ou=People,dc=example,dc=com", new Attribute("objectClass", "top", "person", "organizationalPerson", "inetOrgPerson"), new Attribute("uid", "user." + i), new Attribute("givenName", "User"), new Attribute("sn", String.valueOf(i)), new Attribute("cn", "User " + i), new Attribute("mail", "user." + i + "@example.com"), new Attribute("userPassword", "password")));
    }
    // Create a canned response request handler to use to return the responses.
    final CannedResponseRequestHandler cannedResponseRequestHandler = new CannedResponseRequestHandler(ResultCode.valueOf(resultCodeArg.getValue()), // Matched DN
    null, diagnosticMessageArg.getValue(), // Referral URLs
    Collections.<String>emptyList(), entries, Collections.<SearchResultReference>emptyList());
    // Create the LDAP listener to handle the requests.
    final LDAPListenerConfig listenerConfig = new LDAPListenerConfig(0, cannedResponseRequestHandler);
    listenerConfig.setServerSocketFactory(serverSocketFactory);
    final LDAPListener ldapListener = new LDAPListener(listenerConfig);
    try {
        ldapListener.startListening();
    } catch (final Exception e) {
        Debug.debugException(e);
        final String message = "ERROR:  Unable to start listening for client " + "connections:  " + StaticUtils.getExceptionMessage(e);
        completionMessage.compareAndSet(null, message);
        wrapErr(0, WRAP_COLUMN, message);
        return ResultCode.LOCAL_ERROR;
    }
    try {
        final int listenPort = ldapListener.getListenPort();
        final String toolName = StaticUtils.toLowerCase(toolArg.getValue());
        switch(toolName) {
            case TOOL_NAME_SEARCHRATE:
                return invokeSearchRate(listenPort);
            case TOOL_NAME_MODRATE:
                return invokeModRate(listenPort);
            case TOOL_NAME_AUTHRATE:
                return invokeAuthRate(listenPort);
            case TOOL_NAME_SEARCH_AND_MOD_RATE:
                return invokeSearchAndModRate(listenPort);
            default:
                // This should never happen.
                final String message = "ERROR:  Unrecognized tool name:  " + toolName;
                completionMessage.compareAndSet(null, message);
                wrapErr(0, WRAP_COLUMN, message);
                return ResultCode.PARAM_ERROR;
        }
    } finally {
        ldapListener.shutDown(true);
    }
}
Also used : KeyStoreKeyManager(com.unboundid.util.ssl.KeyStoreKeyManager) Attribute(com.unboundid.ldap.sdk.Attribute) ServerSocketFactory(javax.net.ServerSocketFactory) CannedResponseRequestHandler(com.unboundid.ldap.listener.CannedResponseRequestHandler) LDAPListener(com.unboundid.ldap.listener.LDAPListener) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TrustAllTrustManager(com.unboundid.util.ssl.TrustAllTrustManager) ArgumentException(com.unboundid.util.args.ArgumentException) SSLUtil(com.unboundid.util.ssl.SSLUtil) Entry(com.unboundid.ldap.sdk.Entry) LDAPListenerConfig(com.unboundid.ldap.listener.LDAPListenerConfig) File(java.io.File) ResultCode(com.unboundid.ldap.sdk.ResultCode) NotNull(com.unboundid.util.NotNull)

Aggregations

LDAPListener (com.unboundid.ldap.listener.LDAPListener)3 LDAPListenerConfig (com.unboundid.ldap.listener.LDAPListenerConfig)3 File (java.io.File)3 NotNull (com.unboundid.util.NotNull)2 ArgumentException (com.unboundid.util.args.ArgumentException)2 KeyStoreKeyManager (com.unboundid.util.ssl.KeyStoreKeyManager)2 SSLUtil (com.unboundid.util.ssl.SSLUtil)2 TrustAllTrustManager (com.unboundid.util.ssl.TrustAllTrustManager)2 FileHandler (java.util.logging.FileHandler)2 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1 AccessLogRequestHandler (com.unboundid.ldap.listener.AccessLogRequestHandler)1 CannedResponseRequestHandler (com.unboundid.ldap.listener.CannedResponseRequestHandler)1 LDAPDebuggerRequestHandler (com.unboundid.ldap.listener.LDAPDebuggerRequestHandler)1 LDAPListenerRequestHandler (com.unboundid.ldap.listener.LDAPListenerRequestHandler)1 ProxyRequestHandler (com.unboundid.ldap.listener.ProxyRequestHandler)1 TestRequestHandler (com.unboundid.ldap.listener.TestRequestHandler)1 ToCodeRequestHandler (com.unboundid.ldap.listener.ToCodeRequestHandler)1 Attribute (com.unboundid.ldap.sdk.Attribute)1 Control (com.unboundid.ldap.sdk.Control)1 Entry (com.unboundid.ldap.sdk.Entry)1