101
101
102
102
import javax .annotation .concurrent .GuardedBy ;
103
103
import javax .net .ssl .SSLContext ;
104
- import java .io .File ;
105
- import java .io .IOException ;
106
- import java .io .InterruptedIOException ;
107
- import java .io .UncheckedIOException ;
104
+ import javax .net .ssl .TrustManagerFactory ;
105
+ import java .io .*;
108
106
import java .lang .management .ManagementFactory ;
109
107
import java .lang .management .MemoryMXBean ;
110
108
import java .lang .management .MemoryUsage ;
111
109
import java .lang .reflect .InvocationTargetException ;
112
110
import java .lang .reflect .Method ;
113
111
import java .net .URI ;
114
112
import java .nio .channels .ClosedByInterruptException ;
113
+ import java .security .KeyStore ;
115
114
import java .security .NoSuchAlgorithmException ;
116
115
import java .text .ParseException ;
117
116
import java .text .SimpleDateFormat ;
@@ -432,6 +431,34 @@ public abstract class ModelMesh extends ThriftService
432
431
}
433
432
}
434
433
434
+ private static final String SSL_TRUSTSTORE_PATH_PROPERTY = "watson.ssl.truststore.path" ;
435
+ private static final String SSL_TRUSTSTORE_PASSWORD_PROPERTY = "watson.ssl.truststore.password" ;
436
+
437
+ private static SSLContext sslContext = null ;
438
+
439
+ private static SSLContext loadSSLContext () throws Exception {
440
+ if (sslContext == null ) {
441
+ final String trustStorePath = System .getProperty (SSL_TRUSTSTORE_PATH_PROPERTY );
442
+ final String trustStorePassword = System .getProperty (SSL_TRUSTSTORE_PASSWORD_PROPERTY );
443
+
444
+ if (trustStorePath == null || trustStorePassword == null ) {
445
+ throw new IllegalArgumentException ("Truststore settings not found in system properties" );
446
+ }
447
+
448
+ final KeyStore trustStore = KeyStore .getInstance ("JKS" );
449
+ try (FileInputStream trustStoreStream = new FileInputStream (trustStorePath )) {
450
+ trustStore .load (trustStoreStream , trustStorePassword .toCharArray ());
451
+ }
452
+
453
+ final TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance (TrustManagerFactory .getDefaultAlgorithm ());
454
+ trustManagerFactory .init (trustStore );
455
+
456
+ sslContext = SSLContext .getInstance ("TLS" );
457
+ sslContext .init (null , trustManagerFactory .getTrustManagers (), null );
458
+ }
459
+ return sslContext ;
460
+ }
461
+
435
462
private PayloadProcessor initPayloadProcessor () {
436
463
String payloadProcessorsDefinitions = getStringParameter (MM_PAYLOAD_PROCESSORS , null );
437
464
logger .info ("Parsing PayloadProcessor definition '{}'" , payloadProcessorsDefinitions );
@@ -445,14 +472,16 @@ private PayloadProcessor initPayloadProcessor() {
445
472
String modelId = uri .getQuery ();
446
473
String method = uri .getFragment ();
447
474
if ("http" .equals (processorName )) {
475
+ logger .info ("Initializing HTTP payload processor" );
448
476
processor = new RemotePayloadProcessor (uri );
449
477
} else if ("https" .equals (processorName )) {
450
478
SSLContext sslContext ;
451
479
try {
452
- sslContext = SSLContext . getDefault ();
453
- } catch (NoSuchAlgorithmException missingAlgorithmException ) {
480
+ sslContext = loadSSLContext ();
481
+ } catch (Exception missingAlgorithmException ) {
454
482
throw new UncheckedIOException (new IOException (missingAlgorithmException ));
455
483
}
484
+ logger .info ("Initializing HTTPS payload processor" );
456
485
processor = new RemotePayloadProcessor (uri , sslContext , sslContext .getDefaultSSLParameters ());
457
486
} else if ("logger" .equals (processorName )) {
458
487
processor = new LoggingPayloadProcessor ();
0 commit comments