Skip to content

Commit 4d3b762

Browse files
Test case for bypassing certificate validation added.
Update: - Test case added to bypass certificate validation.
1 parent e3f1a6c commit 4d3b762

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

splunk/src/main/java/com/splunk/HttpService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,6 @@ Socket open() throws IOException {
415415
public ResponseMessage send(String path, RequestMessage request) {
416416
// Construct a full URL to the resource
417417
URL url = getUrl(path);
418-
419418
// Create and initialize the connection object
420419
HttpURLConnection cn;
421420
try {
@@ -531,6 +530,10 @@ public static SSLSocketFactory getSSLSocketFactory() {
531530
return HttpService.sslSocketFactory;
532531
}
533532

533+
public static void setValidateCertificates(boolean validateCertificates) {
534+
HttpService.validateCertificates = validateCertificates;
535+
}
536+
534537
public static SSLSocketFactory createSSLFactory() {
535538

536539
try {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2012 Splunk, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package com.splunk;
18+
19+
import org.junit.Assert;
20+
import org.junit.Before;
21+
import org.junit.BeforeClass;
22+
import org.junit.Test;
23+
24+
import javax.net.ssl.SSLContext;
25+
import javax.net.ssl.SSLSocketFactory;
26+
import javax.net.ssl.TrustManager;
27+
import javax.net.ssl.X509TrustManager;
28+
import java.security.SecureRandom;
29+
import java.security.cert.X509Certificate;
30+
31+
public class HttpCertificateValidationTest extends SDKTestCase {
32+
private HttpService httpService;
33+
34+
@BeforeClass
35+
public static void ignoreCertificateValidation() {
36+
// Bypass the certification validation here.
37+
HttpService.setValidateCertificates(false);
38+
}
39+
40+
@Before
41+
@Override
42+
public void setUp() throws Exception {
43+
super.setUp();
44+
45+
httpService = new HttpService(
46+
(String) command.opts.get("host"),
47+
(Integer) command.opts.get("port"),
48+
(String) command.opts.get("scheme")
49+
);
50+
}
51+
52+
@Test
53+
public void testSSLSocketUsingCertificateFlag() throws Exception {
54+
55+
try {
56+
SSLContext sslContext = SSLContext.getInstance("TLS");
57+
TrustManager[] byPassTrustManagers = new TrustManager[]{
58+
new X509TrustManager() {
59+
public X509Certificate[] getAcceptedIssuers() {
60+
return new X509Certificate[0];
61+
}
62+
63+
public void checkClientTrusted(X509Certificate[] chain, String authType) {
64+
}
65+
66+
public void checkServerTrusted(X509Certificate[] chain, String authType) {
67+
}
68+
}
69+
};
70+
sslContext.init(null, byPassTrustManagers, new SecureRandom());
71+
SSLSocketFactory TLSOnlySSLFactory = sslContext.getSocketFactory();
72+
Service.setSSLSocketFactory(TLSOnlySSLFactory);
73+
74+
validateSSLSocketFactory(Service.getSSLSocketFactory());
75+
} catch (Exception e) {
76+
Assert.assertNull(e);
77+
}
78+
}
79+
80+
public void validateSSLSocketFactory(SSLSocketFactory factory) {
81+
// Backup the old value
82+
SSLSocketFactory old = Service.getSSLSocketFactory();
83+
84+
Service.setSSLSocketFactory(factory);
85+
Service s = new Service(service.getHost());
86+
s.login(service.getUsername(), service.getPassword());
87+
Assert.assertEquals(service.getUsername(), s.getUsername());
88+
Assert.assertEquals(service.getPassword(), s.getPassword());
89+
Assert.assertEquals(service.getInfo().keySet(), s.getInfo().keySet());
90+
Assert.assertEquals(service.getInfo().getVersion(), s.getInfo().getVersion());
91+
92+
// Restore the old value
93+
Service.setSSLSocketFactory(old);
94+
}
95+
}

0 commit comments

Comments
 (0)