diff options
author | Alain Kermis <alain.kermis@sonarsource.com> | 2024-12-10 10:46:46 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2025-01-07 20:03:31 +0000 |
commit | ea8a3d053703ad69b17993472a51d5df489feff6 (patch) | |
tree | 5f89c621b51022956eff213fe2e9a79004efbda3 /server | |
parent | e54f99e4713f48c144a54894dde6fca54542a4fe (diff) | |
download | sonarqube-ea8a3d053703ad69b17993472a51d5df489feff6.tar.gz sonarqube-ea8a3d053703ad69b17993472a51d5df489feff6.zip |
SONAR-22047 Remove deprecated SecurityManager code
Diffstat (limited to 'server')
9 files changed, 3 insertions, 417 deletions
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/app/CeSecurityManager.java b/server/sonar-ce/src/main/java/org/sonar/ce/app/CeSecurityManager.java deleted file mode 100644 index 2d0bd4b18cd..00000000000 --- a/server/sonar-ce/src/main/java/org/sonar/ce/app/CeSecurityManager.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2024 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.ce.app; - -import org.sonar.ce.security.PluginCeRule; -import org.sonar.process.PluginFileWriteRule; -import org.sonar.process.PluginSecurityManager; -import org.sonar.process.ProcessProperties; -import org.sonar.process.Props; - -public class CeSecurityManager { - private final PluginSecurityManager pluginSecurityManager; - private final Props props; - - private boolean applied; - - public CeSecurityManager(PluginSecurityManager pluginSecurityManager, Props props) { - this.pluginSecurityManager = pluginSecurityManager; - this.props = props; - } - - public void apply() { - if (applied) { - throw new IllegalStateException("can't apply twice"); - } - applied = true; - - PluginFileWriteRule writeRule = new PluginFileWriteRule( - props.nonNullValueAsFile(ProcessProperties.Property.PATH_HOME.getKey()).toPath(), - props.nonNullValueAsFile(ProcessProperties.Property.PATH_TEMP.getKey()).toPath()); - PluginCeRule ceRule = new PluginCeRule(); - pluginSecurityManager.restrictPlugins(writeRule, ceRule); - } -} diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/app/CeServer.java b/server/sonar-ce/src/main/java/org/sonar/ce/app/CeServer.java index 2422f1f9907..f7d86e8e40c 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/app/CeServer.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/app/CeServer.java @@ -31,7 +31,6 @@ import org.sonar.ce.container.ComputeEngineContainerImpl; import org.sonar.ce.logging.CeProcessLogging; import org.sonar.process.MinimumViableSystem; import org.sonar.process.Monitored; -import org.sonar.process.PluginSecurityManager; import org.sonar.process.ProcessEntryPoint; import org.sonar.process.Props; @@ -56,8 +55,7 @@ public class CeServer implements Monitored { private CeMainThread ceMainThread = null; @VisibleForTesting - protected CeServer(ComputeEngine computeEngine, MinimumViableSystem mvs, CeSecurityManager securityManager) { - securityManager.apply(); + protected CeServer(ComputeEngine computeEngine, MinimumViableSystem mvs) { this.computeEngine = computeEngine; mvs .checkWritableTempDir() @@ -121,8 +119,7 @@ public class CeServer implements Monitored { CeServer server = new CeServer( new ComputeEngineImpl(props, new ComputeEngineContainerImpl()), - new MinimumViableSystem(), - new CeSecurityManager(new PluginSecurityManager(), props)); + new MinimumViableSystem()); entryPoint.launch(server); } diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/app/CeSecurityManagerTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/app/CeSecurityManagerTest.java deleted file mode 100644 index b69dc8e4b96..00000000000 --- a/server/sonar-ce/src/test/java/org/sonar/ce/app/CeSecurityManagerTest.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2024 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.ce.app; - -import java.util.Properties; -import org.junit.Test; -import org.sonar.ce.security.PluginCeRule; -import org.sonar.process.PluginFileWriteRule; -import org.sonar.process.PluginSecurityManager; -import org.sonar.process.Props; - -import static org.junit.Assert.assertThrows; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.sonar.process.ProcessProperties.Property.PATH_HOME; -import static org.sonar.process.ProcessProperties.Property.PATH_TEMP; - -public class CeSecurityManagerTest { - private final PluginSecurityManager pluginSecurityManager = mock(PluginSecurityManager.class); - - @Test - public void apply_calls_PluginSecurityManager() { - Properties properties = new Properties(); - properties.setProperty(PATH_HOME.getKey(), "home"); - properties.setProperty(PATH_TEMP.getKey(), "temp"); - Props props = new Props(properties); - CeSecurityManager ceSecurityManager = new CeSecurityManager(pluginSecurityManager, props); - ceSecurityManager.apply(); - - verify(pluginSecurityManager).restrictPlugins(any(PluginFileWriteRule.class), any(PluginCeRule.class)); - } - - @Test - public void fail_if_runs_twice() { - Properties properties = new Properties(); - properties.setProperty(PATH_HOME.getKey(), "home"); - properties.setProperty(PATH_TEMP.getKey(), "temp"); - Props props = new Props(properties); - CeSecurityManager ceSecurityManager = new CeSecurityManager(pluginSecurityManager, props); - ceSecurityManager.apply(); - assertThrows(IllegalStateException.class, ceSecurityManager::apply); - } -} diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/app/CeServerTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/app/CeServerTest.java index 33d7098f140..dd8120c5780 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/app/CeServerTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/app/CeServerTest.java @@ -55,7 +55,6 @@ public class CeServerTest { private CeServer underTest = null; private Thread waitingThread = null; private final MinimumViableSystem minimumViableSystem = mock(MinimumViableSystem.class, Mockito.RETURNS_MOCKS); - private final CeSecurityManager ceSecurityManager = mock(CeSecurityManager.class); @After public void tearDown() throws Exception { @@ -77,12 +76,6 @@ public class CeServerTest { } @Test - public void constructor_calls_ceSecurityManager() { - newCeServer(); - verify(ceSecurityManager).apply(); - } - - @Test public void awaitStop_throws_ISE_if_called_before_start() { CeServer ceServer = newCeServer(); @@ -269,7 +262,7 @@ public class CeServerTest { private CeServer newCeServer(ComputeEngine computeEngine) { checkState(this.underTest == null, "Only one CeServer can be created per test method"); - this.underTest = new CeServer(computeEngine, minimumViableSystem, ceSecurityManager); + this.underTest = new CeServer(computeEngine, minimumViableSystem); return underTest; } diff --git a/server/sonar-process/src/main/java/org/sonar/process/PluginSecurityManager.java b/server/sonar-process/src/main/java/org/sonar/process/PluginSecurityManager.java deleted file mode 100644 index 46b445d036c..00000000000 --- a/server/sonar-process/src/main/java/org/sonar/process/PluginSecurityManager.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2024 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.process; - -import java.security.CodeSource; -import java.security.Permission; -import java.security.PermissionCollection; -import java.security.Permissions; -import java.security.Policy; -import java.security.ProtectionDomain; -import java.security.Security; -import java.util.List; - -public class PluginSecurityManager { - private static final String CACHE_TTL_KEY = "networkaddress.cache.ttl"; - private boolean alreadyRan = false; - - public void restrictPlugins(PluginPolicyRule... rules) { - if (alreadyRan) { - throw new IllegalStateException("can't run twice"); - } - alreadyRan = true; - SecurityManager sm = new SecurityManager(); - Policy.setPolicy(new PluginPolicy(List.of(rules))); - System.setSecurityManager(sm); - // SONAR-14870 By default, with a security manager installed, the DNS cache never times out. See InetAddressCachePolicy. - if (Security.getProperty(CACHE_TTL_KEY) == null) { - Security.setProperty(CACHE_TTL_KEY, "30"); - } - } - - static class PluginPolicy extends Policy { - private final List<PluginPolicyRule> rules; - - PluginPolicy(List<PluginPolicyRule> rules) { - this.rules = rules; - } - - @Override - public boolean implies(ProtectionDomain domain, Permission permission) { - // classloader used to load plugins - String clName = getDomainClassLoaderName(domain); - if ("org.sonar.classloader.ClassRealm".equals(clName)) { - return rules.stream().allMatch(p -> p.implies(permission)); - } - return true; - } - - // workaround for SONAR-13559 / JDK-8014008 - // borrowed as-is from https://github.com/elastic/elasticsearch/pull/14274 - @Override - public PermissionCollection getPermissions(CodeSource codesource) { - // code should not rely on this method, or at least use it correctly: - // https://bugs.openjdk.java.net/browse/JDK-8014008 - // return them a new empty permissions object so jvisualvm etc work - for (StackTraceElement element : Thread.currentThread().getStackTrace()) { - if ("sun.rmi.server.LoaderHandler".equals(element.getClassName()) && - "loadClass".equals(element.getMethodName())) { - return new Permissions(); - } - } - // return UNSUPPORTED_EMPTY_COLLECTION since it is safe. - return super.getPermissions(codesource); - } - - String getDomainClassLoaderName(ProtectionDomain domain) { - ClassLoader classLoader = domain.getClassLoader(); - return classLoader != null ? classLoader.getClass().getName() : null; - } - } -} diff --git a/server/sonar-process/src/test/java/org/sonar/process/PluginSecurityManagerTest.java b/server/sonar-process/src/test/java/org/sonar/process/PluginSecurityManagerTest.java deleted file mode 100644 index f492a7ddbbd..00000000000 --- a/server/sonar-process/src/test/java/org/sonar/process/PluginSecurityManagerTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2024 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.process; - -import java.security.Permission; -import java.security.Policy; -import java.security.ProtectionDomain; -import java.util.Arrays; -import javax.management.MBeanPermission; -import org.junit.Test; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoInteractions; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - -public class PluginSecurityManagerTest { - private final ClassLoader classRealm = mock(ClassLoader.class, RETURNS_DEEP_STUBS); - private final ProtectionDomain pd = new ProtectionDomain(null, null, classRealm, null); - private final Permission permission = mock(Permission.class); - private final PluginPolicyRule rule1 = mock(PluginPolicyRule.class); - private final PluginPolicyRule rule2 = mock(PluginPolicyRule.class); - - @Test - public void constructor_dontSetAnyPolicy() { - Policy policy = Policy.getPolicy(); - - new PluginSecurityManager(); - - assertThat(policy).isEqualTo(Policy.getPolicy()); - } - - @Test - public void protection_domain_can_have_no_classloader() { - PluginSecurityManager.PluginPolicy policy = new PluginSecurityManager.PluginPolicy(Arrays.asList(rule1, rule2)); - - ProtectionDomain domain = new ProtectionDomain(null, null, null, null); - Permission permission = new MBeanPermission("com.sun.management.internal.HotSpotThreadImpl", "getMBeanInfo"); - - assertThat(policy.implies(domain, permission)).isTrue(); - verifyNoInteractions(rule1, rule2); - } - - @Test - public void policy_doesnt_restrict_other_classloaders() { - PluginSecurityManager.PluginPolicy policy = new PluginSecurityManager.PluginPolicy(Arrays.asList(rule1, rule2)) { - @Override - String getDomainClassLoaderName(ProtectionDomain domain) { - return "classloader"; - } - }; - - policy.implies(pd, permission); - verifyNoInteractions(rule1, rule2); - } - - @Test - public void policy_restricts_class_realm_classloader() { - when(rule1.implies(permission)).thenReturn(true); - PluginSecurityManager.PluginPolicy policy = new PluginSecurityManager.PluginPolicy(Arrays.asList(rule1, rule2)) { - @Override - String getDomainClassLoaderName(ProtectionDomain domain) { - return "org.sonar.classloader.ClassRealm"; - } - }; - - policy.implies(pd, permission); - verify(rule1).implies(permission); - verify(rule2).implies(permission); - verifyNoMoreInteractions(rule1, rule2); - } - -} diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/app/WebSecurityManager.java b/server/sonar-webserver/src/main/java/org/sonar/server/app/WebSecurityManager.java deleted file mode 100644 index 8f483b6849a..00000000000 --- a/server/sonar-webserver/src/main/java/org/sonar/server/app/WebSecurityManager.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2024 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.server.app; - -import org.sonar.process.PluginFileWriteRule; -import org.sonar.process.PluginSecurityManager; -import org.sonar.process.ProcessProperties; -import org.sonar.process.Props; - -public class WebSecurityManager { - private final PluginSecurityManager pluginSecurityManager; - private final Props props; - - private boolean applied; - - public WebSecurityManager(PluginSecurityManager pluginSecurityManager, Props props) { - this.pluginSecurityManager = pluginSecurityManager; - this.props = props; - } - - public void apply() { - if (applied) { - throw new IllegalStateException("can't apply twice"); - } - applied = true; - - PluginFileWriteRule writeRule = new PluginFileWriteRule( - props.nonNullValueAsFile(ProcessProperties.Property.PATH_HOME.getKey()).toPath(), - props.nonNullValueAsFile(ProcessProperties.Property.PATH_TEMP.getKey()).toPath()); - pluginSecurityManager.restrictPlugins(writeRule); - } -} diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/app/WebServer.java b/server/sonar-webserver/src/main/java/org/sonar/server/app/WebServer.java index 1c1164dc799..52a345deeaa 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/app/WebServer.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/app/WebServer.java @@ -24,7 +24,6 @@ import java.io.File; import org.slf4j.LoggerFactory; import org.sonar.process.MinimumViableSystem; import org.sonar.process.Monitored; -import org.sonar.process.PluginSecurityManager; import org.sonar.process.ProcessEntryPoint; import org.sonar.process.ProcessId; import org.sonar.process.Props; @@ -98,7 +97,6 @@ public class WebServer implements Monitored { ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(args); Props props = entryPoint.getProps(); new WebServerProcessLogging().configure(props); - new WebSecurityManager(new PluginSecurityManager(), props).apply(); WebServer server = new WebServer(props); entryPoint.launch(server); diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/app/WebSecurityManagerTest.java b/server/sonar-webserver/src/test/java/org/sonar/server/app/WebSecurityManagerTest.java deleted file mode 100644 index 91c90ae9860..00000000000 --- a/server/sonar-webserver/src/test/java/org/sonar/server/app/WebSecurityManagerTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2024 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.server.app; - -import java.util.Properties; -import org.junit.Test; -import org.sonar.process.PluginFileWriteRule; -import org.sonar.process.PluginSecurityManager; -import org.sonar.process.Props; - -import static org.junit.Assert.assertThrows; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.sonar.process.ProcessProperties.Property.PATH_HOME; -import static org.sonar.process.ProcessProperties.Property.PATH_TEMP; - -public class WebSecurityManagerTest { - private final PluginSecurityManager pluginSecurityManager = mock(PluginSecurityManager.class); - - @Test - public void apply_calls_PluginSecurityManager() { - Properties properties = new Properties(); - properties.setProperty(PATH_HOME.getKey(), "home"); - properties.setProperty(PATH_TEMP.getKey(), "temp"); - Props props = new Props(properties); - WebSecurityManager securityManager = new WebSecurityManager(pluginSecurityManager, props); - securityManager.apply(); - - verify(pluginSecurityManager).restrictPlugins(any(PluginFileWriteRule.class)); - } - - @Test - public void fail_if_runs_twice() { - Properties properties = new Properties(); - properties.setProperty(PATH_HOME.getKey(), "home"); - properties.setProperty(PATH_TEMP.getKey(), "temp"); - Props props = new Props(properties); - WebSecurityManager securityManager = new WebSecurityManager(pluginSecurityManager, props); - securityManager.apply(); - assertThrows(IllegalStateException.class, securityManager::apply); - } -} |