Skip to content

Commit c3d6996

Browse files
committed
improve protocol enabling based on socket object type
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> (cherry picked from commit d62d511) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent df92a45 commit c3d6996

3 files changed

Lines changed: 59 additions & 25 deletions

File tree

‎plugins/network-elements/opendaylight/src/main/java/org/apache/cloudstack/network/opendaylight/api/NeutronRestApi.java‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,28 @@ public void checkServerTrusted(final X509Certificate[] certs, final String authT
189189

190190
@Override
191191
public Socket createSocket(final String host, final int port) throws IOException {
192-
SSLSocket s = (SSLSocket) ssf.createSocket(host, port);
193-
s.setEnabledProtocols(SSLUtils.getSupportedProtocols(s.getEnabledProtocols()));
192+
Socket s = ssf.createSocket(host, port);
193+
if (s instanceof SSLSocket) {
194+
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
195+
}
194196
return s;
195197
}
196198

197199
@Override
198200
public Socket createSocket(final String address, final int port, final InetAddress localAddress, final int localPort) throws IOException, UnknownHostException {
199-
SSLSocket s = (SSLSocket) ssf.createSocket(address, port, localAddress, localPort);
200-
s.setEnabledProtocols(SSLUtils.getSupportedProtocols(s.getEnabledProtocols()));
201+
Socket s = ssf.createSocket(address, port, localAddress, localPort);
202+
if (s instanceof SSLSocket) {
203+
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
204+
}
201205
return s;
202206
}
203207

204208
@Override
205209
public Socket createSocket(final Socket socket, final String host, final int port, final boolean autoClose) throws IOException, UnknownHostException {
206-
SSLSocket s = (SSLSocket) ssf.createSocket(socket, host, port, autoClose);
207-
s.setEnabledProtocols(SSLUtils.getSupportedProtocols(s.getEnabledProtocols()));
210+
Socket s = ssf.createSocket(socket, host, port, autoClose);
211+
if (s instanceof SSLSocket) {
212+
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
213+
}
208214
return s;
209215
}
210216

@@ -213,10 +219,16 @@ public Socket createSocket(final String host, final int port, final InetAddress
213219
UnknownHostException, ConnectTimeoutException {
214220
int timeout = params.getConnectionTimeout();
215221
if (timeout == 0) {
216-
return createSocket(host, port, localAddress, localPort);
222+
Socket s = createSocket(host, port, localAddress, localPort);
223+
if (s instanceof SSLSocket) {
224+
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
225+
}
226+
return s;
217227
} else {
218-
SSLSocket s = (SSLSocket) ssf.createSocket();
219-
s.setEnabledProtocols(SSLUtils.getSupportedProtocols(s.getEnabledProtocols()));
228+
Socket s = ssf.createSocket();
229+
if (s instanceof SSLSocket) {
230+
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
231+
}
220232
s.bind(new InetSocketAddress(localAddress, localPort));
221233
s.connect(new InetSocketAddress(host, port), timeout);
222234
return s;

‎utils/src/com/cloud/utils/rest/RESTServiceConnector.java‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,19 @@ public Socket createSocket(final String host, final int port) throws IOException
356356

357357
@Override
358358
public Socket createSocket(final String address, final int port, final InetAddress localAddress, final int localPort) throws IOException, UnknownHostException {
359-
SSLSocket socket = (SSLSocket) ssf.createSocket(address, port, localAddress, localPort);
360-
socket.setEnabledProtocols(SSLUtils.getSupportedProtocols(socket.getEnabledProtocols()));
359+
Socket socket = ssf.createSocket(address, port, localAddress, localPort);
360+
if (socket instanceof SSLSocket) {
361+
((SSLSocket)socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)socket).getEnabledProtocols()));
362+
}
361363
return socket;
362364
}
363365

364366
@Override
365367
public Socket createSocket(final Socket socket, final String host, final int port, final boolean autoClose) throws IOException, UnknownHostException {
366-
SSLSocket s = (SSLSocket) ssf.createSocket(socket, host, port, autoClose);
367-
s.setEnabledProtocols(SSLUtils.getSupportedProtocols(s.getEnabledProtocols()));
368+
Socket s = ssf.createSocket(socket, host, port, autoClose);
369+
if (s instanceof SSLSocket) {
370+
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
371+
}
368372
return s;
369373
}
370374

@@ -373,10 +377,16 @@ public Socket createSocket(final String host, final int port, final InetAddress
373377
throws IOException, UnknownHostException, ConnectTimeoutException {
374378
final int timeout = params.getConnectionTimeout();
375379
if (timeout == 0) {
376-
return createSocket(host, port, localAddress, localPort);
380+
Socket socket = createSocket(host, port, localAddress, localPort);
381+
if (socket instanceof SSLSocket) {
382+
((SSLSocket)socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)socket).getEnabledProtocols()));
383+
}
384+
return socket;
377385
} else {
378-
final SSLSocket s = (SSLSocket) ssf.createSocket();
379-
s.setEnabledProtocols(SSLUtils.getSupportedProtocols(s.getEnabledProtocols()));
386+
final Socket s = ssf.createSocket();
387+
if (s instanceof SSLSocket) {
388+
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
389+
}
380390
s.bind(new InetSocketAddress(localAddress, localPort));
381391
s.connect(new InetSocketAddress(host, port), timeout);
382392
return s;

‎utils/src/org/apache/commons/httpclient/contrib/ssl/EasySSLProtocolSocketFactory.java‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ private SSLContext getSSLContext() {
124124
*/
125125
@Override
126126
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException, UnknownHostException {
127-
SSLSocket socket = (SSLSocket) getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
128-
socket.setEnabledProtocols(SSLUtils.getSupportedProtocols(socket.getEnabledProtocols()));
127+
Socket socket = getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
128+
if (socket instanceof SSLSocket) {
129+
((SSLSocket)socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)socket).getEnabledProtocols()));
130+
}
129131
return socket;
130132
}
131133

