Skip to content

Commit

Permalink
Fixing resource leak due to open file descriptors in SecurityUtility.…
Browse files Browse the repository at this point in the history
…java (#1851)
  • Loading branch information
Jai Asher authored May 29, 2018
1 parent b3d5256 commit c9a3699
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static SSLContext createSslContext(boolean allowInsecureConnection, Certi
}

public static SslContext createNettySslContextForClient(boolean allowInsecureConnection, String trustCertsFilePath)
throws GeneralSecurityException, SSLException, FileNotFoundException {
throws IOException, GeneralSecurityException, SSLException, FileNotFoundException {
return createNettySslContextForClient(allowInsecureConnection, trustCertsFilePath, (Certificate[]) null,
(PrivateKey) null);
}
Expand All @@ -73,21 +73,23 @@ public static SSLContext createSslContext(boolean allowInsecureConnection, Strin

public static SslContext createNettySslContextForClient(boolean allowInsecureConnection, String trustCertsFilePath,
String certFilePath, String keyFilePath)
throws GeneralSecurityException, SSLException, FileNotFoundException {
throws IOException, GeneralSecurityException, SSLException, FileNotFoundException {
X509Certificate[] certificates = loadCertificatesFromPemFile(certFilePath);
PrivateKey privateKey = loadPrivateKeyFromPemFile(keyFilePath);
return createNettySslContextForClient(allowInsecureConnection, trustCertsFilePath, certificates, privateKey);
}

public static SslContext createNettySslContextForClient(boolean allowInsecureConnection, String trustCertsFilePath,
Certificate[] certificates, PrivateKey privateKey)
throws GeneralSecurityException, SSLException, FileNotFoundException {
throws GeneralSecurityException, IOException, FileNotFoundException {
SslContextBuilder builder = SslContextBuilder.forClient();
if (allowInsecureConnection) {
builder.trustManager(InsecureTrustManagerFactory.INSTANCE);
} else {
if (trustCertsFilePath != null && trustCertsFilePath.length() != 0) {
builder.trustManager(new FileInputStream(trustCertsFilePath));
try (FileInputStream input = new FileInputStream(trustCertsFilePath)) {
builder.trustManager(input);
}
}
}
builder.keyManager(privateKey, (X509Certificate[]) certificates);
Expand All @@ -96,7 +98,7 @@ public static SslContext createNettySslContextForClient(boolean allowInsecureCon

public static SslContext createNettySslContextForServer(boolean allowInsecureConnection, String trustCertsFilePath,
String certFilePath, String keyFilePath)
throws GeneralSecurityException, SSLException, FileNotFoundException {
throws IOException, GeneralSecurityException, SSLException, FileNotFoundException {
X509Certificate[] certificates = loadCertificatesFromPemFile(certFilePath);
PrivateKey privateKey = loadPrivateKeyFromPemFile(keyFilePath);

Expand All @@ -105,7 +107,9 @@ public static SslContext createNettySslContextForServer(boolean allowInsecureCon
builder.trustManager(InsecureTrustManagerFactory.INSTANCE);
} else {
if (trustCertsFilePath != null && trustCertsFilePath.length() != 0) {
builder.trustManager(new FileInputStream(trustCertsFilePath));
try (FileInputStream input = new FileInputStream(trustCertsFilePath)) {
builder.trustManager(input);
}
} else {
builder.trustManager((File) null);
}
Expand Down

0 comments on commit c9a3699

Please sign in to comment.