You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SettingsSectionTest.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.platform.monitoring;
  21. import org.junit.Test;
  22. import org.sonar.api.PropertyType;
  23. import org.sonar.api.config.PropertyDefinition;
  24. import org.sonar.api.config.PropertyDefinitions;
  25. import org.sonar.api.config.internal.Settings;
  26. import org.sonar.api.config.internal.MapSettings;
  27. import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
  28. import static org.apache.commons.lang.StringUtils.repeat;
  29. import static org.assertj.core.api.Assertions.assertThat;
  30. import static org.sonar.process.systeminfo.SystemInfoUtils.attribute;
  31. import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
  32. public class SettingsSectionTest {
  33. private static final String PASSWORD_PROPERTY = "sonar.password";
  34. private PropertyDefinitions defs = new PropertyDefinitions(PropertyDefinition.builder(PASSWORD_PROPERTY).type(PropertyType.PASSWORD).build());
  35. private Settings settings = new MapSettings(defs);
  36. private SettingsSection underTest = new SettingsSection(settings);
  37. @Test
  38. public void return_properties_and_sort_by_key() {
  39. settings.setProperty("foo", "foo value");
  40. settings.setProperty("bar", "bar value");
  41. ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
  42. assertThatAttributeIs(protobuf, "bar", "bar value");
  43. assertThatAttributeIs(protobuf, "foo", "foo value");
  44. // keys are ordered alphabetically
  45. assertThat(protobuf.getAttributesList())
  46. .extracting(ProtobufSystemInfo.Attribute::getKey)
  47. .containsExactly("bar", "foo");
  48. }
  49. @Test
  50. public void truncate_long_property_values() {
  51. settings.setProperty("foo", repeat("abcde", 1_000));
  52. ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
  53. String value = attribute(protobuf, "foo").getStringValue();
  54. assertThat(value).hasSize(500).startsWith("abcde");
  55. }
  56. @Test
  57. public void value_is_obfuscated_if_key_matches_patterns() {
  58. verifyObfuscated(PASSWORD_PROPERTY);
  59. verifyObfuscated("foo.password.something");
  60. // case insensitive search of "password" term
  61. verifyObfuscated("bar.CheckPassword");
  62. verifyObfuscated("foo.passcode.something");
  63. // case insensitive search of "passcode" term
  64. verifyObfuscated("bar.CheckPassCode");
  65. verifyObfuscated("foo.something.secured");
  66. verifyObfuscated("bar.something.Secured");
  67. verifyObfuscated("sonar.auth.jwtBase64Hs256Secret");
  68. verifyNotObfuscated("securedStuff");
  69. verifyNotObfuscated("foo");
  70. }
  71. private void verifyObfuscated(String key) {
  72. settings.setProperty(key, "foo");
  73. ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
  74. assertThatAttributeIs(protobuf, key, "xxxxxxxx");
  75. }
  76. private void verifyNotObfuscated(String key) {
  77. settings.setProperty(key, "foo");
  78. ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
  79. assertThatAttributeIs(protobuf, key, "foo");
  80. }
  81. @Test
  82. public void test_monitor_name() {
  83. assertThat(underTest.toProtobuf().getName()).isEqualTo("Settings");
  84. }
  85. }