Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SettingValidations.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.setting.ws;
  21. import com.google.gson.Gson;
  22. import com.google.gson.JsonElement;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.util.Collection;
  26. import java.util.List;
  27. import java.util.Locale;
  28. import java.util.Optional;
  29. import java.util.Set;
  30. import java.util.function.Consumer;
  31. import java.util.stream.Collectors;
  32. import javax.annotation.CheckForNull;
  33. import javax.annotation.Nullable;
  34. import org.everit.json.schema.ValidationException;
  35. import org.everit.json.schema.loader.SchemaLoader;
  36. import org.json.JSONObject;
  37. import org.json.JSONTokener;
  38. import org.sonar.api.PropertyType;
  39. import org.sonar.api.config.PropertyDefinition;
  40. import org.sonar.api.config.PropertyDefinitions;
  41. import org.sonar.api.resources.Qualifiers;
  42. import org.sonar.api.resources.Scopes;
  43. import org.sonar.core.i18n.I18n;
  44. import org.sonar.db.DbClient;
  45. import org.sonar.db.DbSession;
  46. import org.sonar.db.component.ComponentDto;
  47. import org.sonar.db.metric.MetricDto;
  48. import org.sonar.db.user.UserDto;
  49. import org.sonar.server.exceptions.BadRequestException;
  50. import static java.lang.String.format;
  51. import static java.util.Arrays.asList;
  52. import static java.util.Objects.requireNonNull;
  53. import static org.sonar.server.exceptions.BadRequestException.checkRequest;
  54. public class SettingValidations {
  55. private static final Collection<String> SECURITY_JSON_PROPERTIES = asList(
  56. "sonar.security.config.javasecurity",
  57. "sonar.security.config.phpsecurity",
  58. "sonar.security.config.pythonsecurity",
  59. "sonar.security.config.roslyn.sonaranalyzer.security.cs"
  60. );
  61. private static final Set<String> SUPPORTED_QUALIFIERS = Set.of(Qualifiers.PROJECT, Qualifiers.VIEW, Qualifiers.APP, Qualifiers.SUBVIEW);
  62. private final PropertyDefinitions definitions;
  63. private final DbClient dbClient;
  64. private final I18n i18n;
  65. public SettingValidations(PropertyDefinitions definitions, DbClient dbClient, I18n i18n) {
  66. this.definitions = definitions;
  67. this.dbClient = dbClient;
  68. this.i18n = i18n;
  69. }
  70. public Consumer<SettingData> scope() {
  71. return data -> {
  72. PropertyDefinition definition = definitions.get(data.key);
  73. checkRequest(data.component != null || definition == null || definition.global() || isGlobal(definition),
  74. "Setting '%s' cannot be global", data.key);
  75. };
  76. }
  77. public Consumer<SettingData> qualifier() {
  78. return data -> {
  79. String qualifier = data.component == null ? "" : data.component.qualifier();
  80. PropertyDefinition definition = definitions.get(data.key);
  81. checkRequest(checkComponentScopeAndQualifier(data, definition),
  82. "Setting '%s' cannot be set on a %s", data.key, i18n.message(Locale.ENGLISH, "qualifier." + qualifier, null));
  83. };
  84. }
  85. private static boolean checkComponentScopeAndQualifier(SettingData data, @Nullable PropertyDefinition definition) {
  86. ComponentDto component = data.component;
  87. if (component == null) {
  88. return true;
  89. }
  90. if (!Scopes.PROJECT.equals(component.scope())) {
  91. return false;
  92. }
  93. if (definition == null) {
  94. return SUPPORTED_QUALIFIERS.contains(component.qualifier());
  95. }
  96. return definition.qualifiers().contains(component.qualifier());
  97. }
  98. public Consumer<SettingData> valueType() {
  99. return new ValueTypeValidation();
  100. }
  101. private static boolean isGlobal(PropertyDefinition definition) {
  102. return !definition.global() && definition.qualifiers().isEmpty();
  103. }
  104. static class SettingData {
  105. private final String key;
  106. private final List<String> values;
  107. @CheckForNull
  108. private final ComponentDto component;
  109. SettingData(String key, List<String> values, @Nullable ComponentDto component) {
  110. this.key = requireNonNull(key);
  111. this.values = requireNonNull(values);
  112. this.component = component;
  113. }
  114. }
  115. private class ValueTypeValidation implements Consumer<SettingData> {
  116. @Override
  117. public void accept(SettingData data) {
  118. PropertyDefinition definition = definitions.get(data.key);
  119. if (definition == null) {
  120. return;
  121. }
  122. if (definition.type() == PropertyType.METRIC) {
  123. validateMetric(data);
  124. } else if (definition.type() == PropertyType.USER_LOGIN) {
  125. validateLogin(data);
  126. } else if (definition.type() == PropertyType.JSON) {
  127. validateJson(data, definition);
  128. } else {
  129. validateOtherTypes(data, definition);
  130. }
  131. }
  132. private void validateOtherTypes(SettingData data, PropertyDefinition definition) {
  133. data.values.stream()
  134. .map(definition::validate)
  135. .filter(result -> !result.isValid())
  136. .findAny()
  137. .ifPresent(result -> {
  138. throw BadRequestException.create(i18n.message(Locale.ENGLISH, "property.error." + result.getErrorKey(),
  139. format("Error when validating setting with key '%s' and value [%s]", data.key, data.values.stream().collect(Collectors.joining(", ")))));
  140. });
  141. }
  142. private void validateMetric(SettingData data) {
  143. try (DbSession dbSession = dbClient.openSession(false)) {
  144. List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, data.values).stream().filter(MetricDto::isEnabled).toList();
  145. checkRequest(data.values.size() == metrics.size(), "Error when validating metric setting with key '%s' and values [%s]. A value is not a valid metric key.",
  146. data.key, data.values.stream().collect(Collectors.joining(", ")));
  147. }
  148. }
  149. private void validateLogin(SettingData data) {
  150. try (DbSession dbSession = dbClient.openSession(false)) {
  151. List<UserDto> users = dbClient.userDao().selectByLogins(dbSession, data.values).stream().filter(UserDto::isActive).toList();
  152. checkRequest(data.values.size() == users.size(), "Error when validating login setting with key '%s' and values [%s]. A value is not a valid login.",
  153. data.key, data.values.stream().collect(Collectors.joining(", ")));
  154. }
  155. }
  156. private void validateJson(SettingData data, PropertyDefinition definition) {
  157. Optional<String> jsonContent = data.values.stream().findFirst();
  158. if (jsonContent.isPresent()) {
  159. try {
  160. new Gson().getAdapter(JsonElement.class).fromJson(jsonContent.get());
  161. validateJsonSchema(jsonContent.get(), definition);
  162. } catch (ValidationException e) {
  163. throw new IllegalArgumentException(String.format("Provided JSON is invalid [%s]", e.getMessage()));
  164. } catch (IOException e) {
  165. throw new IllegalArgumentException("Provided JSON is invalid");
  166. }
  167. }
  168. }
  169. private void validateJsonSchema(String json, PropertyDefinition definition) {
  170. if (SECURITY_JSON_PROPERTIES.contains(definition.key())) {
  171. InputStream jsonSchemaInputStream = this.getClass().getClassLoader().getResourceAsStream("json-schemas/security.json");
  172. if (jsonSchemaInputStream != null) {
  173. JSONObject jsonSchema = new JSONObject(new JSONTokener(jsonSchemaInputStream));
  174. JSONObject jsonSubject = new JSONObject(new JSONTokener(json));
  175. SchemaLoader.load(jsonSchema).validate(jsonSubject);
  176. }
  177. }
  178. }
  179. }
  180. }