Search in sources :

Example 1 with PasswordFileReader

use of com.unboundid.util.PasswordFileReader in project ldapsdk by pingidentity.

the class InternalSDKHelper method selectDefaultNonInteractiveTrustManagers.

/**
 * Selects an appropriate set of default trust managers that may be used for
 * non-interactively determining whether to trust a presented certificate
 * chain.
 *
 * @param  trustManagers  The list to which the selected trust managers will
 *                        be added.  It must not be {@code null}, and it must
 *                        be updatable.
 */
private static void selectDefaultNonInteractiveTrustManagers(@NotNull final List<X509TrustManager> trustManagers) {
    trustManagers.add(JVMDefaultTrustManager.getInstance());
    final File pingIdentityServerRoot = InternalSDKHelper.getPingIdentityServerRoot();
    if (pingIdentityServerRoot != null) {
        // Check to see if a trust store file exists.  If a config/truststore file
        // exists, then we'll use that.  Otherwise, if a config/truststore.pin
        // file exists and either config/truststore.p12 or config/truststore.bcfks
        // exists, then we'll use one of those.
        final File defaultJKSServerTrustStore = StaticUtils.constructPath(pingIdentityServerRoot, "config", "truststore");
        if (defaultJKSServerTrustStore.exists()) {
            trustManagers.add(new TrustStoreTrustManager(defaultJKSServerTrustStore, null, CryptoHelper.KEY_STORE_TYPE_JKS, true));
        } else {
            final File trustStorePINFile = StaticUtils.constructPath(pingIdentityServerRoot, "config", "truststore.pin");
            final File defaultPKCS12TrustStore = StaticUtils.constructPath(pingIdentityServerRoot, "config", "truststore.p12");
            final File defaultBCFKSTrustStore = StaticUtils.constructPath(pingIdentityServerRoot, "config", "truststore.bcfks");
            if (trustStorePINFile.exists() && (defaultPKCS12TrustStore.exists() || defaultBCFKSTrustStore.exists())) {
                try {
                    final char[] trustStorePIN = new PasswordFileReader(false).readPassword(trustStorePINFile);
                    if (defaultPKCS12TrustStore.exists()) {
                        trustManagers.add(new TrustStoreTrustManager(defaultPKCS12TrustStore, trustStorePIN, CryptoHelper.KEY_STORE_TYPE_PKCS_12, true));
                    } else if (defaultBCFKSTrustStore.exists()) {
                        trustManagers.add(new TrustStoreTrustManager(defaultPKCS12TrustStore, trustStorePIN, CryptoHelper.KEY_STORE_TYPE_BCFKS, true));
                    }
                } catch (final Exception e) {
                    Debug.debugException(e);
                }
            }
        }
        final File serverConfigFile = StaticUtils.constructPath(pingIdentityServerRoot, "config", "config.ldif");
        if (serverConfigFile.exists()) {
            trustManagers.add(new TopologyRegistryTrustManager(serverConfigFile, TimeUnit.MINUTES.toMillis(5L)));
        }
    }
}
Also used : PasswordFileReader(com.unboundid.util.PasswordFileReader) TopologyRegistryTrustManager(com.unboundid.ldap.sdk.unboundidds.TopologyRegistryTrustManager) TrustStoreTrustManager(com.unboundid.util.ssl.TrustStoreTrustManager) File(java.io.File)

Example 2 with PasswordFileReader

use of com.unboundid.util.PasswordFileReader in project ldapsdk by pingidentity.

the class LDIFModifyTestCase method createTempFile.

