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

expand values for url, username, password that use secrets vaults in maven plugin (DAT-16431) #5527

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.liquibase.maven.plugins;

import liquibase.*;
import liquibase.GlobalConfiguration;
import liquibase.Liquibase;
import liquibase.Scope;
import liquibase.ThreadLocalScopeManager;
import liquibase.changelog.visitor.ChangeExecListener;
import liquibase.changelog.visitor.DefaultChangeExecListener;
import liquibase.command.core.helpers.DbUrlConnectionCommandStep;
import liquibase.configuration.ConfiguredValueModifierFactory;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.configuration.core.DefaultsFileValueProvider;
import liquibase.database.Database;
Expand Down Expand Up @@ -100,6 +104,10 @@ public abstract class AbstractLiquibaseMojo extends AbstractMojo {
@PropertyElement
protected String url;

public void setUrl(String url) throws Exception {
this.url = modifyValue(url);
}

/**
* The Maven Wagon manager to use when obtaining server authentication details.
*
Expand All @@ -115,6 +123,11 @@ public abstract class AbstractLiquibaseMojo extends AbstractMojo {
*/
@PropertyElement
protected String username;

public void setUsername(String username) throws Exception {
this.username = modifyValue(username);
}

/**
* Specifies the database password for database connection.
*
Expand All @@ -123,6 +136,10 @@ public abstract class AbstractLiquibaseMojo extends AbstractMojo {
@PropertyElement
protected String password;

public void setPassword(String password) throws Exception {
this.password = modifyValue(password);
}

/**
* Use an empty string as the password for the database connection. This should not be
* used along side the {@link #password} setting.
Expand Down Expand Up @@ -1176,6 +1193,7 @@ private Object getDefaultValue(Field field) throws IllegalAccessException {
}

private void setFieldValue(Field field, String value) throws IllegalAccessException {
value = Scope.getCurrentScope().getSingleton(ConfiguredValueModifierFactory.class).override(value);
if (field.getType().equals(Boolean.class) || field.getType().equals(boolean.class)) {
field.set(this, Boolean.valueOf(value));
} else if (field.getType().isEnum()) {
Expand Down Expand Up @@ -1261,4 +1279,13 @@ protected OutputStream getOutputStream(String outputFile) throws LiquibaseExcept
}
return fileOut;
}

/**
* Calls the {@link ConfiguredValueModifierFactory} to expand the provided value.
* @return the expanded value, or the original value if expansion is not needed
* @throws Exception if expansion fails
*/
private String modifyValue(String value) throws Exception {
return Scope.child(Collections.singletonMap("liquibase.licenseKey", getLicenseKey()), () -> Scope.getCurrentScope().getSingleton(ConfiguredValueModifierFactory.class).override(value));
}
}