From b089857aa59398d11f9844fd7513a7f1eec2c892 Mon Sep 17 00:00:00 2001 From: Scott Kurz Date: Thu, 25 Feb 2021 09:20:27 -0500 Subject: [PATCH] Merge in keystore_password in target server.env if present Signed-off-by: Scott Kurz --- docs/common-server-parameters.md | 3 +- .../maven/server/StartDebugMojoSupport.java | 40 +++++++++++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/docs/common-server-parameters.md b/docs/common-server-parameters.md index 15aafdc0e..1e49199fd 100644 --- a/docs/common-server-parameters.md +++ b/docs/common-server-parameters.md @@ -121,7 +121,8 @@ Starting with the 3.1 release of the liberty-maven-plugin, support is added to s | liberty.var.{var} | `` | liberty-plugin-variable-config.xml | The server configuration file is generated in the configDropins/overrides folder of the target server. | | liberty.defaultVar.{var} | `` | liberty-plugin-variable-config.xml | The server configuration file is generated in the configDropins/overrides folder of the target server. | -If Liberty configuration is specified with Maven properties, the above indicated files are created in the target Liberty server. By default there is no merging behavior for the Maven properties with files located in the `configDirectory` or the specific configuration file parameters such as `bootstrapPropertiesFile`, `jvmOptionsFile` and `serverEnvFile`. However, the `liberty.env.{var}` Maven properties can be merged with other configured `server.env` files by setting the `mergeServerEnv` parameter to `true`. +If Liberty configuration is specified with Maven properties, the above indicated files are created in the target Liberty server. By default there is no merging behavior for the Maven properties with files located in the `configDirectory` or the specific configuration file parameters such as `bootstrapPropertiesFile`, `jvmOptionsFile` and `serverEnvFile`. However, the `liberty.env.{var}` Maven properties can be merged with other configured `server.env` files by setting the `mergeServerEnv` parameter to `true`. As a special case when `mergeServerEnv` is `false`, an existing "keystore_password" property in the default generated `server.env` file in the target server will be merged in if there is no +serverEnvFile` configured nor `server.env` file located in the `configDirectory`, and the "keystore_password" env var is not defined as a Maven property. Note that properties specified with `-D` on the command line are also analyzed for the property name formats listed above and take precedence over Maven properties specified in the pom.xml. diff --git a/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/StartDebugMojoSupport.java b/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/StartDebugMojoSupport.java index dab87d93f..3bddc71ca 100644 --- a/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/StartDebugMojoSupport.java +++ b/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/StartDebugMojoSupport.java @@ -17,14 +17,14 @@ import static org.twdata.maven.mojoexecutor.MojoExecutor.artifactId; import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration; +import static org.twdata.maven.mojoexecutor.MojoExecutor.element; import static org.twdata.maven.mojoexecutor.MojoExecutor.executeMojo; import static org.twdata.maven.mojoexecutor.MojoExecutor.executionEnvironment; import static org.twdata.maven.mojoexecutor.MojoExecutor.goal; import static org.twdata.maven.mojoexecutor.MojoExecutor.groupId; +import static org.twdata.maven.mojoexecutor.MojoExecutor.name; import static org.twdata.maven.mojoexecutor.MojoExecutor.plugin; import static org.twdata.maven.mojoexecutor.MojoExecutor.version; -import static org.twdata.maven.mojoexecutor.MojoExecutor.element; -import static org.twdata.maven.mojoexecutor.MojoExecutor.name; import java.io.BufferedReader; import java.io.File; @@ -36,6 +36,7 @@ import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -493,10 +494,14 @@ protected void copyConfigFiles() throws Exception { } else { if (!envMavenProps.isEmpty()) { - if (serverEnvPath != null) { + Map envPropsToWrite = envMavenProps; + if (serverEnvFile == null && serverEnvPath == null) { + // Do a special case merge but ONLY if there are no other config options present + envPropsToWrite = mergeSpecialPropsFromInstallServerEnvIfAbsent(envMavenProps); + } else if (serverEnvPath != null) { log.warn("The " + serverEnvPath + " file is overwritten by inlined configuration."); } - writeServerEnvProperties(envFile, envMavenProps); + writeServerEnvProperties(envFile, envPropsToWrite); serverEnvPath = "inlined configuration"; } else if (serverEnvFile != null && serverEnvFile.exists()) { Copy copy = (Copy) ant.createTask("copy"); @@ -540,6 +545,33 @@ protected void copyConfigFiles() throws Exception { copyDependencies(); } + /** + * Merges envProps with special properties found in the install (target) server.env. We return a clone/copy of + * envProps, to which any of a list of special properties found in server.env have been added. We give precedence + * to properties already in envProps. + */ + private Map mergeSpecialPropsFromInstallServerEnvIfAbsent(Map envProps) throws IOException { + + String[] specialProps = { "keystore_password" }; + + Map mergedProps = new HashMap(); + + // Clone to avoid side effects + for (String key : envProps.keySet()) { + mergedProps.put(key, envProps.get(key)); + } + + // From install (target) dir + File serverEnv = new File(serverDirectory, "server.env"); + Map serverEnvProps = convertServerEnvToProperties(serverEnv); + + for (String propertyName : specialProps) { + mergedProps.putIfAbsent(propertyName, serverEnvProps.get(propertyName)); + } + + return mergedProps; + } + // Merges configured serverEnvFile with envMavenProps if specified, and returns the updated serverEnvPath private String mergeServerEnvFileAndEnvMavenProps(String serverEnvPath) throws IOException { String modifiedServerEnvPath = serverEnvPath;