]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18759 introduce a new mechanism to activate SCIM
authorPierre <pierre.guillot@sonarsource.com>
Wed, 15 Mar 2023 15:28:55 +0000 (16:28 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 22 Mar 2023 20:04:08 +0000 (20:04 +0000)
server/sonar-auth-saml/src/main/java/org/sonar/auth/saml/SamlSettings.java
server/sonar-db-dao/src/it/java/org/sonar/db/scim/ScimUserDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimGroupMapper.xml
server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimUserMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/scim/ScimGroupDaoTest.java
server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java
server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java

index 4f1a024a8df27017139554d8a6fd92e6bc78ac42..39184848603726ea764897fba5d202bc8a23d131 100644 (file)
@@ -106,7 +106,7 @@ public class SamlSettings {
     return configuration.get(GROUP_NAME_ATTRIBUTE);
   }
 
-  boolean isEnabled() {
+  public boolean isEnabled() {
     return configuration.getBoolean(ENABLED).orElse(false) &&
       configuration.get(PROVIDER_ID).isPresent() &&
       configuration.get(APPLICATION_ID).isPresent() &&
index 2fae3b3e5185a9fb92c329058134729a8257c21b..b48dbb441d2cac6d60bdd3e73db7b0bb2d569cda 100644 (file)
@@ -330,6 +330,16 @@ public class ScimUserDaoIT {
     assertThat(remainingUser.getUserUuid()).isEqualTo(scimUserTestData2.userUuid);
   }
 
+  @Test
+  public void deleteAll_should_remove_all_ScimUsers(){
+    insertScimUser("scimUser");
+    insertScimUser("scimUser2");
+
+    scimUserDao.deleteAll(dbSession);
+
+    assertThat(scimUserDao.findAll(dbSession)).isEmpty();
+  }
+
   @Test
   public void deleteFromUserUuid_shouldNotFail_whenNoUser() {
     assertThatCode(() -> scimUserDao.deleteByUserUuid(dbSession, randomAlphanumeric(6))).doesNotThrowAnyException();
index b6962213c8a61f0a2ab6fc15602ef7b16200c84e..ae1a8be730e58b39188aac5327f6b84afc7c19de 100644 (file)
@@ -74,4 +74,8 @@ public class ScimGroupDao implements Dao {
   public String getManagedGroupSqlFilter(boolean filterByManaged) {
     return String.format("%s exists (select group_uuid from scim_groups sg where sg.group_uuid = uuid)", filterByManaged ? "" : "not");
   }
+
+  public void deleteAll(DbSession dbSession) {
+    mapper(dbSession).deleteAll();
+  }
 }
index 391ec3faa5cb8f1585f85de7ce4b7c5b1b801e13..ec52527f85636dc49a734cc13dfcae828e4e9b68 100644 (file)
@@ -43,4 +43,6 @@ public interface ScimGroupMapper {
   void deleteByGroupUuid(@Param("groupUuid") String groupUuid);
 
   void deleteByScimUuid(@Param("scimUuid") String scimUuid);
+
+  void deleteAll();
 }
index 43827e664c2557cc3019168eebdc03e68325526e..c68481ae812c593e36568af62e19efad95a83f3b 100644 (file)
@@ -107,4 +107,8 @@ public class ScimUserDao implements Dao {
   public String getManagedUserSqlFilter(boolean filterByManaged) {
     return String.format("%s exists (select user_uuid from scim_users su where su.user_uuid = uuid)", filterByManaged ? "" : "not");
   }
+
+  public void deleteAll(DbSession dbSession) {
+    mapper(dbSession).deleteAll();
+  }
 }
index c850448f0f3650feb1054641546c2c3c29748f9f..8fe42ca6f90a57bd68c3b8f2ccfe9fb243c7bb28 100644 (file)
@@ -43,4 +43,6 @@ public interface ScimUserMapper {
   void deleteByUserUuid(@Param("userUuid") String userUuid);
 
   void deleteByScimUuid(@Param("scimUuid") String scimUuid);
+
+  void deleteAll();
 }
index 17461b227cda31889a26e0e0351c409c3116e929..779940e91f19e684c9222f611361a6c925224fd5 100644 (file)
@@ -68,5 +68,9 @@
     delete from scim_groups where scim_uuid = #{scimUuid, jdbcType=VARCHAR}
   </delete>
 
+  <update id="deleteAll">
+    delete from scim_groups
+  </update>
+
 </mapper>
 
index 44965cb925d6af55924cabd9aadf48d40fd2b47b..97a61c41d2efa105752f49dc9c71d5cca50d6a5a 100644 (file)
@@ -98,5 +98,9 @@
     delete from scim_users where scim_uuid = #{scimUuid, jdbcType=VARCHAR}
   </delete>
 
+  <update id="deleteAll">
+    delete from scim_users
+  </update>
+
 </mapper>
 
index a69b54e89e5c68f2d26bebe6f5d3157c8a1e4532..17377e563616b8cc88353fab5c515cd396321a2e 100644 (file)
@@ -255,4 +255,14 @@ public class ScimGroupDaoTest {
     assertThatCode(() -> scimGroupDao.deleteByGroupUuid(db.getSession(), randomAlphanumeric(6))).doesNotThrowAnyException();
   }
 
+
+  @Test
+  public void deleteAll_should_remove_all_ScimGroups(){
+    insertScimGroup("scim-group-uuid1", "group-uuid1");
+    insertScimGroup("scim-group-uuid2", "group-uuid2");
+
+    scimGroupDao.deleteAll(db.getSession());
+
+    assertThat(scimGroupDao.findAll(db.getSession())).isEmpty();
+  }
 }
index 98251d34a7fd3d102427cd4acba3dea726d78e66..bf8dad42226d78ef5e36f7a9e145b1d2a2c38987 100644 (file)
@@ -356,6 +356,6 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
   }
 
   private boolean isScimEnabled() {
-    return this.configuration.getBoolean(SCIM_PROPERTY_ENABLED).orElse(false);
+    return this.internalProperties.read(SCIM_PROPERTY_ENABLED).map(Boolean::parseBoolean).orElse(false);
   }
 }
index e996981a90939ce56c6d64d17175b40b7bba0013..d5cddf88838d2097c2dcb2969002480347cf2de8 100644 (file)
@@ -24,6 +24,7 @@ import com.tngtech.java.junit.dataprovider.DataProviderRunner;
 import com.tngtech.java.junit.dataprovider.UseDataProvider;
 import java.sql.DatabaseMetaData;
 import java.sql.SQLException;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
@@ -440,13 +441,13 @@ public class TelemetryDataLoaderImplTest {
 
   @Test
   @UseDataProvider("getScimFeatureStatues")
-  public void detect_scim_feature_status(boolean isEnabled) {
+  public void detect_scim_feature_status(String isEnabled) {
     db.components().insertPublicProject();
-    when(configuration.getBoolean(SCIM_PROPERTY_ENABLED)).thenReturn(Optional.of(isEnabled));
+    when(internalProperties.read(SCIM_PROPERTY_ENABLED)).thenReturn(Optional.ofNullable(isEnabled));
 
     TelemetryData data = communityUnderTest.load();
 
-    assertThat(data.isScimEnabled()).isEqualTo(isEnabled);
+    assertThat(data.isScimEnabled()).isEqualTo(Boolean.parseBoolean(isEnabled));
   }
 
   @Test
@@ -475,7 +476,11 @@ public class TelemetryDataLoaderImplTest {
   }
 
   @DataProvider
-  public static Set<Boolean> getScimFeatureStatues() {
-    return Set.of(true, false);
+  public static Set<String> getScimFeatureStatues() {
+    HashSet<String> result = new HashSet<>();
+    result.add("true");
+    result.add("false");
+    result.add(null);
+    return result;
   }
 }