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.

SearchAction.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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.rule.ws;
  21. import com.google.common.collect.ArrayListMultimap;
  22. import com.google.common.collect.ListMultimap;
  23. import com.google.common.collect.Lists;
  24. import com.google.common.collect.Maps;
  25. import com.google.common.collect.Ordering;
  26. import java.util.ArrayList;
  27. import java.util.Collection;
  28. import java.util.Collections;
  29. import java.util.HashMap;
  30. import java.util.HashSet;
  31. import java.util.Iterator;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.util.Objects;
  35. import java.util.Set;
  36. import java.util.stream.Collectors;
  37. import javax.annotation.CheckForNull;
  38. import javax.annotation.Nullable;
  39. import org.sonar.api.rule.Severity;
  40. import org.sonar.api.rules.RuleType;
  41. import org.sonar.api.server.ws.Change;
  42. import org.sonar.api.server.ws.Request;
  43. import org.sonar.api.server.ws.Response;
  44. import org.sonar.api.server.ws.WebService;
  45. import org.sonar.db.DbClient;
  46. import org.sonar.db.DbSession;
  47. import org.sonar.db.rule.DeprecatedRuleKeyDto;
  48. import org.sonar.db.rule.RuleDto;
  49. import org.sonar.db.rule.RuleParamDto;
  50. import org.sonar.db.user.UserDto;
  51. import org.sonar.server.es.Facets;
  52. import org.sonar.server.es.SearchIdResult;
  53. import org.sonar.server.es.SearchOptions;
  54. import org.sonar.server.rule.index.RuleIndex;
  55. import org.sonar.server.rule.index.RuleQuery;
  56. import org.sonarqube.ws.Common;
  57. import org.sonarqube.ws.Rules.SearchResponse;
  58. import static java.lang.String.format;
  59. import static org.sonar.api.server.ws.WebService.Param.ASCENDING;
  60. import static org.sonar.api.server.ws.WebService.Param.FACETS;
  61. import static org.sonar.api.server.ws.WebService.Param.FIELDS;
  62. import static org.sonar.api.server.ws.WebService.Param.PAGE;
  63. import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
  64. import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE;
  65. import static org.sonar.server.rule.index.RuleIndex.ALL_STATUSES_EXCEPT_REMOVED;
  66. import static org.sonar.server.rule.index.RuleIndex.FACET_ACTIVE_SEVERITIES;
  67. import static org.sonar.server.rule.index.RuleIndex.FACET_CWE;
  68. import static org.sonar.server.rule.index.RuleIndex.FACET_LANGUAGES;
  69. import static org.sonar.server.rule.index.RuleIndex.FACET_OLD_DEFAULT;
  70. import static org.sonar.server.rule.index.RuleIndex.FACET_OWASP_TOP_10;
  71. import static org.sonar.server.rule.index.RuleIndex.FACET_OWASP_TOP_10_2021;
  72. import static org.sonar.server.rule.index.RuleIndex.FACET_REPOSITORIES;
  73. import static org.sonar.server.rule.index.RuleIndex.FACET_SANS_TOP_25;
  74. import static org.sonar.server.rule.index.RuleIndex.FACET_SEVERITIES;
  75. import static org.sonar.server.rule.index.RuleIndex.FACET_SONARSOURCE_SECURITY;
  76. import static org.sonar.server.rule.index.RuleIndex.FACET_STATUSES;
  77. import static org.sonar.server.rule.index.RuleIndex.FACET_TAGS;
  78. import static org.sonar.server.rule.index.RuleIndex.FACET_TYPES;
  79. import static org.sonar.server.rule.ws.RulesWsParameters.FIELD_DEPRECATED_KEYS;
  80. import static org.sonar.server.rule.ws.RulesWsParameters.OPTIONAL_FIELDS;
  81. import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_ACTIVE_SEVERITIES;
  82. import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_CWE;
  83. import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_LANGUAGES;
  84. import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_OWASP_TOP_10;
  85. import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_OWASP_TOP_10_2021;
  86. import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_REPOSITORIES;
  87. import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_SANS_TOP_25;
  88. import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_SEVERITIES;
  89. import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_SONARSOURCE_SECURITY;
  90. import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_STATUSES;
  91. import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_TAGS;
  92. import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_TYPES;
  93. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  94. public class SearchAction implements RulesWsAction {
  95. public static final String ACTION = "search";
  96. private static final Collection<String> DEFAULT_FACETS = Set.of(PARAM_LANGUAGES, PARAM_REPOSITORIES, "tags");
  97. private static final String[] POSSIBLE_FACETS = new String[]{
  98. FACET_LANGUAGES,
  99. FACET_REPOSITORIES,
  100. FACET_TAGS,
  101. FACET_SEVERITIES,
  102. FACET_ACTIVE_SEVERITIES,
  103. FACET_STATUSES,
  104. FACET_TYPES,
  105. FACET_OLD_DEFAULT,
  106. FACET_CWE,
  107. FACET_OWASP_TOP_10,
  108. FACET_OWASP_TOP_10_2021,
  109. FACET_SANS_TOP_25,
  110. FACET_SONARSOURCE_SECURITY};
  111. private final RuleQueryFactory ruleQueryFactory;
  112. private final DbClient dbClient;
  113. private final RuleIndex ruleIndex;
  114. private final ActiveRuleCompleter activeRuleCompleter;
  115. private final RuleMapper mapper;
  116. private final RuleWsSupport ruleWsSupport;
  117. public SearchAction(RuleIndex ruleIndex, ActiveRuleCompleter activeRuleCompleter, RuleQueryFactory ruleQueryFactory, DbClient dbClient, RuleMapper mapper,
  118. RuleWsSupport ruleWsSupport) {
  119. this.ruleIndex = ruleIndex;
  120. this.activeRuleCompleter = activeRuleCompleter;
  121. this.ruleQueryFactory = ruleQueryFactory;
  122. this.dbClient = dbClient;
  123. this.mapper = mapper;
  124. this.ruleWsSupport = ruleWsSupport;
  125. }
  126. @Override
  127. public void define(WebService.NewController controller) {
  128. WebService.NewAction action = controller.createAction(ACTION)
  129. .addPagingParams(100, MAX_PAGE_SIZE)
  130. .setHandler(this)
  131. .setChangelog(
  132. new Change("5.5", "The field 'effortToFixDescription' has been deprecated, use 'gapDescription' instead"),
  133. new Change("5.5", "The field 'debtRemFnCoeff' has been deprecated, use 'remFnGapMultiplier' instead"),
  134. new Change("5.5", "The field 'defaultDebtRemFnCoeff' has been deprecated, use 'defaultRemFnGapMultiplier' instead"),
  135. new Change("5.5", "The field 'debtRemFnOffset' has been deprecated, use 'remFnBaseEffort' instead"),
  136. new Change("5.5", "The field 'defaultDebtRemFnOffset' has been deprecated, use 'defaultRemFnBaseEffort' instead"),
  137. new Change("5.5", "The field 'debtOverloaded' has been deprecated, use 'remFnOverloaded' instead"),
  138. new Change("7.1", "The field 'scope' has been added to the response"),
  139. new Change("7.1", "The field 'scope' has been added to the 'f' parameter"),
  140. new Change("7.2", "The field 'isExternal' has been added to the response"),
  141. new Change("7.2", "The field 'includeExternal' has been added to the 'f' parameter"),
  142. new Change("7.5", "The field 'updatedAt' has been added to the 'f' parameter"),
  143. new Change("9.5", "The field 'htmlDesc' has been deprecated, use 'descriptionSections' instead"),
  144. new Change("9.5", "The field 'descriptionSections' has been added to the payload"),
  145. new Change("9.5", "The field 'descriptionSections' has been added to the 'f' parameter"),
  146. new Change("9.6", "'descriptionSections' can optionally embed a context field"),
  147. new Change("9.6", "The field 'educationPrinciples' has been added to the 'f' parameter"),
  148. new Change("9.8", "response fields 'total', 's', 'ps' have been deprecated, please use 'paging' object instead"),
  149. new Change("9.8", "The field 'paging' has been added to the response"),
  150. new Change("10.0", "The deprecated field 'effortToFixDescription' has been removed, use 'gapDescription' instead."),
  151. new Change("10.0", "The deprecated field 'debtRemFnCoeff' has been removed, use 'remFnGapMultiplier' instead."),
  152. new Change("10.0", "The deprecated field 'defaultDebtRemFnCoeff' has been removed, use 'defaultRemFnGapMultiplier' instead."),
  153. new Change("10.0", "The deprecated field 'debtRemFnOffset' has been removed, use 'remFnBaseEffort' instead."),
  154. new Change("10.0", "The deprecated field 'defaultDebtRemFnOffset' has been removed, use 'defaultRemFnBaseEffort' instead."),
  155. new Change("10.0", "The deprecated field 'debtOverloaded' has been removed, use 'remFnOverloaded' instead."),
  156. new Change("10.0", "The field 'defaultDebtRemFnType' has been deprecated, use 'defaultRemFnType' instead"),
  157. new Change("10.0", "The field 'debtRemFnType' has been deprecated, use 'remFnType' instead"),
  158. new Change("10.0", "The value 'debtRemFn' for the 'f' parameter has been deprecated, use 'remFn' instead"),
  159. new Change("10.0", "The value 'defaultDebtRemFn' for the 'f' parameter has been deprecated, use 'defaultRemFn' instead"),
  160. new Change("10.0", "The value 'sansTop25' for the parameter 'facets' has been deprecated"),
  161. new Change("10.0", "Parameter 'sansTop25' is deprecated"),
  162. new Change("10.2", "Response fields 'total', 's', 'ps' dropped")
  163. );
  164. action.createParam(FACETS)
  165. .setDescription("Comma-separated list of the facets to be computed. No facet is computed by default.")
  166. .setPossibleValues(POSSIBLE_FACETS)
  167. .setExampleValue(format("%s,%s", POSSIBLE_FACETS[0], POSSIBLE_FACETS[1]));
  168. WebService.NewParam paramFields = action.createParam(FIELDS)
  169. .setDescription("Comma-separated list of additional fields to be returned in the response. All the fields are returned by default, except actives.")
  170. .setPossibleValues(Ordering.natural().sortedCopy(OPTIONAL_FIELDS));
  171. Iterator<String> it = OPTIONAL_FIELDS.iterator();
  172. paramFields.setExampleValue(format("%s,%s", it.next(), it.next()));
  173. action.setDescription("Search for a collection of relevant rules matching a specified query.<br/>")
  174. .setResponseExample(getClass().getResource("search-example.json"))
  175. .setSince("4.4")
  176. .setHandler(this);
  177. // Rule-specific search parameters
  178. RuleWsSupport.defineGenericRuleSearchParameters(action);
  179. RuleWsSupport.defineIsExternalParam(action);
  180. }
  181. @Override
  182. public void handle(Request request, Response response) throws Exception {
  183. try (DbSession dbSession = dbClient.openSession(false)) {
  184. SearchRequest searchWsRequest = toSearchWsRequest(request);
  185. SearchOptions context = buildSearchOptions(searchWsRequest);
  186. RuleQuery query = ruleQueryFactory.createRuleSearchQuery(dbSession, request);
  187. SearchResult searchResult = doSearch(dbSession, query, context);
  188. SearchResponse responseBuilder = buildResponse(dbSession, searchWsRequest, context, searchResult, query);
  189. writeProtobuf(responseBuilder, request, response);
  190. }
  191. }
  192. private SearchResponse buildResponse(DbSession dbSession, SearchRequest request, SearchOptions context, SearchResult result, RuleQuery query) {
  193. SearchResponse.Builder responseBuilder = SearchResponse.newBuilder();
  194. writePaging(responseBuilder, result, context);
  195. doContextResponse(dbSession, request, result, responseBuilder, query);
  196. if (!context.getFacets().isEmpty()) {
  197. writeFacets(responseBuilder, request, context, result);
  198. }
  199. return responseBuilder.build();
  200. }
  201. private static void writePaging(SearchResponse.Builder response, SearchResult searchResult, SearchOptions context) {
  202. response.setPaging(formatPaging(searchResult.total, context.getPage(), context.getLimit()));
  203. }
  204. private static Common.Paging.Builder formatPaging(Long total, int pageIndex, int limit) {
  205. return Common.Paging.newBuilder()
  206. .setPageIndex(pageIndex)
  207. .setPageSize(limit)
  208. .setTotal(total.intValue());
  209. }
  210. private void writeRules(DbSession dbSession, SearchResponse.Builder response, SearchResult result, SearchOptions context) {
  211. Map<String, UserDto> usersByUuid = ruleWsSupport.getUsersByUuid(dbSession, result.rules);
  212. Map<String, List<DeprecatedRuleKeyDto>> deprecatedRuleKeysByRuleUuid = getDeprecatedRuleKeysByRuleUuid(dbSession, result.rules, context);
  213. result.rules.forEach(rule -> response.addRules(mapper.toWsRule(rule, result, context.getFields(), usersByUuid,
  214. deprecatedRuleKeysByRuleUuid)));
  215. }
  216. private Map<String, List<DeprecatedRuleKeyDto>> getDeprecatedRuleKeysByRuleUuid(DbSession dbSession, List<RuleDto> rules, SearchOptions context) {
  217. if (!RuleMapper.shouldReturnField(context.getFields(), FIELD_DEPRECATED_KEYS)) {
  218. return Collections.emptyMap();
  219. }
  220. Set<String> ruleUuidsSet = rules.stream()
  221. .map(RuleDto::getUuid)
  222. .collect(Collectors.toSet());
  223. if (ruleUuidsSet.isEmpty()) {
  224. return Collections.emptyMap();
  225. } else {
  226. return dbClient.ruleDao().selectDeprecatedRuleKeysByRuleUuids(dbSession, ruleUuidsSet).stream()
  227. .collect(Collectors.groupingBy(DeprecatedRuleKeyDto::getRuleUuid));
  228. }
  229. }
  230. private static SearchOptions buildSearchOptions(SearchRequest request) {
  231. SearchOptions context = loadCommonContext(request);
  232. SearchOptions searchOptions = new SearchOptions()
  233. .setLimit(context.getLimit())
  234. .setOffset(context.getOffset());
  235. if (context.getFacets().contains(RuleIndex.FACET_OLD_DEFAULT)) {
  236. searchOptions.addFacets(DEFAULT_FACETS);
  237. } else {
  238. searchOptions.addFacets(context.getFacets());
  239. }
  240. return searchOptions;
  241. }
  242. private static SearchOptions loadCommonContext(SearchRequest request) {
  243. int pageSize = Integer.parseInt(request.getPs());
  244. SearchOptions context = new SearchOptions().addFields(request.getF());
  245. if (request.getFacets() != null) {
  246. context.addFacets(request.getFacets());
  247. }
  248. if (pageSize < 1) {
  249. context.setPage(Integer.parseInt(request.getP()), 0).setLimit(MAX_PAGE_SIZE);
  250. } else {
  251. context.setPage(Integer.parseInt(request.getP()), pageSize);
  252. }
  253. return context;
  254. }
  255. private SearchResult doSearch(DbSession dbSession, RuleQuery query, SearchOptions context) {
  256. SearchIdResult<String> result = ruleIndex.search(query, context);
  257. List<String> ruleUuids = result.getUuids();
  258. // rule order is managed by ES, this order by must be kept when fetching rule details
  259. Map<String, RuleDto> rulesByRuleKey = Maps.uniqueIndex(dbClient.ruleDao().selectByUuids(dbSession, ruleUuids), RuleDto::getUuid);
  260. List<RuleDto> rules = new ArrayList<>();
  261. for (String ruleUuid : ruleUuids) {
  262. RuleDto rule = rulesByRuleKey.get(ruleUuid);
  263. if (rule != null) {
  264. rules.add(rule);
  265. }
  266. }
  267. List<String> templateRuleUuids = rules.stream()
  268. .map(RuleDto::getTemplateUuid)
  269. .filter(Objects::nonNull)
  270. .toList();
  271. List<RuleDto> templateRules = dbClient.ruleDao().selectByUuids(dbSession, templateRuleUuids);
  272. List<RuleParamDto> ruleParamDtos = dbClient.ruleDao().selectRuleParamsByRuleUuids(dbSession, ruleUuids);
  273. return new SearchResult()
  274. .setRules(rules)
  275. .setRuleParameters(ruleParamDtos)
  276. .setTemplateRules(templateRules)
  277. .setFacets(result.getFacets())
  278. .setTotal(result.getTotal());
  279. }
  280. private void doContextResponse(DbSession dbSession, SearchRequest request, SearchResult result, SearchResponse.Builder response, RuleQuery query) {
  281. SearchOptions contextForResponse = loadCommonContext(request);
  282. writeRules(dbSession, response, result, contextForResponse);
  283. if (contextForResponse.getFields().contains("actives")) {
  284. activeRuleCompleter.completeSearch(dbSession, query, result.rules, response);
  285. }
  286. }
  287. private static void writeFacets(SearchResponse.Builder response, SearchRequest request, SearchOptions context, SearchResult results) {
  288. addMandatoryFacetValues(results, FACET_LANGUAGES, request.getLanguages());
  289. addMandatoryFacetValues(results, FACET_REPOSITORIES, request.getRepositories());
  290. addMandatoryFacetValues(results, FACET_STATUSES, ALL_STATUSES_EXCEPT_REMOVED);
  291. addMandatoryFacetValues(results, FACET_SEVERITIES, Severity.ALL);
  292. addMandatoryFacetValues(results, FACET_ACTIVE_SEVERITIES, Severity.ALL);
  293. addMandatoryFacetValues(results, FACET_TAGS, request.getTags());
  294. addMandatoryFacetValues(results, FACET_TYPES, RuleType.names());
  295. addMandatoryFacetValues(results, FACET_CWE, request.getCwe());
  296. addMandatoryFacetValues(results, FACET_OWASP_TOP_10, request.getOwaspTop10());
  297. addMandatoryFacetValues(results, FACET_OWASP_TOP_10_2021, request.getOwaspTop10For2021());
  298. addMandatoryFacetValues(results, FACET_SANS_TOP_25, request.getSansTop25());
  299. addMandatoryFacetValues(results, FACET_SONARSOURCE_SECURITY, request.getSonarsourceSecurity());
  300. Common.Facet.Builder facet = Common.Facet.newBuilder();
  301. Common.FacetValue.Builder value = Common.FacetValue.newBuilder();
  302. Map<String, List<String>> facetValuesByFacetKey = new HashMap<>();
  303. facetValuesByFacetKey.put(FACET_LANGUAGES, request.getLanguages());
  304. facetValuesByFacetKey.put(FACET_REPOSITORIES, request.getRepositories());
  305. facetValuesByFacetKey.put(FACET_STATUSES, request.getStatuses());
  306. facetValuesByFacetKey.put(FACET_SEVERITIES, request.getSeverities());
  307. facetValuesByFacetKey.put(FACET_ACTIVE_SEVERITIES, request.getActiveSeverities());
  308. facetValuesByFacetKey.put(FACET_TAGS, request.getTags());
  309. facetValuesByFacetKey.put(FACET_TYPES, request.getTypes());
  310. facetValuesByFacetKey.put(FACET_CWE, request.getCwe());
  311. facetValuesByFacetKey.put(FACET_OWASP_TOP_10, request.getOwaspTop10());
  312. facetValuesByFacetKey.put(FACET_OWASP_TOP_10_2021, request.getOwaspTop10For2021());
  313. facetValuesByFacetKey.put(FACET_SANS_TOP_25, request.getSansTop25());
  314. facetValuesByFacetKey.put(FACET_SONARSOURCE_SECURITY, request.getSonarsourceSecurity());
  315. for (String facetName : context.getFacets()) {
  316. facet.clear().setProperty(facetName);
  317. Map<String, Long> facets = results.facets.get(facetName);
  318. if (facets != null) {
  319. Set<String> itemsFromFacets = new HashSet<>();
  320. for (Map.Entry<String, Long> facetValue : facets.entrySet()) {
  321. itemsFromFacets.add(facetValue.getKey());
  322. facet.addValues(value
  323. .clear()
  324. .setVal(facetValue.getKey())
  325. .setCount(facetValue.getValue()));
  326. }
  327. addZeroFacetsForSelectedItems(facet, facetValuesByFacetKey.get(facetName), itemsFromFacets);
  328. }
  329. response.getFacetsBuilder().addFacets(facet);
  330. }
  331. }
  332. private static void addZeroFacetsForSelectedItems(Common.Facet.Builder facet, @Nullable List<String> requestParams, Set<String> itemsFromFacets) {
  333. if (requestParams != null) {
  334. Common.FacetValue.Builder value = Common.FacetValue.newBuilder();
  335. for (String param : requestParams) {
  336. if (!itemsFromFacets.contains(param)) {
  337. facet.addValues(value.clear()
  338. .setVal(param)
  339. .setCount(0L));
  340. }
  341. }
  342. }
  343. }
  344. private static void addMandatoryFacetValues(SearchResult results, String facetName, @Nullable Collection<String> mandatoryValues) {
  345. Map<String, Long> facetValues = results.facets.get(facetName);
  346. if (facetValues != null) {
  347. Collection<String> valuesToAdd = mandatoryValues == null ? Lists.newArrayList() : mandatoryValues;
  348. for (String item : valuesToAdd) {
  349. if (!facetValues.containsKey(item)) {
  350. facetValues.put(item, 0L);
  351. }
  352. }
  353. }
  354. }
  355. private static SearchRequest toSearchWsRequest(Request request) {
  356. request.mandatoryParamAsBoolean(ASCENDING);
  357. return new SearchRequest()
  358. .setActiveSeverities(request.paramAsStrings(PARAM_ACTIVE_SEVERITIES))
  359. .setF(request.paramAsStrings(FIELDS))
  360. .setFacets(request.paramAsStrings(FACETS))
  361. .setLanguages(request.paramAsStrings(PARAM_LANGUAGES))
  362. .setP("" + request.mandatoryParamAsInt(PAGE))
  363. .setPs("" + request.mandatoryParamAsInt(PAGE_SIZE))
  364. .setRepositories(request.paramAsStrings(PARAM_REPOSITORIES))
  365. .setSeverities(request.paramAsStrings(PARAM_SEVERITIES))
  366. .setStatuses(request.paramAsStrings(PARAM_STATUSES))
  367. .setTags(request.paramAsStrings(PARAM_TAGS))
  368. .setTypes(request.paramAsStrings(PARAM_TYPES))
  369. .setCwe(request.paramAsStrings(PARAM_CWE))
  370. .setOwaspTop10(request.paramAsStrings(PARAM_OWASP_TOP_10))
  371. .setOwaspTop10For2021(request.paramAsStrings(PARAM_OWASP_TOP_10_2021))
  372. .setSansTop25(request.paramAsStrings(PARAM_SANS_TOP_25))
  373. .setSonarsourceSecurity(request.paramAsStrings(PARAM_SONARSOURCE_SECURITY));
  374. }
  375. static class SearchResult {
  376. private List<RuleDto> rules;
  377. private final ListMultimap<String, RuleParamDto> ruleParamsByRuleUuid;
  378. private final Map<String, RuleDto> templateRulesByRuleUuid;
  379. private Long total;
  380. private Facets facets;
  381. public SearchResult() {
  382. this.rules = new ArrayList<>();
  383. this.ruleParamsByRuleUuid = ArrayListMultimap.create();
  384. this.templateRulesByRuleUuid = new HashMap<>();
  385. }
  386. public List<RuleDto> getRules() {
  387. return rules;
  388. }
  389. public SearchResult setRules(List<RuleDto> rules) {
  390. this.rules = rules;
  391. return this;
  392. }
  393. public ListMultimap<String, RuleParamDto> getRuleParamsByRuleUuid() {
  394. return ruleParamsByRuleUuid;
  395. }
  396. public SearchResult setRuleParameters(List<RuleParamDto> ruleParams) {
  397. ruleParamsByRuleUuid.clear();
  398. for (RuleParamDto ruleParam : ruleParams) {
  399. ruleParamsByRuleUuid.put(ruleParam.getRuleUuid(), ruleParam);
  400. }
  401. return this;
  402. }
  403. public Map<String, RuleDto> getTemplateRulesByRuleUuid() {
  404. return templateRulesByRuleUuid;
  405. }
  406. public SearchResult setTemplateRules(List<RuleDto> templateRules) {
  407. templateRulesByRuleUuid.clear();
  408. for (RuleDto templateRule : templateRules) {
  409. templateRulesByRuleUuid.put(templateRule.getUuid(), templateRule);
  410. }
  411. return this;
  412. }
  413. @CheckForNull
  414. public Long getTotal() {
  415. return total;
  416. }
  417. public SearchResult setTotal(Long total) {
  418. this.total = total;
  419. return this;
  420. }
  421. @CheckForNull
  422. public Facets getFacets() {
  423. return facets;
  424. }
  425. public SearchResult setFacets(Facets facets) {
  426. this.facets = facets;
  427. return this;
  428. }
  429. }
  430. private static class SearchRequest {
  431. private List<String> activeSeverities;
  432. private List<String> f;
  433. private List<String> facets;
  434. private List<String> languages;
  435. private String p;
  436. private String ps;
  437. private List<String> repositories;
  438. private List<String> severities;
  439. private List<String> statuses;
  440. private List<String> tags;
  441. private List<String> types;
  442. private List<String> cwe;
  443. private List<String> owaspTop10;
  444. private List<String> owaspTop10For2021;
  445. private List<String> sansTop25;
  446. private List<String> sonarsourceSecurity;
  447. private SearchRequest setActiveSeverities(List<String> activeSeverities) {
  448. this.activeSeverities = activeSeverities;
  449. return this;
  450. }
  451. private List<String> getActiveSeverities() {
  452. return activeSeverities;
  453. }
  454. private SearchRequest setF(List<String> f) {
  455. this.f = f;
  456. return this;
  457. }
  458. private List<String> getF() {
  459. return f;
  460. }
  461. private SearchRequest setFacets(List<String> facets) {
  462. this.facets = facets;
  463. return this;
  464. }
  465. private List<String> getFacets() {
  466. return facets;
  467. }
  468. private SearchRequest setLanguages(List<String> languages) {
  469. this.languages = languages;
  470. return this;
  471. }
  472. private List<String> getLanguages() {
  473. return languages;
  474. }
  475. private SearchRequest setP(String p) {
  476. this.p = p;
  477. return this;
  478. }
  479. private String getP() {
  480. return p;
  481. }
  482. private SearchRequest setPs(String ps) {
  483. this.ps = ps;
  484. return this;
  485. }
  486. private String getPs() {
  487. return ps;
  488. }
  489. private SearchRequest setRepositories(List<String> repositories) {
  490. this.repositories = repositories;
  491. return this;
  492. }
  493. private List<String> getRepositories() {
  494. return repositories;
  495. }
  496. private SearchRequest setSeverities(List<String> severities) {
  497. this.severities = severities;
  498. return this;
  499. }
  500. private List<String> getSeverities() {
  501. return severities;
  502. }
  503. private SearchRequest setStatuses(List<String> statuses) {
  504. this.statuses = statuses;
  505. return this;
  506. }
  507. private List<String> getStatuses() {
  508. return statuses;
  509. }
  510. private SearchRequest setTags(List<String> tags) {
  511. this.tags = tags;
  512. return this;
  513. }
  514. private List<String> getTags() {
  515. return tags;
  516. }
  517. private SearchRequest setTypes(@Nullable List<String> types) {
  518. this.types = types;
  519. return this;
  520. }
  521. private List<String> getTypes() {
  522. return types;
  523. }
  524. public List<String> getCwe() {
  525. return cwe;
  526. }
  527. public SearchRequest setCwe(@Nullable List<String> cwe) {
  528. this.cwe = cwe;
  529. return this;
  530. }
  531. public List<String> getOwaspTop10() {
  532. return owaspTop10;
  533. }
  534. public SearchRequest setOwaspTop10(@Nullable List<String> owaspTop10) {
  535. this.owaspTop10 = owaspTop10;
  536. return this;
  537. }
  538. public List<String> getOwaspTop10For2021() {
  539. return owaspTop10For2021;
  540. }
  541. public SearchRequest setOwaspTop10For2021(@Nullable List<String> owaspTop10For2021) {
  542. this.owaspTop10For2021 = owaspTop10For2021;
  543. return this;
  544. }
  545. /**
  546. * @deprecated SansTop25 report is outdated, it has been completely deprecated in version 10.0 and will be removed from version 11.0
  547. */
  548. @Deprecated(since = "10.0", forRemoval = true)
  549. public List<String> getSansTop25() {
  550. return sansTop25;
  551. }
  552. @Deprecated(since = "10.0", forRemoval = true)
  553. public SearchRequest setSansTop25(@Nullable List<String> sansTop25) {
  554. this.sansTop25 = sansTop25;
  555. return this;
  556. }
  557. public List<String> getSonarsourceSecurity() {
  558. return sonarsourceSecurity;
  559. }
  560. public SearchRequest setSonarsourceSecurity(@Nullable List<String> sonarsourceSecurity) {
  561. this.sonarsourceSecurity = sonarsourceSecurity;
  562. return this;
  563. }
  564. }
  565. }