|
1 | 1 | package com.clickhouse.client.api.internal; |
2 | 2 |
|
| 3 | +import com.clickhouse.client.api.ClientConfigProperties; |
| 4 | +import com.clickhouse.client.api.enums.SSLMode; |
3 | 5 | import com.clickhouse.client.api.internal.HttpAPIClientHelper.CustomSSLConnectionFactory; |
| 6 | +import net.jpountz.lz4.LZ4Factory; |
4 | 7 | import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; |
5 | 8 | import org.mockito.ArgumentCaptor; |
| 9 | +import org.mockito.MockedConstruction; |
6 | 10 | import org.testng.annotations.Test; |
7 | 11 |
|
8 | 12 | import javax.net.ssl.SNIHostName; |
|
11 | 15 | import javax.net.ssl.SSLParameters; |
12 | 16 | import javax.net.ssl.SSLSocket; |
13 | 17 | import java.lang.reflect.Field; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.HashMap; |
14 | 22 | import java.util.List; |
| 23 | +import java.util.Map; |
15 | 24 |
|
16 | 25 | import static org.mockito.ArgumentMatchers.any; |
17 | 26 | import static org.mockito.Mockito.mock; |
| 27 | +import static org.mockito.Mockito.mockConstruction; |
18 | 28 | import static org.mockito.Mockito.never; |
19 | 29 | import static org.mockito.Mockito.verify; |
20 | 30 | import static org.mockito.Mockito.when; |
21 | 31 | import static org.testng.Assert.assertEquals; |
| 32 | +import static org.testng.Assert.assertNotNull; |
22 | 33 | import static org.testng.Assert.assertNull; |
23 | 34 |
|
24 | 35 | public class HttpAPIClientHelperTest { |
@@ -85,6 +96,148 @@ public void testBlankSniLeavesSocketParametersUntouched() throws Exception { |
85 | 96 | verify(socket, never()).setSSLParameters(any()); |
86 | 97 | } |
87 | 98 |
|
| 99 | + /** |
| 100 | + * When cipher suites are configured in STRICT mode (no SNI), {@code createHttpClient} must build the |
| 101 | + * cipher-aware {@link CustomSSLConnectionFactory}, forward the configured suites to it, and pass a |
| 102 | + * {@code null} hostname verifier so the base factory keeps its default (verifying) behaviour - i.e. |
| 103 | + * restricting cipher suites must not silently disable hostname verification. |
| 104 | + */ |
| 105 | + @Test |
| 106 | + public void testCreateHttpClientStrictWithCipherSuitesForwardsSuitesAndKeepsHostnameVerification() { |
| 107 | + Map<String, Object> config = new HashMap<>(); |
| 108 | + config.put(ClientConfigProperties.SSL_CIPHER_SUITES.getKey(), |
| 109 | + Arrays.asList("TLS_AES_256_GCM_SHA384", "TLS_AES_128_GCM_SHA256")); |
| 110 | + |
| 111 | + List<List<?>> calls = captureCustomFactoryConstruction(config); |
| 112 | + |
| 113 | + assertEquals(calls.size(), 1, "STRICT + cipher suites must build the cipher-aware custom factory"); |
| 114 | + List<?> args = calls.get(0); // (socketSNI, sslContext, hostnameVerifier, enabledCipherSuites) |
| 115 | + assertNull(args.get(2), "STRICT must keep default hostname verification: the verifier passed to the " |
| 116 | + + "factory must be null so the base factory verifies hostnames"); |
| 117 | + assertEquals((String[]) args.get(3), new String[]{"TLS_AES_256_GCM_SHA384", "TLS_AES_128_GCM_SHA256"}, |
| 118 | + "the configured cipher suites must be forwarded to the connection socket factory"); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * TRUST mode opts out of hostname verification, so the factory must receive a permissive (non-null) |
| 123 | + * verifier; with no cipher suites configured the factory must keep the JVM defaults (no restriction). |
| 124 | + */ |
| 125 | + @Test |
| 126 | + public void testCreateHttpClientTrustModeInstallsPermissiveVerifierWithoutCipherRestriction() { |
| 127 | + Map<String, Object> config = new HashMap<>(); |
| 128 | + config.put(ClientConfigProperties.SSL_MODE.getKey(), SSLMode.TRUST); |
| 129 | + |
| 130 | + List<List<?>> calls = captureCustomFactoryConstruction(config); |
| 131 | + |
| 132 | + assertEquals(calls.size(), 1, "TRUST mode must build the custom factory to skip hostname verification"); |
| 133 | + List<?> args = calls.get(0); |
| 134 | + assertNotNull(args.get(2), "TRUST mode must install a permissive hostname verifier"); |
| 135 | + assertNull(args.get(3), "TRUST mode without configured cipher suites must not restrict cipher suites"); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * A custom SNI host will not match the server certificate, so hostname verification is skipped (a |
| 140 | + * permissive verifier is installed) and the configured SNI is passed through to the factory. |
| 141 | + */ |
| 142 | + @Test |
| 143 | + public void testCreateHttpClientCustomSniInstallsPermissiveVerifier() { |
| 144 | + Map<String, Object> config = new HashMap<>(); |
| 145 | + config.put(ClientConfigProperties.SSL_SOCKET_SNI.getKey(), "sni.example.com"); |
| 146 | + |
| 147 | + List<List<?>> calls = captureCustomFactoryConstruction(config); |
| 148 | + |
| 149 | + assertEquals(calls.size(), 1, "a custom SNI must build the custom factory"); |
| 150 | + List<?> args = calls.get(0); |
| 151 | + assertEquals(args.get(0), "sni.example.com", "the configured SNI must be passed to the factory"); |
| 152 | + assertNotNull(args.get(2), "a custom SNI host won't match the certificate, so a permissive verifier " |
| 153 | + + "is installed"); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Contrast case: the default STRICT path with no SNI and no cipher restriction must be unchanged - it |
| 158 | + * must NOT build the custom factory, so the plain verifying {@link SSLConnectionSocketFactory} is used. |
| 159 | + */ |
| 160 | + @Test |
| 161 | + public void testCreateHttpClientStrictWithoutSniOrCiphersUsesPlainFactory() { |
| 162 | + List<List<?>> calls = captureCustomFactoryConstruction(new HashMap<>()); |
| 163 | + |
| 164 | + assertEquals(calls.size(), 0, "default STRICT with no SNI and no cipher suites must keep using the " |
| 165 | + + "plain SSLConnectionSocketFactory (unchanged behaviour)"); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * VERIFY_CA validates the certificate chain but skips hostname verification (the connection hostname may |
| 170 | + * legitimately differ), so - like TRUST - it must install a permissive verifier. This pins the second |
| 171 | + * mode that opts out of hostname verification, distinct from the TRUST path. |
| 172 | + */ |
| 173 | + @Test |
| 174 | + public void testCreateHttpClientVerifyCaModeInstallsPermissiveVerifier() { |
| 175 | + Map<String, Object> config = new HashMap<>(); |
| 176 | + config.put(ClientConfigProperties.SSL_MODE.getKey(), SSLMode.VERIFY_CA); |
| 177 | + |
| 178 | + List<List<?>> calls = captureCustomFactoryConstruction(config); |
| 179 | + |
| 180 | + assertEquals(calls.size(), 1, "VERIFY_CA must build the custom factory to skip hostname verification"); |
| 181 | + List<?> args = calls.get(0); |
| 182 | + assertNotNull(args.get(2), "VERIFY_CA must install a permissive hostname verifier"); |
| 183 | + assertNull(args.get(3), "VERIFY_CA without configured cipher suites must not restrict cipher suites"); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * SNI and cipher suites are independent concerns and must combine: a custom SNI still installs the |
| 188 | + * permissive verifier while the configured cipher suites are forwarded to the same factory - i.e. cipher |
| 189 | + * forwarding is not tied to the STRICT/cipher-triggered path. |
| 190 | + */ |
| 191 | + @Test |
| 192 | + public void testCreateHttpClientSniAndCipherSuitesCombine() { |
| 193 | + Map<String, Object> config = new HashMap<>(); |
| 194 | + config.put(ClientConfigProperties.SSL_SOCKET_SNI.getKey(), "sni.example.com"); |
| 195 | + config.put(ClientConfigProperties.SSL_CIPHER_SUITES.getKey(), |
| 196 | + Arrays.asList("TLS_AES_256_GCM_SHA384")); |
| 197 | + |
| 198 | + List<List<?>> calls = captureCustomFactoryConstruction(config); |
| 199 | + |
| 200 | + assertEquals(calls.size(), 1, "SNI + cipher suites must build the custom factory"); |
| 201 | + List<?> args = calls.get(0); |
| 202 | + assertEquals(args.get(0), "sni.example.com", "the configured SNI must be passed to the factory"); |
| 203 | + assertNotNull(args.get(2), "a custom SNI installs a permissive verifier even when cipher suites are set"); |
| 204 | + assertEquals((String[]) args.get(3), new String[]{"TLS_AES_256_GCM_SHA384"}, |
| 205 | + "the configured cipher suites must still be forwarded when SNI is also set"); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Boundary case: an empty cipher-suite list must be treated as "no restriction" (JVM defaults), exactly |
| 210 | + * like an unset value - it must NOT be turned into an empty cipher array, which would enable zero suites |
| 211 | + * and make every handshake fail. STRICT with no SNI therefore keeps using the plain factory. |
| 212 | + */ |
| 213 | + @Test |
| 214 | + public void testCreateHttpClientEmptyCipherSuitesTreatedAsNoRestriction() { |
| 215 | + Map<String, Object> config = new HashMap<>(); |
| 216 | + config.put(ClientConfigProperties.SSL_CIPHER_SUITES.getKey(), Collections.emptyList()); |
| 217 | + |
| 218 | + List<List<?>> calls = captureCustomFactoryConstruction(config); |
| 219 | + |
| 220 | + assertEquals(calls.size(), 0, "an empty cipher-suite list must be treated as no restriction (JVM " |
| 221 | + + "defaults), so the plain SSLConnectionSocketFactory is used"); |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Builds an {@link HttpAPIClientHelper} and invokes {@link HttpAPIClientHelper#createHttpClient} with SSL |
| 226 | + * enabled while intercepting every {@link CustomSSLConnectionFactory} construction, returning the |
| 227 | + * constructor arguments of each construction (empty when the plain factory branch is taken instead). |
| 228 | + */ |
| 229 | + private static List<List<?>> captureCustomFactoryConstruction(Map<String, Object> sslConfig) { |
| 230 | + HttpAPIClientHelper helper = new HttpAPIClientHelper(new HashMap<>(), null, false, |
| 231 | + LZ4Factory.fastestJavaInstance()); |
| 232 | + List<List<?>> constructorArgs = new ArrayList<>(); |
| 233 | + try (MockedConstruction<CustomSSLConnectionFactory> mocked = mockConstruction( |
| 234 | + CustomSSLConnectionFactory.class, |
| 235 | + (mock, context) -> constructorArgs.add(context.arguments()))) { |
| 236 | + helper.createHttpClient(true, sslConfig); |
| 237 | + } |
| 238 | + return constructorArgs; |
| 239 | + } |
| 240 | + |
88 | 241 | private static String[] baseSupportedCipherSuites(CustomSSLConnectionFactory factory) throws Exception { |
89 | 242 | Field field = SSLConnectionSocketFactory.class.getDeclaredField("supportedCipherSuites"); |
90 | 243 | field.setAccessible(true); |
|
0 commit comments