Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 7536ce7

Browse files
authored
Merge pull request #24 from snyk/feat/disable-ssl-validation
feat: allow to skip ssl validation [ROAD-21]
2 parents c3b7249 + 9d5462a commit 7536ce7

File tree

5 files changed

+92
-23
lines changed

5 files changed

+92
-23
lines changed

.github/workflows/test.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,7 @@ jobs:
3636
restore-keys: ${{ runner.os }}-gradle
3737

3838
- name: Run tests
39-
run: ./gradlew clean test
39+
env:
40+
DEEPROXY_API_URL: ${{secrets.DEEPROXY_API_URL}}
41+
SNYK_TOKEN: ${{secrets.SNYK_TOKEN}}
42+
run: ./gradlew clean test integTest

build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66

77
group = "io.snyk.code.sdk"
88
archivesBaseName = "snyk-code-client"
9-
version = "2.1.9"
9+
version = "2.1.10"
1010

1111
repositories {
1212
mavenCentral()
@@ -61,6 +61,7 @@ task integTest(type: Test) {
6161
shouldRunAfter test
6262

6363
testLogging {
64+
exceptionFormat "full"
6465
showStandardStreams = true
6566
}
6667
outputs.upToDateWhen { false }

src/integTest/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java

+18-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222

23+
import static org.hamcrest.core.IsEqual.equalTo;
24+
import static org.hamcrest.core.IsNull.notNullValue;
2325
import static org.junit.Assert.*;
2426

2527
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@@ -37,8 +39,8 @@ public class DeepCodeRestApiTest {
3739
+ "}\n";
3840

3941
// !!! Will works only with already logged sessionToken
40-
private static final String loggedToken = System.getenv("DEEPCODE_API_KEY");
41-
private final String deepcodedLoggedToken = System.getenv("DEEPCODE_API_KEY_STAGING");
42+
private static final String loggedToken = System.getenv("SNYK_TOKEN");
43+
private static final String baseUrl = System.getenv("DEEPROXY_API_URL");
4244

4345
private static String bundleId = null;
4446

@@ -70,17 +72,8 @@ public void _020_checkSession() {
7072
System.out.printf("Check Session call with token [%1$s] return [%2$d] code.\n", token, status);
7173
assertEquals(401, status);
7274

73-
token = DeepCodeRestApi.newLogin(userAgent).getSessionToken();
74-
status = DeepCodeRestApi.checkSession(token).getStatusCode();
75-
System.out.printf(
76-
"Check Session call with newly requested but not yet logged token [%1$s] return [%2$d] code.\n",
77-
token, status);
78-
assertEquals(
79-
"Check Session call with newly requested but not yet logged token should return 304 code.",
80-
304,
81-
status);
82-
8375
token = loggedToken;
76+
DeepCodeRestApi.setBaseUrl(baseUrl);
8477
status = DeepCodeRestApi.checkSession(token).getStatusCode();
8578
System.out.printf(
8679
"Check Session call with logged user's token [%1$s] return [%2$d] code.\n", token, status);
@@ -94,8 +87,7 @@ public void _022_setBaseUrl() {
9487
try {
9588
doSetBaseUrlTest("", "blabla", 401);
9689
doSetBaseUrlTest("https://www.google.com/", "blabla", 404);
97-
doSetBaseUrlTest("https://www.deepcoded.com/", "blabla", 401);
98-
doSetBaseUrlTest("https://www.deepcoded.com/", deepcodedLoggedToken, 200);
90+
doSetBaseUrlTest("https://deeproxy.snyk.io/", "blabla", 401);
9991
} finally {
10092
DeepCodeRestApi.setBaseUrl("");
10193
}
@@ -134,6 +126,7 @@ public void _025_getFilters() {
134126
@Test
135127
public void _030_createBundle_from_source() {
136128
System.out.println("\n--------------Create Bundle from Source----------------\n");
129+
DeepCodeRestApi.setBaseUrl(baseUrl);
137130
int status = DeepCodeRestApi.checkSession(loggedToken).getStatusCode();
138131
assertEquals(200, status);
139132
FileContent fileContent = new FileContent("/AnnotatorTest.java", testFileContent);
@@ -270,6 +263,7 @@ public void _036_Check_Bundle() {
270263
}
271264

272265
private FileHashRequest createFileHashRequest(String fakeFileName) {
266+
DeepCodeRestApi.setBaseUrl(baseUrl);
273267
int status = DeepCodeRestApi.checkSession(loggedToken).getStatusCode();
274268
assertEquals(200, status);
275269
final File testFile =
@@ -429,4 +423,14 @@ private void assertAndPrintGetAnalysisResponse(GetAnalysisResponse response) {
429423
// assertEquals("DONE", response.getStatus());
430424
assertEquals("Get Analysis request not succeed", 200, response.getStatusCode());
431425
}
426+
427+
@Test
428+
public void setBaseUrl_shouldUseEmptyTrustManager_whenDisableSslVerificationIsTrue() {
429+
DeepCodeRestApi.setBaseUrl(baseUrl, true);
430+
431+
EmptyResponse emptyResponse = DeepCodeRestApi.checkSession(loggedToken);
432+
433+
assertThat(emptyResponse, notNullValue());
434+
assertThat(emptyResponse.getStatusCode(), equalTo(200));
435+
}
432436
}

src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java

+51-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
*/
44
package ai.deepcode.javaclient;
55

6+
import javax.net.ssl.SSLContext;
7+
import javax.net.ssl.SSLSocketFactory;
8+
import javax.net.ssl.TrustManager;
9+
import javax.net.ssl.X509TrustManager;
10+
611
import ai.deepcode.javaclient.requests.*;
712
import ai.deepcode.javaclient.responses.*;
813

@@ -16,6 +21,10 @@
1621
import retrofit2.http.*;
1722

1823
import java.io.IOException;
24+
import java.security.KeyManagementException;
25+
import java.security.NoSuchAlgorithmException;
26+
import java.security.SecureRandom;
27+
import java.security.cert.X509Certificate;
1928
import java.util.List;
2029
import java.util.concurrent.TimeUnit;
2130

@@ -31,29 +40,65 @@ private DeepCodeRestApi() {}
3140

3241
private static final String API_URL = "https://www.deepcode.ai/";
3342

34-
private static Retrofit retrofit = buildRetrofit(API_URL);
43+
private static Retrofit retrofit = buildRetrofit(API_URL, false);
3544

3645
// Create simple REST adapter which points the baseUrl.
37-
private static Retrofit buildRetrofit(String baseUrl) {
38-
OkHttpClient client = new OkHttpClient.Builder()
46+
private static Retrofit buildRetrofit(String baseUrl, boolean disableSslVerification) {
47+
OkHttpClient.Builder builder = new OkHttpClient.Builder()
3948
.connectTimeout(100, TimeUnit.SECONDS)
4049
.writeTimeout(100, TimeUnit.SECONDS)
41-
.readTimeout(100, TimeUnit.SECONDS).build();
50+
.readTimeout(100, TimeUnit.SECONDS);
51+
if (disableSslVerification) {
52+
X509TrustManager x509TrustManager = buildUnsafeTrustManager();
53+
final TrustManager[] trustAllCertificates = new TrustManager[]{ x509TrustManager };
54+
55+
try {
56+
final String sslProtocol = "SSL";
57+
SSLContext sslContext = SSLContext.getInstance(sslProtocol);
58+
sslContext.init(null, trustAllCertificates, new SecureRandom());
59+
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
60+
builder.sslSocketFactory(sslSocketFactory, x509TrustManager);
61+
} catch (NoSuchAlgorithmException | KeyManagementException e) {
62+
//TODO(pavel): extract Retrofit and OkHttpClient into configuration object to simplify API client building.
63+
e.printStackTrace();
64+
}
65+
}
66+
OkHttpClient client = builder.build();
4267
return new Retrofit.Builder()
4368
.baseUrl(baseUrl + "publicapi/")
44-
.client(client)
69+
.client(client)
4570
.addConverterFactory(GsonConverterFactory.create())
4671
.build();
4772
}
4873

74+
@NotNull
75+
private static X509TrustManager buildUnsafeTrustManager() {
76+
return new X509TrustManager() {
77+
@Override
78+
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
79+
80+
@Override
81+
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
82+
83+
@Override
84+
public X509Certificate[] getAcceptedIssuers() {
85+
return new X509Certificate[]{};
86+
}
87+
};
88+
}
89+
4990
/**
5091
* Re-set baseUrl for retrofit instance
5192
*
5293
* @param baseUrl new baseUrl. <b>Null</b> or empty "" value will reset to default {@code
5394
* #API_URL}
5495
*/
5596
public static void setBaseUrl(@Nullable String baseUrl) {
56-
retrofit = buildRetrofit((baseUrl == null || baseUrl.isEmpty()) ? API_URL : baseUrl);
97+
setBaseUrl(baseUrl, false);
98+
}
99+
100+
public static void setBaseUrl(@Nullable String baseUrl, boolean disableSslVerification) {
101+
retrofit = buildRetrofit((baseUrl == null || baseUrl.isEmpty()) ? API_URL : baseUrl, disableSslVerification);
57102
}
58103

59104
private interface LoginCall {

src/main/java/ai/deepcode/javaclient/core/DeepCodeParamsBase.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public abstract class DeepCodeParamsBase {
88
// Settings
99
private boolean isEnable;
1010
private String apiUrl;
11+
private boolean disableSslVerification;
1112
private boolean useLinter;
1213
private int minSeverity;
1314
private String sessionToken;
@@ -19,13 +20,15 @@ public abstract class DeepCodeParamsBase {
1920
protected DeepCodeParamsBase(
2021
boolean isEnable,
2122
String apiUrl,
23+
boolean disableSslVerification,
2224
boolean useLinter,
2325
int minSeverity,
2426
String sessionToken,
2527
String loginUrl,
2628
String ideProductName) {
2729
this.isEnable = isEnable;
2830
this.apiUrl = apiUrl;
31+
this.disableSslVerification = disableSslVerification;
2932
this.useLinter = useLinter;
3033
this.minSeverity = minSeverity;
3134
this.sessionToken = sessionToken;
@@ -78,11 +81,24 @@ public String getApiUrl() {
7881
}
7982

8083
public void setApiUrl(@NotNull String apiUrl) {
84+
setApiUrl(apiUrl, false);
85+
}
86+
87+
public void setApiUrl(@NotNull String apiUrl, boolean disableSslVerification) {
8188
if (apiUrl.isEmpty()) apiUrl = "https://www.deepcode.ai/";
8289
if (!apiUrl.endsWith("/")) apiUrl += "/";
8390
if (apiUrl.equals(this.apiUrl)) return;
8491
this.apiUrl = apiUrl;
85-
DeepCodeRestApi.setBaseUrl(apiUrl);
92+
this.disableSslVerification = disableSslVerification;
93+
DeepCodeRestApi.setBaseUrl(apiUrl, disableSslVerification);
94+
}
95+
96+
public boolean isDisableSslVerification() {
97+
return disableSslVerification;
98+
}
99+
100+
public void setDisableSslVerification(boolean disableSslVerification) {
101+
this.disableSslVerification = disableSslVerification;
86102
}
87103

88104
public boolean isEnable() {

0 commit comments

Comments
 (0)