Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure wrapped driver exists on classpath at time of registration #164

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ protected AWSSecretsManagerDriver(SecretCache cache) {

setProperties();
AWSSecretsManagerDriver.register(this);

getWrappedDriver();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public final class AWSSecretsManagerMSSQLServerDriver extends AWSSecretsManagerD
*/
public static final String SUBPREFIX = "sqlserver";

/**
* Default driver class to use.
*/
public static final String DEFAULT_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

static {
AWSSecretsManagerDriver.register(new AWSSecretsManagerMSSQLServerDriver());
}
Expand Down Expand Up @@ -127,7 +132,7 @@ public String constructUrlFromEndpointPortDatabase(String endpoint, String port,

@Override
public String getDefaultDriverClass() {
return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
return DEFAULT_DRIVER;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public final class AWSSecretsManagerMariaDBDriver extends AWSSecretsManagerDrive
*/
public static final String SUBPREFIX = "mariadb";

/**
* Default driver class to use.
*/
public static final String DEFAULT_DRIVER = "org.mariadb.jdbc.Driver";

static {
AWSSecretsManagerDriver.register(new AWSSecretsManagerMariaDBDriver());
}
Expand Down Expand Up @@ -122,6 +127,6 @@ public String constructUrlFromEndpointPortDatabase(String endpoint, String port,

@Override
public String getDefaultDriverClass() {
return "org.mariadb.jdbc.Driver";
return DEFAULT_DRIVER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,12 @@ public String constructUrlFromEndpointPortDatabase(String endpoint, String port,

@Override
public String getDefaultDriverClass() {
return getStaticDefaultDriverClass();
}

public static String getStaticDefaultDriverClass() {
try {
Class.forName("com.mysql.cj.jdbc.Driver", false, this.getClass().getClassLoader());
Class.forName("com.mysql.cj.jdbc.Driver", false, AWSSecretsManagerMySQLDriver.class.getClassLoader());
return "com.mysql.cj.jdbc.Driver";
} catch (ClassNotFoundException e) {
return "com.mysql.jdbc.Driver";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public final class AWSSecretsManagerOracleDriver extends AWSSecretsManagerDriver
*/
public static final String SUBPREFIX = "oracle";

/**
* Default driver class to use.
*/
public static final String DEFAULT_DRIVER = "oracle.jdbc.OracleDriver";

static {
AWSSecretsManagerDriver.register(new AWSSecretsManagerOracleDriver());
}
Expand Down Expand Up @@ -144,7 +149,7 @@ public String constructUrlFromEndpointPortDatabase(String endpoint, String port,

@Override
public String getDefaultDriverClass() {
return "oracle.jdbc.OracleDriver";
return DEFAULT_DRIVER;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public final class AWSSecretsManagerPostgreSQLDriver extends AWSSecretsManagerDr
*/
public static final String SUBPREFIX = "postgresql";

/**
* Default driver class to use.
*/
public static final String DEFAULT_DRIVER = "org.postgresql.Driver";

static {
AWSSecretsManagerDriver.register(new AWSSecretsManagerPostgreSQLDriver());
}
Expand Down Expand Up @@ -124,7 +129,7 @@ public String constructUrlFromEndpointPortDatabase(String endpoint, String port,

@Override
public String getDefaultDriverClass() {
return "org.postgresql.Driver";
return DEFAULT_DRIVER;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public final class AWSSecretsManagerRedshiftDriver extends AWSSecretsManagerDriv

public static final String SUBPREFIX = "redshift";

/**
* Default driver class to use.
*/
public static final String DEFAULT_DRIVER = "com.amazon.redshift.Driver";

static {
AWSSecretsManagerDriver.register(new AWSSecretsManagerRedshiftDriver());
}
Expand Down Expand Up @@ -129,6 +134,6 @@ public String constructUrlFromEndpointPortDatabase(String endpoint, String port,

@Override
public String getDefaultDriverClass() {
return "com.amazon.redshift.Driver";
return DEFAULT_DRIVER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ public void test_init_constructor_null_params() {

@Test
public void test_init_works_realDriverFromConfig() {
System.setProperty("drivers.dummy.realDriverClass", "some.other.class");
String driver = "com.amazonaws.secretsmanager.sql.EmptyDriver";
System.setProperty("drivers.dummy.realDriverClass", driver);
AWSSecretsManagerDummyDriver sut2 = new AWSSecretsManagerDummyDriver(cache);
assertEquals(getFieldFrom(sut2, "realDriverClass"), "some.other.class");
assertEquals(getFieldFrom(sut2, "realDriverClass"), driver);
}

/*******************************************************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Tests for the MSSQL Driver.
*/
@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor("com.amazonaws.secretsmanager.sql.AWSSecretsManagerMSSQLServerDriver")
@SuppressStaticInitializationFor({"com.amazonaws.secretsmanager.sql.AWSSecretsManagerMSSQLServerDriver","com.amazonaws.secretsmanager.sql.*"})
@PowerMockIgnore("jdk.internal.reflect.*")
public class AWSSecretsManagerMSSQLServerDriverTest extends TestClass {

Expand All @@ -49,6 +49,8 @@ public void setup() {
sut = new AWSSecretsManagerMSSQLServerDriver(cache);
} catch (Exception e) {
throw new RuntimeException(e);
} catch (Throwable t) {
t.printStackTrace();
}
}

Expand Down Expand Up @@ -99,8 +101,7 @@ public void test_constructUrlNullDatabase() {
@Test
public void test_getDefaultDriverClass() {
System.clearProperty("drivers.sqlserver.realDriverClass");
AWSSecretsManagerMSSQLServerDriver sut2 = new AWSSecretsManagerMSSQLServerDriver(cache);
assertEquals(getFieldFrom(sut2, "realDriverClass"), sut2.getDefaultDriverClass());
assertThrows(IllegalStateException.class, "Could not load real driver with name, \"" + AWSSecretsManagerMSSQLServerDriver.DEFAULT_DRIVER + "\".", () -> new AWSSecretsManagerMSSQLServerDriver(cache));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Tests for the MariaDB Driver.
*/
@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor("com.amazonaws.secretsmanager.sql.AWSSecretsManagerMariaDBDriver")
@SuppressStaticInitializationFor({"com.amazonaws.secretsmanager.sql.AWSSecretsManagerMariaDBDriver","com.amazonaws.secretsmanager.sql.*"})
@PowerMockIgnore("jdk.internal.reflect.*")
public class AWSSecretsManagerMariaDBDriverTest extends TestClass {

Expand Down Expand Up @@ -99,8 +99,7 @@ public void test_constructUrlNullDatabase() {
@Test
public void test_getDefaultDriverClass() {
System.clearProperty("drivers.mariadb.realDriverClass");
AWSSecretsManagerMariaDBDriver sut2 = new AWSSecretsManagerMariaDBDriver(cache);
assertEquals(getFieldFrom(sut2, "realDriverClass"), sut2.getDefaultDriverClass());
assertThrows(IllegalStateException.class, "Could not load real driver with name, \"" + AWSSecretsManagerMariaDBDriver.DEFAULT_DRIVER + "\".", () -> new AWSSecretsManagerMariaDBDriver(cache));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Tests for the MySQL Driver.
*/
@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor("com.amazonaws.secretsmanager.sql.AWSSecretsManagerMySQLDriver")
@SuppressStaticInitializationFor({"com.amazonaws.secretsmanager.sql.AWSSecretsManagerMySQLDriver","com.amazonaws.secretsmanager.sql.*"})
@PowerMockIgnore("jdk.internal.reflect.*")
public class AWSSecretsManagerMySQLDriverTest extends TestClass {

Expand Down Expand Up @@ -99,8 +99,7 @@ public void test_constructUrlNullDatabase() {
@Test
public void test_getDefaultDriverClass() {
System.clearProperty("drivers.mysql.realDriverClass");
AWSSecretsManagerMySQLDriver sut2 = new AWSSecretsManagerMySQLDriver(cache);
assertEquals(getFieldFrom(sut2, "realDriverClass"), sut2.getDefaultDriverClass());
assertThrows(IllegalStateException.class, "Could not load real driver with name, \"" + AWSSecretsManagerMySQLDriver.getStaticDefaultDriverClass() + "\".", () -> new AWSSecretsManagerMySQLDriver(cache));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Tests for the Oracle Driver.
*/
@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor("com.amazonaws.secretsmanager.sql.AWSSecretsManagerOracleDriver")
@SuppressStaticInitializationFor({"com.amazonaws.secretsmanager.sql.AWSSecretsManagerOracleDriver","com.amazonaws.secretsmanager.sql.*"})
@PowerMockIgnore("jdk.internal.reflect.*")
public class AWSSecretsManagerOracleDriverTest extends TestClass {

Expand Down Expand Up @@ -104,8 +104,7 @@ public void test_constructUrlNullDatabase() {
@Test
public void test_getDefaultDriverClass() {
System.clearProperty("drivers.oracle.realDriverClass");
AWSSecretsManagerOracleDriver sut2 = new AWSSecretsManagerOracleDriver(cache);
assertEquals(getFieldFrom(sut2, "realDriverClass"), sut2.getDefaultDriverClass());
assertThrows(IllegalStateException.class, "Could not load real driver with name, \"" + AWSSecretsManagerOracleDriver.DEFAULT_DRIVER + "\".", () -> new AWSSecretsManagerOracleDriver(cache));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Tests for the PostgreSQL Driver.
*/
@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor("com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver")
@SuppressStaticInitializationFor({"com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver","com.amazonaws.secretsmanager.sql.*"})
@PowerMockIgnore("jdk.internal.reflect.*")
public class AWSSecretsManagerPostgreSQLDriverTest extends TestClass {

Expand Down Expand Up @@ -99,8 +99,7 @@ public void test_constructUrlNullDatabase() {
@Test
public void test_getDefaultDriverClass() {
System.clearProperty("drivers.postgresql.realDriverClass");
AWSSecretsManagerPostgreSQLDriver sut2 = new AWSSecretsManagerPostgreSQLDriver(cache);
assertEquals(getFieldFrom(sut2, "realDriverClass"), sut2.getDefaultDriverClass());
assertThrows(IllegalStateException.class, "Could not load real driver with name, \"" + AWSSecretsManagerPostgreSQLDriver.DEFAULT_DRIVER + "\".", () -> new AWSSecretsManagerPostgreSQLDriver(cache));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Tests for the Redshift Driver.
*/
@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor("com.amazonaws.secretsmanager.sql.AWSSecretsManagerRedshiftDriver")
@SuppressStaticInitializationFor({"com.amazonaws.secretsmanager.sql.AWSSecretsManagerRedshiftDriver","com.amazonaws.secretsmanager.sql.*"})
@PowerMockIgnore("jdk.internal.reflect.*")
public class AWSSecretsManagerRedshiftDriverTest extends TestClass {

Expand Down Expand Up @@ -99,7 +99,6 @@ public void test_constructUrlNullDatabase() {
@Test
public void test_getDefaultDriverClass() {
System.clearProperty("drivers.redshift.realDriverClass");
AWSSecretsManagerRedshiftDriver sut2 = new AWSSecretsManagerRedshiftDriver(cache);
assertEquals(getFieldFrom(sut2, "realDriverClass"), sut2.getDefaultDriverClass());
assertThrows(IllegalStateException.class, "Could not load real driver with name, \"" + AWSSecretsManagerRedshiftDriver.DEFAULT_DRIVER + "\".", () -> new AWSSecretsManagerRedshiftDriver(cache));
}
}
65 changes: 65 additions & 0 deletions src/test/java/com/amazonaws/secretsmanager/sql/EmptyDriver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.amazonaws.secretsmanager.sql;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;

public class EmptyDriver implements Driver {
public static EmptyDriver instance;

static {
instance = new EmptyDriver();
try {
DriverManager.registerDriver(instance);
} catch (SQLException e) {
throw new RuntimeException("Driver could not be registered.", e);
}
}

@Override
public boolean acceptsURL(String arg0) throws SQLException {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'acceptsURL'");
}

@Override
public Connection connect(String arg0, Properties arg1) throws SQLException {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'connect'");
}

@Override
public int getMajorVersion() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getMajorVersion'");
}

@Override
public int getMinorVersion() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getMinorVersion'");
}

@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getParentLogger'");
}

@Override
public DriverPropertyInfo[] getPropertyInfo(String arg0, Properties arg1) throws SQLException {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getPropertyInfo'");
}

@Override
public boolean jdbcCompliant() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'jdbcCompliant'");
}
}
12 changes: 12 additions & 0 deletions src/test/java/com/amazonaws/secretsmanager/util/TestClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ public void assertThrows(Exception exception, throwingRunnable code) {
}
}

public void assertThrows(Class<? extends Exception> exception, String message, throwingRunnable code) {
try {
code.run();
throw new RuntimeException("Should have thrown a " + exception.getName() + " but threw nothing.");
} catch (Exception e) {
if (!exception.isAssignableFrom(e.getClass()) && !message.equals(e.getMessage())) {
e.printStackTrace();
throw new RuntimeException("Should have thrown a " + exception.getName() + " with message " + message + " but threw " + e.getClass().getName() + " with message " + e.getMessage());
}
}
}

public void assertNotThrows(throwingRunnable code) {
try {
code.run();
Expand Down