Skip to content

Commit

Permalink
Bugfix for TLS version error (protocol not supported error)
Browse files Browse the repository at this point in the history
    Added a method to the Analytics Builder class forceTlsVersion1() - name subject to change
    Usage -
            Analytics.Builder builder = Analytics.builder("someKey");
            Analytics analytics = builder.userAgent("someUserAgent")
            .client(someClient)
            .endpoint("some endpoint")
            .anotherAlreadyExistingMethod(someParam)
            .forceTlsVersion1()
            .build();

    this new method forces the httpClient to be created using  ConnectionSpec of MODERN_TLS and including Tls Versions: TLS_1_0, TLS_1_1, TLS_1_2, TLS_1_3

    Also added a simple test case
  • Loading branch information
Jorge Rodriguera committed Dec 8, 2021
1 parent a61788f commit ca95853
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
27 changes: 23 additions & 4 deletions analytics/src/main/java/com/segment/analytics/Analytics.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
import com.segment.analytics.internal.AnalyticsVersion;
import com.segment.analytics.messages.Message;
import com.segment.analytics.messages.MessageBuilder;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import okhttp3.HttpUrl;
import okhttp3.ConnectionSpec;
import okhttp3.OkHttpClient;
import okhttp3.HttpUrl;
import okhttp3.TlsVersion;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
Expand Down Expand Up @@ -145,6 +148,7 @@ public static class Builder {
private long flushIntervalInMillis;
private List<Callback> callbacks;
private int queueCapacity;
private boolean forceTlsV1 = false;

Builder(String writeKey) {
if (writeKey == null || writeKey.trim().length() == 0) {
Expand Down Expand Up @@ -329,6 +333,12 @@ public Builder plugin(Plugin plugin) {
return this;
}

/** Specify if need TlsV1 */
public Builder forceTlsVersion1() {
forceTlsV1 = true;
return this;
}

/** Create a {@link Analytics} client. */
public Analytics build() {
Gson gson =
Expand Down Expand Up @@ -400,13 +410,22 @@ public void log(String message) {

interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);

client =
OkHttpClient.Builder builder =
client
.newBuilder()
.addInterceptor(new AnalyticsRequestInterceptor(writeKey, userAgent))
.addInterceptor(interceptor)
.addInterceptor(interceptor);

if (forceTlsV1) {
ConnectionSpec connectionSpec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_0, TlsVersion.TLS_1_1, TlsVersion.TLS_1_2, TlsVersion.TLS_1_3)
.build();

builder = builder.connectionSpecs(Arrays.asList(connectionSpec));
}

client = builder.build();

Retrofit restAdapter =
new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -378,6 +377,12 @@ public void buildsWithValidCallback() {
assertThat(analytics).isNotNull();
}

@Test
public void buildsWithForceTlsV1() {
Analytics analytics = builder.forceTlsVersion1().build();
assertThat(analytics).isNotNull();
}

@Test
public void multipleCallbacks() {
Analytics analytics =
Expand Down

0 comments on commit ca95853

Please sign in to comment.