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 444ea3f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 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,24 +58,24 @@ 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()));
clusterStatsJdbcMonitor = new ClusterStatsJdbcMonitor(backendStateConfiguration);
}

private static Stream<Arguments> provideSchemeAndPort(){
private static Stream<Arguments> provideProtocolAndPortAndSsl(){
return Stream.of(
Arguments.of("https", "90", "jdbc:trino://trino.example.com:90/system"),
Arguments.of("http", "90", "jdbc:trino://trino.example.com:90/system"),
Arguments.of("https", "", "jdbc:trino://trino.example.com:443/system"),
Arguments.of("http", "", "jdbc:trino://trino.example.com:80/system")
Arguments.of("https", "90", "jdbc:trino://trino.example.com:90/system", "true"),
Arguments.of("http", "90", "jdbc:trino://trino.example.com:90/system", "false"),
Arguments.of("https", "", "jdbc:trino://trino.example.com:443/system", "true"),
Arguments.of("http", "", "jdbc:trino://trino.example.com:80/system", "false")
);
}
@ParameterizedTest
@MethodSource("provideSchemeAndPort")
public void testProtocol(String scheme, String port, String expectedJdbcUrl) throws java.sql.SQLException {
@MethodSource("provideProtocolAndPortAndSsl")
public void testProtocolAndSsl(String scheme, String port, String expectedJdbcUrl, String expectedSsl) 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);
Expand All @@ -84,6 +86,8 @@ public void testProtocol(String scheme, String port, String expectedJdbcUrl) thr

clusterStatsJdbcMonitor.monitor(proxyBackend);

properties.setProperty("SSL", expectedSsl);

m.verify(() -> DriverManager.getConnection(expectedJdbcUrl, properties));
}
}
Expand Down
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 444ea3f

Please sign in to comment.