Skip to content

Commit

Permalink
Fix extraneous use of connection string useSSL flag (#569)
Browse files Browse the repository at this point in the history
Limit use of `useSSL` flag to MySQL container
Add template method allowing JDBCContainer subclasses to have a specific
form of JDBC URL used for establishing Connections.

Fixes #568
  • Loading branch information
rnorth authored Feb 3, 2018
1 parent 4edf9e2 commit a6d8bf2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Change Log
All notable changes to this project will be documented in this file.

## [1.6.1] - 2018-01-31

### Fixed

- Fixed extraneous insertion of `useSSL=false` in all JDBC URL strings, even for DBs that do not understand it. Usage is now restricted to MySQL by default and can be overridden by authors of `JdbcDatabaseContainer` subclasses ([\#568](https://github.com/testcontainers/testcontainers-java/issues/568))

## [1.6.0] - 2018-01-28

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static Iterable<Object[]> data() {
{"jdbc:tc:mysql://hostname/databasename?TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction", true, false, false},
{"jdbc:tc:mysql://hostname/databasename?useUnicode=yes&characterEncoding=utf8", false, true, false},
{"jdbc:tc:mysql://hostname/databasename", false, false, false},
{"jdbc:tc:mysql://hostname/databasename?useSSL=false", false, false, false},
{"jdbc:tc:postgresql://hostname/databasename", false, false, false},
{"jdbc:tc:mysql:5.6://hostname/databasename?TC_MY_CNF=somepath/mysql_conf_override", false, false, true},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public SELF withUsername(String username) {
throw new UnsupportedOperationException();
}

public SELF withPassword(String password){
public SELF withPassword(String password) {
throw new UnsupportedOperationException();
}

Expand Down Expand Up @@ -127,16 +127,17 @@ public Driver getJdbcDriverInstance() {
/**
* Creates a connection to the underlying containerized database instance.
*
* @param queryString any special query string parameters that should be appended to the JDBC connection URL. The
* '?' character must be included
* @param queryString
* query string parameters that should be appended to the JDBC connection URL.
* The '?' character must be included
* @return a Connection
* @throws SQLException if there is a repeated failure to create the connection
*/
public Connection createConnection(String queryString) throws SQLException {
final Properties info = new Properties();
info.put("user", this.getUsername());
info.put("password", this.getPassword());
final String url = appendDisableSslConfig(this.getJdbcUrl() + queryString);
final String url = constructUrlForConnection(queryString);

final Driver jdbcDriverInstance = getJdbcDriverInstance();

Expand All @@ -147,9 +148,18 @@ public Connection createConnection(String queryString) throws SQLException {
}
}

private String appendDisableSslConfig(String connectionString){
String separator = connectionString.contains("?") ? "&" : "?";
return connectionString + separator + "useSSL=false";
/**
* Template method for constructing the JDBC URL to be used for creating {@link Connection}s.
* This should be overridden if the JDBC URL and query string concatenation or URL string
* construction needs to be different to normal.
*
* @param queryString
* query string parameters that should be appended to the JDBC connection URL.
* The '?' character must be included
* @return a full JDBC URL including queryString
*/
protected String constructUrlForConnection(String queryString) {
return getJdbcUrl() + queryString;
}

protected void optionallyMapResourceParameterAsVolume(@NotNull String paramName, @NotNull String pathNameInContainer, @NotNull String defaultResource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ public String getJdbcUrl() {
return "jdbc:mysql://" + getContainerIpAddress() + ":" + getMappedPort(MYSQL_PORT) + "/" + databaseName;
}

@Override
protected String constructUrlForConnection(String queryString) {
String url = super.constructUrlForConnection(queryString);

if (! url.contains("useSSL=")) {
String separator = url.contains("?") ? "&" : "?";
return url + separator + "useSSL=false";
} else {
return url;
}
}

@Override
public String getDatabaseName() {
return databaseName;
Expand Down

0 comments on commit a6d8bf2

Please sign in to comment.