Skip to content

Commit 4549349

Browse files
committed
Merge pull request #157 from alexpogue/ssltoggle
Add toggle to allow bad SSL certificates to HTTP operators
2 parents c9a514a + 67410e4 commit 4549349

File tree

6 files changed

+91
-8
lines changed

6 files changed

+91
-8
lines changed

com.ibm.streamsx.inet/impl/java/src/com/ibm/streamsx/inet/http/AbstractHTTPGetContent.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public abstract class AbstractHTTPGetContent<A> extends
4747
protected int contentAttributeIndex;
4848

4949
private Metric nFailedRequests;
50+
51+
boolean acceptAllCertificates = false;
5052

5153
public String getUrl() {
5254
return url;
@@ -62,6 +64,12 @@ public void setExtraHeaders(List<String> extraHeaders) {
6264
this.extraHeaders = extraHeaders;
6365
}
6466

67+
@Parameter(optional = true, description = "Accept all SSL certificates, even those that are self-signed. " +
68+
"Setting this option will allow potentially insecure connections. Default is false.")
69+
public void setAcceptAllCertificates(boolean acceptAllCertificates) {
70+
this.acceptAllCertificates = acceptAllCertificates;
71+
}
72+
6573
public Metric getnFailedRequests() {
6674
return nFailedRequests;
6775
}
@@ -81,7 +89,12 @@ public TupleAttribute<Tuple, A> getContentAttribute() {
8189
public synchronized void initialize(OperatorContext context)
8290
throws Exception {
8391
super.initialize(context);
84-
client = new DefaultHttpClient();
92+
93+
if(acceptAllCertificates)
94+
client = HTTPUtils.getHttpClientWithNoSSLValidation();
95+
else
96+
client = new DefaultHttpClient();
97+
8598
builder = new URIBuilder(getUrl());
8699
get = new HttpGet(builder.build());
87100
get.addHeader("Accept", acceptedContentTypes());

com.ibm.streamsx.inet/impl/java/src/com/ibm/streamsx/inet/http/HTTPPostOper.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class HTTPPostOper extends AbstractOperator
7272
private List<String> authenticationProperties = new ArrayList<String>();
7373

7474
private String headerContentType = MIME_FORM;
75+
private boolean acceptAllCertificates = false;
7576

7677
private List<String> extraHeaders = new ArrayList<String>();
7778

@@ -111,10 +112,17 @@ public void setRetryDelay(double val) {
111112
public void setHeaderContentType(String val) {
112113
this.headerContentType = val;
113114
}
114-
@Parameter(optional=true, description="Extra headers to send with request, format is \\\"Header-Name: value\\\".")
115+
@Parameter(optional=true,
116+
description="Extra headers to send with request, format is \\\"Header-Name: value\\\".")
115117
public void setExtraHeaders(List<String> val) {
116118
this.extraHeaders = val;
117119
}
120+
@Parameter(optional=true,
121+
description="Accept all SSL certificates, even those that are self-signed. " +
122+
"Setting this option will allow potentially insecure connections. Default is false.")
123+
public void setAcceptAllCertificates(boolean val) {
124+
this.acceptAllCertificates = val;
125+
}
118126

119127
//consistent region checks
120128
@ContextCheck(compile = true)
@@ -165,6 +173,7 @@ public synchronized void process(StreamingInput<Tuple> stream, Tuple tuple) thro
165173
HTTPRequest req = new HTTPRequest(url);
166174
req.setHeader("Content-Type", headerContentType);
167175
req.setType(RequestType.POST);
176+
req.setInsecure(acceptAllCertificates);
168177

169178
if(headerContentType.equals(MIME_FORM)) {
170179
Map<String, String> params = new HashMap<String, String>();

com.ibm.streamsx.inet/impl/java/src/com/ibm/streamsx/inet/http/HTTPRequest.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class HTTPRequest {
3838

3939
public static enum RequestType {GET, POST};
4040
private RequestType type = RequestType.GET;
41+
private boolean insecure = false;
4142

4243
private HttpUriRequest req = null;
4344
private HttpEntity entity = null;
@@ -66,6 +67,14 @@ HttpUriRequest getReq() {
6667
return req;
6768
}
6869

70+
public boolean isInsecure() {
71+
return insecure;
72+
}
73+
74+
public void setInsecure(boolean insecure) {
75+
this.insecure = insecure;
76+
}
77+
6978
/**
7079
* Set the parameters for a POST request
7180
* @param params
@@ -100,7 +109,14 @@ public void setParams(String value) throws Exception {
100109
* @throws Exception
101110
*/
102111
public HTTPResponse sendRequest(IAuthenticate auth) throws Exception {
103-
HttpClient client = new DefaultHttpClient();
112+
HttpClient client;
113+
if(insecure) {
114+
client = HTTPUtils.getHttpClientWithNoSSLValidation();
115+
}
116+
else {
117+
client = new DefaultHttpClient();
118+
}
119+
104120
if(type == RequestType.GET) {
105121
HttpGet get = new HttpGet(url);
106122
req=get;
@@ -124,8 +140,6 @@ public HTTPResponse sendRequest(IAuthenticate auth) throws Exception {
124140
return new HTTPResponse(client.execute(req));
125141
}
126142

127-
128-
129143
@Override
130144
public String toString() {
131145
return "URL: " + url;

com.ibm.streamsx.inet/impl/java/src/com/ibm/streamsx/inet/http/HTTPStreamReader.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class HTTPStreamReader extends AbstractOperator {
6363
private static Logger trace = Logger.getLogger(CLASS_NAME);
6464
private boolean retryOnClose = false;
6565
private boolean disableCompression = false;
66+
private boolean acceptAllCertificates = false;
6667

6768
@Parameter(optional= false, description="URL endpoint to connect to.")
6869
public void setUrl(String url) {
@@ -123,6 +124,12 @@ public void setDisableCompression(boolean val) {
123124
public void setExtraHeaders(List<String> val) {
124125
this.extraHeaders = val;
125126
}
127+
@Parameter(optional=true,
128+
description="Accept all SSL certificates, even those that are self-signed. " +
129+
"Setting this option will allow potentially insecure connections. Default is false.")
130+
public void setAcceptAllCertificates(boolean val) {
131+
this.acceptAllCertificates = val;
132+
}
126133

127134
@ContextCheck(compile=true)
128135
public static boolean checkAuthParams(OperatorContextChecker occ) {
@@ -189,8 +196,7 @@ public void initialize(OperatorContext op) throws Exception {
189196
URI baseConfigURI = op.getPE().getApplicationDirectory().toURI();
190197
IAuthenticate auth = AuthHelper.getAuthenticator(authenticationType, PathConversionHelper.convertToAbsPath(baseConfigURI, authenticationFile), authenticationProperties);
191198
Map<String, String> extraHeaderMap = HTTPUtils.getHeaderMap(extraHeaders);
192-
193-
reader = new HTTPStreamReaderObj(this.url, auth, this, postDataParams, disableCompression, extraHeaderMap);
199+
reader = new HTTPStreamReaderObj(this.url, auth, this, postDataParams, disableCompression, extraHeaderMap, acceptAllCertificates);
194200
th = op.getThreadFactory().newThread(reader);
195201
th.setDaemon(false);
196202
}

com.ibm.streamsx.inet/impl/java/src/com/ibm/streamsx/inet/http/HTTPStreamReaderObj.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class HTTPStreamReaderObj implements Runnable
4848

4949
public HTTPStreamReaderObj(String url, IAuthenticate auth,
5050
HTTPStreamReader reader, Map<String, String> postD,
51-
boolean disableCompression, Map<String, String> extraHeaders)
51+
boolean disableCompression, Map<String, String> extraHeaders,
52+
boolean insecure)
5253
throws Exception {
5354
this.auth = auth;
5455
this.reader = reader;
@@ -67,6 +68,7 @@ public HTTPStreamReaderObj(String url, IAuthenticate auth,
6768
req.setHeader(header.getKey(), header.getValue());
6869
}
6970
req.setParams(postData);
71+
req.setInsecure(insecure);
7072
}
7173

7274

com.ibm.streamsx.inet/impl/java/src/com/ibm/streamsx/inet/http/HTTPUtils.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@
1515
import java.util.HashMap;
1616
import java.util.List;
1717
import java.util.Map;
18+
import java.security.SecureRandom;
19+
import java.security.cert.X509Certificate;
20+
21+
import javax.net.ssl.SSLContext;
22+
import javax.net.ssl.TrustManager;
23+
import javax.net.ssl.X509TrustManager;
24+
25+
import org.apache.http.client.HttpClient;
26+
import org.apache.http.conn.ClientConnectionManager;
27+
import org.apache.http.conn.scheme.Scheme;
28+
import org.apache.http.conn.scheme.SchemeRegistry;
29+
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
30+
import org.apache.http.conn.ssl.SSLSocketFactory;
31+
import org.apache.http.impl.client.DefaultHttpClient;
32+
import org.apache.http.impl.conn.BasicClientConnectionManager;
1833

1934
public class HTTPUtils {
2035

@@ -53,4 +68,28 @@ public static Map<String, String> getHeaderMap(List<String> headers) {
5368
}
5469
return headerMap;
5570
}
71+
72+
public static HttpClient getHttpClientWithNoSSLValidation() throws Exception {
73+
SSLContext sslContext = SSLContext.getInstance("SSL");
74+
sslContext.init(null, new TrustManager[] {
75+
new X509TrustManager() {
76+
public X509Certificate[] getAcceptedIssuers() {
77+
return null;
78+
}
79+
public void checkClientTrusted(X509Certificate[] certs, String authType) {
80+
}
81+
public void checkServerTrusted(X509Certificate[] certs, String authType) {
82+
}
83+
}
84+
}, new SecureRandom());
85+
86+
SSLSocketFactory sf = new SSLSocketFactory(sslContext, new AllowAllHostnameVerifier());
87+
Scheme httpsScheme = new Scheme("https", 443, sf);
88+
SchemeRegistry schemeRegistry = new SchemeRegistry();
89+
schemeRegistry.register(httpsScheme);
90+
91+
ClientConnectionManager cm = new BasicClientConnectionManager(schemeRegistry);
92+
93+
return new DefaultHttpClient(cm);
94+
}
5695
}

0 commit comments

Comments
 (0)