From 5901e51045ad7f3140e8200bb99b93ffb44fe57b Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 17 May 2016 14:46:40 +0200 Subject: [PATCH] SONAR-7271 add section "Settings" to WS api/system/info --- .../platform/monitoring/SettingsMonitor.java | 59 ++++++++++++++++ .../platformlevel/PlatformLevel4.java | 2 + .../monitoring/SettingsMonitorTest.java | 70 +++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/SettingsMonitor.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SettingsMonitorTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/SettingsMonitor.java b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/SettingsMonitor.java new file mode 100644 index 00000000000..90969dc2634 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/SettingsMonitor.java @@ -0,0 +1,59 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.monitoring; + +import com.google.common.collect.ImmutableSortedMap; +import java.util.Map; +import java.util.SortedMap; +import org.sonar.api.PropertyType; +import org.sonar.api.config.PropertyDefinition; +import org.sonar.api.config.PropertyDefinitions; +import org.sonar.api.config.Settings; + +import static org.apache.commons.lang.StringUtils.abbreviate; + +public class SettingsMonitor implements Monitor { + + static final int MAX_VALUE_LENGTH = 500; + private final Settings settings; + + public SettingsMonitor(Settings settings) { + this.settings = settings; + } + + @Override + public String name() { + return "Settings"; + } + + @Override + public SortedMap attributes() { + PropertyDefinitions definitions = settings.getDefinitions(); + ImmutableSortedMap.Builder builder = ImmutableSortedMap.naturalOrder(); + for (Map.Entry prop : settings.getProperties().entrySet()) { + String key = prop.getKey(); + PropertyDefinition def = definitions.get(key); + if (def == null || def.type() != PropertyType.PASSWORD) { + builder.put(key, abbreviate(prop.getValue(), MAX_VALUE_LENGTH)); + } + } + return builder.build(); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 40de8952056..9e70631f342 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -154,6 +154,7 @@ import org.sonar.server.platform.monitoring.EsMonitor; import org.sonar.server.platform.monitoring.JvmPropsMonitor; import org.sonar.server.platform.monitoring.PluginsMonitor; import org.sonar.server.platform.monitoring.ProcessSystemInfoClient; +import org.sonar.server.platform.monitoring.SettingsMonitor; import org.sonar.server.platform.monitoring.SonarQubeMonitor; import org.sonar.server.platform.monitoring.SystemMonitor; import org.sonar.server.platform.ws.ChangeLogLevelAction; @@ -641,6 +642,7 @@ public class PlatformLevel4 extends PlatformLevel { StatusAction.class, SystemWs.class, SystemMonitor.class, + SettingsMonitor.class, SonarQubeMonitor.class, EsMonitor.class, PluginsMonitor.class, diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SettingsMonitorTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SettingsMonitorTest.java new file mode 100644 index 00000000000..07e49e6d04b --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SettingsMonitorTest.java @@ -0,0 +1,70 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.monitoring; + +import java.util.SortedMap; +import org.junit.Test; +import org.sonar.api.PropertyType; +import org.sonar.api.config.PropertyDefinition; +import org.sonar.api.config.PropertyDefinitions; +import org.sonar.api.config.Settings; + +import static org.apache.commons.lang.StringUtils.repeat; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +public class SettingsMonitorTest { + + private static final String PASSWORD_PROPERTY = "sonar.password"; + + PropertyDefinitions defs = new PropertyDefinitions(PropertyDefinition.builder(PASSWORD_PROPERTY).type(PropertyType.PASSWORD).build()); + Settings settings = new Settings(defs); + SettingsMonitor underTest = new SettingsMonitor(settings); + + @Test + public void return_properties_and_sort_by_key() { + settings.setProperty("foo", "foo value"); + settings.setProperty("bar", "bar value"); + + SortedMap attributes = underTest.attributes(); + assertThat(attributes).containsExactly(entry("bar", "bar value"), entry("foo", "foo value")); + } + + @Test + public void truncate_long_property_values() { + settings.setProperty("foo", repeat("abcde", 1_000)); + + String value = (String) underTest.attributes().get("foo"); + assertThat(value).hasSize(SettingsMonitor.MAX_VALUE_LENGTH).startsWith("abcde"); + } + + @Test + public void exclude_password_properties() { + settings.setProperty(PASSWORD_PROPERTY, "abcde"); + + assertThat(underTest.attributes()).isEmpty(); + } + + @Test + public void test_monitor_name() { + assertThat(underTest.name()).isEqualTo("Settings"); + + } +} -- 2.39.5