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;
AlmConfigurationSection.class,
ServerPushSection.class,
BundledSection.class,
- StatisticsSupport.class
+ StatisticsSupport.class,
+ CommonSystemInformation.class
);
if (standalone) {
add(
--- /dev/null
+/*
+ * 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();
+ }
+}
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;
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
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
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));
- }
- }
}
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
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();
- }
-
}
when(nodeInformation.isStandalone()).thenReturn(false);
ListContainer container = new ListContainer();
underTest.configure(container);
- assertThat(container.getAddedObjects()).hasSize(21);
+ assertThat(container.getAddedObjects()).hasSize(22);
}
@Test
ListContainer container = new ListContainer();
underTest.configure(container);
- assertThat(container.getAddedObjects()).hasSize(15);
+ assertThat(container.getAddedObjects()).hasSize(16);
}
}
--- /dev/null
+/*
+ * 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);
+ }
+}
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;
@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() {
}
@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);
}
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[][] {
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() {
}
@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);
}