]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18654 Add managed provider in system/info endpoint
authorAntoine Vigneau <antoine.vigneau@sonarsource.com>
Thu, 9 Mar 2023 15:24:43 +0000 (16:24 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 22 Mar 2023 20:04:07 +0000 (20:04 +0000)
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/CommonSystemInformation.java [new file with mode: 0644]
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/CommonSystemInformationTest.java [new file with mode: 0644]
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java

index 251325c2bc213e4a1e8e673ab6380d5193eec89d..f7e8124713dbc0bc4be0bf75a2da5e6d66a18f81 100644 (file)
@@ -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 (file)
index 0000000..f37d4bd
--- /dev/null
@@ -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();
+  }
+}
index 3c0eddce03f8343010145d45e36f988c32012002..451c9865a4ba2fc7f0aefe96a83dfd6f58ed9703 100644 (file)
 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));
-    }
-  }
 }
index 368b6c523b0704d959c709ccadf4dfdf3ca3e012..4d9f45b4d71cc335c23c7e60eb3a91f2aa228d7b 100644 (file)
 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();
-  }
-
 }
index aa2b33b264da787d33795c8a633c45d12c2ddf34..c535731a3c3d86e4a5dbd6c9912b96be6a086249 100644 (file)
@@ -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 (file)
index 0000000..ebda4bb
--- /dev/null
@@ -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);
+  }
+}
index 8d96c6f8bc1ce74a2ea36c3348fc2a7b7beca78d..b0c62e92d3bbc1da9a3b9de2e46aa726d64462e5 100644 (file)
@@ -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[][] {
index 91c2b435aa65dceb4bf870020985d22681315d00..578ae8605d77968b54cc1809a270f9658aabfc85 100644 (file)
@@ -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);
   }