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

Violette Roche-Montane
05.09.2014 dbac9a30c43cb6542d95f656a866af94f922d70e
Checkpoint OPENDJ-1343 Migrate dsconfig
Minor code cleanup and reorganized imports.
3 files modified
260 ■■■■■ changed files
opendj-sdk/opendj3-server-dev/src/server/org/opends/server/admin/client/cli/SecureConnectionCliArgs.java 91 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj3-server-dev/src/server/org/opends/server/admin/client/cli/SecureConnectionCliParser.java 27 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj3-server-dev/src/server/org/opends/server/util/cli/LDAPConnectionConsoleInteraction.java 142 ●●●● patch | view | raw | blame | history
opendj-sdk/opendj3-server-dev/src/server/org/opends/server/admin/client/cli/SecureConnectionCliArgs.java
@@ -26,19 +26,16 @@
 */
package org.opends.server.admin.client.cli;
import static com.forgerock.opendj.cli.ArgumentConstants.OPTION_LONG_ADMIN_UID;
import static com.forgerock.opendj.cli.CliMessages.INFO_DESCRIPTION_ADMIN_PORT;
import static com.forgerock.opendj.cli.ReturnCode.CONFLICTING_ARGS;
import static com.forgerock.opendj.cli.ReturnCode.SUCCESS;
import static com.forgerock.opendj.cli.Utils.LINE_SEPARATOR;
import static org.opends.server.util.ServerConstants.MAX_LINE_WIDTH;
import static org.opends.server.util.StaticUtils.close;
import static org.opends.server.util.StaticUtils.wrapText;
import static org.opends.messages.AdminToolMessages.*;
import static org.opends.messages.ToolMessages.*;
import static com.forgerock.opendj.cli.ReturnCode.SUCCESS;
import static com.forgerock.opendj.cli.ReturnCode.CONFLICTING_ARGS;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizableMessageBuilder;
import static org.opends.server.util.ServerConstants.MAX_LINE_WIDTH;
import static org.opends.server.util.StaticUtils.*;
import static com.forgerock.opendj.cli.ArgumentConstants.*;
import java.io.File;
import java.io.FileInputStream;
@@ -53,10 +50,11 @@
import java.util.ArrayList;
import java.util.LinkedHashSet;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import javax.net.ssl.KeyManager;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizableMessageBuilder;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.opends.admin.ads.util.ApplicationKeyManager;
import org.opends.admin.ads.util.ApplicationTrustManager;
import org.opends.admin.ads.util.ConnectionUtils;
@@ -237,10 +235,7 @@
    {
      return adminUidArg.getValue();
    }
    else
    {
      return adminUidArg.getDefaultValue();
    }
    return adminUidArg.getDefaultValue();
  }
  /**
@@ -266,10 +261,7 @@
    {
      return bindDnArg.getValue();
    }
    else
    {
      return bindDnArg.getDefaultValue();
    }
    return bindDnArg.getDefaultValue();
  }
  /**
@@ -388,21 +380,15 @@
  public String getBindPassword(StringArgument clearArg,
      FileBasedArgument fileArg)
  {
    String pwd;
    if (clearArg.isPresent())
    {
      pwd = clearArg.getValue();
      return clearArg.getValue();
    }
    else
      if (fileArg.isPresent())
      {
        pwd = fileArg.getValue();
      }
      else
      {
        pwd = null;
      }
    return pwd;
    else if (fileArg.isPresent())
    {
      return fileArg.getValue();
    }
    return null;
  }
  /**
@@ -813,36 +799,29 @@
      catch (KeyStoreException e)
      {
        // Nothing to do: if this occurs we will systematically refuse
        // the
        // certificates. Maybe we should avoid this and be strict, but
        // the certificates. Maybe we should avoid this and be strict, but
        // we are in a best effort mode.
        logger.warn(LocalizableMessage.raw("Error with the keystore"), e);
      }
      catch (NoSuchAlgorithmException e)
      {
        // Nothing to do: if this occurs we will systematically refuse
        // the
        // certificates. Maybe we should avoid this and be strict, but
        // we are
        // in a best effort mode.
        // the certificates. Maybe we should avoid this and be strict, but
        // we are in a best effort mode.
        logger.warn(LocalizableMessage.raw("Error with the keystore"), e);
      }
      catch (CertificateException e)
      {
        // Nothing to do: if this occurs we will systematically refuse
        // the
        // certificates. Maybe we should avoid this and be strict, but
        // we are
        // in a best effort mode.
        // the certificates. Maybe we should avoid this and be strict, but
        // we are in a best effort mode.
        logger.warn(LocalizableMessage.raw("Error with the keystore"), e);
      }
      catch (IOException e)
      {
        // Nothing to do: if this occurs we will systematically refuse
        // the
        // certificates. Maybe we should avoid this and be strict, but
        // we are
        // in a best effort mode.
        // the certificates. Maybe we should avoid this and be strict, but
        // we are in a best effort mode.
        logger.warn(LocalizableMessage.raw("Error with the keystore"), e);
      }
      finally
@@ -861,15 +840,10 @@
        return new SelectableCertificateKeyManager(akm, certNicknameArg
            .getValue());
      }
      else
      {
        return akm;
      }
      return akm;
    }
    else
    {
      return null;
    }
    return null;
  }
  /**
@@ -881,17 +855,12 @@
   */
  private boolean canRead(String path)
  {
    boolean canRead;
    File file = new File(path);
    final File file = new File(path);
    if (file.exists())
    {
      canRead = file.canRead();
      return file.canRead();
    }
    else
    {
      canRead = false;
    }
    return canRead;
    return false;
  }
  /**
opendj-sdk/opendj3-server-dev/src/server/org/opends/server/admin/client/cli/SecureConnectionCliParser.java
@@ -27,6 +27,11 @@
package org.opends.server.admin.client.cli;
import static org.opends.server.util.ServerConstants.EOL;
import static org.opends.server.util.ServerConstants.MAX_LINE_WIDTH;
import static org.opends.server.util.StaticUtils.wrapText;
import static org.opends.messages.ToolMessages.*;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
@@ -41,18 +46,20 @@
import org.opends.admin.ads.util.ApplicationTrustManager;
import org.opends.server.util.PasswordReader;
import com.forgerock.opendj.cli.*;
import static org.opends.messages.ToolMessages.*;
import static org.opends.server.util.ServerConstants.*;
import static org.opends.server.util.StaticUtils.*;
import com.forgerock.opendj.cli.Argument;
import com.forgerock.opendj.cli.ArgumentException;
import com.forgerock.opendj.cli.ArgumentGroup;
import com.forgerock.opendj.cli.BooleanArgument;
import com.forgerock.opendj.cli.CommonArguments;
import com.forgerock.opendj.cli.FileBasedArgument;
import com.forgerock.opendj.cli.StringArgument;
import com.forgerock.opendj.cli.SubCommandArgumentParser;
/**
 * This is a commodity class that can be used to check the arguments required
 * to establish a secure connection in the command line.  It can be used
 * to generate an ApplicationTrustManager object based on the options provided
 * by the user in the command line.
 *
 * This is a commodity class that can be used to check the arguments required to
 * establish a secure connection in the command line. It can be used to generate
 * an ApplicationTrustManager object based on the options provided by the user
 * in the command line.
 */
