Skip to content

Commit c9a3699

Browse files
author
Jai Asher
authored
Fixing resource leak due to open file descriptors in SecurityUtility.java (#1851)
1 parent b3d5256 commit c9a3699

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

pulsar-common/src/main/java/org/apache/pulsar/common/util/SecurityUtility.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static SSLContext createSslContext(boolean allowInsecureConnection, Certi
5858
}
5959

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

7474
public static SslContext createNettySslContextForClient(boolean allowInsecureConnection, String trustCertsFilePath,
7575
String certFilePath, String keyFilePath)
76-
throws GeneralSecurityException, SSLException, FileNotFoundException {
76+
throws IOException, GeneralSecurityException, SSLException, FileNotFoundException {
7777
X509Certificate[] certificates = loadCertificatesFromPemFile(certFilePath);
7878
PrivateKey privateKey = loadPrivateKeyFromPemFile(keyFilePath);
7979
return createNettySslContextForClient(allowInsecureConnection, trustCertsFilePath, certificates, privateKey);
8080
}
8181

8282
public static SslContext createNettySslContextForClient(boolean allowInsecureConnection, String trustCertsFilePath,
8383
Certificate[] certificates, PrivateKey privateKey)
84-
throws GeneralSecurityException, SSLException, FileNotFoundException {
84+
throws GeneralSecurityException, IOException, FileNotFoundException {
8585
SslContextBuilder builder = SslContextBuilder.forClient();
8686
if (allowInsecureConnection) {
8787
builder.trustManager(InsecureTrustManagerFactory.INSTANCE);
8888
} else {
8989
if (trustCertsFilePath != null && trustCertsFilePath.length() != 0) {
90-
builder.trustManager(new FileInputStream(trustCertsFilePath));
90+
try (FileInputStream input = new FileInputStream(trustCertsFilePath)) {
91+
builder.trustManager(input);
92+
}
9193
}
9294
}
9395
builder.keyManager(privateKey, (X509Certificate[]) certificates);
@@ -96,7 +98,7 @@ public static SslContext createNettySslContextForClient(boolean allowInsecureCon
9698

9799
public static SslContext createNettySslContextForServer(boolean allowInsecureConnection, String trustCertsFilePath,
98100
String certFilePath, String keyFilePath)
99-
throws GeneralSecurityException, SSLException, FileNotFoundException {
101+
throws IOException, GeneralSecurityException, SSLException, FileNotFoundException {
100102
X509Certificate[] certificates = loadCertificatesFromPemFile(certFilePath);
101103
PrivateKey privateKey = loadPrivateKeyFromPemFile(keyFilePath);
102104

@@ -105,7 +107,9 @@ public static SslContext createNettySslContextForServer(boolean allowInsecureCon
105107
builder.trustManager(InsecureTrustManagerFactory.INSTANCE);
106108
} else {
107109
if (trustCertsFilePath != null && trustCertsFilePath.length() != 0) {
108-
builder.trustManager(new FileInputStream(trustCertsFilePath));
110+
try (FileInputStream input = new FileInputStream(trustCertsFilePath)) {
111+
builder.trustManager(input);
112+
}
109113
} else {
110114
builder.trustManager((File) null);
111115
}

0 commit comments

Comments
 (0)