/**
 * Writes the provided lines to an optionally compressed and/or encrypted
 * output file.
 *
 * @param  compress   Indicates whether to compress the file.
 * @param  encPWFile  A file containing the passphrase to use to encrypt the
 *                    contents of the file.  It may be {@code null} if the
 *                    file should not be encrypted.
 * @param  lines      The lines to be written.
 *
 * @return  The file to which the lines were written.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
private static File createTempFile(final boolean compress, final File encPWFile, final String... lines) throws Exception {
    File f = File.createTempFile("ldapsdk-", ".tmp");
    f.deleteOnExit();
    OutputStream outputStream = new FileOutputStream(f);
    try {
        if (encPWFile != null) {
            final char[] pwChars = new PasswordFileReader().readPassword(encPWFile);
            outputStream = new PassphraseEncryptedOutputStream(pwChars, outputStream);
        }
        if (compress) {
            outputStream = new GZIPOutputStream(outputStream);
        }
        try (PrintWriter printStream = new PrintWriter(outputStream)) {
            for (final String line : lines) {
                printStream.println(line);
            }
        }
    } finally {
        outputStream.close();
    }
    return f;
}
Also used : PasswordFileReader(com.unboundid.util.PasswordFileReader) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) PrintWriter(java.io.PrintWriter)

Example 3 with PasswordFileReader

use of com.unboundid.util.PasswordFileReader in project ldapsdk by pingidentity.

the class LDIFSearchTestCase method createTempFile.

/**
 * Writes the provided lines to an optionally compressed and/or encrypted
 * output file.
 *
 * @param  compress   Indicates whether to compress the file.
 * @param  encPWFile  A file containing the passphrase to use to encrypt the
 *                    contents of the file.  It may be {@code null} if the
 *                    file should not be encrypted.
 * @param  lines      The lines to be written.
 *
 * @return  The file to which the lines were written.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
private static File createTempFile(final boolean compress, final File encPWFile, final String... lines) throws Exception {
    File f = File.createTempFile("ldapsdk-", ".tmp");
    f.deleteOnExit();
    OutputStream outputStream = new FileOutputStream(f);
    try {
        if (encPWFile != null) {
            final char[] pwChars = new PasswordFileReader().readPassword(encPWFile);
            outputStream = new PassphraseEncryptedOutputStream(pwChars, outputStream);
        }
        if (compress) {
            outputStream = new GZIPOutputStream(outputStream);
        }
        try (PrintWriter printStream = new PrintWriter(outputStream)) {
            for (final String line : lines) {
                printStream.println(line);
            }
        }
    } finally {
        outputStream.close();
    }
    return f;
}
Also used : PasswordFileReader(com.unboundid.util.PasswordFileReader) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) FileOutputStream(java.io.FileOutputStream) JSONString(com.unboundid.util.json.JSONString) File(java.io.File) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) PrintWriter(java.io.PrintWriter)

Example 4 with PasswordFileReader

use of com.unboundid.util.PasswordFileReader in project ldapsdk by pingidentity.

the class LDIFSearchTestCase method readEntries.

/**
 * Reads the LDIF entries from the specified file.
 *
 * @param  ldifFile   The file from which to read the entries.  It may
 *                    optionally be compressed, and it may be encrypted if a
 *                    password file is provided.
 * @param  encPWFile  A file containing the encryption passphrase needed to
 *                    read the file.  It may be {@code null} if the file is
 *                    not encrypted.
 *
 * @return  The list of LDIF change records that were read.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
private static List<Entry> readEntries(final File ldifFile, final File encPWFile) throws Exception {
    InputStream inputStream = new FileInputStream(ldifFile);
    if (encPWFile != null) {
        final char[] pwChars = new PasswordFileReader().readPassword(encPWFile);
        inputStream = ToolUtils.getPossiblyPassphraseEncryptedInputStream(inputStream, Collections.singleton(pwChars), false, "Enter the passphrase:", "confirm the passphrase:", System.out, System.err).getFirst();
    }
    inputStream = ToolUtils.getPossiblyGZIPCompressedInputStream(inputStream);
    final List<Entry> entries = new ArrayList<>();
    try (LDIFReader ldifReader = new LDIFReader(inputStream)) {
        while (true) {
            final Entry entry = ldifReader.readEntry();
            if (entry == null) {
                return entries;
            }
            entries.add(entry);
        }
    }
}
Also used : PasswordFileReader(com.unboundid.util.PasswordFileReader) Entry(com.unboundid.ldap.sdk.Entry) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream)

Example 5 with PasswordFileReader

use of com.unboundid.util.PasswordFileReader in project ldapsdk by pingidentity.

the class LDIFDiffTestCase method createTempFile.

/**
 * Writes the provided lines to an optionally compressed and/or encrypted
 * output file.
 *
 * @param  compress   Indicates whether to compress the file.
 * @param  encPWFile  A file containing the passphrase to use to encrypt the
 *                    contents of the file.  It may be {@code null} if the
 *                    file should not be encrypted.
 * @param  lines      The lines to be written.
 *
 * @return  The file to which the lines were written.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
private static File createTempFile(final boolean compress, final File encPWFile, final String... lines) throws Exception {
    final File f = File.createTempFile("ldapsdk-", ".tmp");
    f.deleteOnExit();
    OutputStream outputStream = new FileOutputStream(f);
    try {
        if (encPWFile != null) {
            final char[] pwChars = new PasswordFileReader().readPassword(encPWFile);
            outputStream = new PassphraseEncryptedOutputStream(pwChars, outputStream);
        }
        if (compress) {
            outputStream = new GZIPOutputStream(outputStream);
        }
        try (PrintWriter printStream = new PrintWriter(outputStream)) {
            for (final String line : lines) {
                printStream.println(line);
            }
        }
    } finally {
        outputStream.close();
    }
    return f;
}
Also used : PasswordFileReader(com.unboundid.util.PasswordFileReader) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) PrintWriter(java.io.PrintWriter)

Aggregations

PasswordFileReader (com.unboundid.util.PasswordFileReader)6 File (java.io.File)4 PassphraseEncryptedOutputStream (com.unboundid.util.PassphraseEncryptedOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 PrintWriter (java.io.PrintWriter)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Entry (com.unboundid.ldap.sdk.Entry)1 TopologyRegistryTrustManager (com.unboundid.ldap.sdk.unboundidds.TopologyRegistryTrustManager)1 JSONString (com.unboundid.util.json.JSONString)1 TrustStoreTrustManager (com.unboundid.util.ssl.TrustStoreTrustManager)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1