Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SarifIssuesImportSensor.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.scanner.externalissue.sarif;
  21. import java.nio.file.NoSuchFileException;
  22. import java.nio.file.Path;
  23. import java.util.Arrays;
  24. import java.util.Collections;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Set;
  29. import java.util.stream.Collectors;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. import org.sonar.api.CoreProperties;
  33. import org.sonar.api.batch.sensor.SensorContext;
  34. import org.sonar.api.batch.sensor.SensorDescriptor;
  35. import org.sonar.api.config.Configuration;
  36. import org.sonar.api.config.PropertyDefinition;
  37. import org.sonar.api.resources.Qualifiers;
  38. import org.sonar.api.scanner.ScannerSide;
  39. import org.sonar.api.scanner.sensor.ProjectSensor;
  40. import org.sonar.api.utils.MessageException;
  41. import org.sonar.core.sarif.Sarif210;
  42. import org.sonar.core.sarif.SarifSerializer;
  43. import static java.lang.String.format;
  44. @ScannerSide
  45. public class SarifIssuesImportSensor implements ProjectSensor {
  46. private static final Logger LOG = LoggerFactory.getLogger(SarifIssuesImportSensor.class);
  47. static final String SARIF_REPORT_PATHS_PROPERTY_KEY = "sonar.sarifReportPaths";
  48. private final SarifSerializer sarifSerializer;
  49. private final Sarif210Importer sarifImporter;
  50. private final Configuration config;
  51. public SarifIssuesImportSensor(SarifSerializer sarifSerializer, Sarif210Importer sarifImporter, Configuration config) {
  52. this.sarifSerializer = sarifSerializer;
  53. this.sarifImporter = sarifImporter;
  54. this.config = config;
  55. }
  56. public static List<PropertyDefinition> properties() {
  57. return Collections.singletonList(
  58. PropertyDefinition.builder(SARIF_REPORT_PATHS_PROPERTY_KEY)
  59. .name("SARIF report paths")
  60. .description("List of comma-separated paths (absolute or relative) containing a SARIF report with issues created by external rule engines.")
  61. .category(CoreProperties.CATEGORY_EXTERNAL_ISSUES)
  62. .onQualifiers(Qualifiers.PROJECT)
  63. .build());
  64. }
  65. @Override
  66. public void describe(SensorDescriptor descriptor) {
  67. descriptor.name("Import external issues report from SARIF file.")
  68. .onlyWhenConfiguration(c -> c.hasKey(SARIF_REPORT_PATHS_PROPERTY_KEY));
  69. }
  70. @Override
  71. public void execute(SensorContext context) {
  72. Set<String> reportPaths = loadReportPaths();
  73. Map<String, SarifImportResults> filePathToImportResults = new HashMap<>();
  74. for (String reportPath : reportPaths) {
  75. try {
  76. SarifImportResults sarifImportResults = processReport(context, reportPath);
  77. filePathToImportResults.put(reportPath, sarifImportResults);
  78. } catch (NoSuchFileException e) {
  79. throw MessageException.of(format("SARIF report file not found: %s", e.getFile()));
  80. } catch (Exception exception) {
  81. LOG.warn("Failed to process SARIF report from file '{}', error: '{}'", reportPath, exception.getMessage());
  82. }
  83. }
  84. filePathToImportResults.forEach(SarifIssuesImportSensor::displayResults);
  85. }
  86. private Set<String> loadReportPaths() {
  87. return Arrays.stream(config.getStringArray(SARIF_REPORT_PATHS_PROPERTY_KEY)).collect(Collectors.toSet());
  88. }
  89. private SarifImportResults processReport(SensorContext context, String reportPath) throws NoSuchFileException {
  90. LOG.debug("Importing SARIF issues from '{}'", reportPath);
  91. Path reportFilePath = context.fileSystem().resolvePath(reportPath).toPath();
  92. Sarif210 sarifReport = sarifSerializer.deserialize(reportFilePath);
  93. return sarifImporter.importSarif(sarifReport);
  94. }
  95. private static void displayResults(String filePath, SarifImportResults sarifImportResults) {
  96. if (sarifImportResults.getFailedRuns() > 0 && sarifImportResults.getSuccessFullyImportedRuns() > 0) {
  97. LOG.warn("File {}: {} run(s) could not be imported (see warning above) and {} run(s) successfully imported ({} vulnerabilities in total).",
  98. filePath, sarifImportResults.getFailedRuns(), sarifImportResults.getSuccessFullyImportedRuns(), sarifImportResults.getSuccessFullyImportedIssues());
  99. } else if (sarifImportResults.getFailedRuns() > 0) {
  100. LOG.warn("File {}: {} run(s) could not be imported (see warning above).",
  101. filePath, sarifImportResults.getFailedRuns());
  102. } else {
  103. LOG.info("File {}: {} run(s) successfully imported ({} vulnerabilities in total).",
  104. filePath, sarifImportResults.getSuccessFullyImportedRuns(), sarifImportResults.getSuccessFullyImportedIssues());
  105. }
  106. }
  107. }