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.

SettingsSection.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 java.util.Map;
  22. import java.util.TreeMap;
  23. import org.sonar.api.PropertyType;
  24. import org.sonar.api.config.PropertyDefinition;
  25. import org.sonar.api.config.PropertyDefinitions;
  26. import org.sonar.api.config.internal.Settings;
  27. import org.sonar.api.server.ServerSide;
  28. import org.sonar.process.systeminfo.Global;
  29. import org.sonar.process.systeminfo.SystemInfoSection;
  30. import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
  31. import static org.apache.commons.lang.StringUtils.abbreviate;
  32. import static org.apache.commons.lang.StringUtils.containsIgnoreCase;
  33. import static org.apache.commons.lang.StringUtils.endsWithIgnoreCase;
  34. import static org.sonar.process.ProcessProperties.Property.AUTH_JWT_SECRET;
  35. import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute;
  36. @ServerSide
  37. public class SettingsSection implements SystemInfoSection, Global {
  38. private static final int MAX_VALUE_LENGTH = 500;
  39. private static final String PASSWORD_VALUE = "xxxxxxxx";
  40. private final Settings settings;
  41. public SettingsSection(Settings settings) {
  42. this.settings = settings;
  43. }
  44. @Override
  45. public ProtobufSystemInfo.Section toProtobuf() {
  46. ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder();
  47. protobuf.setName("Settings");
  48. PropertyDefinitions definitions = settings.getDefinitions();
  49. TreeMap<String, String> orderedProps = new TreeMap<>(settings.getProperties());
  50. for (Map.Entry<String, String> prop : orderedProps.entrySet()) {
  51. String key = prop.getKey();
  52. String value = obfuscateValue(definitions, key, prop.getValue());
  53. setAttribute(protobuf, key, value);
  54. }
  55. return protobuf.build();
  56. }
  57. private static String obfuscateValue(PropertyDefinitions definitions, String key, String value) {
  58. PropertyDefinition def = definitions.get(key);
  59. if (def != null && def.type() == PropertyType.PASSWORD) {
  60. return PASSWORD_VALUE;
  61. }
  62. if (endsWithIgnoreCase(key, ".secured") ||
  63. containsIgnoreCase(key, "password") ||
  64. containsIgnoreCase(key, "passcode") ||
  65. AUTH_JWT_SECRET.getKey().equals(key)) {
  66. return PASSWORD_VALUE;
  67. }
  68. return abbreviate(value, MAX_VALUE_LENGTH);
  69. }
  70. }