Skip to content

Commit 7874431

Browse files
committed
Trust all certificates
1 parent e12d374 commit 7874431

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

src/main/java/io/tpd/quboo/sonarplugin/dtos/UsersWrapper.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import java.util.ArrayList;
77
import java.util.List;
8-
import java.util.stream.Collectors;
98

109
public class UsersWrapper {
1110

@@ -17,9 +16,7 @@ public UsersWrapper() {
1716

1817
public void filterAndAddUsers(final Users users) {
1918
this.users.addAll(
20-
users.getUsers().stream()
21-
.filter(User::isActive)
22-
.collect(Collectors.toList())
19+
users.getUsers()
2320
);
2421
}
2522

src/main/java/io/tpd/quboo/sonarplugin/hooks/QubooConnector.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.tpd.quboo.sonarplugin.QubooPlugin;
55
import io.tpd.quboo.sonarplugin.dtos.IssuesWrapper;
66
import io.tpd.quboo.sonarplugin.dtos.UsersWrapper;
7+
import io.tpd.quboo.sonarplugin.http.HttpClients;
78
import io.tpd.quboo.sonarplugin.pojos.Issues;
89
import io.tpd.quboo.sonarplugin.pojos.Paging;
910
import io.tpd.quboo.sonarplugin.pojos.Users;
@@ -28,7 +29,7 @@ public class QubooConnector implements PostProjectAnalysisTask {
2829
private final OkHttpClient http;
2930

3031
public QubooConnector(final Server server) {
31-
this.http = new OkHttpClient();
32+
this.http = HttpClients.getUnsafeOkHttpClient();
3233
this.server = server;
3334
this.mapper = new ObjectMapper();
3435
}
@@ -57,7 +58,7 @@ private void sendIssuesToQuboo(final IssuesWrapper allIssues, final String quboo
5758
.build();
5859
final Response response = http.newCall(request).execute();
5960
final String body = response.body().string();
60-
log.info("Response " + body);
61+
log.info("Response " + response.code() + " | " + body);
6162
}
6263

6364
private IssuesWrapper getIssues() throws Exception {
@@ -86,7 +87,7 @@ private void sendUsersToQuboo(final UsersWrapper allUsers, final String qubooKey
8687
.build();
8788
final Response response = http.newCall(request).execute();
8889
final String body = response.body().string();
89-
log.info("Response " + body);
90+
log.info("Response " + response.code() + " | " + body);
9091
}
9192

9293
private UsersWrapper getUsers() {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.tpd.quboo.sonarplugin.http;
2+
3+
import okhttp3.OkHttpClient;
4+
5+
import javax.net.ssl.SSLContext;
6+
import javax.net.ssl.SSLSocketFactory;
7+
import javax.net.ssl.TrustManager;
8+
import javax.net.ssl.X509TrustManager;
9+
10+
public class HttpClients {
11+
12+
private HttpClients() {
13+
}
14+
15+
/**
16+
* Even though Quboo uses a valid LetsEncrypt certificate, its CA might not be included by default in some
17+
* Java distributions. For that reason we use a client that trusts all certificates.
18+
*
19+
* @return an OkHttpClient that trusts all certificates
20+
*/
21+
public static OkHttpClient getUnsafeOkHttpClient() {
22+
try {
23+
// No-op trust manager
24+
final TrustManager[] trustAllCerts = new TrustManager[]{
25+
new X509TrustManager() {
26+
@Override
27+
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
28+
}
29+
30+
@Override
31+
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
32+
}
33+
34+
@Override
35+
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
36+
return new java.security.cert.X509Certificate[]{};
37+
}
38+
}
39+
};
40+
41+
// Install the all-trusting trust manager
42+
final SSLContext sslContext = SSLContext.getInstance("SSL");
43+
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
44+
// Create an ssl socket factory with our all-trusting manager
45+
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
46+
47+
OkHttpClient.Builder builder = new OkHttpClient.Builder();
48+
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
49+
builder.hostnameVerifier((hostname, session) -> true);
50+
51+
return builder.build();
52+
} catch (Exception e) {
53+
throw new RuntimeException(e);
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)