2727
2828import javax .ejb .Local ;
2929import javax .naming .ConfigurationException ;
30+ import javax .net .ssl .SSLContext ;
31+ import javax .net .ssl .SSLEngine ;
3032
3133import org .apache .log4j .Logger ;
3234
@@ -89,6 +91,7 @@ public class ClusteredAgentManagerImpl extends AgentManagerImpl implements Clust
8991 protected ClusterManager _clusterMgr = null ;
9092
9193 protected HashMap <String , SocketChannel > _peers ;
94+ protected HashMap <String , SSLEngine > _sslEngines ;
9295 private final Timer _timer = new Timer ("ClusteredAgentManager Timer" );
9396
9497 @ Inject
@@ -106,6 +109,7 @@ protected ClusteredAgentManagerImpl() {
106109 @ Override
107110 public boolean configure (String name , Map <String , Object > xmlParams ) throws ConfigurationException {
108111 _peers = new HashMap <String , SocketChannel >(7 );
112+ _sslEngines = new HashMap <String , SSLEngine >(7 );
109113 _nodeId = _clusterMgr .getManagementNodeId ();
110114
111115 ConfigurationDao configDao = ComponentLocator .getCurrentLocator ().getDao (ConfigurationDao .class );
@@ -406,6 +410,7 @@ protected static void logI(byte[] bytes, final String msg) {
406410 public boolean routeToPeer (String peer , byte [] bytes ) {
407411 int i = 0 ;
408412 SocketChannel ch = null ;
413+ SSLEngine sslEngine = null ;
409414 while (i ++ < 5 ) {
410415 ch = connectToPeer (peer , ch );
411416 if (ch == null ) {
@@ -415,11 +420,16 @@ public boolean routeToPeer(String peer, byte[] bytes) {
415420 }
416421 return false ;
417422 }
423+ sslEngine = getSSLEngine (peer );
424+ if (sslEngine == null ) {
425+ logD (bytes , "Unable to get SSLEngine of peer: " + peer );
426+ return false ;
427+ }
418428 try {
419429 if (s_logger .isDebugEnabled ()) {
420430 logD (bytes , "Routing to peer" );
421431 }
422- Link .write (ch , new ByteBuffer [] { ByteBuffer .wrap (bytes ) });
432+ Link .write (ch , new ByteBuffer [] { ByteBuffer .wrap (bytes ) }, sslEngine );
423433 return true ;
424434 } catch (IOException e ) {
425435 try {
@@ -434,6 +444,10 @@ public boolean routeToPeer(String peer, byte[] bytes) {
434444 public String findPeer (long hostId ) {
435445 return _clusterMgr .getPeerName (hostId );
436446 }
447+
448+ public SSLEngine getSSLEngine (String peerName ) {
449+ return _sslEngines .get (peerName );
450+ }
437451
438452 public void cancel (String peerName , long hostId , long sequence , String reason ) {
439453 CancelCommand cancel = new CancelCommand (sequence , reason );
@@ -453,12 +467,14 @@ public void closePeer(String peerName) {
453467 }
454468 }
455469 _peers .remove (peerName );
470+ _sslEngines .remove (peerName );
456471 }
457472 }
458473
459474 public SocketChannel connectToPeer (String peerName , SocketChannel prevCh ) {
460475 synchronized (_peers ) {
461476 SocketChannel ch = _peers .get (peerName );
477+ SSLEngine sslEngine = null ;
462478 if (prevCh != null ) {
463479 try {
464480 prevCh .close ();
@@ -483,10 +499,21 @@ public SocketChannel connectToPeer(String peerName, SocketChannel prevCh) {
483499 ch .configureBlocking (true ); // make sure we are working at blocking mode
484500 ch .socket ().setKeepAlive (true );
485501 ch .socket ().setSoTimeout (60 * 1000 );
502+ try {
503+ SSLContext sslContext = Link .initSSLContext (true );
504+ sslEngine = sslContext .createSSLEngine (ip , _port );
505+ sslEngine .setUseClientMode (true );
506+
507+ Link .doHandshake (ch , sslEngine , true );
508+ s_logger .info ("SSL: Handshake done" );
509+ } catch (Exception e ) {
510+ throw new IOException ("SSL: Fail to init SSL! " + e );
511+ }
486512 if (s_logger .isDebugEnabled ()) {
487513 s_logger .debug ("Connection to peer opened: " + peerName + ", ip: " + ip );
488514 }
489515 _peers .put (peerName , ch );
516+ _sslEngines .put (peerName , sslEngine );
490517 } catch (IOException e ) {
491518 s_logger .warn ("Unable to connect to peer management server: " + peerName + ", ip: " + ip + " due to " + e .getMessage (), e );
492519 return null ;
0 commit comments