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.

DefaultConfiguration.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.scanner.config;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.Optional;
  25. import javax.annotation.concurrent.Immutable;
  26. import org.apache.commons.lang.ArrayUtils;
  27. import org.sonar.api.config.Configuration;
  28. import org.sonar.api.config.internal.Encryption;
  29. import org.sonar.api.config.PropertyDefinition;
  30. import org.sonar.api.config.PropertyDefinitions;
  31. import org.sonar.api.utils.log.Logger;
  32. import org.sonar.api.utils.log.Loggers;
  33. import static java.util.Objects.requireNonNull;
  34. import static org.apache.commons.lang.StringUtils.trim;
  35. import static org.sonar.api.config.internal.MultivalueProperty.parseAsCsv;
  36. @Immutable
  37. public abstract class DefaultConfiguration implements Configuration {
  38. private static final Logger LOG = Loggers.get(DefaultConfiguration.class);
  39. private final PropertyDefinitions definitions;
  40. private final Encryption encryption;
  41. private final Map<String, String> properties;
  42. private final Map<String, String> originalProperties;
  43. public DefaultConfiguration(PropertyDefinitions propertyDefinitions, Encryption encryption, Map<String, String> props) {
  44. this.definitions = requireNonNull(propertyDefinitions);
  45. this.encryption = encryption;
  46. this.properties = unmodifiableMapWithTrimmedValues(definitions, props);
  47. this.originalProperties = Collections.unmodifiableMap(props);
  48. }
  49. protected static Map<String, String> unmodifiableMapWithTrimmedValues(PropertyDefinitions definitions, Map<String, String> props) {
  50. Map<String, String> map = new HashMap<>(props.size());
  51. props.forEach((k, v) -> {
  52. String validKey = definitions.validKey(k);
  53. map.put(validKey, trim(v));
  54. });
  55. return Collections.unmodifiableMap(map);
  56. }
  57. public Encryption getEncryption() {
  58. return encryption;
  59. }
  60. public PropertyDefinitions getDefinitions() {
  61. return definitions;
  62. }
  63. public Map<String, String> getProperties() {
  64. return properties;
  65. }
  66. public Map<String, String> getOriginalProperties() {
  67. return originalProperties;
  68. }
  69. @Override
  70. public boolean hasKey(String key) {
  71. return properties.containsKey(key);
  72. }
  73. @Override
  74. public Optional<String> get(String key) {
  75. String effectiveKey = definitions.validKey(key);
  76. PropertyDefinition def = definitions.get(effectiveKey);
  77. if (def != null && (def.multiValues() || !def.fields().isEmpty())) {
  78. LOG.warn("Access to the multi-values/property set property '{}' should be made using 'getStringArray' method. The SonarQube plugin using this property should be updated.",
  79. key);
  80. }
  81. return getInternal(effectiveKey);
  82. }
  83. @Override
  84. public String[] getStringArray(String key) {
  85. String effectiveKey = definitions.validKey(key);
  86. PropertyDefinition def = definitions.get(effectiveKey);
  87. if (def != null && !def.multiValues() && def.fields().isEmpty()) {
  88. LOG.warn(
  89. "Property '{}' is not declared as multi-values/property set but was read using 'getStringArray' method. The SonarQube plugin declaring this property should be updated.",
  90. key);
  91. }
  92. Optional<String> value = getInternal(effectiveKey);
  93. if (value.isPresent()) {
  94. return parseAsCsv(effectiveKey, value.get());
  95. }
  96. return ArrayUtils.EMPTY_STRING_ARRAY;
  97. }
  98. private Optional<String> getInternal(String key) {
  99. Optional<String> value = Optional.ofNullable(properties.get(key));
  100. if (!value.isPresent()) {
  101. // default values cannot be encrypted, so return value as-is.
  102. return Optional.ofNullable(definitions.getDefaultValue(key));
  103. }
  104. if (encryption.isEncrypted(value.get())) {
  105. try {
  106. return Optional.of(encryption.decrypt(value.get()));
  107. } catch (Exception e) {
  108. throw new IllegalStateException("Fail to decrypt the property " + key + ". Please check your secret key.", e);
  109. }
  110. }
  111. return value;
  112. }
  113. }