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.

AnalysisContextReportPublisherTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.report;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.io.File;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.Arrays;
  25. import java.util.List;
  26. import java.util.Properties;
  27. import org.apache.commons.io.FileUtils;
  28. import org.apache.commons.lang.StringUtils;
  29. import org.junit.Before;
  30. import org.junit.Rule;
  31. import org.junit.Test;
  32. import org.junit.rules.TemporaryFolder;
  33. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  34. import org.sonar.api.utils.System2;
  35. import org.sonar.api.utils.log.LogTester;
  36. import org.sonar.api.utils.log.LoggerLevel;
  37. import org.sonar.core.platform.PluginInfo;
  38. import org.sonar.scanner.bootstrap.GlobalServerSettings;
  39. import org.sonar.scanner.bootstrap.ScannerPluginRepository;
  40. import org.sonar.api.batch.fs.internal.DefaultInputModule;
  41. import org.sonar.scanner.fs.InputModuleHierarchy;
  42. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  43. import org.sonar.scanner.scan.ProjectServerSettings;
  44. import org.sonar.scanner.scan.filesystem.InputComponentStore;
  45. import org.sonar.updatecenter.common.Version;
  46. import static java.util.Collections.singletonList;
  47. import static org.assertj.core.api.Assertions.assertThat;
  48. import static org.mockito.Mockito.mock;
  49. import static org.mockito.Mockito.verifyZeroInteractions;
  50. import static org.mockito.Mockito.when;
  51. public class AnalysisContextReportPublisherTest {
  52. private static final String BIZ = "BIZ";
  53. private static final String FOO = "FOO";
  54. private static final String SONAR_SKIP = "sonar.skip";
  55. private static final String COM_FOO = "com.foo";
  56. @Rule
  57. public LogTester logTester = new LogTester();
  58. @Rule
  59. public TemporaryFolder temp = new TemporaryFolder();
  60. private ScannerPluginRepository pluginRepo = mock(ScannerPluginRepository.class);
  61. private AnalysisContextReportPublisher publisher;
  62. private System2 system2;
  63. private GlobalServerSettings globalServerSettings;
  64. private InputModuleHierarchy hierarchy;
  65. private InputComponentStore store;
  66. private ProjectServerSettings projectServerSettings;
  67. @Before
  68. public void prepare() {
  69. logTester.setLevel(LoggerLevel.INFO);
  70. system2 = mock(System2.class);
  71. when(system2.properties()).thenReturn(new Properties());
  72. globalServerSettings = mock(GlobalServerSettings.class);
  73. hierarchy = mock(InputModuleHierarchy.class);
  74. store = mock(InputComponentStore.class);
  75. projectServerSettings = mock(ProjectServerSettings.class);
  76. publisher = new AnalysisContextReportPublisher(projectServerSettings, pluginRepo, system2, globalServerSettings, hierarchy, store);
  77. }
  78. @Test
  79. public void shouldOnlyDumpPluginsByDefault() throws Exception {
  80. when(pluginRepo.getPluginInfos()).thenReturn(singletonList(new PluginInfo("xoo").setName("Xoo").setVersion(Version.create("1.0"))));
  81. ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder());
  82. DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create()
  83. .setBaseDir(temp.newFolder())
  84. .setWorkDir(temp.newFolder()));
  85. when(store.allModules()).thenReturn(singletonList(rootModule));
  86. when(hierarchy.root()).thenReturn(rootModule);
  87. publisher.init(writer);
  88. assertThat(writer.getFileStructure().analysisLog()).exists();
  89. assertThat(FileUtils.readFileToString(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8)).contains("Xoo 1.0 (xoo)");
  90. verifyZeroInteractions(system2);
  91. }
  92. @Test
  93. public void dumpServerSideGlobalProps() throws Exception {
  94. logTester.setLevel(LoggerLevel.DEBUG);
  95. ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder());
  96. when(globalServerSettings.properties()).thenReturn(ImmutableMap.of(COM_FOO, "bar", SONAR_SKIP, "true"));
  97. DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create()
  98. .setBaseDir(temp.newFolder())
  99. .setWorkDir(temp.newFolder())
  100. .setProperty("sonar.projectKey", "foo"));
  101. when(store.allModules()).thenReturn(singletonList(rootModule));
  102. when(hierarchy.root()).thenReturn(rootModule);
  103. publisher.init(writer);
  104. String content = FileUtils.readFileToString(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8);
  105. assertThat(content).containsOnlyOnce(COM_FOO);
  106. assertThat(content).containsOnlyOnce(SONAR_SKIP);
  107. }
  108. @Test
  109. public void dumpServerSideProjectProps() throws Exception {
  110. logTester.setLevel(LoggerLevel.DEBUG);
  111. ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder());
  112. DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create()
  113. .setBaseDir(temp.newFolder())
  114. .setWorkDir(temp.newFolder())
  115. .setProperty("sonar.projectKey", "foo"));
  116. when(store.allModules()).thenReturn(singletonList(rootModule));
  117. when(hierarchy.root()).thenReturn(rootModule);
  118. when(projectServerSettings.properties()).thenReturn(ImmutableMap.of(COM_FOO, "bar", SONAR_SKIP, "true"));
  119. publisher.init(writer);
  120. List<String> lines = FileUtils.readLines(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8);
  121. assertThat(lines).containsExactly(
  122. "SonarQube plugins:",
  123. "Global server settings:",
  124. "Project server settings:",
  125. " - com.foo=bar",
  126. " - sonar.skip=true",
  127. "Project scanner properties:",
  128. " - sonar.projectKey=foo");
  129. }
  130. @Test
  131. public void shouldNotDumpSensitiveModuleProperties() throws Exception {
  132. ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder());
  133. DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create()
  134. .setBaseDir(temp.newFolder())
  135. .setWorkDir(temp.newFolder())
  136. .setProperty("sonar.projectKey", "foo")
  137. .setProperty("sonar.projectKey", "foo")
  138. .setProperty("sonar.login", "my_token")
  139. .setProperty("sonar.password", "azerty")
  140. .setProperty("sonar.cpp.license.secured", "AZERTY"));
  141. when(store.allModules()).thenReturn(singletonList(rootModule));
  142. when(hierarchy.root()).thenReturn(rootModule);
  143. publisher.init(writer);
  144. assertThat(writer.getFileStructure().analysisLog()).exists();
  145. assertThat(FileUtils.readFileToString(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8)).containsSubsequence(
  146. "sonar.cpp.license.secured=******",
  147. "sonar.login=******",
  148. "sonar.password=******",
  149. "sonar.projectKey=foo");
  150. }
  151. @Test
  152. public void shouldShortenModuleProperties() throws Exception {
  153. File baseDir = temp.newFolder();
  154. ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder());
  155. DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create()
  156. .setBaseDir(baseDir)
  157. .setWorkDir(temp.newFolder())
  158. .setProperty("sonar.projectKey", "foo")
  159. .setProperty("sonar.projectBaseDir", baseDir.toString())
  160. .setProperty("sonar.aVeryLongProp", StringUtils.repeat("abcde", 1000)));
  161. when(store.allModules()).thenReturn(singletonList(rootModule));
  162. when(hierarchy.root()).thenReturn(rootModule);
  163. publisher.init(writer);
  164. assertThat(writer.getFileStructure().analysisLog()).exists();
  165. assertThat(FileUtils.readFileToString(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8)).containsSubsequence(
  166. "sonar.aVeryLongProp=" + StringUtils.repeat("abcde", 199) + "ab...",
  167. "sonar.projectBaseDir=" + baseDir.toString(),
  168. "sonar.projectKey=foo");
  169. }
  170. // SONAR-7598
  171. @Test
  172. public void shouldNotDumpSensitiveGlobalProperties() throws Exception {
  173. ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder());
  174. when(globalServerSettings.properties()).thenReturn(ImmutableMap.of("sonar.login", "my_token", "sonar.password", "azerty", "sonar.cpp.license.secured", "AZERTY"));
  175. DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create()
  176. .setBaseDir(temp.newFolder())
  177. .setWorkDir(temp.newFolder())
  178. .setProperty("sonar.projectKey", "foo"));
  179. when(store.allModules()).thenReturn(singletonList(rootModule));
  180. when(hierarchy.root()).thenReturn(rootModule);
  181. publisher.init(writer);
  182. assertThat(FileUtils.readFileToString(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8)).containsSubsequence(
  183. "sonar.cpp.license.secured=******",
  184. "sonar.login=******",
  185. "sonar.password=******");
  186. }
  187. // SONAR-7371
  188. @Test
  189. public void dontDumpParentProps() throws Exception {
  190. logTester.setLevel(LoggerLevel.DEBUG);
  191. ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder());
  192. DefaultInputModule module = new DefaultInputModule(ProjectDefinition.create()
  193. .setBaseDir(temp.newFolder())
  194. .setWorkDir(temp.newFolder())
  195. .setProperty("sonar.projectKey", "foo")
  196. .setProperty(SONAR_SKIP, "true"));
  197. DefaultInputModule parent = new DefaultInputModule(ProjectDefinition.create()
  198. .setBaseDir(temp.newFolder())
  199. .setWorkDir(temp.newFolder())
  200. .setProperty("sonar.projectKey", "parent")
  201. .setProperty(SONAR_SKIP, "true"));
  202. when(hierarchy.parent(module)).thenReturn(parent);
  203. when(store.allModules()).thenReturn(Arrays.asList(parent, module));
  204. when(hierarchy.root()).thenReturn(parent);
  205. publisher.init(writer);
  206. List<String> lines = FileUtils.readLines(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8);
  207. assertThat(lines).containsExactly(
  208. "SonarQube plugins:",
  209. "Global server settings:",
  210. "Project server settings:",
  211. "Project scanner properties:",
  212. " - sonar.projectKey=parent",
  213. " - sonar.skip=true",
  214. "Scanner properties of module: foo",
  215. " - sonar.projectKey=foo"
  216. );
  217. }
  218. }