Skip to content

Commit 8680eb6

Browse files
committed
SCANJLIB-258 Log the value of SONAR_SCANNER_JAVA_OPTS
1 parent 8233f5b commit 8680eb6

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

lib/src/main/java/org/sonarsource/scanner/lib/internal/facade/forked/ScannerEngineLauncher.java

+28-6
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
import java.util.ArrayList;
2727
import java.util.Arrays;
2828
import java.util.List;
29+
import java.util.Locale;
2930
import java.util.Map;
3031
import java.util.Optional;
32+
import java.util.Set;
3133
import java.util.stream.Collectors;
3234
import org.slf4j.Logger;
3335
import org.slf4j.LoggerFactory;
@@ -36,6 +38,10 @@
3638
import org.sonarsource.scanner.lib.internal.http.OkHttpClientFactory;
3739

3840
public class ScannerEngineLauncher {
41+
private static final Set<String> SENSITIVE_JVM_ARGUMENTS = Set.of(
42+
"sonar.login",
43+
"password",
44+
"token");
3945

4046
private static final Logger LOG = LoggerFactory.getLogger(ScannerEngineLauncher.class);
4147

@@ -104,14 +110,30 @@ private List<String> buildArgs(Map<String, String> properties) {
104110
List<String> args = new ArrayList<>();
105111
String javaOpts = properties.get(ScannerProperties.SCANNER_JAVA_OPTS);
106112
if (javaOpts != null) {
107-
args.addAll(split(javaOpts));
113+
var split = split(javaOpts);
114+
LOG.atInfo().addArgument(() -> redactSensitiveArguments(split)).log("SONAR_SCANNER_JAVA_OPTS={}");
115+
args.addAll(split);
108116
}
109117
args.add("-D" + OkHttpClientFactory.BC_IGNORE_USELESS_PASSWD + "=true");
110118
args.add("-jar");
111119
args.add(scannerEngineJar.getPathInCache().toAbsolutePath().toString());
112120
return args;
113121
}
114122

123+
private static String redactSensitiveArguments(List<String> scannerOpts) {
124+
return scannerOpts.stream()
125+
.map(ScannerEngineLauncher::redactArgumentIfSensistive)
126+
.collect(Collectors.joining(" "));
127+
}
128+
129+
private static String redactArgumentIfSensistive(String argument) {
130+
String[] elems = argument.split("=");
131+
if (elems.length > 0 && SENSITIVE_JVM_ARGUMENTS.stream().anyMatch(p -> elems[0].toLowerCase(Locale.ENGLISH).contains(p))) {
132+
return elems[0] + "=*";
133+
}
134+
return argument;
135+
}
136+
115137
private static List<String> split(String value) {
116138
return Arrays.stream(value.split("\\s+"))
117139
.map(String::trim)
@@ -124,11 +146,11 @@ private static String buildJsonProperties(Map<String, String> properties) {
124146
properties.entrySet().stream()
125147
.filter(prop -> prop.getKey() != null)
126148
.sorted(Map.Entry.comparingByKey()).forEach(prop -> {
127-
JsonObject property = new JsonObject();
128-
property.addProperty("key", prop.getKey());
129-
property.addProperty("value", Optional.ofNullable(prop.getValue()).orElse(""));
130-
propertiesArray.add(property);
131-
});
149+
JsonObject property = new JsonObject();
150+
property.addProperty("key", prop.getKey());
151+
property.addProperty("value", Optional.ofNullable(prop.getValue()).orElse(""));
152+
propertiesArray.add(property);
153+
});
132154
JsonObject jsonObject = new JsonObject();
133155
jsonObject.add(JSON_FIELD_SCANNER_PROPERTIES, propertiesArray);
134156
return new Gson().toJson(jsonObject);

lib/src/test/java/org/sonarsource/scanner/lib/internal/facade/forked/ScannerEngineLauncherTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ void execute() {
6262
eq(List.of("-Xmx4g", "-Xms1g", "-Dorg.bouncycastle.pkcs12.ignore_useless_passwd=true", "-jar", scannerEngine.toAbsolutePath().toString())),
6363
eq("{\"scannerProperties\":[{\"key\":\"sonar.host.url\",\"value\":\"http://localhost:9000\"},{\"key\":\"sonar.scanner.javaOpts\",\"value\":\"-Xmx4g -Xms1g\"}]}"),
6464
any());
65+
66+
assertThat(logTester.logs(Level.INFO)).containsOnly("SONAR_SCANNER_JAVA_OPTS=-Xmx4g -Xms1g");
67+
}
68+
69+
@Test
70+
void execute_log_info_when_java_opts_provided_obfuscating_sensitive_values() {
71+
var scannerEngine = temp.resolve("scanner-engine.jar");
72+
73+
ScannerEngineLauncher launcher = new ScannerEngineLauncher(javaRunner, new CachedFile(scannerEngine, true));
74+
75+
Map<String, String> properties = Map.of(
76+
ScannerProperties.SCANNER_JAVA_OPTS, "-Xmx4g -Xms1g -Dsonar.login=secret1 -Dsonar.password=secret2 -Dsonar.token=secret3 -Djava.net.ssl.trustStorePassword=secret4",
77+
ScannerProperties.HOST_URL, "http://localhost:9000");
78+
launcher.execute(properties);
79+
80+
assertThat(logTester.logs(Level.INFO)).containsOnly("SONAR_SCANNER_JAVA_OPTS=-Xmx4g -Xms1g -Dsonar.login=* -Dsonar.password=* -Dsonar.token=* -Djava.net.ssl.trustStorePassword=*");
6581
}
6682

6783
@Test

0 commit comments

Comments
 (0)