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)));
}
}
}
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;
}
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;
}
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);
}
}
}
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;
}
Aggregations