|
6 | 6 | package org.elasticsearch.xpack.sql.jdbc;
|
7 | 7 |
|
8 | 8 | import org.elasticsearch.test.ESTestCase;
|
| 9 | +import org.elasticsearch.xpack.sql.client.SslConfig; |
9 | 10 |
|
| 11 | +import java.net.URI; |
| 12 | +import java.net.URISyntaxException; |
| 13 | +import java.sql.DriverManager; |
10 | 14 | import java.sql.SQLException;
|
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
11 | 17 | import java.util.Properties;
|
| 18 | +import java.util.stream.Collectors; |
12 | 19 |
|
13 | 20 | import static org.elasticsearch.xpack.sql.client.ConnectionConfiguration.CONNECT_TIMEOUT;
|
14 | 21 | import static org.elasticsearch.xpack.sql.client.ConnectionConfiguration.PAGE_TIMEOUT;
|
@@ -130,5 +137,153 @@ public void testTimoutOverride() throws Exception {
|
130 | 137 | assertThat(ci.pageTimeout(), equalTo(4L));
|
131 | 138 | }
|
132 | 139 |
|
133 |
| - |
| 140 | + public void testSSLPropertiesInUrl() throws Exception { |
| 141 | + Map<String, String> urlPropMap = sslProperties(); |
| 142 | + |
| 143 | + Properties allProps = new Properties(); |
| 144 | + allProps.putAll(urlPropMap); |
| 145 | + String sslUrlProps = urlPropMap.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&")); |
| 146 | + |
| 147 | + assertSslConfig(allProps, ci("jdbc:es://test?" + sslUrlProps.toString()).sslConfig()); |
| 148 | + } |
| 149 | + |
| 150 | + public void testSSLPropertiesInUrlAndProperties() throws Exception { |
| 151 | + Map<String, String> urlPropMap = new HashMap<>(4); |
| 152 | + urlPropMap.put("ssl", "false"); |
| 153 | + urlPropMap.put("ssl.protocol", "SSLv3"); |
| 154 | + urlPropMap.put("ssl.keystore.location", "/abc/xyz"); |
| 155 | + urlPropMap.put("ssl.keystore.pass", "mypass"); |
| 156 | + |
| 157 | + Map<String, String> propMap = new HashMap<>(4); |
| 158 | + propMap.put("ssl.keystore.type", "PKCS12"); |
| 159 | + propMap.put("ssl.truststore.location", "/foo/bar"); |
| 160 | + propMap.put("ssl.truststore.pass", "anotherpass"); |
| 161 | + propMap.put("ssl.truststore.type", "jks"); |
| 162 | + |
| 163 | + Properties props = new Properties(); |
| 164 | + props.putAll(propMap); |
| 165 | + String sslUrlProps = urlPropMap.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&")); |
| 166 | + |
| 167 | + Properties allProps = new Properties(); |
| 168 | + allProps.putAll(urlPropMap); |
| 169 | + allProps.putAll(propMap); |
| 170 | + assertSslConfig(allProps, JdbcConfiguration.create("jdbc:es://test?" + sslUrlProps.toString(), props, 0).sslConfig()); |
| 171 | + } |
| 172 | + |
| 173 | + public void testSSLPropertiesOverride() throws Exception { |
| 174 | + Map<String, String> urlPropMap = sslProperties(); |
| 175 | + Map<String, String> propMap = new HashMap<>(8); |
| 176 | + propMap.put("ssl", "false"); |
| 177 | + propMap.put("ssl.protocol", "TLS"); |
| 178 | + propMap.put("ssl.keystore.location", "/xyz"); |
| 179 | + propMap.put("ssl.keystore.pass", "different_mypass"); |
| 180 | + propMap.put("ssl.keystore.type", "JKS"); |
| 181 | + propMap.put("ssl.truststore.location", "/baz"); |
| 182 | + propMap.put("ssl.truststore.pass", "different_anotherpass"); |
| 183 | + propMap.put("ssl.truststore.type", "PKCS11"); |
| 184 | + |
| 185 | + Properties props = new Properties(); |
| 186 | + props.putAll(propMap); |
| 187 | + String sslUrlProps = urlPropMap.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&")); |
| 188 | + assertSslConfig(props, JdbcConfiguration.create("jdbc:es://test?" + sslUrlProps.toString(), props, 0).sslConfig()); |
| 189 | + } |
| 190 | + |
| 191 | + public void testDriverConfigurationWithSSLInURL() { |
| 192 | + Map<String, String> urlPropMap = sslProperties(); |
| 193 | + |
| 194 | + Properties allProps = new Properties(); |
| 195 | + allProps.putAll(urlPropMap); |
| 196 | + String sslUrlProps = urlPropMap.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&")); |
| 197 | + |
| 198 | + try { |
| 199 | + DriverManager.getDriver("jdbc:es://test?" + sslUrlProps); |
| 200 | + } catch (SQLException sqle) { |
| 201 | + fail("Driver registration should have been successful. Error: " + sqle); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + public void testDataSourceConfigurationWithSSLInURL() throws SQLException, URISyntaxException { |
| 206 | + Map<String, String> urlPropMap = sslProperties(); |
| 207 | + |
| 208 | + Properties allProps = new Properties(); |
| 209 | + allProps.putAll(urlPropMap); |
| 210 | + String sslUrlProps = urlPropMap.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&")); |
| 211 | + |
| 212 | + EsDataSource dataSource = new EsDataSource(); |
| 213 | + String address = "jdbc:es://test?" + sslUrlProps; |
| 214 | + dataSource.setUrl(address); |
| 215 | + JdbcConnection connection = null; |
| 216 | + |
| 217 | + try { |
| 218 | + connection = (JdbcConnection) dataSource.getConnection(); |
| 219 | + } catch (SQLException sqle) { |
| 220 | + fail("Connection creation should have been successful. Error: " + sqle); |
| 221 | + } |
| 222 | + |
| 223 | + assertEquals(address, connection.getURL()); |
| 224 | + assertSslConfig(allProps, connection.cfg.sslConfig()); |
| 225 | + } |
| 226 | + |
| 227 | + public void testTyposInSslConfigInUrl(){ |
| 228 | + assertJdbcSqlExceptionFromUrl("ssl.protocl", "ssl.protocol"); |
| 229 | + assertJdbcSqlExceptionFromUrl("sssl", "ssl"); |
| 230 | + assertJdbcSqlExceptionFromUrl("ssl.keystore.lction", "ssl.keystore.location"); |
| 231 | + assertJdbcSqlExceptionFromUrl("ssl.keystore.pss", "ssl.keystore.pass"); |
| 232 | + assertJdbcSqlExceptionFromUrl("ssl.keystore.typ", "ssl.keystore.type"); |
| 233 | + assertJdbcSqlExceptionFromUrl("ssl.trustsore.location", "ssl.truststore.location"); |
| 234 | + assertJdbcSqlExceptionFromUrl("ssl.tuststore.pass", "ssl.truststore.pass"); |
| 235 | + assertJdbcSqlExceptionFromUrl("ssl.ruststore.type", "ssl.truststore.type"); |
| 236 | + } |
| 237 | + |
| 238 | + public void testTyposInSslConfigInProperties() { |
| 239 | + assertJdbcSqlExceptionFromProperties("ssl.protocl", "ssl.protocol"); |
| 240 | + assertJdbcSqlExceptionFromProperties("sssl", "ssl"); |
| 241 | + assertJdbcSqlExceptionFromProperties("ssl.keystore.lction", "ssl.keystore.location"); |
| 242 | + assertJdbcSqlExceptionFromProperties("ssl.keystore.pss", "ssl.keystore.pass"); |
| 243 | + assertJdbcSqlExceptionFromProperties("ssl.keystore.typ", "ssl.keystore.type"); |
| 244 | + assertJdbcSqlExceptionFromProperties("ssl.trustsore.location", "ssl.truststore.location"); |
| 245 | + assertJdbcSqlExceptionFromProperties("ssl.tuststore.pass", "ssl.truststore.pass"); |
| 246 | + assertJdbcSqlExceptionFromProperties("ssl.ruststore.type", "ssl.truststore.type"); |
| 247 | + } |
| 248 | + |
| 249 | + private Map<String, String> sslProperties() { |
| 250 | + Map<String, String> sslPropertiesMap = new HashMap<>(8); |
| 251 | + // always using "false" so that the SSLContext doesn't actually start verifying the keystore and trustore |
| 252 | + // locations, as we don't have file permissions to access them. |
| 253 | + sslPropertiesMap.put("ssl", "false"); |
| 254 | + sslPropertiesMap.put("ssl.protocol", "SSLv3"); |
| 255 | + sslPropertiesMap.put("ssl.keystore.location", "/abc/xyz"); |
| 256 | + sslPropertiesMap.put("ssl.keystore.pass", "mypass"); |
| 257 | + sslPropertiesMap.put("ssl.keystore.type", "PKCS12"); |
| 258 | + sslPropertiesMap.put("ssl.truststore.location", "/foo/bar"); |
| 259 | + sslPropertiesMap.put("ssl.truststore.pass", "anotherpass"); |
| 260 | + sslPropertiesMap.put("ssl.truststore.type", "jks"); |
| 261 | + |
| 262 | + return sslPropertiesMap; |
| 263 | + } |
| 264 | + |
| 265 | + private void assertSslConfig(Properties allProperties, SslConfig sslConfig) throws URISyntaxException { |
| 266 | + // because SslConfig doesn't expose its internal properties (and it shouldn't), |
| 267 | + // we compare a newly created SslConfig with the one from the JdbcConfiguration with the equals() method |
| 268 | + SslConfig mockSslConfig = new SslConfig(allProperties, new URI("http://test:9200/")); |
| 269 | + assertEquals(mockSslConfig, sslConfig); |
| 270 | + } |
| 271 | + |
| 272 | + private void assertJdbcSqlExceptionFromUrl(String wrongSetting, String correctSetting) { |
| 273 | + String url = "jdbc:es://test?" + wrongSetting + "=foo"; |
| 274 | + assertJdbcSqlException(wrongSetting, correctSetting, url, null); |
| 275 | + } |
| 276 | + |
| 277 | + private void assertJdbcSqlExceptionFromProperties(String wrongSetting, String correctSetting) { |
| 278 | + String url = "jdbc:es://test"; |
| 279 | + Properties props = new Properties(); |
| 280 | + props.put(wrongSetting, correctSetting); |
| 281 | + assertJdbcSqlException(wrongSetting, correctSetting, url, props); |
| 282 | + } |
| 283 | + |
| 284 | + private void assertJdbcSqlException(String wrongSetting, String correctSetting, String url, Properties props) { |
| 285 | + JdbcSQLException ex = expectThrows(JdbcSQLException.class, |
| 286 | + () -> JdbcConfiguration.create(url, props, 0)); |
| 287 | + assertEquals("Unknown parameter [" + wrongSetting + "] ; did you mean [" + correctSetting + "]", ex.getMessage()); |
| 288 | + } |
134 | 289 | }
|
0 commit comments