From: Zipeng WU Date: Tue, 28 Mar 2023 12:46:14 +0000 (+0200) Subject: SONAR-18924 Remove node JVM setting from Global Setting of System Info if SQ is in... X-Git-Tag: 10.0.0.68432~49 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=692a4f9557d2050f354c44b2bc074a3a11c2be06;p=sonarqube.git SONAR-18924 Remove node JVM setting from Global Setting of System Info if SQ is in cluster mode --- diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/SettingsSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/SettingsSection.java index 5fd07459159..5ba82ce3154 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/SettingsSection.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/SettingsSection.java @@ -19,9 +19,11 @@ */ package org.sonar.server.platform.monitoring; +import java.util.Collection; import java.util.Map.Entry; import java.util.Optional; import java.util.TreeMap; +import java.util.stream.Stream; import org.sonar.api.PropertyType; import org.sonar.api.config.PropertyDefinition; import org.sonar.api.config.PropertyDefinitions; @@ -30,28 +32,48 @@ import org.sonar.api.server.ServerSide; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.newcodeperiod.NewCodePeriodDto; +import org.sonar.process.ProcessProperties.Property; import org.sonar.process.systeminfo.Global; import org.sonar.process.systeminfo.SystemInfoSection; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo.Section.Builder; +import org.sonar.server.platform.NodeInformation; +import static java.util.stream.Collectors.toUnmodifiableSet; import static org.apache.commons.lang.StringUtils.abbreviate; import static org.apache.commons.lang.StringUtils.containsIgnoreCase; import static org.apache.commons.lang.StringUtils.endsWithIgnoreCase; import static org.sonar.process.ProcessProperties.Property.AUTH_JWT_SECRET; +import static org.sonar.process.ProcessProperties.Property.CE_JAVA_ADDITIONAL_OPTS; +import static org.sonar.process.ProcessProperties.Property.CE_JAVA_OPTS; +import static org.sonar.process.ProcessProperties.Property.SEARCH_JAVA_ADDITIONAL_OPTS; +import static org.sonar.process.ProcessProperties.Property.SEARCH_JAVA_OPTS; +import static org.sonar.process.ProcessProperties.Property.WEB_JAVA_ADDITIONAL_OPTS; +import static org.sonar.process.ProcessProperties.Property.WEB_JAVA_OPTS; import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute; @ServerSide public class SettingsSection implements SystemInfoSection, Global { private static final int MAX_VALUE_LENGTH = 500; private static final String PASSWORD_VALUE = "xxxxxxxx"; + private static final Collection IGNORED_SETTINGS_IN_CLUSTER = Stream.of( + WEB_JAVA_OPTS, + WEB_JAVA_ADDITIONAL_OPTS, + CE_JAVA_OPTS, + CE_JAVA_ADDITIONAL_OPTS, + SEARCH_JAVA_OPTS, + SEARCH_JAVA_ADDITIONAL_OPTS) + .map(Property::getKey) + .collect(toUnmodifiableSet()); private final DbClient dbClient; private final Settings settings; + private final NodeInformation nodeInformation; - public SettingsSection(DbClient dbClient, Settings settings) { + public SettingsSection(DbClient dbClient, Settings settings, NodeInformation nodeInformation) { this.dbClient = dbClient; this.settings = settings; + this.nodeInformation = nodeInformation; } @Override @@ -61,14 +83,15 @@ public class SettingsSection implements SystemInfoSection, Global { PropertyDefinitions definitions = settings.getDefinitions(); TreeMap orderedProps = new TreeMap<>(settings.getProperties()); - for (Entry prop : orderedProps.entrySet()) { - includeSetting(protobuf, definitions, prop); - } + orderedProps.entrySet() + .stream() + .filter(prop -> nodeInformation.isStandalone() || !IGNORED_SETTINGS_IN_CLUSTER.contains(prop.getKey())) + .forEach(prop -> includeSetting(protobuf, definitions, prop)); addDefaultNewCodeDefinition(protobuf); return protobuf.build(); } - private static void includeSetting(Builder protobuf, PropertyDefinitions definitions, Entry prop) { + private void includeSetting(Builder protobuf, PropertyDefinitions definitions, Entry prop) { String key = prop.getKey(); String value = obfuscateValue(definitions, key, prop.getValue()); setAttribute(protobuf, key, value); diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/SettingsSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/SettingsSectionTest.java index eeed8ab3c79..ff4a919f668 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/SettingsSectionTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/SettingsSectionTest.java @@ -19,6 +19,7 @@ */ package org.sonar.server.platform.monitoring; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.sonar.api.PropertyType; @@ -30,10 +31,17 @@ import org.sonar.api.utils.System2; import org.sonar.db.DbTester; import org.sonar.db.newcodeperiod.NewCodePeriodType; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; +import org.sonar.server.platform.NodeInformation; import static org.apache.commons.lang.StringUtils.repeat; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.process.ProcessProperties.Property.CE_JAVA_OPTS; +import static org.sonar.process.ProcessProperties.Property.SEARCH_JAVA_OPTS; +import static org.sonar.process.ProcessProperties.Property.WEB_JAVA_OPTS; import static org.sonar.process.systeminfo.SystemInfoUtils.attribute; +import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeDoesNotExist; import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs; public class SettingsSectionTest { @@ -45,7 +53,39 @@ public class SettingsSectionTest { private PropertyDefinitions defs = new PropertyDefinitions(System2.INSTANCE, PropertyDefinition.builder(PASSWORD_PROPERTY).type(PropertyType.PASSWORD).build()); private Settings settings = new MapSettings(defs); - private SettingsSection underTest = new SettingsSection(dbTester.getDbClient(), settings); + private NodeInformation nodeInformation = mock(NodeInformation.class); + private SettingsSection underTest= new SettingsSection(dbTester.getDbClient(), settings, nodeInformation); + + @Before + public void setup(){ + when(nodeInformation.isStandalone()).thenReturn(true); + } + + @Test + public void should_show_java_settings_in_standalone(){ + settings.setProperty(WEB_JAVA_OPTS.getKey(), WEB_JAVA_OPTS.getDefaultValue()); + settings.setProperty(CE_JAVA_OPTS.getKey(), CE_JAVA_OPTS.getDefaultValue()); + settings.setProperty(SEARCH_JAVA_OPTS.getKey(), SEARCH_JAVA_OPTS.getDefaultValue()); + + ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); + + assertThatAttributeIs(protobuf, WEB_JAVA_OPTS.getKey(), WEB_JAVA_OPTS.getDefaultValue()); + assertThatAttributeIs(protobuf, CE_JAVA_OPTS.getKey(), CE_JAVA_OPTS.getDefaultValue()); + assertThatAttributeIs(protobuf, SEARCH_JAVA_OPTS.getKey(), SEARCH_JAVA_OPTS.getDefaultValue()); + } + @Test + public void should_not_show_java_settings_in_cluster(){ + when(nodeInformation.isStandalone()).thenReturn(false); + settings.setProperty(WEB_JAVA_OPTS.getKey(), WEB_JAVA_OPTS.getDefaultValue()); + settings.setProperty(CE_JAVA_OPTS.getKey(), CE_JAVA_OPTS.getDefaultValue()); + settings.setProperty(SEARCH_JAVA_OPTS.getKey(), SEARCH_JAVA_OPTS.getDefaultValue()); + + ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); + + assertThatAttributeDoesNotExist(protobuf, WEB_JAVA_OPTS.getKey()); + assertThatAttributeDoesNotExist(protobuf, CE_JAVA_OPTS.getKey()); + assertThatAttributeDoesNotExist(protobuf, SEARCH_JAVA_OPTS.getKey()); + } @Test public void return_properties_and_sort_by_key() {