]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19084 Return GitHub as provider managing the instance in system/info
authorAntoine Vigneau <antoine.vigneau@sonarsource.com>
Thu, 4 May 2023 12:58:54 +0000 (14:58 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 11 May 2023 20:03:14 +0000 (20:03 +0000)
server/sonar-server-common/src/main/java/org/sonar/server/management/DelegatingManagedInstanceService.java
server/sonar-server-common/src/main/java/org/sonar/server/management/ManagedInstanceService.java
server/sonar-server-common/src/test/java/org/sonar/server/management/DelegatingManagedInstanceServiceTest.java
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/CommonSystemInformation.java
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/monitoring/CommonSystemInformationTest.java
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 4ff988baab3f7c70aa2257d75731d3e5ae4e02b2..f4db6c5516aa68fcb6f243f8c0ba24d1e84ffb09 100644 (file)
@@ -47,6 +47,13 @@ public class DelegatingManagedInstanceService implements ManagedInstanceService
     return delegates.stream().anyMatch(ManagedInstanceService::isInstanceExternallyManaged);
   }
 
+  @Override
+  public String getProviderName() {
+    return findManagedInstanceService()
+      .map(ManagedInstanceService::getProviderName)
+      .orElseThrow(() -> NOT_MANAGED_INSTANCE_EXCEPTION);
+  }
+
   @Override
   public Map<String, Boolean> getUserUuidToManaged(DbSession dbSession, Set<String> userUuids) {
     return findManagedInstanceService()
index 1a615555d92fa5b0a504e795454e5d34dba8be5e..2580f55dc4d95f16c3fa3dcefee8bbe15a97f0ab 100644 (file)
@@ -29,6 +29,8 @@ public interface ManagedInstanceService {
 
   boolean isInstanceExternallyManaged();
 
+  String getProviderName();
+
   Map<String, Boolean> getUserUuidToManaged(DbSession dbSession, Set<String> userUuids);
 
   Map<String, Boolean> getGroupUuidToManaged(DbSession dbSession, Set<String> groupUuids);
index aee383029019aadc1e8aee663865fc471900e20c..5f3b35dac9397ef81a8313e09c14d3dccb806ff2 100644 (file)
@@ -30,6 +30,7 @@ import org.sonar.db.DbSession;
 import static java.util.Collections.emptySet;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -39,6 +40,22 @@ public class DelegatingManagedInstanceServiceTest {
   @Mock
   private DbSession dbSession;
 
+  @Test
+  public void getProviderName_whenNotManaged_shouldThrow() {
+    DelegatingManagedInstanceService managedInstanceService = new DelegatingManagedInstanceService(emptySet());
+
+    assertThatThrownBy(() -> managedInstanceService.getProviderName())
+      .isInstanceOf(IllegalStateException.class)
+      .hasMessage("This instance is not managed.");
+  }
+
+  @Test
+  public void getProviderName_whenManaged_shouldReturnName() {
+    DelegatingManagedInstanceService managedInstanceService = new DelegatingManagedInstanceService(Set.of(new AlwaysManagedInstanceService()));
+
+    assertThat(managedInstanceService.getProviderName()).isEqualTo("Always");
+  }
+
   @Test
   public void isInstanceExternallyManaged_whenNoManagedInstanceService_returnsFalse() {
     DelegatingManagedInstanceService managedInstanceService = new DelegatingManagedInstanceService(emptySet());
@@ -176,6 +193,11 @@ public class DelegatingManagedInstanceServiceTest {
       return false;
     }
 
+    @Override
+    public String getProviderName() {
+      return "Never";
+    }
+
     @Override
     public Map<String, Boolean> getUserUuidToManaged(DbSession dbSession, Set<String> userUuids) {
       return null;
@@ -204,6 +226,11 @@ public class DelegatingManagedInstanceServiceTest {
       return true;
     }
 
+    @Override
+    public String getProviderName() {
+      return "Always";
+    }
+
     @Override
     public Map<String, Boolean> getUserUuidToManaged(DbSession dbSession, Set<String> userUuids) {
       return null;
index 40629019e54963ea23c363c3c8842bd592291d83..8789f0f5ca20aaae1d95c898d7dfa715ed76bcd7 100644 (file)
@@ -70,15 +70,9 @@ public class CommonSystemInformation {
       .toList();
   }
 
-  public String getManagedProvider() {
+  public String getManagedInstanceProviderName() {
     if (managedInstanceService.isInstanceExternallyManaged()) {
-      return identityProviderRepository.getAllEnabledAndSorted()
-        .stream()
-        .filter(provider -> provider.getKey().equalsIgnoreCase("saml"))
-        .filter(IdentityProvider::isEnabled)
-        .findFirst()
-        .map(IdentityProvider::getName)
-        .orElse(null);
+      return managedInstanceService.getProviderName();
     }
     return null;
   }
index c238e175deb0fb6d000a07275963ffb038822996..c7f433985ef4bdb1da6c57d7564fb55d0d05539e 100644 (file)
@@ -91,7 +91,7 @@ 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 Users and Groups Provisioning", commonSystemInformation.getManagedProvider());
+    setAttribute(protobuf, "External Users and Groups Provisioning", commonSystemInformation.getManagedInstanceProviderName());
     setAttribute(protobuf, "External User Authentication", commonSystemInformation.getExternalUserAuthentication());
     addIfNotEmpty(protobuf, "Accepted external identity providers",
       commonSystemInformation.getEnabledIdentityProviders());
index 31b3cbb77986df230ae51bca4f65db82b1b0d54b..308b1733c1d40ea3b588a0bb54571b4b24e7d856 100644 (file)
@@ -62,7 +62,7 @@ public class GlobalSystemSection implements SystemInfoSection, Global {
     setAttribute(protobuf, "Docker", dockerSupport.isRunningInDocker());
     setAttribute(protobuf, "High Availability", true);
     setAttribute(protobuf, "External Users and Groups Provisioning",
-      commonSystemInformation.getManagedProvider());
+      commonSystemInformation.getManagedInstanceProviderName());
     setAttribute(protobuf, "External User Authentication",
       commonSystemInformation.getExternalUserAuthentication());
     addIfNotEmpty(protobuf, "Accepted external identity providers",
index bc12e9fe82f90cb29d60abdf5b7e9dd093ca5ec5..ac0771c3b55e53c8d6ea2c5fb58af6c2dd4fbd1d 100644 (file)
@@ -121,33 +121,20 @@ public class CommonSystemInformationTest {
   }
 
   @Test
-  public void getManagedProvider_whenInstanceNotManaged_shouldReturnNull() {
+  public void getManagedInstanceProvider_whenInstanceNotManaged_shouldReturnNull() {
     mockIdentityProviders(List.of());
     mockManagedInstance(false);
 
-    assertThat(commonSystemInformation.getManagedProvider())
+    assertThat(commonSystemInformation.getManagedInstanceProviderName())
       .isNull();
   }
 
   @Test
-  public void getManagedProvider_whenInstanceManagedButNoValidProviderDefined_shouldReturnNull() {
-    mockIdentityProviders(List.of());
-    mockManagedInstance(true);
-
-    assertThat(commonSystemInformation.getManagedProvider())
-      .isNull();
-  }
-
-  @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)
-    ));
+  public void getManagedInstanceProvider_whenInstanceManaged_shouldReturnName() {
     mockManagedInstance(true);
 
-    assertThat(commonSystemInformation.getManagedProvider())
-      .isEqualTo("Okta");
+    assertThat(commonSystemInformation.getManagedInstanceProviderName())
+      .isEqualTo("Provider");
   }
 
   @Test
@@ -170,6 +157,7 @@ public class CommonSystemInformationTest {
 
   private void mockManagedInstance(boolean managed) {
     when(managedInstanceService.isInstanceExternallyManaged()).thenReturn(managed);
+    when(managedInstanceService.getProviderName()).thenReturn("Provider");
   }
 
   private void mockSecurityRealmFactory(String name) {
index 1735090758b2f14354676d93e2a0966caa485c10..25b06a0dcbf089ba5ae3337e28e7d3ed0398b744 100644 (file)
@@ -184,7 +184,7 @@ public class StandaloneSystemSectionTest {
 
   @Test
   public void toProtobuf_whenInstanceIsNotManaged_shouldWriteNothing() {
-    when(commonSystemInformation.getManagedProvider()).thenReturn(null);
+    when(commonSystemInformation.getManagedInstanceProviderName()).thenReturn(null);
     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
 
     assertThatAttributeDoesNotExist(protobuf, "External Users and Groups Provisioning");
@@ -192,7 +192,7 @@ public class StandaloneSystemSectionTest {
 
   @Test
   public void toProtobuf_whenInstanceIsManaged_shouldWriteItsProviderName() {
-    when(commonSystemInformation.getManagedProvider()).thenReturn("Okta");
+    when(commonSystemInformation.getManagedInstanceProviderName()).thenReturn("Okta");
 
     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
     assertThatAttributeIs(protobuf, "External Users and Groups Provisioning", "Okta");
index 86c42a4fb52af183dac5ed6be4ca9cfd7c1653f3..95711e233602761d2119fdfa100c63e4e0a07fce 100644 (file)
@@ -111,7 +111,7 @@ public class GlobalSystemSectionTest {
 
   @Test
   public void toProtobuf_whenInstanceIsNotManaged_shouldWriteNothing() {
-    when(commonSystemInformation.getManagedProvider()).thenReturn(null);
+    when(commonSystemInformation.getManagedInstanceProviderName()).thenReturn(null);
 
     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
     assertThatAttributeDoesNotExist(protobuf, "External Users and Groups Provisioning");
@@ -119,7 +119,7 @@ public class GlobalSystemSectionTest {
 
   @Test
   public void toProtobuf_whenInstanceIsManaged_shouldWriteItsProviderName() {
-    when(commonSystemInformation.getManagedProvider()).thenReturn("Okta");
+    when(commonSystemInformation.getManagedInstanceProviderName()).thenReturn("Okta");
 
     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
     assertThatAttributeIs(protobuf, "External Users and Groups Provisioning", "Okta");