Skip to content

Commit

Permalink
Set ssl automatically depending on the jdbcUrl protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
andythsu committed Nov 13, 2023
1 parent a4fe1b0 commit 8d95ab1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public ClusterStats monitor(ProxyBackendConfiguration backend) {
parsedUrl.getHost(),
parsedUrl.getPort() == -1 ? parsedUrl.getDefaultPort() : parsedUrl.getPort()
);
// automatically set ssl config based on url protocol
properties.setProperty("SSL", String.valueOf(parsedUrl.getProtocol().equals("https")));
} catch (MalformedURLException e) {
log.error("could not parse backend url {} ", url);
return clusterStats;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -36,7 +38,7 @@ public class TestClusterStatsJdbcMonitor {
@Mock
private ResultSet resultSet;

private java.util.Properties properties;
private Properties properties;

@BeforeEach
public void initMocks() {
Expand All @@ -56,7 +58,7 @@ public void resetMocks() {
public void setUp() {
BackendStateConfiguration backendStateConfiguration = new BackendStateConfiguration();
backendStateConfiguration.setUsername("Trino");
properties = new java.util.Properties();
properties = new Properties();
properties.setProperty("user", backendStateConfiguration.getUsername());
properties.setProperty("password", backendStateConfiguration.getPassword());
properties.setProperty("SSL", String.valueOf(backendStateConfiguration.getSsl()));
Expand Down Expand Up @@ -87,5 +89,46 @@ public void testProtocol(String scheme, String port, String expectedJdbcUrl) thr
m.verify(() -> DriverManager.getConnection(expectedJdbcUrl, properties));
}
}

@Test
public void testSslFalseWhenSchemeIsHttp() throws java.sql.SQLException {
try(MockedStatic<DriverManager> m = Mockito.mockStatic(java.sql.DriverManager.class)) {
m.when(() -> DriverManager.getConnection(anyString(), any())).thenReturn(connection);
when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
when(preparedStatement.executeQuery()).thenReturn(resultSet);
ProxyBackendConfiguration proxyBackend = new ProxyBackendConfiguration();
proxyBackend.setProxyTo(String.format("%s://trino.example.com:%s", "http", 90));
proxyBackend.setName("abc");

clusterStatsJdbcMonitor.monitor(proxyBackend);

String expectedUrl = "jdbc:trino://trino.example.com:90/system";

m.verify(() -> DriverManager.getConnection(expectedUrl, properties));
}
}

@Test
public void testSslTrueWhenSchemeIsHttp() throws java.sql.SQLException {
try(MockedStatic<DriverManager> m = Mockito.mockStatic(java.sql.DriverManager.class)) {
m.when(() -> DriverManager.getConnection(anyString(), any())).thenReturn(connection);
when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
when(preparedStatement.executeQuery()).thenReturn(resultSet);
ProxyBackendConfiguration proxyBackend = new ProxyBackendConfiguration();
proxyBackend.setProxyTo(String.format("%s://trino.example.com:%s", "https", 90));
proxyBackend.setName("abc");

clusterStatsJdbcMonitor.monitor(proxyBackend);

Properties expectedProperties = new java.util.Properties();
expectedProperties.setProperty("password", properties.getProperty("password"));
expectedProperties.setProperty("user", properties.getProperty("user"));
expectedProperties.setProperty("SSL", "true");

String expectedUrl = "jdbc:trino://trino.example.com:90/system";

m.verify(() -> DriverManager.getConnection(expectedUrl, expectedProperties));
}
}
}

5 changes: 0 additions & 5 deletions proxyserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down

0 comments on commit 8d95ab1

Please sign in to comment.