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.

SettingValidations.java 7.9KB

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