Skip to content

Commit 4cf692f

Browse files
committed
Pull in SonarQube report generation capability from BenchmarkJava and
make it configurable. Enhance CppCheck, Klocwork, and SonarQube scoring for more finding types.
1 parent f74e1b4 commit 4cf692f

File tree

12 files changed

+838
-127
lines changed

12 files changed

+838
-127
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* OWASP Benchmark Project
3+
*
4+
* <p>This file is part of the Open Web Application Security Project (OWASP) Benchmark Project For
5+
* details, please see <a
6+
* href="https://owasp.org/www-project-benchmark/">https://owasp.org/www-project-benchmark/</a>.
7+
*
8+
* <p>The OWASP Benchmark is free software: you can redistribute it and/or modify it under the terms
9+
* of the GNU General Public License as published by the Free Software Foundation, version 2.
10+
*
11+
* <p>The OWASP Benchmark is distributed in the hope that it will be useful, but WITHOUT ANY
12+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13+
* PURPOSE. See the GNU General Public License for more details.
14+
*
15+
* @author Dave Wichers
16+
* @created 2025
17+
*/
18+
package org.owasp.benchmarkutils.report.sonarqube;
19+
20+
import java.io.FileInputStream;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.io.SequenceInputStream;
24+
import java.util.Map;
25+
import org.yaml.snakeyaml.LoaderOptions;
26+
import org.yaml.snakeyaml.Yaml;
27+
28+
/**
29+
* The values of these scorecard generation variables can be changed via scorecardconfig.yaml files.
30+
* These affect overall scorecard generation. These were the original command line params to
31+
* scorecard generation.
32+
*/
33+
public class SonarQubeConfiguration {
34+
35+
// DRW TODO: Test use of DEFAULT_CONFIG file with Docker version of SonarQube server from
36+
// runSonarQube.sh script
37+
// Loading of default yaml file likely not implemented yet.
38+
public static final String DEFAULT_CONFIG = "defaultsonarqubeconfig.yaml";
39+
public static final String DEFAULT_SUCCESS_MESSAGE =
40+
"INFO: Default SonarQube report config file found and loaded.";
41+
public static final String NON_DEFAULT_SUCCESS_MESSAGE =
42+
"INFO: Custom YAML SonarQube report config file found and loaded.";
43+
44+
public String SONAR_USER = "admin";
45+
public String SONAR_PASSWORD = "P4ssword!!!!";
46+
public String SONAR_PROJECT = "benchmark";
47+
public String SONAR_HOST = "ubuntu-server";
48+
public Integer SONAR_PORT = 9876;
49+
public String TEST_SUITE_NAME = "Benchmark"; // Default value
50+
51+
private static final Yaml yaml = new Yaml(defaultLoaderOptions());
52+
53+
private static LoaderOptions defaultLoaderOptions() {
54+
LoaderOptions loaderOptions = new LoaderOptions();
55+
56+
loaderOptions.setAllowDuplicateKeys(true);
57+
loaderOptions.setWarnOnDuplicateKeys(false);
58+
59+
return loaderOptions;
60+
}
61+
62+
public static SonarQubeConfiguration fromDefaultConfig() {
63+
return fromInputStream(resourceAsStream(DEFAULT_CONFIG), DEFAULT_SUCCESS_MESSAGE);
64+
}
65+
66+
public static SonarQubeConfiguration fromResourceFile(String resourceFile) {
67+
return fromInputStream(resourceAsStream(resourceFile), NON_DEFAULT_SUCCESS_MESSAGE);
68+
}
69+
70+
public static InputStream resourceAsStream(String resourceFile) {
71+
InputStream resourceAsStream =
72+
SonarQubeConfiguration.class.getClassLoader().getResourceAsStream(resourceFile);
73+
74+
if (resourceAsStream == null) {
75+
throw new ConfigCouldNotBeParsed(
76+
"YAML SonarQube configuration file: '"
77+
+ resourceFile
78+
+ "' not found on classpath!");
79+
}
80+
81+
return resourceAsStream;
82+
}
83+
84+
public static SonarQubeConfiguration fromInputStream(
85+
InputStream stream, String successMessage) {
86+
SequenceInputStream sequenceInputStream =
87+
new SequenceInputStream(resourceAsStream(DEFAULT_CONFIG), stream);
88+
89+
SonarQubeConfiguration configuration = null;
90+
try {
91+
configuration = new SonarQubeConfiguration(yaml.load(sequenceInputStream));
92+
93+
} catch (org.yaml.snakeyaml.scanner.ScannerException e) {
94+
System.out.println("FATAL ERROR: SonarQube YAML configuration file format error.");
95+
e.printStackTrace();
96+
System.exit(-1);
97+
}
98+
99+
System.out.println(successMessage);
100+
return configuration;
101+
}
102+
103+
private SonarQubeConfiguration(Map<String, Object> yamlConfig) {
104+
105+
SONAR_USER = (String) yamlConfig.get("sonaruser");
106+
SONAR_PASSWORD = (String) yamlConfig.get("sonarpassword");
107+
SONAR_PROJECT = (String) yamlConfig.get("sonarproject");
108+
SONAR_HOST = (String) yamlConfig.get("sonarhost");
109+
SONAR_PORT = (Integer) yamlConfig.get("sonarport");
110+
// Optionally, the config file can specific the name of the test suite being scored
111+
if (yamlConfig.containsKey("testsuitename"))
112+
TEST_SUITE_NAME = (String) yamlConfig.get("testsuitename");
113+
}
114+
115+
public static SonarQubeConfiguration fromFile(String pathToFile) {
116+
try (FileInputStream fileInputStream = new FileInputStream(pathToFile)) {
117+
return fromInputStream(fileInputStream, NON_DEFAULT_SUCCESS_MESSAGE);
118+
} catch (IOException e) {
119+
throw new ConfigCouldNotBeParsed(
120+
"SonarQube YAML configuration file: '" + pathToFile + "' not found!");
121+
}
122+
}
123+
124+
public static class ConfigCouldNotBeParsed extends RuntimeException {
125+
public ConfigCouldNotBeParsed(String message) {
126+
super(message);
127+
}
128+
}
129+
}

0 commit comments

Comments
 (0)