aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/main/java/org/sonar/scanner
diff options
context:
space:
mode:
authorBenjamin Campomenosi <benjamin.campomenosi@sonarsource.com>2022-09-29 17:26:32 +0200
committerPhilippe Perrin <philippe.perrin@sonarsource.com>2022-10-07 12:13:56 +0200
commitd46113d7fe9f9c3f9fd82c7e71da3b5b8342098d (patch)
treea49ed6337228b151a625956b72e32e678e455a2b /sonar-scanner-engine/src/main/java/org/sonar/scanner
parentce8ec9e82b7031005b038d2de3ac58519878f910 (diff)
downloadsonarqube-d46113d7fe9f9c3f9fd82c7e71da3b5b8342098d.tar.gz
sonarqube-d46113d7fe9f9c3f9fd82c7e71da3b5b8342098d.zip
SONAR-17305 prevent set sonar.global.XX properties in scanner
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org/sonar/scanner')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleConfigurationProvider.java10
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectConfigurationProvider.java12
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectServerSettingsProvider.java2
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SonarGlobalPropertiesFilter.java57
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringModuleScanContainer.java3
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringProjectScanContainer.java3
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFileIndexer.java7
7 files changed, 89 insertions, 5 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleConfigurationProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleConfigurationProvider.java
index 5cb88b0e7b3..72d212bab43 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleConfigurationProvider.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleConfigurationProvider.java
@@ -30,6 +30,14 @@ import org.sonar.scanner.bootstrap.GlobalServerSettings;
import org.springframework.context.annotation.Bean;
public class ModuleConfigurationProvider {
+
+ private final SonarGlobalPropertiesFilter sonarGlobalPropertiesFilter;
+
+ public ModuleConfigurationProvider(SonarGlobalPropertiesFilter sonarGlobalPropertiesFilter) {
+ this.sonarGlobalPropertiesFilter = sonarGlobalPropertiesFilter;
+ }
+
+
@Bean("ModuleConfiguration")
public ModuleConfiguration provide(GlobalConfiguration globalConfig, DefaultInputModule module, GlobalServerSettings globalServerSettings,
ProjectServerSettings projectServerSettings) {
@@ -38,6 +46,8 @@ public class ModuleConfigurationProvider {
settings.putAll(projectServerSettings.properties());
addScannerSideProperties(settings, module.definition());
+ settings = sonarGlobalPropertiesFilter.enforceOnlyServerSideSonarGlobalPropertiesAreUsed(settings, globalServerSettings.properties());
+
return new ModuleConfiguration(globalConfig.getDefinitions(), globalConfig.getEncryption(), settings);
}
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectConfigurationProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectConfigurationProvider.java
index d20ff578439..e53fe9b4490 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectConfigurationProvider.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectConfigurationProvider.java
@@ -26,7 +26,15 @@ import org.sonar.scanner.bootstrap.GlobalConfiguration;
import org.sonar.scanner.bootstrap.GlobalServerSettings;
import org.springframework.context.annotation.Bean;
+
public class ProjectConfigurationProvider {
+
+ private final SonarGlobalPropertiesFilter sonarGlobalPropertiesFilter;
+
+ public ProjectConfigurationProvider(SonarGlobalPropertiesFilter sonarGlobalPropertiesFilter) {
+ this.sonarGlobalPropertiesFilter = sonarGlobalPropertiesFilter;
+ }
+
@Bean("ProjectConfiguration")
public ProjectConfiguration provide(DefaultInputProject project, GlobalConfiguration globalConfig, GlobalServerSettings globalServerSettings,
ProjectServerSettings projectServerSettings, MutableProjectSettings projectSettings) {
@@ -35,8 +43,12 @@ public class ProjectConfigurationProvider {
settings.putAll(projectServerSettings.properties());
settings.putAll(project.properties());
+ settings = sonarGlobalPropertiesFilter.enforceOnlyServerSideSonarGlobalPropertiesAreUsed(settings, globalServerSettings.properties());
+
ProjectConfiguration projectConfig = new ProjectConfiguration(globalConfig.getDefinitions(), globalConfig.getEncryption(), settings);
projectSettings.complete(projectConfig);
return projectConfig;
}
+
+
}
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectServerSettingsProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectServerSettingsProvider.java
index 55516177aa9..3f2825b7288 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectServerSettingsProvider.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectServerSettingsProvider.java
@@ -30,7 +30,7 @@ import org.springframework.context.annotation.Bean;
public class ProjectServerSettingsProvider {
- private static final Logger LOG = Loggers.get(ProjectConfigurationProvider.class);
+ private static final Logger LOG = Loggers.get(ProjectServerSettingsProvider.class);
private static final String MODULE_LEVEL_ARCHIVED_SETTINGS_WARNING = "Settings that were previously configured at " +
"sub-project level are not used anymore. Transition the settings listed in ‘General Settings -> General -> " +
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SonarGlobalPropertiesFilter.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SonarGlobalPropertiesFilter.java
new file mode 100644
index 00000000000..2dec13058b8
--- /dev/null
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SonarGlobalPropertiesFilter.java
@@ -0,0 +1,57 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.scan;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class SonarGlobalPropertiesFilter {
+
+ @VisibleForTesting
+ static final String SONAR_GLOBAL_PROPERTIES_PREFIX = "sonar.global.";
+
+ public Map<String, String> enforceOnlyServerSideSonarGlobalPropertiesAreUsed(Map<String, String> settingProperties, Map<String, String> globalServerSettingsProperties) {
+ Map<String, String> settings = getNonSonarGlobalProperties(settingProperties);
+ settings.putAll(getSonarGlobalProperties(globalServerSettingsProperties));
+ return settings;
+ }
+
+
+ private static Map<String, String> getNonSonarGlobalProperties(Map<String, String> settingProperties) {
+ return settingProperties.entrySet()
+ .stream()
+ .filter(entry -> !isSonarGlobalProperty(entry.getKey()))
+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+ }
+
+ private static Map<String, String> getSonarGlobalProperties(Map<String, String> properties) {
+ return properties
+ .entrySet()
+ .stream()
+ .filter(entry -> isSonarGlobalProperty(entry.getKey()))
+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+ }
+
+ private static boolean isSonarGlobalProperty(String propertiesCode) {
+ return propertiesCode.startsWith(SONAR_GLOBAL_PROPERTIES_PREFIX);
+ }
+
+}
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringModuleScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringModuleScanContainer.java
index 4707354a706..0f80f0f3c74 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringModuleScanContainer.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringModuleScanContainer.java
@@ -55,7 +55,8 @@ public class SpringModuleScanContainer extends SpringComponentContainer {
module.definition(),
module,
MutableModuleSettings.class,
- new ModuleConfigurationProvider(),
+ SonarGlobalPropertiesFilter.class,
+ ModuleConfigurationProvider.class,
ModuleSensorsExecutor.class,
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringProjectScanContainer.java
index e3d2f50f631..38198a08e42 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringProjectScanContainer.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringProjectScanContainer.java
@@ -228,7 +228,8 @@ public class SpringProjectScanContainer extends SpringComponentContainer {
MutableProjectSettings.class,
ScannerProperties.class,
- new ProjectConfigurationProvider(),
+ SonarGlobalPropertiesFilter.class,
+ ProjectConfigurationProvider.class,
ProjectCoverageAndDuplicationExclusions.class,
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFileIndexer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFileIndexer.java
index e70f14ee468..e648fe77c79 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFileIndexer.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFileIndexer.java
@@ -50,6 +50,7 @@ import org.sonar.scanner.fs.InputModuleHierarchy;
import org.sonar.scanner.scan.ModuleConfiguration;
import org.sonar.scanner.scan.ModuleConfigurationProvider;
import org.sonar.scanner.scan.ProjectServerSettings;
+import org.sonar.scanner.scan.SonarGlobalPropertiesFilter;
import org.sonar.scanner.scm.ScmConfiguration;
import org.sonar.scanner.util.ProgressReport;
@@ -63,6 +64,7 @@ public class ProjectFileIndexer {
private static final Logger LOG = Loggers.get(ProjectFileIndexer.class);
private final ProjectExclusionFilters projectExclusionFilters;
+ private final SonarGlobalPropertiesFilter sonarGlobalPropertiesFilter;
private final ProjectCoverageAndDuplicationExclusions projectCoverageAndDuplicationExclusions;
private final ScmConfiguration scmConfiguration;
private final InputComponentStore componentStore;
@@ -77,9 +79,10 @@ public class ProjectFileIndexer {
private ProgressReport progressReport;
public ProjectFileIndexer(InputComponentStore componentStore, ProjectExclusionFilters exclusionFilters,
- InputModuleHierarchy inputModuleHierarchy, GlobalConfiguration globalConfig, GlobalServerSettings globalServerSettings, ProjectServerSettings projectServerSettings,
+ SonarGlobalPropertiesFilter sonarGlobalPropertiesFilter, InputModuleHierarchy inputModuleHierarchy, GlobalConfiguration globalConfig, GlobalServerSettings globalServerSettings, ProjectServerSettings projectServerSettings,
FileIndexer fileIndexer, ProjectCoverageAndDuplicationExclusions projectCoverageAndDuplicationExclusions, ScmConfiguration scmConfiguration) {
this.componentStore = componentStore;
+ this.sonarGlobalPropertiesFilter = sonarGlobalPropertiesFilter;
this.inputModuleHierarchy = inputModuleHierarchy;
this.globalConfig = globalConfig;
this.globalServerSettings = globalServerSettings;
@@ -140,7 +143,7 @@ public class ProjectFileIndexer {
private void index(DefaultInputModule module, ExclusionCounter exclusionCounter) {
// Emulate creation of module level settings
- ModuleConfiguration moduleConfig = new ModuleConfigurationProvider().provide(globalConfig, module, globalServerSettings, projectServerSettings);
+ ModuleConfiguration moduleConfig = new ModuleConfigurationProvider(sonarGlobalPropertiesFilter).provide(globalConfig, module, globalServerSettings, projectServerSettings);
ModuleExclusionFilters moduleExclusionFilters = new ModuleExclusionFilters(moduleConfig);
ModuleCoverageAndDuplicationExclusions moduleCoverageAndDuplicationExclusions = new ModuleCoverageAndDuplicationExclusions(moduleConfig);
if (componentStore.allModules().size() > 1) {