Skip to content

Commit

Permalink
Add File checkers
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Nov 25, 2024
1 parent 309d477 commit 42596bd
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/main/java/redis/clients/jedis/SslOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.KeyManager;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
Expand Down Expand Up @@ -169,8 +169,7 @@ public Builder keystore(File keystore) {
public Builder keystore(File keystore, char[] keystorePassword) {

Objects.requireNonNull(keystore, "Keystore must not be null");
// LettuceAssert.isTrue(keystore.exists(), () -> String.format("Keystore file %s does not exist", truststore));
// LettuceAssert.isTrue(keystore.isFile(), () -> String.format("Keystore %s is not a file", truststore));
assertFile("Keystore", keystore);

return keystore(Resource.from(keystore), keystorePassword);
}
Expand Down Expand Up @@ -214,10 +213,9 @@ public Builder keystore(URL keystore, char[] keystorePassword) {
*/
public Builder keystore(Resource resource, char[] keystorePassword) {

Objects.requireNonNull(resource, "Keystore InputStreamProvider must not be null");
this.keystoreResource = Objects.requireNonNull(resource, "Keystore InputStreamProvider must not be null");

this.keystorePassword = getPassword(keystorePassword);
this.keystoreResource = resource;

return this;
}
Expand Down Expand Up @@ -246,8 +244,7 @@ public Builder truststore(File truststore) {
public Builder truststore(File truststore, String truststorePassword) {

Objects.requireNonNull(truststore, "Truststore must not be null");
// LettuceAssert.isTrue(truststore.exists(), () -> String.format("Truststore file %s does not exist", truststore));
// LettuceAssert.isTrue(truststore.isFile(), () -> String.format("Truststore file %s is not a file", truststore));
assertFile("Truststore", truststore);

return truststore(Resource.from(truststore), getPassword(truststorePassword));
}
Expand Down Expand Up @@ -291,10 +288,9 @@ public Builder truststore(URL truststore, String truststorePassword) {
*/
private Builder truststore(Resource resource, char[] truststorePassword) {

Objects.requireNonNull(resource, "Truststore InputStreamProvider must not be null");
this.truststoreResource = Objects.requireNonNull(resource, "Truststore InputStreamProvider must not be null");

this.truststorePassword = getPassword(truststorePassword);
this.truststoreResource = resource;

return this;
}
Expand Down Expand Up @@ -361,7 +357,7 @@ public SSLContext createSslContext() throws IOException, GeneralSecurityExceptio
}

if (trustManagers != null) {
// skip
// already processed
} else if (truststoreResource != null) {

KeyStore trustStore = KeyStore.getInstance(trustStoreType);
Expand All @@ -388,18 +384,30 @@ public SSLParameters getSslParameters() {
return sslParameters;
}

private static boolean isEmpty(String str) {
return str == null || str.isEmpty();
}

private static char[] getPassword(String truststorePassword) {
return !isEmpty(truststorePassword) ? truststorePassword.toCharArray() : null;
private static char[] getPassword(String password) {
return password != null ? password.toCharArray() : null;
}

private static char[] getPassword(char[] chars) {
return chars != null ? Arrays.copyOf(chars, chars.length) : null;
}

/**
* Assert that {@code file} {@link File#exists() exists}.
*
* @param keyword file recognizer
* @param file
* @throws IllegalArgumentException if the file doesn't exist
*/
public static void assertFile(String keyword, File file) {
if (!file.exists()) {
throw new IllegalArgumentException(String.format("%s file %s does not exist", keyword, file));
}
if (!file.isFile()) {
throw new IllegalArgumentException(String.format("%s file %s is not a file", keyword, file));
}
}

/**
* Supplier for a {@link InputStream} representing a resource. The resulting {@link InputStream} must be closed by
* the calling code.
Expand Down

0 comments on commit 42596bd

Please sign in to comment.