@@ -159,10 +161,16 @@ public Socket createSocket(final String host, final int port, final InetAddress
159161
int timeout = params.getConnectionTimeout();
160162
SocketFactory socketfactory = getSSLContext().getSocketFactory();
161163
if (timeout == 0) {
162-
return socketfactory.createSocket(host, port, localAddress, localPort);
164+
Socket socket = socketfactory.createSocket(host, port, localAddress, localPort);
165+
if (socket instanceof SSLSocket) {
166+
((SSLSocket)socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)socket).getEnabledProtocols()));
167+
}
168+
return socket;
163169
} else {
164-
SSLSocket socket = (SSLSocket) socketfactory.createSocket();
165-
socket.setEnabledProtocols(SSLUtils.getSupportedProtocols(socket.getEnabledProtocols()));
170+
Socket socket = socketfactory.createSocket();
171+
if (socket instanceof SSLSocket) {
172+
((SSLSocket)socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)socket).getEnabledProtocols()));
173+
}
166174
SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
167175
SocketAddress remoteaddr = new InetSocketAddress(host, port);
168176
socket.bind(localaddr);
@@ -176,8 +184,10 @@ public Socket createSocket(final String host, final int port, final InetAddress
176184
*/
177185
@Override
178186
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
179-
SSLSocket socket = (SSLSocket) getSSLContext().getSocketFactory().createSocket(host, port);
180-
socket.setEnabledProtocols(SSLUtils.getSupportedProtocols(socket.getEnabledProtocols()));
187+
Socket socket = (Socket) getSSLContext().getSocketFactory().createSocket(host, port);
188+
if (socket instanceof SSLSocket) {
189+
((SSLSocket)socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)socket).getEnabledProtocols()));
190+
}
181191
return socket;
182192
}
183193

@@ -186,8 +196,10 @@ public Socket createSocket(String host, int port) throws IOException, UnknownHos
186196
*/
187197
@Override
188198
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
189-
SSLSocket s= (SSLSocket) getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
190-
s.setEnabledProtocols(SSLUtils.getSupportedProtocols(s.getEnabledProtocols()));
199+
Socket s = getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
200+
if (s instanceof SSLSocket) {
201+
((SSLSocket)s).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket)s).getEnabledProtocols()));
202+
}
191203
return s;
192204
}
193205

0 commit comments

Comments
 (0)