From e95907edae36260a2116187ab5cf925b76c7dd34 Mon Sep 17 00:00:00 2001 From: Eric Giffon Date: Thu, 24 Aug 2023 14:52:43 +0200 Subject: [PATCH] SONAR-19960 Drop environment variable interpolation in sonar.properties --- .../config/AppSettingsLoaderImpl.java | 2 -- .../org/sonar/process/ConfigurationUtils.java | 27 ------------------- .../sonar/process/ConfigurationUtilsTest.java | 26 ------------------ 3 files changed, 55 deletions(-) diff --git a/server/sonar-main/src/main/java/org/sonar/application/config/AppSettingsLoaderImpl.java b/server/sonar-main/src/main/java/org/sonar/application/config/AppSettingsLoaderImpl.java index e0e64277141..c96f054417d 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/config/AppSettingsLoaderImpl.java +++ b/server/sonar-main/src/main/java/org/sonar/application/config/AppSettingsLoaderImpl.java @@ -34,7 +34,6 @@ import java.util.stream.Collectors; import org.slf4j.LoggerFactory; import org.sonar.core.extension.ServiceLoaderWrapper; import org.sonar.core.util.SettingFormatter; -import org.sonar.process.ConfigurationUtils; import org.sonar.process.NetworkUtilsImpl; import org.sonar.process.ProcessProperties; import org.sonar.process.Props; @@ -110,7 +109,6 @@ public class AppSettingsLoaderImpl implements AppSettingsLoader { loadPropertiesFromEnvironment(system, p, keysOverridableFromEnv); p.putAll(CommandLineParser.parseArguments(cliArguments)); p.setProperty(PATH_HOME.getKey(), homeDir.getAbsolutePath()); - p = ConfigurationUtils.interpolateVariables(p, system.getenv()); // the difference between Properties and Props is that the latter // supports decryption of values, so it must be used when values diff --git a/server/sonar-process/src/main/java/org/sonar/process/ConfigurationUtils.java b/server/sonar-process/src/main/java/org/sonar/process/ConfigurationUtils.java index f034ae71dd9..eeac5496d98 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/ConfigurationUtils.java +++ b/server/sonar-process/src/main/java/org/sonar/process/ConfigurationUtils.java @@ -24,44 +24,17 @@ import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.StandardCharsets; -import java.util.Enumeration; -import java.util.Map; import java.util.Properties; import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.text.StrSubstitutor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.core.util.SettingFormatter; import static org.sonar.process.FileUtils2.deleteQuietly; public final class ConfigurationUtils { - private static final Logger LOG = LoggerFactory.getLogger(ConfigurationUtils.class); - private static final String ENV_VAR_INTERPOLATION_PREFIX = "${env:"; - private static final String ENV_VAR_INTERPOLATION_POSTFIX = "}"; private ConfigurationUtils() { // Utility class } - public static Properties interpolateVariables(Properties properties, Map variables) { - Properties result = new Properties(); - Enumeration keys = properties.keys(); - while (keys.hasMoreElements()) { - String key = (String) keys.nextElement(); - String value = (String) properties.get(key); - if (value.contains(ENV_VAR_INTERPOLATION_PREFIX)) { - String environmentVariableName = SettingFormatter.fromJavaPropertyToEnvVariable(key); - LOG.warn("Referencing environment variables in configuration is deprecated and will be removed in a future version of SonarQube. " + - "You should stop using '{}' in your configuration and use the '{}' environment variable instead.", value, environmentVariableName); - } - - String interpolatedValue = StrSubstitutor.replace(value, variables, ENV_VAR_INTERPOLATION_PREFIX, ENV_VAR_INTERPOLATION_POSTFIX); - result.setProperty(key, interpolatedValue); - } - return result; - } - static Props loadPropsFromCommandLineArgs(String[] args) { if (args.length != 1) { throw new IllegalArgumentException("Only a single command-line argument is accepted " + diff --git a/server/sonar-process/src/test/java/org/sonar/process/ConfigurationUtilsTest.java b/server/sonar-process/src/test/java/org/sonar/process/ConfigurationUtilsTest.java index e344711b6d6..f9a3bdf061e 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/ConfigurationUtilsTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/ConfigurationUtilsTest.java @@ -21,9 +21,6 @@ package org.sonar.process; import java.io.File; import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; import org.apache.commons.io.FileUtils; import org.junit.Rule; import org.junit.Test; @@ -38,29 +35,6 @@ public class ConfigurationUtilsTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); - @Test - public void shouldInterpolateVariables() { - Properties input = new Properties(); - input.setProperty("hello", "world"); - input.setProperty("url", "${env:SONAR_JDBC_URL}"); - input.setProperty("do_not_change", "${SONAR_JDBC_URL}"); - Map variables = new HashMap<>(); - variables.put("SONAR_JDBC_URL", "jdbc:h2:mem"); - - Properties output = ConfigurationUtils.interpolateVariables(input, variables); - - assertThat(output).hasSize(3); - assertThat(output.getProperty("hello")).isEqualTo("world"); - assertThat(output.getProperty("url")).isEqualTo("jdbc:h2:mem"); - assertThat(output.getProperty("do_not_change")).isEqualTo("${SONAR_JDBC_URL}"); - - // input is not changed - assertThat(input).hasSize(3); - assertThat(input.getProperty("hello")).isEqualTo("world"); - assertThat(input.getProperty("url")).isEqualTo("${env:SONAR_JDBC_URL}"); - assertThat(input.getProperty("do_not_change")).isEqualTo("${SONAR_JDBC_URL}"); - } - @Test public void loadPropsFromCommandLineArgs_missing_argument() { try { -- 2.39.5