Skip to content

Commit

Permalink
Remove ability to specify arbitrary driver classnames
Browse files Browse the repository at this point in the history
Remove need to choose the database type separately from the URL

DAT-4624
SECURITY-519
  • Loading branch information
nvoxland committed May 5, 2020
1 parent df5bbb6 commit 382a1ea
Show file tree
Hide file tree
Showing 21 changed files with 21 additions and 354 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Expand Up @@ -72,7 +72,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>2.3.5</version>
<version>2.3.7</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down Expand Up @@ -104,6 +104,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.cloudbees.jenkins.plugins</groupId>
<artifactId>cloudbees-credentials</artifactId>
<version>3.3</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>job-dsl</artifactId>
Expand Down
6 changes: 1 addition & 5 deletions src/docs/jobdsl.md
Expand Up @@ -28,9 +28,6 @@ job() {
changeLogFile('changeset.yml')
testRollbacks(true)
url('jdbc:postgresql://localhost:5432/sample-db')
driverClassname('org.postgresql.Driver')
// instead of driverClassname, you can set databaseEngine to MySQL, Derby, Postgres, Derby, or Hypersonic
databaseEngine('MySQL')
credentialsId('database_password_credentials_id')
liquibasePropertiesPath('/etc/liquibase.properties')
contexts('staging') // can be comma delimited list
Expand All @@ -57,7 +54,6 @@ job() {
changeLogFile('changeset.yml')
testRollbacks(true)
url('jdbc:postgresql://localhost:5432/sample-db')
driverClassname('org.postgresql.Driver')
credentialsId('database_password_credentials_id')
rollbackToTag('deploy-2.5')
rollbackCount(2)
Expand All @@ -66,4 +62,4 @@ job() {
}
}
}
```
```
7 changes: 1 addition & 6 deletions src/docs/pipeline.md
Expand Up @@ -21,10 +21,7 @@ node {
liquibaseUpdate(changeLogFile: 'changeset.yml',
testRollbacks: true,
url: 'jdbc:postgresql://localhost:5432/sample-db',
driverClassname: 'org.postgresql.Driver',
classpath: 'path/to/additional/classes', // may be relative or absolute
// instead of driverClassname, you can set databaseEngine to MySQL, Derby, Postgres, Derby, or Hypersonic
databaseEngine: 'MySQL',
credentialsId: 'database_password_credentials_id',
liquibasePropertiesPath: '/etc/liquibase.properties',
contexts: 'staging',
Expand All @@ -47,7 +44,6 @@ node {
// rollbackCount, rollbackToTag, rollbackToDate, or rollbackLastHours.
liquibaseRollback(changeLogFile: 'changeset.yml',
url: 'jdbc:postgresql://localhost:5432/sample-db',
driverClassname: 'org.postgresql.Driver',
credentialsId: 'database_password_credentials_id',
liquibasePropertiesPath: '/etc/liquibase.properties',
rollbackToTag: 'deploy-2.5',
Expand All @@ -65,7 +61,6 @@ DbDoc Generation
node {
liquibaseDbDoc( changeLogFile: 'changeset.yml', // same basic configuration parameters
url: 'jdbc:postgresql://localhost:5432/sample-db',
driverClassname: 'org.postgresql.Driver',
outputDirectory: 'dbdoc')
// works great with the HTML publisher plugin (sold separately)
Expand All @@ -76,4 +71,4 @@ node {
}
```
```
Expand Up @@ -11,8 +11,7 @@ public enum LiquibaseProperty {
URL(),
DEFAULT_SCHEMA_NAME("defaultSchemaName"),
LOG_LEVEL("logLevel"),
DEFAULT_CATALOG_NAME("defaultCatalogName"),
DRIVER();
DEFAULT_CATALOG_NAME("defaultCatalogName");


private String cliOption;
Expand Down
Expand Up @@ -12,7 +12,6 @@

import org.apache.commons.io.IOUtils;
import org.jenkinsci.plugins.liquibase.evaluator.AbstractLiquibaseBuilder;
import org.jenkinsci.plugins.liquibase.evaluator.IncludedDatabaseDriver;
import org.jenkinsci.plugins.liquibase.exception.LiquibaseRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -26,7 +25,6 @@
public class PropertiesAssembler {
private static final Logger LOG = LoggerFactory.getLogger(PropertiesAssembler.class);
private static final String DEFAULT_JDBC_URL = "jdbc:h2:mem:builder-db";
private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";

/**
* Creates a properties instance for use with liquibase execution. Property values may come from these sources,
Expand Down Expand Up @@ -100,30 +98,8 @@ protected static void assembleFromProjectConfiguration(AbstractLiquibaseBuilder
environment, build);
addPropertyIfDefined(properties, LiquibaseProperty.LABELS, liquibaseBuilder.getLabels(), environment, build);
addPropertyIfDefined(properties, LiquibaseProperty.CONTEXTS, liquibaseBuilder.getContexts(), environment, build);
resolveDatabaseDriver(liquibaseBuilder, properties, environment, build);
}

private static void resolveDatabaseDriver(AbstractLiquibaseBuilder liquibaseBuilder,
Properties properties,
Map environment, Run<?, ?> build) {


boolean useIncludedDriver = isBuilderUsingIncludedDriver(liquibaseBuilder);
if (useIncludedDriver) {
setDriverFromDBEngine(liquibaseBuilder, properties);
} else {
addPropertyIfDefined(properties, LiquibaseProperty.DRIVER, liquibaseBuilder.getDriverClassname(),
environment, build);
}
}

private static boolean isBuilderUsingIncludedDriver(AbstractLiquibaseBuilder liquibaseBuilder) {
boolean useIncludedDriver =
liquibaseBuilder.hasUseIncludedDriverBeenSet() && liquibaseBuilder.isUseIncludedDriver();
return useIncludedDriver && !Strings.isNullOrEmpty(liquibaseBuilder.getDatabaseEngine());
}


private static void assembleFromPropertiesFile(Properties properties,
String liquibasePropertiesPath,
FilePath workspace) {
Expand Down Expand Up @@ -153,22 +129,7 @@ private static void assembleFromPropertiesFile(Properties properties,
}
}

public static void setDriverFromDBEngine(AbstractLiquibaseBuilder liquibaseBuilder, Properties properties) {
if (!Strings.isNullOrEmpty(liquibaseBuilder.getDatabaseEngine())) {
for (IncludedDatabaseDriver includedDatabaseDriver : liquibaseBuilder.getDrivers()) {
if (includedDatabaseDriver.getDisplayName().equals(liquibaseBuilder.getDatabaseEngine())) {
setProperty(properties, LiquibaseProperty.DRIVER, includedDatabaseDriver.getDriverClassName());
if (LOG.isDebugEnabled()) {
LOG.debug("using db driver class[" + includedDatabaseDriver.getDriverClassName() + "] ");
}
break;
}
}
}
}

private static void assembleDefaults(Properties properties) {
setProperty(properties, LiquibaseProperty.DRIVER, DEFAULT_DB_DRIVER);
setProperty(properties, LiquibaseProperty.URL, DEFAULT_JDBC_URL);
}

Expand Down
Expand Up @@ -9,18 +9,15 @@
*/
public class LiquibaseContext implements Context {

protected String databaseEngine;
protected String changeLogFile;
protected String url;
protected String defaultSchemaName;
protected String contexts;
protected String liquibasePropertiesPath;
protected String classpath;
protected String driverClassname;
protected String labels;
private Map<String, String> changeLogParameters;
private String basePath;
private Boolean useIncludedDriver;
private String credentialsId;
protected Integer rollbackCount = 0;
private Integer rollbackLastHours;
Expand All @@ -31,11 +28,6 @@ public class LiquibaseContext implements Context {
protected boolean tagOnSuccessfulBuild;
private String outputDirectory;


void databaseEngine(String databaseEngine) {
this.databaseEngine = databaseEngine;
}

void changeLogFile(String changeLogFile) {
this.changeLogFile = changeLogFile;
}
Expand Down Expand Up @@ -63,10 +55,6 @@ void classpath(String classpath) {
this.classpath = classpath;
}

void driverClassname(String driverClassname) {
this.driverClassname = driverClassname;
}

void labels(String labels) {
this.labels = labels;
}
Expand All @@ -82,9 +70,6 @@ void basePath(String basePath) {
void rollbackCount(int rollbackCount) {
this.rollbackCount = rollbackCount;
}
public String getDatabaseEngine() {
return databaseEngine;
}

public String getChangeLogFile() {
return changeLogFile;
Expand All @@ -110,10 +95,6 @@ public String getClasspath() {
return classpath;
}

public String getDriverClassname() {
return driverClassname;
}

public String getLabels() {
return labels;
}
Expand All @@ -126,14 +107,6 @@ public String getBasePath() {
return basePath;
}

public Boolean getUseIncludedDriver() {
return useIncludedDriver;
}

public void setUseIncludedDriver(Boolean useIncludedDriver) {
this.useIncludedDriver = useIncludedDriver;
}

public String getCredentialsId() {
return credentialsId;
}
Expand Down
Expand Up @@ -79,13 +79,8 @@ private static void setCommonBuilderProperties(AbstractLiquibaseBuilder builder,
builder.setContexts(context.getContexts());
builder.setLiquibasePropertiesPath(context.getLiquibasePropertiesPath());
builder.setClasspath(context.getClasspath());
builder.setDriverClassname(context.getDriverClassname());
builder.setLabels(context.getLabels());
builder.setCredentialsId(context.getCredentialsId());
builder.setBasePath(context.getBasePath());
if (context.getDatabaseEngine() != null) {
builder.setUseIncludedDriver(true);
}

}
}
Expand Up @@ -53,11 +53,9 @@ public abstract class AbstractLiquibaseBuilder extends Builder implements Simple
protected String contexts;
protected String liquibasePropertiesPath;
protected String classpath;
protected String driverClassname;
protected String labels;
private String changeLogParameters;
private String basePath;
private Boolean useIncludedDriver;
private String credentialsId;


Expand All @@ -74,23 +72,20 @@ public AbstractLiquibaseBuilder(String databaseEngine,
String contexts,
String liquibasePropertiesPath,
String classpath,
String driverClassname,
String changeLogParameters,
String labels,
String basePath,
boolean useIncludedDriver, String credentialsId) {
String credentialsId) {
this.databaseEngine = databaseEngine;
this.changeLogFile = changeLogFile;
this.url = url;
this.defaultSchemaName = defaultSchemaName;
this.contexts = contexts;
this.liquibasePropertiesPath = liquibasePropertiesPath;
this.classpath = classpath;
this.driverClassname = driverClassname;
this.changeLogParameters = changeLogParameters;
this.labels = labels;
this.basePath = basePath;
this.useIncludedDriver = useIncludedDriver;
this.credentialsId = credentialsId;
}

Expand All @@ -99,9 +94,6 @@ public AbstractLiquibaseBuilder() {
}

protected Object readResolve() {
if (useIncludedDriver == null) {
useIncludedDriver = Strings.isNullOrEmpty(driverClassname);
}
return this;
}

Expand Down Expand Up @@ -151,7 +143,6 @@ public Liquibase createLiquibase(Run<?, ?> build,
Properties configProperties,
Launcher launcher, FilePath workspace) throws IOException, InterruptedException {
Liquibase liquibase;
String driverName = getProperty(configProperties, LiquibaseProperty.DRIVER);
String resolvedClasspath = getProperty(configProperties, LiquibaseProperty.CLASSPATH);

boolean resolveMacros = build instanceof AbstractBuild;
Expand All @@ -161,7 +152,7 @@ public Liquibase createLiquibase(Run<?, ?> build,
if (!Strings.isNullOrEmpty(resolvedClasspath)) {
Util.addClassloader(launcher.isUnix(), workspace, resolvedClasspath);
}
JdbcConnection jdbcConnection = createJdbcConnection(configProperties, driverName);
JdbcConnection jdbcConnection = createJdbcConnection(configProperties);
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection);
ResourceAccessor resourceAccessor = createResourceAccessor(workspace, environment, resolveMacros);

Expand Down Expand Up @@ -222,11 +213,11 @@ protected static void populateChangeLogParameters(Liquibase liquibase,
}
}

private static JdbcConnection createJdbcConnection(Properties configProperties, String driverName) {
private static JdbcConnection createJdbcConnection(Properties configProperties) {
Connection connection;
String dbUrl = getProperty(configProperties, LiquibaseProperty.URL);
final String driverName = DatabaseFactory.getInstance().findDefaultDriver(dbUrl);
try {

Util.registerDatabaseDriver(driverName,
configProperties.getProperty(LiquibaseProperty.CLASSPATH.propertyName()));
String userName = getProperty(configProperties, LiquibaseProperty.USERNAME);
Expand Down Expand Up @@ -266,10 +257,6 @@ private static void closeLiquibase(Liquibase liquibase) {
}
}

public List<IncludedDatabaseDriver> getDrivers() {
return ChangesetEvaluator.DESCRIPTOR.getIncludedDatabaseDrivers();
}

public String getDatabaseEngine() {
return databaseEngine;
}
Expand Down Expand Up @@ -333,15 +320,6 @@ public void setClasspath(String classpath) {
this.classpath = classpath;
}

public String getDriverClassname() {
return driverClassname;
}

@DataBoundSetter
public void setDriverClassname(String driverClassname) {
this.driverClassname = driverClassname;
}

public String getChangeLogParameters() {
return changeLogParameters;
}
Expand All @@ -368,25 +346,10 @@ public String getBasePath() {
public void setBasePath(String basePath) {
this.basePath = basePath;
}
public void clearDriverClassname() {
driverClassname = null;
}

public void clearDatabaseEngine() {
databaseEngine=null;
}
public boolean hasUseIncludedDriverBeenSet() {
return useIncludedDriver!=null;
}

public boolean isUseIncludedDriver() {
return useIncludedDriver;
}

@DataBoundSetter
public void setUseIncludedDriver(Boolean useIncludedDriver) {
this.useIncludedDriver = useIncludedDriver;
}

public String getCredentialsId() {
return credentialsId;
Expand Down

0 comments on commit 382a1ea

Please sign in to comment.