diff options
author | Antoine Vigneau <antoine.vigneau@sonarsource.com> | 2023-03-09 16:24:43 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-03-22 20:04:07 +0000 |
commit | 3ef3da67d478090c2f7961d50a433033be19b3c4 (patch) | |
tree | a09462dff80dac95cd730bc095ae86d49894241b /server/sonar-webserver-core | |
parent | fae006196acf91109f453aeac95acdcc5db6a24e (diff) | |
download | sonarqube-3ef3da67d478090c2f7961d50a433033be19b3c4.tar.gz sonarqube-3ef3da67d478090c2f7961d50a433033be19b3c4.zip |
SONAR-18654 Add managed provider in system/info endpoint
Diffstat (limited to 'server/sonar-webserver-core')
8 files changed, 357 insertions, 247 deletions
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java index 251325c2bc2..f7e8124713d 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java @@ -24,6 +24,7 @@ import org.sonar.process.systeminfo.JvmPropertiesSection; import org.sonar.process.systeminfo.JvmStateSection; import org.sonar.server.platform.monitoring.AlmConfigurationSection; import org.sonar.server.platform.monitoring.BundledSection; +import org.sonar.server.platform.monitoring.CommonSystemInformation; import org.sonar.server.platform.monitoring.DbConnectionSection; import org.sonar.server.platform.monitoring.DbSection; import org.sonar.server.platform.monitoring.EsIndexesSection; @@ -65,7 +66,8 @@ public class SystemInfoWriterModule extends Module { AlmConfigurationSection.class, ServerPushSection.class, BundledSection.class, - StatisticsSupport.class + StatisticsSupport.class, + CommonSystemInformation.class ); if (standalone) { add( diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/CommonSystemInformation.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/CommonSystemInformation.java new file mode 100644 index 00000000000..f37d4bdaeb7 --- /dev/null +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/CommonSystemInformation.java @@ -0,0 +1,91 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.platform.monitoring; + +import java.util.List; +import javax.annotation.CheckForNull; +import org.sonar.api.CoreProperties; +import org.sonar.api.config.Configuration; +import org.sonar.api.security.SecurityRealm; +import org.sonar.api.server.authentication.IdentityProvider; +import org.sonar.server.authentication.IdentityProviderRepository; +import org.sonar.server.management.ManagedInstanceService; +import org.sonar.server.user.SecurityRealmFactory; + +import static java.util.Collections.emptyList; +import static org.sonar.api.CoreProperties.CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE; + +public class CommonSystemInformation { + private final Configuration config; + private final IdentityProviderRepository identityProviderRepository; + private final ManagedInstanceService managedInstanceService; + private final SecurityRealmFactory securityRealmFactory; + + public CommonSystemInformation(Configuration config, IdentityProviderRepository identityProviderRepository, + ManagedInstanceService managedInstanceService, SecurityRealmFactory securityRealmFactory) { + this.config = config; + this.identityProviderRepository = identityProviderRepository; + this.managedInstanceService = managedInstanceService; + this.securityRealmFactory = securityRealmFactory; + } + + public boolean getForceAuthentication() { + return config.getBoolean(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY).orElse(CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE); + } + + public List<String> getEnabledIdentityProviders() { + return identityProviderRepository.getAllEnabledAndSorted() + .stream() + .filter(IdentityProvider::isEnabled) + .map(IdentityProvider::getName) + .toList(); + } + + public List<String> getAllowsToSignUpEnabledIdentityProviders() { + if (managedInstanceService.isInstanceExternallyManaged()) { + return emptyList(); + } + return identityProviderRepository.getAllEnabledAndSorted() + .stream() + .filter(IdentityProvider::isEnabled) + .filter(IdentityProvider::allowsUsersToSignUp) + .map(IdentityProvider::getName) + .toList(); + } + + public String getManagedProvider() { + if (managedInstanceService.isInstanceExternallyManaged()) { + return identityProviderRepository.getAllEnabledAndSorted() + .stream() + .filter(provider -> provider.getKey().equalsIgnoreCase("saml")) + .filter(IdentityProvider::isEnabled) + .findFirst() + .map(IdentityProvider::getName) + .orElse(""); + } + return ""; + } + + @CheckForNull + public String getExternalUserAuthentication() { + SecurityRealm realm = securityRealmFactory.getRealm(); + return realm == null ? "" : realm.getName(); + } +} diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java index 3c0eddce03f..451c9865a4b 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java @@ -20,26 +20,16 @@ package org.sonar.server.platform.monitoring; import com.google.common.base.Joiner; -import java.util.List; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.sonar.api.CoreProperties; import org.sonar.api.SonarRuntime; import org.sonar.api.config.Configuration; import org.sonar.api.platform.Server; -import org.sonar.api.security.SecurityRealm; -import org.sonar.api.server.authentication.IdentityProvider; -import org.sonar.core.util.stream.MoreCollectors; import org.sonar.process.systeminfo.BaseSectionMBean; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; -import org.sonar.server.authentication.IdentityProviderRepository; import org.sonar.server.log.ServerLogging; import org.sonar.server.platform.DockerSupport; import org.sonar.server.platform.OfficialDistribution; import org.sonar.server.platform.StatisticsSupport; -import org.sonar.server.user.SecurityRealmFactory; -import static org.sonar.api.CoreProperties.CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE; import static org.sonar.api.measures.CoreMetrics.NCLOC; import static org.sonar.process.ProcessProperties.Property.PATH_DATA; import static org.sonar.process.ProcessProperties.Property.PATH_HOME; @@ -51,28 +41,25 @@ public class StandaloneSystemSection extends BaseSectionMBean implements SystemS private static final Joiner COMMA_JOINER = Joiner.on(", "); private final Configuration config; - private final SecurityRealmFactory securityRealmFactory; - private final IdentityProviderRepository identityProviderRepository; private final Server server; private final ServerLogging serverLogging; private final OfficialDistribution officialDistribution; private final DockerSupport dockerSupport; private final StatisticsSupport statisticsSupport; - private final SonarRuntime sonarRuntime; + private final CommonSystemInformation commonSystemInformation; - public StandaloneSystemSection(Configuration config, SecurityRealmFactory securityRealmFactory, - IdentityProviderRepository identityProviderRepository, Server server, ServerLogging serverLogging, - OfficialDistribution officialDistribution, DockerSupport dockerSupport, StatisticsSupport statisticsSupport, SonarRuntime sonarRuntime) { + public StandaloneSystemSection(Configuration config, Server server, ServerLogging serverLogging, + OfficialDistribution officialDistribution, DockerSupport dockerSupport, StatisticsSupport statisticsSupport, + SonarRuntime sonarRuntime, CommonSystemInformation commonSystemInformation) { this.config = config; - this.securityRealmFactory = securityRealmFactory; - this.identityProviderRepository = identityProviderRepository; this.server = server; this.serverLogging = serverLogging; this.officialDistribution = officialDistribution; this.dockerSupport = dockerSupport; this.statisticsSupport = statisticsSupport; this.sonarRuntime = sonarRuntime; + this.commonSystemInformation = commonSystemInformation; } @Override @@ -90,33 +77,6 @@ public class StandaloneSystemSection extends BaseSectionMBean implements SystemS return serverLogging.getRootLoggerLevel().name(); } - @CheckForNull - private String getExternalUserAuthentication() { - SecurityRealm realm = securityRealmFactory.getRealm(); - return realm == null ? null : realm.getName(); - } - - private List<String> getEnabledIdentityProviders() { - return identityProviderRepository.getAllEnabledAndSorted() - .stream() - .filter(IdentityProvider::isEnabled) - .map(IdentityProvider::getName) - .collect(MoreCollectors.toList()); - } - - private List<String> getAllowsToSignUpEnabledIdentityProviders() { - return identityProviderRepository.getAllEnabledAndSorted() - .stream() - .filter(IdentityProvider::isEnabled) - .filter(IdentityProvider::allowsUsersToSignUp) - .map(IdentityProvider::getName) - .collect(MoreCollectors.toList()); - } - - private boolean getForceAuthentication() { - return config.getBoolean(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY).orElse(CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE); - } - @Override public String name() { // JMX name @@ -133,22 +93,18 @@ public class StandaloneSystemSection extends BaseSectionMBean implements SystemS setAttribute(protobuf, "Edition", sonarRuntime.getEdition().getLabel()); setAttribute(protobuf, NCLOC.getName(), statisticsSupport.getLinesOfCode()); setAttribute(protobuf, "Docker", dockerSupport.isRunningInDocker()); - setAttribute(protobuf, "External User Authentication", getExternalUserAuthentication()); - addIfNotEmpty(protobuf, "Accepted external identity providers", getEnabledIdentityProviders()); - addIfNotEmpty(protobuf, "External identity providers whose users are allowed to sign themselves up", getAllowsToSignUpEnabledIdentityProviders()); + setAttribute(protobuf, "External Users and Groups Provisioning", commonSystemInformation.getManagedProvider()); + setAttribute(protobuf, "External User Authentication", commonSystemInformation.getExternalUserAuthentication()); + setAttribute(protobuf, "Accepted external identity providers", COMMA_JOINER.join(commonSystemInformation.getEnabledIdentityProviders())); + setAttribute(protobuf, "External identity providers whose users are allowed to sign themselves up", + COMMA_JOINER.join(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders())); setAttribute(protobuf, "High Availability", false); setAttribute(protobuf, "Official Distribution", officialDistribution.check()); - setAttribute(protobuf, "Force authentication", getForceAuthentication()); + setAttribute(protobuf, "Force authentication", commonSystemInformation.getForceAuthentication()); setAttribute(protobuf, "Home Dir", config.get(PATH_HOME.getKey()).orElse(null)); setAttribute(protobuf, "Data Dir", config.get(PATH_DATA.getKey()).orElse(null)); setAttribute(protobuf, "Temp Dir", config.get(PATH_TEMP.getKey()).orElse(null)); setAttribute(protobuf, "Processors", Runtime.getRuntime().availableProcessors()); return protobuf.build(); } - - private static void addIfNotEmpty(ProtobufSystemInfo.Section.Builder protobuf, String key, @Nullable List<String> values) { - if (values != null && !values.isEmpty()) { - setAttribute(protobuf, key, COMMA_JOINER.join(values)); - } - } } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java index 368b6c523b0..4d9f45b4d71 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java @@ -20,51 +20,37 @@ package org.sonar.server.platform.monitoring.cluster; import com.google.common.base.Joiner; -import java.util.List; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.sonar.api.CoreProperties; import org.sonar.api.SonarRuntime; -import org.sonar.api.config.Configuration; import org.sonar.api.platform.Server; -import org.sonar.api.security.SecurityRealm; import org.sonar.api.server.ServerSide; -import org.sonar.api.server.authentication.IdentityProvider; -import org.sonar.core.util.stream.MoreCollectors; import org.sonar.process.systeminfo.Global; import org.sonar.process.systeminfo.SystemInfoSection; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; -import org.sonar.server.authentication.IdentityProviderRepository; import org.sonar.server.platform.DockerSupport; import org.sonar.server.platform.StatisticsSupport; -import org.sonar.server.user.SecurityRealmFactory; +import org.sonar.server.platform.monitoring.CommonSystemInformation; -import static org.sonar.api.CoreProperties.CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE; import static org.sonar.api.measures.CoreMetrics.NCLOC; import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute; @ServerSide public class GlobalSystemSection implements SystemInfoSection, Global { + private static final Joiner COMMA_JOINER = Joiner.on(", "); - private final Configuration config; private final Server server; - private final SecurityRealmFactory securityRealmFactory; - private final IdentityProviderRepository identityProviderRepository; private final DockerSupport dockerSupport; private final StatisticsSupport statisticsSupport; - private final SonarRuntime sonarRuntime; + private final CommonSystemInformation commonSystemInformation; - public GlobalSystemSection(Configuration config, Server server, SecurityRealmFactory securityRealmFactory, - IdentityProviderRepository identityProviderRepository, DockerSupport dockerSupport, StatisticsSupport statisticsSupport, SonarRuntime sonarRuntime) { - this.config = config; + public GlobalSystemSection(Server server, DockerSupport dockerSupport, StatisticsSupport statisticsSupport, SonarRuntime sonarRuntime, + CommonSystemInformation commonSystemInformation) { this.server = server; - this.securityRealmFactory = securityRealmFactory; - this.identityProviderRepository = identityProviderRepository; this.dockerSupport = dockerSupport; this.statisticsSupport = statisticsSupport; this.sonarRuntime = sonarRuntime; + this.commonSystemInformation = commonSystemInformation; } @Override @@ -77,44 +63,11 @@ public class GlobalSystemSection implements SystemInfoSection, Global { setAttribute(protobuf, NCLOC.getName() ,statisticsSupport.getLinesOfCode()); setAttribute(protobuf, "Docker", dockerSupport.isRunningInDocker()); setAttribute(protobuf, "High Availability", true); - setAttribute(protobuf, "External User Authentication", getExternalUserAuthentication()); - addIfNotEmpty(protobuf, "Accepted external identity providers", getEnabledIdentityProviders()); - addIfNotEmpty(protobuf, "External identity providers whose users are allowed to sign themselves up", getAllowsToSignUpEnabledIdentityProviders()); - setAttribute(protobuf, "Force authentication", getForceAuthentication()); + setAttribute(protobuf, "External Users and Groups Provisioning", commonSystemInformation.getManagedProvider()); + setAttribute(protobuf, "External User Authentication", commonSystemInformation.getExternalUserAuthentication()); + setAttribute(protobuf, "Accepted external identity providers", COMMA_JOINER.join(commonSystemInformation.getEnabledIdentityProviders())); + setAttribute(protobuf, "External identity providers whose users are allowed to sign themselves up", COMMA_JOINER.join(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders())); + setAttribute(protobuf, "Force authentication", commonSystemInformation.getForceAuthentication()); return protobuf.build(); } - - private List<String> getEnabledIdentityProviders() { - return identityProviderRepository.getAllEnabledAndSorted() - .stream() - .filter(IdentityProvider::isEnabled) - .map(IdentityProvider::getName) - .collect(MoreCollectors.toList()); - } - - private List<String> getAllowsToSignUpEnabledIdentityProviders() { - return identityProviderRepository.getAllEnabledAndSorted() - .stream() - .filter(IdentityProvider::isEnabled) - .filter(IdentityProvider::allowsUsersToSignUp) - .map(IdentityProvider::getName) - .collect(MoreCollectors.toList()); - } - - private boolean getForceAuthentication() { - return config.getBoolean(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY).orElse(CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE); - } - - private static void addIfNotEmpty(ProtobufSystemInfo.Section.Builder protobuf, String key, @Nullable List<String> values) { - if (values != null && !values.isEmpty()) { - setAttribute(protobuf, key, COMMA_JOINER.join(values)); - } - } - - @CheckForNull - private String getExternalUserAuthentication() { - SecurityRealm realm = securityRealmFactory.getRealm(); - return realm == null ? null : realm.getName(); - } - } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java index aa2b33b264d..c535731a3c3 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java @@ -35,7 +35,7 @@ public class SystemInfoWriterModuleTest { when(nodeInformation.isStandalone()).thenReturn(false); ListContainer container = new ListContainer(); underTest.configure(container); - assertThat(container.getAddedObjects()).hasSize(21); + assertThat(container.getAddedObjects()).hasSize(22); } @Test @@ -44,6 +44,6 @@ public class SystemInfoWriterModuleTest { ListContainer container = new ListContainer(); underTest.configure(container); - assertThat(container.getAddedObjects()).hasSize(15); + assertThat(container.getAddedObjects()).hasSize(16); } } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/CommonSystemInformationTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/CommonSystemInformationTest.java new file mode 100644 index 00000000000..ebda4bb2c03 --- /dev/null +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/CommonSystemInformationTest.java @@ -0,0 +1,180 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.platform.monitoring; + +import java.util.List; +import java.util.Optional; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.sonar.api.CoreProperties; +import org.sonar.api.config.Configuration; +import org.sonar.api.security.SecurityRealm; +import org.sonar.api.server.authentication.IdentityProvider; +import org.sonar.server.authentication.IdentityProviderRepository; +import org.sonar.server.authentication.TestIdentityProvider; +import org.sonar.server.management.ManagedInstanceService; +import org.sonar.server.user.SecurityRealmFactory; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.api.CoreProperties.CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE; + +@RunWith(MockitoJUnitRunner.class) +public class CommonSystemInformationTest { + @Mock + private Configuration config; + @Mock + private IdentityProviderRepository identityProviderRepository; + @Mock + private ManagedInstanceService managedInstanceService; + @Mock + private SecurityRealmFactory securityRealmFactory; + @InjectMocks + private CommonSystemInformation commonSystemInformation; + + @Test + public void getForceAuthentication_whenNotDefined_shouldUseDefault() { + assertThat(commonSystemInformation.getForceAuthentication()) + .isEqualTo(CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE); + } + + @Test + public void getForceAuthentication_whenDefined_shouldBeUsed() { + when(config.getBoolean(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY)).thenReturn(Optional.of(false)); + + assertThat(commonSystemInformation.getForceAuthentication()) + .isFalse(); + } + + @Test + public void getEnabledIdentityProviders_whenNonDefined_shouldReturnEmpty() { + mockIdentityProviders(List.of()); + + assertThat(commonSystemInformation.getEnabledIdentityProviders()) + .isEmpty(); + } + + @Test + public void getEnabledIdentityProviders_whenDefined_shouldReturnOnlyEnabled() { + mockIdentityProviders(List.of( + new TestIdentityProvider().setKey("saml").setName("Okta").setEnabled(true), + new TestIdentityProvider().setKey("github").setName("GitHub").setEnabled(true), + new TestIdentityProvider().setKey("bitbucket").setName("BitBucket").setEnabled(false) + )); + + assertThat(commonSystemInformation.getEnabledIdentityProviders()) + .containsExactlyInAnyOrder("Okta", "GitHub"); + } + + @Test + public void getAllowsToSignUpEnabledIdentityProviders_whenNonDefined_shouldReturnEmpty() { + mockIdentityProviders(List.of()); + + assertThat(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()) + .isEmpty(); + } + + @Test + public void getAllowsToSignUpEnabledIdentityProviders_whenDefinedButInstanceManaged_shouldReturnNull() { + mockIdentityProviders(List.of( + new TestIdentityProvider().setKey("saml").setName("Okta").setEnabled(true).setAllowsUsersToSignUp(true), + new TestIdentityProvider().setKey("github").setName("GitHub").setEnabled(true).setAllowsUsersToSignUp(false), + new TestIdentityProvider().setKey("bitbucket").setName("BitBucket").setEnabled(false).setAllowsUsersToSignUp(false) + )); + mockManagedInstance(true); + + assertThat(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()) + .isEmpty(); + } + + @Test + public void getAllowsToSignUpEnabledIdentityProviders_whenDefined_shouldReturnOnlyEnabled() { + mockIdentityProviders(List.of( + new TestIdentityProvider().setKey("saml").setName("Okta").setEnabled(true).setAllowsUsersToSignUp(true), + new TestIdentityProvider().setKey("github").setName("GitHub").setEnabled(true).setAllowsUsersToSignUp(false), + new TestIdentityProvider().setKey("bitbucket").setName("BitBucket").setEnabled(false).setAllowsUsersToSignUp(false) + )); + + assertThat(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()) + .containsExactly("Okta"); + } + + @Test + public void getManagedProvider_whenInstanceNotManaged_shouldReturnNull() { + mockIdentityProviders(List.of()); + mockManagedInstance(false); + + assertThat(commonSystemInformation.getManagedProvider()) + .isEmpty(); + } + + @Test + public void getManagedProvider_whenInstanceManagedButNoValidProviderDefined_shouldReturnNull() { + mockIdentityProviders(List.of()); + mockManagedInstance(true); + + assertThat(commonSystemInformation.getManagedProvider()) + .isEmpty(); + } + + @Test + public void getManagedProvider_whenInstanceManagedAndValidProviderDefined_shouldReturnProviderName() { + mockIdentityProviders(List.of( + new TestIdentityProvider().setKey("saml").setName("Okta").setEnabled(true).setAllowsUsersToSignUp(true), + new TestIdentityProvider().setKey("github").setName("GitHub").setEnabled(true).setAllowsUsersToSignUp(true) + )); + mockManagedInstance(true); + + assertThat(commonSystemInformation.getManagedProvider()) + .isEqualTo("Okta"); + } + + @Test + public void getExternalUserAuthentication_whenNotDefined_shouldReturnNull() { + assertThat(commonSystemInformation.getExternalUserAuthentication()) + .isEmpty(); + } + + @Test + public void getExternalUserAuthentication_whenDefined_shouldReturnName() { + mockSecurityRealmFactory("Security Realm"); + + assertThat(commonSystemInformation.getExternalUserAuthentication()) + .isEqualTo("Security Realm"); + } + + private void mockIdentityProviders(List<IdentityProvider> identityProviders) { + when(identityProviderRepository.getAllEnabledAndSorted()).thenReturn(identityProviders); + } + + private void mockManagedInstance(boolean managed) { + when(managedInstanceService.isInstanceExternallyManaged()).thenReturn(managed); + } + + private void mockSecurityRealmFactory(String name) { + SecurityRealm securityRealm = mock(SecurityRealm.class); + when(securityRealm.getName()).thenReturn(name); + when(securityRealmFactory.getRealm()).thenReturn(securityRealm); + } +} diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java index 8d96c6f8bc1..b0c62e92d3b 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java @@ -22,25 +22,21 @@ package org.sonar.server.platform.monitoring; import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; +import java.util.List; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import org.sonar.api.CoreProperties; import org.sonar.api.SonarEdition; import org.sonar.api.SonarRuntime; +import org.sonar.api.config.Configuration; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.platform.Server; -import org.sonar.api.security.SecurityRealm; import org.sonar.api.utils.log.LoggerLevel; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; -import org.sonar.server.authentication.IdentityProviderRepositoryRule; -import org.sonar.server.authentication.TestIdentityProvider; import org.sonar.server.log.ServerLogging; import org.sonar.server.platform.DockerSupport; import org.sonar.server.platform.OfficialDistribution; import org.sonar.server.platform.StatisticsSupport; -import org.sonar.server.user.SecurityRealmFactory; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -55,21 +51,18 @@ import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatA @RunWith(DataProviderRunner.class) public class StandaloneSystemSectionTest { - @Rule - public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule(); + private final MapSettings settings = new MapSettings(); + private final Configuration config = settings.asConfig(); + private final Server server = mock(Server.class); + private final ServerLogging serverLogging = mock(ServerLogging.class); + private final OfficialDistribution officialDistribution = mock(OfficialDistribution.class); + private final DockerSupport dockerSupport = mock(DockerSupport.class); + private final StatisticsSupport statisticsSupport = mock(StatisticsSupport.class); + private final SonarRuntime sonarRuntime = mock(SonarRuntime.class); + private final CommonSystemInformation commonSystemInformation = mock(CommonSystemInformation.class); - private MapSettings settings = new MapSettings(); - private Server server = mock(Server.class); - private ServerLogging serverLogging = mock(ServerLogging.class); - private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class); - private OfficialDistribution officialDistribution = mock(OfficialDistribution.class); - private DockerSupport dockerSupport = mock(DockerSupport.class); - private StatisticsSupport statisticsSupport = mock(StatisticsSupport.class); - - private SonarRuntime sonarRuntime = mock(SonarRuntime.class); - - private StandaloneSystemSection underTest = new StandaloneSystemSection(settings.asConfig(), securityRealmFactory, identityProviderRepository, server, - serverLogging, officialDistribution, dockerSupport, statisticsSupport, sonarRuntime); + private final StandaloneSystemSection underTest = new StandaloneSystemSection(config, server, serverLogging, + officialDistribution, dockerSupport, statisticsSupport, sonarRuntime, commonSystemInformation); @Before public void setUp() { @@ -105,59 +98,31 @@ public class StandaloneSystemSectionTest { } @Test - public void get_realm() { - SecurityRealm realm = mock(SecurityRealm.class); - when(realm.getName()).thenReturn("LDAP"); - when(securityRealmFactory.getRealm()).thenReturn(realm); - + public void toProtobuf_whenExternalUserAuthentication_shouldWriteIt() { + when(commonSystemInformation.getExternalUserAuthentication()).thenReturn("LDAP"); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); assertThatAttributeIs(protobuf, "External User Authentication", "LDAP"); } @Test - public void no_realm() { - when(securityRealmFactory.getRealm()).thenReturn(null); + public void toProtobuf_whenNoExternalUserAuthentication_shouldWriteNothing() { + when(commonSystemInformation.getExternalUserAuthentication()).thenReturn(""); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); - assertThat(attribute(protobuf, "External User Authentication")).isNull(); + assertThatAttributeIs(protobuf, "External User Authentication", ""); } @Test - public void get_enabled_identity_providers() { - identityProviderRepository.addIdentityProvider(new TestIdentityProvider() - .setKey("github") - .setName("GitHub") - .setEnabled(true)); - identityProviderRepository.addIdentityProvider(new TestIdentityProvider() - .setKey("bitbucket") - .setName("Bitbucket") - .setEnabled(true)); - identityProviderRepository.addIdentityProvider(new TestIdentityProvider() - .setKey("disabled") - .setName("Disabled") - .setEnabled(false)); + public void toProtobuf_whenEnabledIdentityProviders_shouldWriteThem() { + when(commonSystemInformation.getEnabledIdentityProviders()).thenReturn(List.of("Bitbucket, GitHub")); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub"); } @Test - public void get_enabled_identity_providers_allowing_users_to_signup() { - identityProviderRepository.addIdentityProvider(new TestIdentityProvider() - .setKey("github") - .setName("GitHub") - .setEnabled(true) - .setAllowsUsersToSignUp(true)); - identityProviderRepository.addIdentityProvider(new TestIdentityProvider() - .setKey("bitbucket") - .setName("Bitbucket") - .setEnabled(true) - .setAllowsUsersToSignUp(false)); - identityProviderRepository.addIdentityProvider(new TestIdentityProvider() - .setKey("disabled") - .setName("Disabled") - .setEnabled(false) - .setAllowsUsersToSignUp(true)); + public void toProtobuf_whenAllowsToSignUpEnabledIdentityProviders_shouldWriteThem() { + when(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()).thenReturn(List.of("GitHub")); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub"); @@ -170,14 +135,8 @@ public class StandaloneSystemSectionTest { } @Test - public void get_force_authentication_defaults_to_true() { - ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); - assertThatAttributeIs(protobuf, "Force authentication", true); - } - - @Test - public void get_force_authentication() { - settings.setProperty(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY, false); + public void toProtobuf_whenForceAuthentication_returnIt() { + when(commonSystemInformation.getForceAuthentication()).thenReturn(false); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); assertThatAttributeIs(protobuf, "Force authentication", false); } @@ -205,6 +164,22 @@ public class StandaloneSystemSectionTest { assertThatAttributeIs(protobuf, "Edition", editionLabel); } + @Test + public void toProtobuf_whenInstanceIsManaged_shouldWriteItsProviderName() { + when(commonSystemInformation.getManagedProvider()).thenReturn("OKTA"); + + ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); + assertThatAttributeIs(protobuf, "External Users and Groups Provisioning", "OKTA"); + } + + @Test + public void toProtobuf_whenInstanceIsNotManaged_shouldWriteNothing() { + when(commonSystemInformation.getManagedProvider()).thenReturn(""); + ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); + + assertThatAttributeIs(protobuf, "External Users and Groups Provisioning", ""); + } + @DataProvider public static Object[][] trueOrFalse() { return new Object[][] { diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java index 91c2b435aa6..578ae8605d7 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java @@ -22,46 +22,33 @@ package org.sonar.server.platform.monitoring.cluster; import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; +import java.util.List; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import org.sonar.api.CoreProperties; import org.sonar.api.SonarRuntime; -import org.sonar.api.config.internal.MapSettings; import org.sonar.api.platform.Server; -import org.sonar.api.security.SecurityRealm; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; -import org.sonar.server.authentication.IdentityProviderRepositoryRule; -import org.sonar.server.authentication.TestIdentityProvider; import org.sonar.server.platform.DockerSupport; import org.sonar.server.platform.StatisticsSupport; -import org.sonar.server.user.SecurityRealmFactory; +import org.sonar.server.platform.monitoring.CommonSystemInformation; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.sonar.api.SonarEdition.COMMUNITY; -import static org.sonar.process.systeminfo.SystemInfoUtils.attribute; import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs; @RunWith(DataProviderRunner.class) public class GlobalSystemSectionTest { - @Rule - public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule(); + private final Server server = mock(Server.class); + private final DockerSupport dockerSupport = mock(DockerSupport.class); + private final StatisticsSupport statisticsSupport = mock(StatisticsSupport.class); + private final SonarRuntime sonarRuntime = mock(SonarRuntime.class); + private final CommonSystemInformation commonSystemInformation = mock(CommonSystemInformation.class); - private MapSettings settings = new MapSettings(); - private Server server = mock(Server.class); - private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class); - - private DockerSupport dockerSupport = mock(DockerSupport.class); - private StatisticsSupport statisticsSupport = mock(StatisticsSupport.class); - - private SonarRuntime sonarRuntime = mock(SonarRuntime.class); - - private GlobalSystemSection underTest = new GlobalSystemSection(settings.asConfig(), - server, securityRealmFactory, identityProviderRepository, dockerSupport, statisticsSupport, sonarRuntime); + private final GlobalSystemSection underTest = new GlobalSystemSection(server, dockerSupport, statisticsSupport, sonarRuntime, commonSystemInformation); @Before public void setUp() { @@ -74,73 +61,39 @@ public class GlobalSystemSectionTest { } @Test - public void get_realm() { - SecurityRealm realm = mock(SecurityRealm.class); - when(realm.getName()).thenReturn("LDAP"); - when(securityRealmFactory.getRealm()).thenReturn(realm); - + public void toProtobuf_whenExternalUserAuthentication_shouldWriteIt() { + when(commonSystemInformation.getExternalUserAuthentication()).thenReturn("LDAP"); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); assertThatAttributeIs(protobuf, "External User Authentication", "LDAP"); } @Test - public void no_realm() { - when(securityRealmFactory.getRealm()).thenReturn(null); + public void toProtobuf_whenNoExternalUserAuthentication_shouldWriteNothing() { + when(commonSystemInformation.getExternalUserAuthentication()).thenReturn(""); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); - assertThat(attribute(protobuf, "External User Authentication")).isNull(); + assertThatAttributeIs(protobuf, "External User Authentication", ""); } @Test - public void get_enabled_identity_providers() { - identityProviderRepository.addIdentityProvider(new TestIdentityProvider() - .setKey("github") - .setName("GitHub") - .setEnabled(true)); - identityProviderRepository.addIdentityProvider(new TestIdentityProvider() - .setKey("bitbucket") - .setName("Bitbucket") - .setEnabled(true)); - identityProviderRepository.addIdentityProvider(new TestIdentityProvider() - .setKey("disabled") - .setName("Disabled") - .setEnabled(false)); + public void toProtobuf_whenEnabledIdentityProviders_shouldWriteThem() { + when(commonSystemInformation.getEnabledIdentityProviders()).thenReturn(List.of("Bitbucket, GitHub")); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub"); } @Test - public void get_enabled_identity_providers_allowing_users_to_signup() { - identityProviderRepository.addIdentityProvider(new TestIdentityProvider() - .setKey("github") - .setName("GitHub") - .setEnabled(true) - .setAllowsUsersToSignUp(true)); - identityProviderRepository.addIdentityProvider(new TestIdentityProvider() - .setKey("bitbucket") - .setName("Bitbucket") - .setEnabled(true) - .setAllowsUsersToSignUp(false)); - identityProviderRepository.addIdentityProvider(new TestIdentityProvider() - .setKey("disabled") - .setName("Disabled") - .setEnabled(false) - .setAllowsUsersToSignUp(true)); + public void toProtobuf_whenAllowsToSignUpEnabledIdentityProviders_shouldWriteThem() { + when(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()).thenReturn(List.of("GitHub")); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub"); } @Test - public void get_force_authentication_defaults_to_true() { - ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); - assertThatAttributeIs(protobuf, "Force authentication", true); - } - - @Test - public void get_force_authentication() { - settings.setProperty(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY, false); + public void toProtobuf_whenForceAuthentication_returnIt() { + when(commonSystemInformation.getForceAuthentication()).thenReturn(false); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); assertThatAttributeIs(protobuf, "Force authentication", false); } |