@Override public void start() { reportDir = moduleHierarchy.root().getWorkDir().resolve("scanner-report"); writer = new ScannerReportWriter(reportDir.toFile()); contextPublisher.init(writer); if (!analysisMode.isIssues() && !analysisMode.isMediumTest()) { String publicUrl = server.getPublicRootUrl(); if (HttpUrl.parse(publicUrl) == null) { throw MessageException.of("Failed to parse public URL set in SonarQube server: " + publicUrl); } } }
private void writeModulesSettings(BufferedWriter fileWriter) throws IOException { for (DefaultInputModule module : store.allModules()) { if (module.equals(hierarchy.root())) { continue; } Map<String, String> moduleSpecificProps = collectModuleSpecificProps(module); fileWriter.append(String.format("Scanner properties of module: %s", module.key())).append('\n'); writeScannerProps(fileWriter, moduleSpecificProps); } }
private void writeScannerProps(BufferedWriter fileWriter, Map<String, String> props) throws IOException { for (Map.Entry<String, String> prop : props.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).collect(toList())) { if (isSystemProp(prop.getKey()) || isEnvVariable(prop.getKey()) || !isSqProp(prop.getKey())) { continue; } dumpPropIfNotSensitive(fileWriter, prop.getKey(), prop.getValue()); } }
public void init(ScannerReportWriter writer) { if (mode.isIssues()) { return; } File analysisLog = writer.getFileStructure().analysisLog(); try (BufferedWriter fileWriter = Files.newBufferedWriter(analysisLog.toPath(), StandardCharsets.UTF_8)) { if (LOG.isDebugEnabled()) { writeEnvVariables(fileWriter); writeSystemProps(fileWriter); } writePlugins(fileWriter); writeGlobalSettings(fileWriter); writeProjectSettings(fileWriter); writeModulesSettings(fileWriter); } catch (IOException e) { throw new IllegalStateException("Unable to write analysis log", e); } }
private void writeProjectSettings(BufferedWriter fileWriter) throws IOException { fileWriter.append("Project server settings:\n"); Map<String, String> props = projectServerSettings.properties(); for (String prop : new TreeSet<>(props.keySet())) { dumpPropIfNotSensitive(fileWriter, prop, props.get(prop)); } fileWriter.append("Project scanner properties:\n"); writeScannerProps(fileWriter, hierarchy.root().properties()); }
private void writeGlobalSettings(BufferedWriter fileWriter) throws IOException { fileWriter.append("Global server settings:\n"); Map<String, String> props = globalServerSettings.properties(); for (String prop : new TreeSet<>(props.keySet())) { dumpPropIfNotSensitive(fileWriter, prop, props.get(prop)); } }
private void writeEnvVariables(BufferedWriter fileWriter) throws IOException { fileWriter.append("Environment variables:\n"); Map<String, String> envVariables = system.envVariables(); new TreeSet<>(envVariables.keySet()) .forEach(envKey -> { try { String envValue = isSensitiveEnvVariable(envKey) ? "******" : envVariables.get(envKey); fileWriter.append(String.format(KEY_VALUE_FORMAT, envKey, envValue)).append('\n'); } catch (IOException e) { throw new IllegalStateException(e); } }); }
private static void dumpPropIfNotSensitive(BufferedWriter fileWriter, String prop, String value) throws IOException { fileWriter.append(String.format(KEY_VALUE_FORMAT, prop, isSensitiveProperty(prop) ? "******" : StringUtils.abbreviate(value, MAX_WIDTH))).append('\n'); }
public void init(ScannerReportWriter writer) { if (mode.isIssues()) { return; } File analysisLog = writer.getFileStructure().analysisLog(); try (BufferedWriter fileWriter = Files.newBufferedWriter(analysisLog.toPath(), StandardCharsets.UTF_8)) { if (LOG.isDebugEnabled()) { writeEnvVariables(fileWriter); writeSystemProps(fileWriter); } writePlugins(fileWriter); writeGlobalSettings(fileWriter); writeProjectSettings(fileWriter); writeModulesSettings(fileWriter); } catch (IOException e) { throw new IllegalStateException("Unable to write analysis log", e); } }
private void writeProjectSettings(BufferedWriter fileWriter) throws IOException { fileWriter.append("Project server settings:\n"); Map<String, String> props = projectServerSettings.properties(); for (String prop : new TreeSet<>(props.keySet())) { dumpPropIfNotSensitive(fileWriter, prop, props.get(prop)); } fileWriter.append("Project scanner properties:\n"); writeScannerProps(fileWriter, hierarchy.root().properties()); }
private void writeGlobalSettings(BufferedWriter fileWriter) throws IOException { fileWriter.append("Global server settings:\n"); Map<String, String> props = globalServerSettings.properties(); for (String prop : new TreeSet<>(props.keySet())) { dumpPropIfNotSensitive(fileWriter, prop, props.get(prop)); } }
private void writeEnvVariables(BufferedWriter fileWriter) throws IOException { fileWriter.append("Environment variables:\n"); Map<String, String> envVariables = system.envVariables(); new TreeSet<>(envVariables.keySet()) .forEach(envKey -> { try { String envValue = isSensitiveEnvVariable(envKey) ? "******" : envVariables.get(envKey); fileWriter.append(String.format(KEY_VALUE_FORMAT, envKey, envValue)).append('\n'); } catch (IOException e) { throw new IllegalStateException(e); } }); }
private static void dumpPropIfNotSensitive(BufferedWriter fileWriter, String prop, String value) throws IOException { fileWriter.append(String.format(KEY_VALUE_FORMAT, prop, isSensitiveProperty(prop) ? "******" : StringUtils.abbreviate(value, MAX_WIDTH))).append('\n'); }
private void writeScannerProps(BufferedWriter fileWriter, Map<String, String> props) throws IOException { for (Map.Entry<String, String> prop : props.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).collect(toList())) { if (isSystemProp(prop.getKey()) || isEnvVariable(prop.getKey()) || !isSqProp(prop.getKey())) { continue; } dumpPropIfNotSensitive(fileWriter, prop.getKey(), prop.getValue()); } }
@Test public void shouldNotDumpInIssuesMode() throws Exception { when(analysisMode.isIssues()).thenReturn(true); ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder()); publisher.init(writer); assertThat(writer.getFileStructure().analysisLog()).doesNotExist(); }
private void writeModulesSettings(BufferedWriter fileWriter) throws IOException { for (DefaultInputModule module : store.allModules()) { if (module.equals(hierarchy.root())) { continue; } Map<String, String> moduleSpecificProps = collectModuleSpecificProps(module); fileWriter.append(String.format("Scanner properties of module: %s", module.key())).append('\n'); writeScannerProps(fileWriter, moduleSpecificProps); } }
@Test public void shouldOnlyDumpPluginsByDefault() throws Exception { when(pluginRepo.getPluginInfos()).thenReturn(singletonList(new PluginInfo("xoo").setName("Xoo").setVersion(Version.create("1.0")))); ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder()); DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create() .setBaseDir(temp.newFolder()) .setWorkDir(temp.newFolder())); when(store.allModules()).thenReturn(singletonList(rootModule)); when(hierarchy.root()).thenReturn(rootModule); publisher.init(writer); assertThat(writer.getFileStructure().analysisLog()).exists(); assertThat(FileUtils.readFileToString(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8)).contains("Xoo 1.0 (xoo)"); verifyZeroInteractions(system2); }
@Test public void dumpServerSideGlobalProps() throws Exception { logTester.setLevel(LoggerLevel.DEBUG); ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder()); when(globalServerSettings.properties()).thenReturn(ImmutableMap.of(COM_FOO, "bar", SONAR_SKIP, "true")); DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create() .setBaseDir(temp.newFolder()) .setWorkDir(temp.newFolder()) .setProperty("sonar.projectKey", "foo")); when(store.allModules()).thenReturn(singletonList(rootModule)); when(hierarchy.root()).thenReturn(rootModule); publisher.init(writer); String content = FileUtils.readFileToString(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8); assertThat(content).containsOnlyOnce(COM_FOO); assertThat(content).containsOnlyOnce(SONAR_SKIP); }
@Test public void shouldNotDumpSensitiveGlobalProperties() throws Exception { ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder()); when(globalServerSettings.properties()).thenReturn(ImmutableMap.of("sonar.login", "my_token", "sonar.password", "azerty", "sonar.cpp.license.secured", "AZERTY")); DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create() .setBaseDir(temp.newFolder()) .setWorkDir(temp.newFolder()) .setProperty("sonar.projectKey", "foo")); when(store.allModules()).thenReturn(singletonList(rootModule)); when(hierarchy.root()).thenReturn(rootModule); publisher.init(writer); assertThat(FileUtils.readFileToString(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8)).containsSubsequence( "sonar.cpp.license.secured=******", "sonar.login=******", "sonar.password=******"); }
@Test public void dumpServerSideProjectProps() throws Exception { logTester.setLevel(LoggerLevel.DEBUG); ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder()); DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create() .setBaseDir(temp.newFolder()) .setWorkDir(temp.newFolder()) .setProperty("sonar.projectKey", "foo")); when(store.allModules()).thenReturn(singletonList(rootModule)); when(hierarchy.root()).thenReturn(rootModule); when(projectServerSettings.properties()).thenReturn(ImmutableMap.of(COM_FOO, "bar", SONAR_SKIP, "true")); publisher.init(writer); List<String> lines = FileUtils.readLines(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8); assertThat(lines).containsExactly("Environment variables:", "System properties:", "SonarQube plugins:", "Global server settings:", "Project server settings:", " - com.foo=bar", " - sonar.skip=true", "Project scanner properties:", " - sonar.projectKey=foo"); }