Skip to content

Commit ff86c86

Browse files
author
Sheng Yang
committed
bug 10135: Add SSL encryption for non-NIO link
Now Link.write() support SSL encryption. And since there is no user of Link.read(), we comment it out.
1 parent d9e0bcf commit ff86c86

5 files changed

Lines changed: 219 additions & 198 deletions

File tree

‎server/src/com/cloud/agent/manager/ClusteredAgentAttache.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.nio.channels.ClosedChannelException;
1111
import java.nio.channels.SocketChannel;
1212

13+
import javax.net.ssl.SSLEngine;
14+
1315
import org.apache.log4j.Logger;
1416

1517
import com.cloud.agent.AgentManager;
@@ -126,6 +128,11 @@ public void send(Request req, Listener listener) throws AgentUnavailableExceptio
126128
}
127129
continue;
128130
}
131+
132+
SSLEngine sslEngine = s_clusteredAgentMgr.getSSLEngine(peerName);
133+
if (sslEngine == null) {
134+
throw new AgentUnavailableException("Unable to get SSLEngine of peer " + peerName, _id);
135+
}
129136

130137
try {
131138
if (s_logger.isDebugEnabled()) {
@@ -135,7 +142,7 @@ public void send(Request req, Listener listener) throws AgentUnavailableExceptio
135142
SynchronousListener synchronous = (SynchronousListener)listener;
136143
synchronous.setPeer(peerName);
137144
}
138-
Link.write(ch, req.toBytes());
145+
Link.write(ch, req.toBytes(), sslEngine);
139146
error = false;
140147
return;
141148
} catch (IOException e) {

‎server/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import javax.ejb.Local;
2929
import javax.naming.ConfigurationException;
30+
import javax.net.ssl.SSLContext;
31+
import javax.net.ssl.SSLEngine;
3032

3133
import 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

Comments
 (0)