Skip to content

Commit

Permalink
Mute org.elasticsearch.xpack.sql.jdbc.JdbcConfigurationTests.testDriv…
Browse files Browse the repository at this point in the history
…erConfigurationWithSSLInURL

tracked in elastic#41557
  • Loading branch information
alpar-t committed Oct 5, 2019
1 parent 07f4ca7 commit 3d4a7d0
Showing 1 changed file with 36 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package org.elasticsearch.xpack.sql.jdbc;

import org.apache.lucene.util.Constants;
import org.elasticsearch.SpecialPermission;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.sql.client.SslConfig;
Expand Down Expand Up @@ -91,17 +92,17 @@ public void testHttpWithSSLEnabledFromProperty() throws Exception {
JdbcConfiguration ci = ci("jdbc:es://test?ssl=true");
assertThat(ci.baseUri().toString(), is("https://test:9200/"));
}

public void testHttpWithSSLEnabledFromPropertyAndDisabledFromProtocol() throws Exception {
JdbcConfiguration ci = ci("jdbc:es://http://test?ssl=true");
assertThat(ci.baseUri().toString(), is("https://test:9200/"));
}

public void testHttpWithSSLEnabledFromProtocol() throws Exception {
JdbcConfiguration ci = ci("jdbc:es://https://test:9200");
assertThat(ci.baseUri().toString(), is("https://test:9200/"));
}

public void testHttpWithSSLEnabledFromProtocolAndProperty() throws Exception {
JdbcConfiguration ci = ci("jdbc:es://https://test:9200?ssl=true");
assertThat(ci.baseUri().toString(), is("https://test:9200/"));
Expand All @@ -111,49 +112,49 @@ public void testHttpWithSSLDisabledFromProperty() throws Exception {
JdbcConfiguration ci = ci("jdbc:es://test?ssl=false");
assertThat(ci.baseUri().toString(), is("http://test:9200/"));
}

public void testHttpWithSSLDisabledFromPropertyAndProtocol() throws Exception {
JdbcConfiguration ci = ci("jdbc:es://http://test?ssl=false");
assertThat(ci.baseUri().toString(), is("http://test:9200/"));
}

public void testHttpWithSSLDisabledFromPropertyAndEnabledFromProtocol() throws Exception {
Exception e = expectThrows(JdbcSQLException.class, () -> ci("jdbc:es://https://test?ssl=false"));
assertEquals("Cannot enable SSL: HTTPS protocol being used in the URL and SSL disabled in properties", e.getMessage());
}

public void testValidatePropertiesDefault() {
Exception e = expectThrows(JdbcSQLException.class, () -> ci("jdbc:es://test:9200?pagee.size=12"));
assertEquals("Unknown parameter [pagee.size]; did you mean [page.size]", e.getMessage());

e = expectThrows(JdbcSQLException.class, () -> ci("jdbc:es://test:9200?foo=bar"));
assertEquals("Unknown parameter [foo]; did you mean [ssl]", e.getMessage());
}

public void testValidateProperties() {
Exception e = expectThrows(JdbcSQLException.class, () -> ci("jdbc:es://test:9200?pagee.size=12&validate.properties=true"));
assertEquals("Unknown parameter [pagee.size]; did you mean [page.size]", e.getMessage());

e = expectThrows(JdbcSQLException.class, () -> ci("jdbc:es://test:9200?&validate.properties=true&something=some_value"));
assertEquals("Unknown parameter [something]; did you mean []", e.getMessage());

Properties properties = new Properties();
properties.setProperty(PROPERTIES_VALIDATION, "true");
e = expectThrows(JdbcSQLException.class, () -> JdbcConfiguration.create("jdbc:es://test:9200?something=some_value", properties, 0));
assertEquals("Unknown parameter [something]; did you mean []", e.getMessage());
}

public void testNoPropertiesValidation() throws SQLException {
JdbcConfiguration ci = ci("jdbc:es://test:9200?pagee.size=12&validate.properties=false");
assertEquals(false, ci.validateProperties());

// URL properties test
long queryTimeout = randomNonNegativeLong();
long connectTimeout = randomNonNegativeLong();
long networkTimeout = randomNonNegativeLong();
long pageTimeout = randomNonNegativeLong();
int pageSize = randomIntBetween(0, Integer.MAX_VALUE);

ci = ci("jdbc:es://test:9200?validate.properties=false&something=some_value&query.timeout=" + queryTimeout + "&connect.timeout="
+ connectTimeout + "&network.timeout=" + networkTimeout + "&page.timeout=" + pageTimeout + "&page.size=" + pageSize);
assertEquals(false, ci.validateProperties());
Expand All @@ -162,7 +163,7 @@ public void testNoPropertiesValidation() throws SQLException {
assertEquals(networkTimeout, ci.networkTimeout());
assertEquals(pageTimeout, ci.pageTimeout());
assertEquals(pageSize, ci.pageSize());

// Properties test
Properties properties = new Properties();
properties.setProperty(PROPERTIES_VALIDATION, "false");
Expand All @@ -171,7 +172,7 @@ public void testNoPropertiesValidation() throws SQLException {
properties.put(CONNECT_TIMEOUT, Long.toString(connectTimeout));
properties.put(NETWORK_TIMEOUT, Long.toString(networkTimeout));
properties.put(PAGE_SIZE, Integer.toString(pageSize));

// also putting validate.properties in URL to be overriden by the properties value
ci = JdbcConfiguration.create("jdbc:es://test:9200?validate.properties=true&something=some_value", properties, 0);
assertEquals(false, ci.validateProperties());
Expand Down Expand Up @@ -207,37 +208,37 @@ public void testTimoutOverride() throws Exception {

public void testSSLPropertiesInUrl() throws Exception {
Map<String, String> urlPropMap = sslProperties();

Properties allProps = new Properties();
allProps.putAll(urlPropMap);
String sslUrlProps = urlPropMap.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));

assertSslConfig(allProps, ci("jdbc:es://test?" + sslUrlProps.toString()).sslConfig());
}

public void testSSLPropertiesInUrlAndProperties() throws Exception {
Map<String, String> urlPropMap = new HashMap<>(4);
urlPropMap.put("ssl", "false");
urlPropMap.put("ssl.protocol", "SSLv3");
urlPropMap.put("ssl.keystore.location", "/abc/xyz");
urlPropMap.put("ssl.keystore.pass", "mypass");

Map<String, String> propMap = new HashMap<>(4);
propMap.put("ssl.keystore.type", "PKCS12");
propMap.put("ssl.truststore.location", "/foo/bar");
propMap.put("ssl.truststore.pass", "anotherpass");
propMap.put("ssl.truststore.type", "jks");

Properties props = new Properties();
props.putAll(propMap);
String sslUrlProps = urlPropMap.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));

Properties allProps = new Properties();
allProps.putAll(urlPropMap);
allProps.putAll(propMap);
assertSslConfig(allProps, JdbcConfiguration.create("jdbc:es://test?" + sslUrlProps.toString(), props, 0).sslConfig());
}

public void testSSLPropertiesOverride() throws Exception {
Map<String, String> urlPropMap = sslProperties();
Map<String, String> propMap = new HashMap<>(8);
Expand All @@ -249,18 +250,19 @@ public void testSSLPropertiesOverride() throws Exception {
propMap.put("ssl.truststore.location", "/baz");
propMap.put("ssl.truststore.pass", "different_anotherpass");
propMap.put("ssl.truststore.type", "PKCS11");

Properties props = new Properties();
props.putAll(propMap);
String sslUrlProps = urlPropMap.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));
assertSslConfig(props, JdbcConfiguration.create("jdbc:es://test?" + sslUrlProps.toString(), props, 0).sslConfig());
}

