Skip to content

Commit 16fc063

Browse files
authored
Merge pull request #993 from microsoft/default_tls1.2
Use tls1.2 by default
2 parents 0b5fbca + 109fa9f commit 16fc063

File tree

5 files changed

+117
-13
lines changed

5 files changed

+117
-13
lines changed

core/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ dependencies {
122122
compile ([group: 'io.grpc', name: 'grpc-protobuf', version: '1.16.1'])
123123
runtime ([group: 'io.grpc', name: 'grpc-netty-shaded', version: '1.16.1'])
124124
// update transitive dependency version
125-
runtime ([group: 'com.google.guava', name: 'guava', version: '27.1-android'])
125+
compile ([group: 'com.google.guava', name: 'guava', version: '27.1-android'])
126126
testCompile group: 'org.hamcrest', name:'hamcrest-core', version:'1.3'
127127
testCompile group: 'org.hamcrest', name:'hamcrest-library', version:'1.3'
128128
testCompile group: 'org.mockito', name: 'mockito-core', version: '1.10.19'

core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/ApacheSender43.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,24 @@
2222
package com.microsoft.applicationinsights.internal.channel.common;
2323

2424
import java.io.IOException;
25-
import java.util.concurrent.ExecutorService;
26-
import java.util.concurrent.Executors;
27-
import java.util.concurrent.LinkedBlockingDeque;
28-
import java.util.concurrent.ThreadPoolExecutor;
29-
import java.util.concurrent.TimeUnit;
3025
import java.util.concurrent.atomic.AtomicReference;
3126

3227
import com.microsoft.applicationinsights.internal.logger.InternalLogger;
3328

34-
import com.microsoft.applicationinsights.internal.shutdown.SDKShutdownActivity;
35-
import com.microsoft.applicationinsights.internal.util.ThreadPoolUtils;
29+
import com.microsoft.applicationinsights.internal.util.SSLOptionsUtil;
3630
import org.apache.http.HttpResponse;
3731
import org.apache.http.client.HttpClient;
3832
import org.apache.http.client.config.RequestConfig;
3933
import org.apache.http.client.methods.CloseableHttpResponse;
4034
import org.apache.http.client.methods.HttpPost;
35+
import org.apache.http.config.RegistryBuilder;
36+
import org.apache.http.conn.socket.ConnectionSocketFactory;
37+
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
38+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
4139
import org.apache.http.impl.client.CloseableHttpClient;
4240
import org.apache.http.impl.client.HttpClients;
4341
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
42+
import org.apache.http.ssl.SSLContexts;
4443

4544
/**
4645
* Created by gupele on 6/4/2015.
@@ -51,16 +50,18 @@ final class ApacheSender43 implements ApacheSender {
5150

5251
static ApacheSender43 create() {
5352
final ApacheSender43 sender = new ApacheSender43();
53+
final String[] allowedProtocols = SSLOptionsUtil.getAllowedProtocols();
5454
Thread initThread = new Thread(
5555
new Runnable() {
56-
5756
@Override
5857
public void run() {
59-
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
58+
final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create()
59+
.register("https", new SSLConnectionSocketFactory(SSLContexts.createDefault(), allowedProtocols, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()))
60+
.register("http", PlainConnectionSocketFactory.getSocketFactory())
61+
.build());
6062
cm.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
6163
cm.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
62-
63-
sender.httpClientRef.compareAndSet(null, HttpClients.custom()
64+
sender.httpClientRef.compareAndSet(null, HttpClients.custom()
6465
.setConnectionManager(cm)
6566
.useSystemProperties()
6667
.build());
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.microsoft.applicationinsights.internal.util;
2+
3+
public class NoSupportedProtocolsException extends RuntimeException {
4+
public NoSupportedProtocolsException() {
5+
}
6+
7+
public NoSupportedProtocolsException(String message) {
8+
super(message);
9+
}
10+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.microsoft.applicationinsights.internal.util;
2+
3+
import com.google.common.base.Splitter;
4+
import com.google.common.base.Strings;
5+
import com.microsoft.applicationinsights.internal.logger.InternalLogger;
6+
import org.apache.commons.lang3.exception.ExceptionUtils;
7+
8+
import javax.net.ssl.SSLContext;
9+
import java.security.NoSuchAlgorithmException;
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
14+
public class SSLOptionsUtil {
15+
16+
private SSLOptionsUtil() {}
17+
18+
public static final String APPLICATION_INSIGHTS_SSL_PROTOCOLS_PROPERTY = "applicationinsights.ssl.protocols";
19+
20+
private static final String[] DEFAULT_SUPPORTED_PROTOCOLS;
21+
private static final String[] DEFAULT_PROTOCOLS = new String[] {"TLSv1.3", "TLSv1.2"};
22+
23+
static {
24+
DEFAULT_SUPPORTED_PROTOCOLS = filterSupportedProtocols(Arrays.asList(DEFAULT_PROTOCOLS), false);
25+
if (DEFAULT_SUPPORTED_PROTOCOLS.length == 0 && InternalLogger.INSTANCE.isErrorEnabled()) {
26+
InternalLogger.INSTANCE.error("Default protocols are not supported in this JVM: %s. System property '%s' can be used to configure supported SSL protocols.",
27+
Arrays.toString(DEFAULT_PROTOCOLS), APPLICATION_INSIGHTS_SSL_PROTOCOLS_PROPERTY);
28+
}
29+
}
30+
31+
private static String[] filterSupportedProtocols(Iterable<String> defaultValue, boolean reportErrors) {
32+
List<String> supported = new ArrayList<>();
33+
for (String protocol : defaultValue) {
34+
try {
35+
SSLContext.getInstance(protocol);
36+
supported.add(protocol);
37+
} catch (NoSuchAlgorithmException e) {
38+
if (InternalLogger.INSTANCE.isErrorEnabled() && reportErrors) {
39+
InternalLogger.INSTANCE.error("Could not find protocol '%s': %s", protocol, ExceptionUtils.getStackTrace(e));
40+
}
41+
}
42+
}
43+
return supported.toArray(new String[0]);
44+
}
45+
46+
/**
47+
* <p>Finds the list of supported SSL/TLS protocols. If custom protocols are specified with the system property {@value #APPLICATION_INSIGHTS_SSL_PROTOCOLS_PROPERTY}, this overrides the defaults.
48+
* An error will be logged if the property contains no supported protocols.</p>
49+
*
50+
* <p>If no supported protocols are specified, the defaults are used (see static constructor). If no default protocols are available on this JVM, an error is logged.</p>
51+
*
52+
* @return An array of supported protocols. If there are none found, an empty array.
53+
* @throws NoSupportedProtocolsException If the defaults are to be used and none of the defaults are supported by this JVM
54+
*/
55+
public static String[] getAllowedProtocols() {
56+
final String rawProp = System.getProperty(APPLICATION_INSIGHTS_SSL_PROTOCOLS_PROPERTY);
57+
if (rawProp == null) {
58+
return defaultSupportedProtocols();
59+
}
60+
61+
if (Strings.isNullOrEmpty(rawProp)) {
62+
if (InternalLogger.INSTANCE.isWarnEnabled()) {
63+
InternalLogger.INSTANCE.warn("%s specifies no protocols; using defaults: %s", APPLICATION_INSIGHTS_SSL_PROTOCOLS_PROPERTY, Arrays.toString(DEFAULT_SUPPORTED_PROTOCOLS));
64+
}
65+
return defaultSupportedProtocols();
66+
}
67+
68+
String[] customProtocols = filterSupportedProtocols(Splitter.on(',').trimResults().omitEmptyStrings().split(rawProp), true);
69+
if (customProtocols.length == 0) {
70+
if (InternalLogger.INSTANCE.isErrorEnabled()) {
71+
InternalLogger.INSTANCE.error("%s contained no supported protocols: '%s'; using default: %s", APPLICATION_INSIGHTS_SSL_PROTOCOLS_PROPERTY, rawProp, Arrays.toString(DEFAULT_SUPPORTED_PROTOCOLS));
72+
}
73+
return defaultSupportedProtocols();
74+
}
75+
76+
if (InternalLogger.INSTANCE.isInfoEnabled()) {
77+
InternalLogger.INSTANCE.info("Found %s='%s'; HTTP client will allow only these protocols", APPLICATION_INSIGHTS_SSL_PROTOCOLS_PROPERTY, Arrays.toString(customProtocols));
78+
}
79+
return customProtocols;
80+
}
81+
82+
private static String[] defaultSupportedProtocols() {
83+
if (DEFAULT_SUPPORTED_PROTOCOLS.length == 0) {
84+
throw new NoSupportedProtocolsException(String.format("None of the default TLS protocols are supported by this JVM: %s. Use the system property '%s' to override.", Arrays.toString(DEFAULT_PROTOCOLS), APPLICATION_INSIGHTS_SSL_PROTOCOLS_PROPERTY));
85+
}
86+
return DEFAULT_SUPPORTED_PROTOCOLS;
87+
}
88+
89+
}

