|
| 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 | +} |
0 commit comments