Skip to content

Commit e2b4eda

Browse files
astefankcm
authored andcommitted
SQL: the SSL default configuration shouldn't override the https protocol if used (#34635)
* The default SSL option shouldn't override the https protocol if specified. Fixes #33817
1 parent e09bd8a commit e2b4eda

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfigurationTests.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,40 @@ public void testDebugOutWithSuffix() throws Exception {
7373
assertThat(ci.debugOut(), is("jdbc.out"));
7474
}
7575

76-
public void testHttpWithSSLEnabled() throws Exception {
76+
public void testHttpWithSSLEnabledFromProperty() throws Exception {
7777
JdbcConfiguration ci = ci("jdbc:es://test?ssl=true");
7878
assertThat(ci.baseUri().toString(), is("https://test:9200/"));
7979
}
80+
81+
public void testHttpWithSSLEnabledFromPropertyAndDisabledFromProtocol() throws Exception {
82+
JdbcConfiguration ci = ci("jdbc:es://http://test?ssl=true");
83+
assertThat(ci.baseUri().toString(), is("https://test:9200/"));
84+
}
85+
86+
public void testHttpWithSSLEnabledFromProtocol() throws Exception {
87+
JdbcConfiguration ci = ci("jdbc:es://https://test:9200");
88+
assertThat(ci.baseUri().toString(), is("https://test:9200/"));
89+
}
90+
91+
public void testHttpWithSSLEnabledFromProtocolAndProperty() throws Exception {
92+
JdbcConfiguration ci = ci("jdbc:es://https://test:9200?ssl=true");
93+
assertThat(ci.baseUri().toString(), is("https://test:9200/"));
94+
}
8095

81-
public void testHttpWithSSLDisabled() throws Exception {
96+
public void testHttpWithSSLDisabledFromProperty() throws Exception {
8297
JdbcConfiguration ci = ci("jdbc:es://test?ssl=false");
8398
assertThat(ci.baseUri().toString(), is("http://test:9200/"));
8499
}
100+
101+
public void testHttpWithSSLDisabledFromPropertyAndProtocol() throws Exception {
102+
JdbcConfiguration ci = ci("jdbc:es://http://test?ssl=false");
103+
assertThat(ci.baseUri().toString(), is("http://test:9200/"));
104+
}
105+
106+
public void testHttpWithSSLDisabledFromPropertyAndEnabledFromProtocol() throws Exception {
107+
Exception e = expectThrows(JdbcSQLException.class, () -> ci("jdbc:es://https://test?ssl=false"));
108+
assertEquals("Cannot enable SSL: HTTPS protocol being used in the URL and SSL disabled in properties", e.getMessage());
109+
}
85110

86111
public void testTimoutOverride() throws Exception {
87112
Properties properties = new Properties();

x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/ConnectionConfiguration.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public ConnectionConfiguration(URI baseURI, String connectionString, Properties
9898
user = settings.getProperty(AUTH_USER);
9999
pass = settings.getProperty(AUTH_PASS);
100100

101-
sslConfig = new SslConfig(settings);
101+
sslConfig = new SslConfig(settings, baseURI);
102102
proxyConfig = new ProxyConfig(settings);
103103

104104
this.baseURI = normalizeSchema(baseURI, connectionString, sslConfig.isEnabled());
@@ -126,20 +126,9 @@ public ConnectionConfiguration(URI baseURI, String connectionString, long connec
126126

127127

128128
private static URI normalizeSchema(URI uri, String connectionString, boolean isSSLEnabled) {
129-
// Make sure the protocol is correct
130-
final String scheme;
131-
if (isSSLEnabled) {
132-
// It's ok to upgrade from http to https
133-
scheme = "https";
134-
} else {
135-
// Silently downgrading from https to http can cause security issues
136-
if ("https".equals(uri.getScheme())) {
137-
throw new ClientException("SSL is disabled");
138-
}
139-
scheme = "http";
140-
}
141129
try {
142-
return new URI(scheme, null, uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment());
130+
return new URI(isSSLEnabled ? "https" : "http", null, uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(),
131+
uri.getFragment());
143132
} catch (URISyntaxException ex) {
144133
throw new ClientException("Cannot parse process baseURI [" + connectionString + "] " + ex.getMessage());
145134
}

x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/SslConfig.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.io.IOException;
99
import java.io.InputStream;
10+
import java.net.URI;
1011
import java.nio.file.Files;
1112
import java.nio.file.Path;
1213
import java.nio.file.Paths;
@@ -62,8 +63,19 @@ public class SslConfig {
6263

6364
private final SSLContext sslContext;
6465

65-
SslConfig(Properties settings) {
66-
enabled = StringUtils.parseBoolean(settings.getProperty(SSL, SSL_DEFAULT));
66+
SslConfig(Properties settings, URI baseURI) {
67+
boolean isSchemaPresent = baseURI.getScheme() != null;
68+
boolean isSSLPropertyPresent = settings.getProperty(SSL) != null;
69+
boolean isHttpsScheme = "https".equals(baseURI.getScheme());
70+
71+
if (!isSSLPropertyPresent && !isSchemaPresent) {
72+
enabled = StringUtils.parseBoolean(SSL_DEFAULT);
73+
} else {
74+
if (isSSLPropertyPresent && isHttpsScheme && !StringUtils.parseBoolean(settings.getProperty(SSL))) {
75+
throw new ClientException("Cannot enable SSL: HTTPS protocol being used in the URL and SSL disabled in properties");
76+
}
77+
enabled = isHttpsScheme || StringUtils.parseBoolean(settings.getProperty(SSL, SSL_DEFAULT));
78+
}
6779
protocol = settings.getProperty(SSL_PROTOCOL, SSL_PROTOCOL_DEFAULT);
6880
keystoreLocation = settings.getProperty(SSL_KEYSTORE_LOCATION, SSL_KEYSTORE_LOCATION_DEFAULT);
6981
keystorePass = settings.getProperty(SSL_KEYSTORE_PASS, SSL_KEYSTORE_PASS_DEFAULT);

0 commit comments

Comments
 (0)