diff options
author | Michal Duda <michal.duda@sonarsource.com> | 2020-04-08 19:38:02 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-04-14 20:04:04 +0000 |
commit | d662d0c0269e1a12da0c3ecdb8f9d33b765ccdcc (patch) | |
tree | 7ec6d738cc8516bf42f42552a64f1cdbb0307a37 /server/sonar-main | |
parent | ab6328a769fbe531464fc9dd77cc64efc72ba390 (diff) | |
download | sonarqube-d662d0c0269e1a12da0c3ecdb8f9d33b765ccdcc.tar.gz sonarqube-d662d0c0269e1a12da0c3ecdb8f9d33b765ccdcc.zip |
SONAR-13272 fix issue with setting some properties through env variables
Diffstat (limited to 'server/sonar-main')
4 files changed, 66 insertions, 18 deletions
diff --git a/server/sonar-main/src/main/java/org/sonar/application/command/CommandFactoryImpl.java b/server/sonar-main/src/main/java/org/sonar/application/command/CommandFactoryImpl.java index 13f0b3a6101..2ad11760b3d 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/command/CommandFactoryImpl.java +++ b/server/sonar-main/src/main/java/org/sonar/application/command/CommandFactoryImpl.java @@ -39,7 +39,7 @@ import static org.sonar.process.ProcessProperties.Property.CE_JAVA_ADDITIONAL_OP import static org.sonar.process.ProcessProperties.Property.CE_JAVA_OPTS; import static org.sonar.process.ProcessProperties.Property.HTTPS_PROXY_HOST; import static org.sonar.process.ProcessProperties.Property.HTTPS_PROXY_PORT; -import static org.sonar.process.ProcessProperties.Property.HTTP_AUTH_NLM_DOMAN; +import static org.sonar.process.ProcessProperties.Property.HTTP_AUTH_NTLM_DOMAIN; import static org.sonar.process.ProcessProperties.Property.HTTP_NON_PROXY_HOSTS; import static org.sonar.process.ProcessProperties.Property.HTTP_PROXY_HOST; import static org.sonar.process.ProcessProperties.Property.HTTP_PROXY_PORT; @@ -65,7 +65,7 @@ public class CommandFactoryImpl implements CommandFactory { HTTP_NON_PROXY_HOSTS.getKey(), HTTPS_PROXY_HOST.getKey(), HTTPS_PROXY_PORT.getKey(), - HTTP_AUTH_NLM_DOMAN.getKey(), + HTTP_AUTH_NTLM_DOMAIN.getKey(), SOCKS_PROXY_HOST.getKey(), SOCKS_PROXY_PORT.getKey()}; 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 3ba85f8e606..d3124e4aa20 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 @@ -25,7 +25,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.net.URISyntaxException; -import java.util.Arrays; +import java.util.HashSet; import java.util.Optional; import java.util.Properties; import java.util.Set; @@ -33,6 +33,7 @@ import java.util.function.Consumer; 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; @@ -40,8 +41,11 @@ import org.sonar.process.Props; import org.sonar.process.System2; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Arrays.stream; import static java.util.Optional.ofNullable; import static org.sonar.core.util.SettingFormatter.fromJavaPropertyToEnvVariable; +import static org.sonar.process.ProcessProperties.Property.LDAP_SERVERS; +import static org.sonar.process.ProcessProperties.Property.MULTI_SERVER_LDAP_SETTINGS; import static org.sonar.process.ProcessProperties.Property.PATH_HOME; public class AppSettingsLoaderImpl implements AppSettingsLoader { @@ -73,7 +77,37 @@ public class AppSettingsLoaderImpl implements AppSettingsLoader { @Override public AppSettings load() { Properties p = loadPropertiesFile(homeDir); - fetchSettingsFromEnvironment(system, p); + Set<String> keysOverridableFromEnv = stream(ProcessProperties.Property.values()).map(ProcessProperties.Property::getKey) + .collect(Collectors.toSet()); + keysOverridableFromEnv.addAll(p.stringPropertyNames()); + + // 1st pass to load static properties + Props staticProps = reloadProperties(keysOverridableFromEnv, p); + keysOverridableFromEnv.addAll(getDynamicPropertiesKeys(staticProps)); + + // 2nd pass to load dynamic properties like `ldap.*.url` or `ldap.*.baseDn` which keys depend on values of static + // properties loaded in 1st step + Props props = reloadProperties(keysOverridableFromEnv, p); + + new ProcessProperties(serviceLoaderWrapper).completeDefaults(props); + stream(consumers).forEach(c -> c.accept(props)); + return new AppSettingsImpl(props); + } + + private static Set<String> getDynamicPropertiesKeys(Props p) { + Set<String> dynamicPropertiesKeys = new HashSet<>(); + String ldapServersValue = p.value(LDAP_SERVERS.getKey()); + if (ldapServersValue != null) { + stream(SettingFormatter.getStringArrayBySeparator(ldapServersValue, ",")).forEach( + ldapServer -> MULTI_SERVER_LDAP_SETTINGS.forEach( + multiLdapSetting -> dynamicPropertiesKeys.add(multiLdapSetting.replace("*", ldapServer)))); + } + + return dynamicPropertiesKeys; + } + + private Props reloadProperties(Set<String> keysOverridableFromEnv, Properties p) { + loadPropertiesFromEnvironment(system, p, keysOverridableFromEnv); p.putAll(CommandLineParser.parseArguments(cliArguments)); p.setProperty(PATH_HOME.getKey(), homeDir.getAbsolutePath()); p = ConfigurationUtils.interpolateVariables(p, system.getenv()); @@ -81,18 +115,11 @@ public class AppSettingsLoaderImpl implements AppSettingsLoader { // the difference between Properties and Props is that the latter // supports decryption of values, so it must be used when values // are accessed - Props props = new Props(p); - new ProcessProperties(serviceLoaderWrapper).completeDefaults(props); - Arrays.stream(consumers).forEach(c -> c.accept(props)); - - return new AppSettingsImpl(props); + return new Props(p); } - private static void fetchSettingsFromEnvironment(System2 system, Properties properties) { - Set<String> possibleSettings = Arrays.stream(ProcessProperties.Property.values()).map(ProcessProperties.Property::getKey) - .collect(Collectors.toSet()); - possibleSettings.addAll(properties.stringPropertyNames()); - possibleSettings.forEach(key -> { + private static void loadPropertiesFromEnvironment(System2 system, Properties properties, Set<String> overridableSettings) { + overridableSettings.forEach(key -> { String environmentVarName = fromJavaPropertyToEnvVariable(key); Optional<String> envVarValue = ofNullable(system.getenv(environmentVarName)); envVarValue.ifPresent(value -> properties.put(key, value)); diff --git a/server/sonar-main/src/main/java/org/sonar/application/es/EsLogging.java b/server/sonar-main/src/main/java/org/sonar/application/es/EsLogging.java index d6c4aa8f9c5..d66029f6ec9 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/es/EsLogging.java +++ b/server/sonar-main/src/main/java/org/sonar/application/es/EsLogging.java @@ -23,15 +23,16 @@ import ch.qos.logback.classic.Level; import java.io.File; import java.util.Properties; import org.sonar.process.ProcessId; +import org.sonar.process.ProcessProperties; import org.sonar.process.Props; import org.sonar.process.logging.Log4JPropertiesBuilder; import org.sonar.process.logging.LogLevelConfig; import org.sonar.process.logging.RootLoggerConfig; +import static org.sonar.process.ProcessProperties.Property.LOG_CONSOLE; import static org.sonar.process.logging.RootLoggerConfig.newRootLoggerConfigBuilder; public class EsLogging { - private static final String ALL_LOGS_TO_CONSOLE_PROPERTY = "sonar.log.console"; public Properties createProperties(Props props, File logDir) { Log4JPropertiesBuilder log4JPropertiesBuilder = new Log4JPropertiesBuilder(props); @@ -51,12 +52,11 @@ public class EsLogging { return log4JPropertiesBuilder.get(); } - /** * Finds out whether we are in testing environment (usually ITs) and logs of all processes must be forward to - * App's System.out. This is specified by the value of property {@link #ALL_LOGS_TO_CONSOLE_PROPERTY}. + * App's System.out. This is specified by the value of property {@link ProcessProperties.Property#LOG_CONSOLE}. */ private static boolean isAllLogsToConsoleEnabled(Props props) { - return props.valueAsBoolean(ALL_LOGS_TO_CONSOLE_PROPERTY, false); + return props.valueAsBoolean(LOG_CONSOLE.getKey(), false); } } diff --git a/server/sonar-main/src/test/java/org/sonar/application/config/AppSettingsLoaderImplTest.java b/server/sonar-main/src/test/java/org/sonar/application/config/AppSettingsLoaderImplTest.java index 988107a6150..db8aa65047c 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/config/AppSettingsLoaderImplTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/config/AppSettingsLoaderImplTest.java @@ -88,6 +88,27 @@ public class AppSettingsLoaderImplTest { } @Test + public void load_multi_ldap_settings() throws IOException { + when(system.getenv()).thenReturn(ImmutableMap.of( + "LDAP_FOO_URL", "url1", + "LDAP_RANDOM_PROP", "5")); + when(system.getenv("LDAP_FOO_URL")).thenReturn("url1"); + when(system.getenv("LDAP_RANDOM_PROP")).thenReturn("5"); + File homeDir = temp.newFolder(); + File propsFile = new File(homeDir, "conf/sonar.properties"); + FileUtils.write(propsFile, "ldap.servers=foo,bar\n" + + "ldap.bar.url=url2", UTF_8); + AppSettingsLoaderImpl underTest = new AppSettingsLoaderImpl(system, new String[0], homeDir, serviceLoaderWrapper); + + AppSettings settings = underTest.load(); + + assertThat(settings.getProps().rawProperties()).contains( + entry("ldap.servers", "foo,bar"), + entry("ldap.foo.url", "url1"), + entry("ldap.bar.url", "url2")); + } + + @Test public void throws_ISE_if_file_fails_to_be_loaded() throws Exception { File homeDir = temp.newFolder(); File propsFileAsDir = new File(homeDir, "conf/sonar.properties"); |