web/src/main/java/com/microsoft/applicationinsights/web/internal/correlation/CdsProfileFetcher.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
import com.microsoft.applicationinsights.internal.logger.InternalLogger;
2525
import com.microsoft.applicationinsights.internal.shutdown.SDKShutdownActivity;
2626
import com.microsoft.applicationinsights.internal.util.PeriodicTaskPool;
27+
import com.microsoft.applicationinsights.internal.util.SSLOptionsUtil;
2728
import org.apache.http.HttpResponse;
2829
import org.apache.http.ParseException;
2930
import org.apache.http.client.config.RequestConfig;
3031
import org.apache.http.client.methods.HttpGet;
3132
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
3233
import org.apache.http.impl.nio.client.HttpAsyncClients;
34+
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
35+
import org.apache.http.ssl.SSLContexts;
3336
import org.apache.http.util.EntityUtils;
3437

3538
import java.io.IOException;
@@ -39,7 +42,6 @@
3942
import java.util.concurrent.ConcurrentMap;
4043
import java.util.concurrent.ExecutionException;
4144
import java.util.concurrent.Future;
42-
import java.util.concurrent.ScheduledFuture;
4345
import java.util.concurrent.TimeUnit;
4446

4547
public class CdsProfileFetcher implements AppProfileFetcher {
@@ -72,8 +74,10 @@ public CdsProfileFetcher(CdsRetryPolicy retryPolicy) {
7274
.setConnectionRequestTimeout(5000)
7375
.build();
7476

77+
final String[] allowedProtocols = SSLOptionsUtil.getAllowedProtocols();
7578
setHttpClient(HttpAsyncClients.custom()
7679
.setDefaultRequestConfig(requestConfig)
80+
.setSSLStrategy(new SSLIOSessionStrategy(SSLContexts.createDefault(), allowedProtocols, null, SSLIOSessionStrategy.getDefaultHostnameVerifier()))
7781
.useSystemProperties()
7882
.build());
7983

0 commit comments

Comments
 (0)