@SuppressForbidden(reason = "JDBC drivers allows logging to Sys.out")
public void testDriverConfigurationWithSSLInURL() {
assumeFalse("https://github.com/elastic/elasticsearch/issues/41557", Constants.WINDOWS);
Map<String, String> urlPropMap = sslProperties();
String sslUrlProps = urlPropMap.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));

SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new SpecialPermission());
Expand All @@ -276,7 +278,7 @@ public void testDriverConfigurationWithSSLInURL() {
fail("Driver registration should have been successful. Error: " + sqle);
}
}

public void testTyposInSslConfigInUrl(){
assertJdbcSqlExceptionFromUrl("ssl.protocl", "ssl.protocol");
assertJdbcSqlExceptionFromUrl("sssl", "ssl");
Expand All @@ -287,7 +289,7 @@ public void testTyposInSslConfigInUrl(){
assertJdbcSqlExceptionFromUrl("ssl.tuststore.pass", "ssl.truststore.pass");
assertJdbcSqlExceptionFromUrl("ssl.ruststore.type", "ssl.truststore.type");
}

public void testTyposInSslConfigInProperties() {
assertJdbcSqlExceptionFromProperties("ssl.protocl", "ssl.protocol");
assertJdbcSqlExceptionFromProperties("sssl", "ssl");
Expand All @@ -298,7 +300,7 @@ public void testTyposInSslConfigInProperties() {
assertJdbcSqlExceptionFromProperties("ssl.tuststore.pass", "ssl.truststore.pass");
assertJdbcSqlExceptionFromProperties("ssl.ruststore.type", "ssl.truststore.type");
}

static Map<String, String> sslProperties() {
Map<String, String> sslPropertiesMap = new HashMap<>(8);
// always using "false" so that the SSLContext doesn't actually start verifying the keystore and trustore
Expand All @@ -311,31 +313,31 @@ static Map<String, String> sslProperties() {
sslPropertiesMap.put("ssl.truststore.location", "/foo/bar");
sslPropertiesMap.put("ssl.truststore.pass", "anotherpass");
sslPropertiesMap.put("ssl.truststore.type", "jks");

return sslPropertiesMap;
}

static void assertSslConfig(Properties allProperties, SslConfig sslConfig) throws URISyntaxException {
// because SslConfig doesn't expose its internal properties (and it shouldn't),
// we compare a newly created SslConfig with the one from the JdbcConfiguration with the equals() method
SslConfig mockSslConfig = new SslConfig(allProperties, new URI("http://test:9200/"));
assertEquals(mockSslConfig, sslConfig);
}

private void assertJdbcSqlExceptionFromUrl(String wrongSetting, String correctSetting) {
String url = "jdbc:es://test?" + wrongSetting + "=foo";
assertJdbcSqlException(wrongSetting, correctSetting, url, null);
}

private void assertJdbcSqlExceptionFromProperties(String wrongSetting, String correctSetting) {
String url = "jdbc:es://test";
Properties props = new Properties();
props.put(wrongSetting, correctSetting);
assertJdbcSqlException(wrongSetting, correctSetting, url, props);
}

private void assertJdbcSqlException(String wrongSetting, String correctSetting, String url, Properties props) {
JdbcSQLException ex = expectThrows(JdbcSQLException.class,
JdbcSQLException ex = expectThrows(JdbcSQLException.class,
() -> JdbcConfiguration.create(url, props, 0));
assertEquals("Unknown parameter [" + wrongSetting + "]; did you mean [" + correctSetting + "]", ex.getMessage());
}
Expand Down

0 comments on commit 3d4a7d0

Please sign in to comment.