public abstract class SecureConnectionCliParser extends SubCommandArgumentParser
{
opendj-sdk/opendj3-server-dev/src/server/org/opends/server/util/cli/LDAPConnectionConsoleInteraction.java
@@ -27,19 +27,40 @@
package org.opends.server.util.cli;
import org.forgerock.i18n.LocalizableMessage;
import static org.opends.messages.UtilityMessages.*;
import static org.opends.messages.QuickSetupMessages.*;
import static org.opends.messages.ToolMessages.*;
import static org.opends.messages.UtilityMessages.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import javax.net.ssl.KeyManager;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.opends.admin.ads.ADSContext;
import org.opends.admin.ads.util.ApplicationKeyManager;
import org.opends.admin.ads.util.ApplicationTrustManager;
import org.opends.quicksetup.Step;
import org.opends.quicksetup.UserDataCertificateException;
import org.opends.quicksetup.util.Utils;
import org.opends.server.tools.dsconfig.ArgumentExceptionFactory;
import org.opends.server.tools.LDAPConnectionOptions;
import org.opends.server.tools.SSLConnectionFactory;
import org.opends.server.tools.SSLConnectionException;
import org.opends.server.admin.client.cli.SecureConnectionCliArgs;
import org.opends.server.tools.LDAPConnectionOptions;
import org.opends.server.tools.SSLConnectionException;
import org.opends.server.tools.SSLConnectionFactory;
import org.opends.server.tools.dsconfig.ArgumentExceptionFactory;
import org.opends.server.util.SelectableCertificateKeyManager;
import com.forgerock.opendj.cli.ArgumentException;
import com.forgerock.opendj.cli.ClientException;
import com.forgerock.opendj.cli.CommandBuilder;
@@ -49,27 +70,6 @@
import com.forgerock.opendj.cli.MenuResult;
import com.forgerock.opendj.cli.ValidationCallback;
import org.opends.server.util.SelectableCertificateKeyManager;
import org.opends.admin.ads.ADSContext;
import org.opends.admin.ads.util.ApplicationTrustManager;
import org.opends.admin.ads.util.ApplicationKeyManager;
import javax.net.ssl.KeyManager;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import org.forgerock.i18n.slf4j.LocalizedLogger;
/**
 * Supports interacting with a user through the command line to
 * prompt for information necessary to create an LDAP connection.
@@ -87,31 +87,35 @@
  private char[] bindPassword;
  private KeyManager keyManager;
  private ApplicationTrustManager trustManager;
  // Boolean that tells if we ask for bind DN or admin UID in the same prompt.
  /** Boolean that tells if we ask for bind DN or admin UID in the same prompt. */
  private boolean useAdminOrBindDn = false;
  // Boolean that tells if we must propose LDAP if it is available even if the
  // user provided certificate parameters.
  /**
   * Boolean that tells if we must propose LDAP if it is available even if the
   * user provided certificate parameters.
   */
  private boolean displayLdapIfSecureParameters = false;
  // The SecureConnectionCliArgsList object.
  /** The SecureConnectionCliArgsList object. */
  private SecureConnectionCliArgs secureArgsList = null;
  // Indicate if we need to display the heading
  /** Indicate if we need to display the heading. */
  private boolean isHeadingDisplayed = false;
  // the Console application
  /** the Console application. */
  private ConsoleApplication app;
  // Indicate if the trust store in in memory
  /** Indicate if the trust store in in memory. */
  private boolean trustStoreInMemory = false;
  // Indicate if the all certificates are accepted
  /** Indicate if the all certificates are accepted. */
  private boolean trustAll = false;
  // Indicate that the trust manager was created with the parameters provided
  /** Indicate that the trust manager was created with the parameters provided. */
  private boolean trustManagerInitialized;
  // The trust store to use for the SSL or STARTTLS connection
  /** The trust store to use for the SSL or STARTTLS connection. */
  private KeyStore truststore;
  private String keystorePath;
@@ -124,15 +128,15 @@
  private char[] truststorePassword;
  // The timeout to be used to connect
  /** The timeout to be used to connect. */
  private int connectTimeout;
  private LocalizableMessage heading = INFO_LDAP_CONN_HEADING_CONNECTION_PARAMETERS.get();
  // A copy of the secureArgList for convenience.
  /** A copy of the secureArgList for convenience. */
  private SecureConnectionCliArgs copySecureArgsList = null;
  // The command builder that we can return with the connection information.
  /** The command builder that we can return with the connection information. */
  private CommandBuilder commandBuilder;
@@ -209,7 +213,7 @@
     */
    private TrustMethod(int i, LocalizableMessage msg)
    {
      choice = new Integer(i);
      choice = Integer.valueOf(i);
      this.msg = msg;
    }
@@ -259,7 +263,7 @@
     */
    private TrustOption(int i, LocalizableMessage msg)
    {
      choice = new Integer(i);
      choice = Integer.valueOf(i);
      this.msg = msg;
    }
@@ -418,19 +422,11 @@
    useSSL = secureArgsList.useSSL();
    useStartTLS = secureArgsList.useStartTLS();
    boolean connectionTypeIsSet =
      (
        secureArgsList.alwaysSSL()
        ||
        secureArgsList.useSSLArg.isPresent()
        ||
        secureArgsList.useStartTLSArg.isPresent()
        ||
        (
          secureArgsList.useSSLArg.isValueSetByProperty()
          &&
          secureArgsList.useStartTLSArg.isValueSetByProperty()
        )
      );
      secureArgsList.alwaysSSL()
        || secureArgsList.useSSLArg.isPresent()
        || secureArgsList.useStartTLSArg.isPresent()
        || (secureArgsList.useSSLArg.isValueSetByProperty()
            && secureArgsList.useStartTLSArg.isValueSetByProperty());
    if (app.isInteractive() && !connectionTypeIsSet)
    {
      checkHeadingDisplayed();
@@ -587,7 +583,7 @@
    commandBuilder.addArgument(copySecureArgsList.portArg);
    // Handle certificate
    if ((useSSL || useStartTLS) && (trustManager == null))
    if ((useSSL || useStartTLS) && trustManager == null)
    {
      initializeTrustManager();
    }
@@ -613,8 +609,7 @@
    {
      providedBindDN = null;
    }
    boolean argIsPresent = (providedAdminUID != null) ||
    (providedBindDN != null);
    boolean argIsPresent = providedAdminUID != null || providedBindDN != null;
    final String tmpBindDN = bindDN;
    final String tmpAdminUID = adminUID;
    if (keyManager == null)
@@ -654,7 +649,7 @@
          app.println();
          if (useAdminOrBindDn)
          {
            String def = (adminUID != null) ? adminUID : bindDN;
            String def = (adminUID != null ? adminUID : bindDN);
            String v = app.readValidatedInput(
                INFO_LDAP_CONN_GLOBAL_ADMINISTRATOR_OR_BINDDN_PROMPT.get(def),
                callback);
@@ -763,7 +758,7 @@
        commandBuilder.addArgument(copySecureArgsList.bindPasswordFileArg);
        addedPasswordFileArgument = true;
      }
      else if (bindPassword == null || bindPassword.equals("-"))
      else if (bindPassword == null || "-".equals(bindPassword))
      {
        // Read the password from the stdin.
        if (!app.isInteractive())
@@ -852,11 +847,9 @@
    // Try to use the local instance trust store, to avoid certificate
    // validation when both the CLI and the server are in the same instance.
    if (weDontKnowTheTrustMethod) {
      if (addLocalTrustStore()) {
        weDontKnowTheTrustMethod = false;
      }
    if (weDontKnowTheTrustMethod && addLocalTrustStore())
    {
      weDontKnowTheTrustMethod = false;
    }
    if (app.isInteractive() && weDontKnowTheTrustMethod)
@@ -876,7 +869,7 @@
        {
          builder.setDefault(
              INFO_LDAP_CONN_PROMPT_SECURITY_PROTOCOL_DEFAULT_CHOICE
                  .get(new Integer(i)), MenuResult.success(t.getChoice()));
                  .get(Integer.valueOf(i)), MenuResult.success(t.getChoice()));
        }
      }
@@ -1003,7 +996,7 @@
      truststorePassword = secureArgsList.trustStorePasswordFileArg
          .getValue().toCharArray();
    }
    if ((truststorePassword !=  null) && (truststorePassword.equals("-")))
    if (truststorePassword !=  null && "-".equals(truststorePassword))
    {
      // Read the password from the stdin.
      if (!app.isInteractive())
@@ -1050,8 +1043,8 @@
        truststore.load(null, null);
      }
      if (secureArgsList.trustStorePasswordFileArg.isPresent() &&
          (truststorePath != null))
      if (secureArgsList.trustStorePasswordFileArg.isPresent()
          && truststorePath != null)
      {
        copySecureArgsList.trustStorePasswordFileArg.clearValues();
        copySecureArgsList.trustStorePasswordFileArg.getNameToValueMap().putAll(
@@ -1059,7 +1052,7 @@
        commandBuilder.addArgument(
            copySecureArgsList.trustStorePasswordFileArg);
      }
      else if ((truststorePassword != null) && (truststorePath != null))
      else if (truststorePassword != null && truststorePath != null)
      {
        // Only add the trust store password if there is one AND if the user
        // specified a trust store path.
@@ -1185,7 +1178,7 @@
        throw ArgumentExceptionFactory.missingBindPassword(keystorePassword);
      }
    }
    else if (keystorePassword == null || keystorePassword.equals("-"))
    else if (keystorePassword == null || "-".equals(keystorePassword))
    {
      // Read the password from the stdin.
      if (!app.isInteractive())
@@ -1559,7 +1552,7 @@
      {
        builder.setDefault(
            INFO_LDAP_CONN_PROMPT_SECURITY_PROTOCOL_DEFAULT_CHOICE
                .get(new Integer(i)), MenuResult.success(t.getChoice()));
                .get(Integer.valueOf(i)), MenuResult.success(t.getChoice()));
      }
    }
@@ -1579,8 +1572,7 @@
            return false;
          }
          if ((result.getValue().equals(TrustOption.CERTIFICATE_DETAILS
              .getChoice())))
          if (result.getValue().equals(TrustOption.CERTIFICATE_DETAILS.getChoice()))
          {
            for (int i = 0; i < chain.length; i++)
            {
@@ -1610,7 +1602,7 @@
          {
            trustManager = new ApplicationTrustManager(truststore);
          }
          if ((authType != null) && (host != null))
          if (authType != null && host != null)
          {
            // Update the trust manager with the new certificate
            trustManager.acceptCertificate(chain, authType, host);