Skip to content

Commit c950001

Browse files
authored
Support for specify tls versions and cipher suites (#653)
1 parent 95d7cfe commit c950001

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

client-runtime/src/main/java/com/microsoft/rest/RestClient.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,22 @@
2020
import com.microsoft.rest.retry.RetryHandler;
2121
import com.microsoft.rest.retry.RetryStrategy;
2222
import okhttp3.Authenticator;
23+
import okhttp3.CipherSuite;
2324
import okhttp3.ConnectionPool;
25+
import okhttp3.ConnectionSpec;
2426
import okhttp3.Dispatcher;
2527
import okhttp3.Interceptor;
2628
import okhttp3.JavaNetCookieJar;
2729
import okhttp3.OkHttpClient;
30+
import okhttp3.TlsVersion;
2831
import okio.AsyncTimeout;
2932
import retrofit2.Retrofit;
3033
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
3134

3235
import java.net.CookieManager;
3336
import java.net.CookiePolicy;
3437
import java.net.Proxy;
38+
import java.util.Arrays;
3539
import java.util.concurrent.Executor;
3640
import java.util.concurrent.TimeUnit;
3741

@@ -181,6 +185,10 @@ public static class Builder {
181185
private boolean useHttpClientThreadPool;
182186
/** The connection pool in use for OkHttp. */
183187
private ConnectionPool connectionPool;
188+
/** The tls versions in use for OkHttp. */
189+
private TlsVersion[] tlsVersions;
190+
/** The cipher suites in use for OkHttp. */
191+
private CipherSuite[] cipherSuites;
184192

185193
/**
186194
* Creates an instance of the builder with a base URL to the service.
@@ -199,6 +207,8 @@ private Builder(final RestClient restClient) {
199207
this.responseBuilderFactory = restClient.builder.responseBuilderFactory;
200208
this.serializerAdapter = restClient.builder.serializerAdapter;
201209
this.useHttpClientThreadPool = restClient.builder.useHttpClientThreadPool;
210+
this.tlsVersions = restClient.builder.tlsVersions;
211+
this.cipherSuites = restClient.builder.cipherSuites;
202212
if (restClient.builder.credentials != null) {
203213
this.credentials = restClient.builder.credentials;
204214
}
@@ -479,6 +489,26 @@ public Builder withRetryStrategy(RetryStrategy strategy) {
479489
return this;
480490
}
481491

492+
/**
493+
* Sets tls versions for OkHttp client.
494+
* @param tlsVersions the tls versions to use
495+
* @return the builder itself for chaining
496+
*/
497+
public Builder withTlsVersions(TlsVersion... tlsVersions) {
498+
this.tlsVersions = tlsVersions;
499+
return this;
500+
}
501+
502+
/**
503+
* Sets cipher suites for OkHttp client.
504+
* @param cipherSuites the cipher suites to use
505+
* @return the builder itself for chaining
506+
*/
507+
public Builder withCipherSuites(CipherSuite... cipherSuites) {
508+
this.cipherSuites = cipherSuites;
509+
return this;
510+
}
511+
482512
/**
483513
* Build a RestClient with all the current configurations.
484514
*
@@ -525,6 +555,17 @@ public RestClient build() {
525555
httpClientBuilder = httpClientBuilder.dispatcher(dispatcher);
526556
}
527557

558+
if (this.tlsVersions != null || this.cipherSuites != null) {
559+
ConnectionSpec.Builder connectionSpecBuilder = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS);
560+
if (this.tlsVersions != null && this.tlsVersions.length > 0) {
561+
connectionSpecBuilder.tlsVersions(this.tlsVersions);
562+
}
563+
if (this.cipherSuites != null && this.cipherSuites.length > 0) {
564+
connectionSpecBuilder.cipherSuites(this.cipherSuites);
565+
}
566+
this.httpClientBuilder.connectionSpecs(Arrays.asList(connectionSpecBuilder.build(), ConnectionSpec.CLEARTEXT));
567+
}
568+
528569
OkHttpClient httpClient = httpClientBuilder
529570
.addInterceptor(userAgentInterceptor)
530571
.addInterceptor(customHeadersInterceptor)

0 commit comments

Comments
 (0)