Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure SSL truststore is present at startup #8631

Merged
merged 4 commits into from
Apr 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/gui/JabRefMain.java
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
import org.jabref.logic.net.ProxyPreferences;
import org.jabref.logic.net.ProxyRegisterer;
import org.jabref.logic.net.ssl.SSLPreferences;
import org.jabref.logic.net.ssl.TrustStoreManager;
import org.jabref.logic.protectedterms.ProtectedTermsLoader;
import org.jabref.logic.remote.RemotePreferences;
import org.jabref.logic.remote.client.RemoteClient;
@@ -151,6 +152,7 @@ private static void configureProxy(ProxyPreferences proxyPreferences) {
}

private static void configureSSL(SSLPreferences sslPreferences) {
TrustStoreManager.createTruststoreFileIfNotExist(Path.of(sslPreferences.getTruststorePath()));
System.setProperty("javax.net.ssl.trustStore", sslPreferences.getTruststorePath());
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
}
35 changes: 22 additions & 13 deletions src/main/java/org/jabref/logic/net/ssl/TrustStoreManager.java
Original file line number Diff line number Diff line change
@@ -31,19 +31,7 @@ public class TrustStoreManager {

public TrustStoreManager(Path storePath) {
this.storePath = storePath;
try {
LOGGER.info("Trust store path: {}", storePath.toAbsolutePath());
Path storeResourcePath = Path.of(TrustStoreManager.class.getResource("/ssl/truststore.jks").toURI());
Files.createDirectories(storePath.getParent());
if (Files.notExists(storePath)) {
Files.copy(storeResourcePath, storePath);
}
} catch (IOException e) {
LOGGER.warn("Bad truststore path", e);
} catch (URISyntaxException e) {
LOGGER.warn("Bad resource path", e);
}

createTruststoreFileIfNotExist(storePath);
try {
store = KeyStore.getInstance(KeyStore.getDefaultType());
store.load(new FileInputStream(storePath.toFile()), STORE_PASSWORD.toCharArray());
@@ -141,4 +129,25 @@ public X509Certificate getCertificate(String alias) {
}
return null;
}

/**
* This method checks to see if the truststore is present in {@code storePath},
* and if it isn't, it copies the default JDK truststore to the specified location.
*
* @param storePath path of the truststore
*/
public static void createTruststoreFileIfNotExist(Path storePath) {
try {
LOGGER.info("Trust store path: {}", storePath.toAbsolutePath());
Path storeResourcePath = Path.of(TrustStoreManager.class.getResource("/ssl/truststore.jks").toURI());
Files.createDirectories(storePath.getParent());
if (Files.notExists(storePath)) {
Files.copy(storeResourcePath, storePath);
}
} catch (IOException e) {
LOGGER.warn("Bad truststore path", e);
} catch (URISyntaxException e) {
LOGGER.warn("Bad resource path", e);
}
}
}