Skip to content

Commit 1689e6f

Browse files
author
Jarrod Ribble
committed
Change keystore validation to throw exceptions directly instead of returning the exception message.
1 parent 949a398 commit 1689e6f

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

src/main/java/com/netiq/websockify/Websockify.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,13 @@ public void doMain(String[] args) throws Exception {
100100
keystoreKeyPassword = keystorePassword;
101101
}
102102

103-
String invalidMsg = WebsockifySslContext.validateKeystore(keystore, keystorePassword, keystoreKeyPassword);
104-
if ( invalidMsg != null ) {
105-
System.err.println("Error validating keystore: " + invalidMsg );
103+
try
104+
{
105+
WebsockifySslContext.validateKeystore(keystore, keystorePassword, keystoreKeyPassword);
106+
}
107+
catch ( Exception e )
108+
{
109+
System.err.println("Error validating keystore: " + e.getMessage() );
106110
printUsage(System.err);
107111
System.exit(2);
108112
}

src/main/java/com/netiq/websockify/WebsockifyServer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
77
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
88

9+
import java.io.IOException;
910
import java.net.InetSocketAddress;
11+
import java.security.KeyManagementException;
12+
import java.security.KeyStoreException;
13+
import java.security.NoSuchAlgorithmException;
14+
import java.security.UnrecoverableKeyException;
15+
import java.security.cert.CertificateException;
1016
import java.util.concurrent.Executor;
1117
import java.util.concurrent.Executors;
1218

@@ -88,8 +94,10 @@ public Channel getChannel ( )
8894
* @param keyPassword - password to the private key in the keystore file
8995
* @return null if valid, otherwise a string describing the error.
9096
*/
91-
public String validateKeystore ( String keystore, String password, String keyPassword ) {
92-
return WebsockifySslContext.validateKeystore(keystore, password, keyPassword);
97+
public void validateKeystore ( String keystore, String password, String keyPassword )
98+
throws KeyManagementException, UnrecoverableKeyException, IOException, NoSuchAlgorithmException, CertificateException, KeyStoreException
99+
{
100+
WebsockifySslContext.validateKeystore(keystore, password, keyPassword);
93101
}
94102

95103
}

src/main/java/com/netiq/websockify/WebsockifySslContext.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,18 @@ private static SSLContext getSSLContext ( String keyStoreFilePath, String passwo
106106
context.init(kmf.getKeyManagers(), null, null);
107107
return context;
108108
}
109-
110-
public static String validateKeystore ( String keystore, String password, String keyPassword ) {
111-
try {
112-
getSSLContext(keystore, password, keyPassword);
113-
} catch (Exception e) {
114-
Logger.getLogger(WebsockifySslContext.class.getName()).severe("Error validating SSL context for keystore " + keystore + ": " + e.getMessage());
115-
return e.getMessage();
116-
}
117-
return null;
109+
110+
/**
111+
* Validates that a keystore with the given parameters exists and can be used for an SSL context.
112+
* @param keystore - path to the keystore file
113+
* @param password - password to the keystore file
114+
* @param keyPassword - password to the private key in the keystore file
115+
* @return null if valid, otherwise a string describing the error.
116+
*/
117+
public static void validateKeystore ( String keystore, String password, String keyPassword )
118+
throws KeyManagementException, UnrecoverableKeyException, IOException, NoSuchAlgorithmException, CertificateException, KeyStoreException
119+
{
120+
121+
getSSLContext(keystore, password, keyPassword);
118122
}
119123
}

0 commit comments

Comments
 (0)