From be71e17311f76dee57342f197ea2c849fdd156e0 Mon Sep 17 00:00:00 2001 From: Pierre Date: Wed, 15 Mar 2023 16:28:55 +0100 Subject: [PATCH] SONAR-18759 introduce a new mechanism to activate SCIM --- .../java/org/sonar/auth/saml/SamlSettings.java | 2 +- .../it/java/org/sonar/db/scim/ScimUserDaoIT.java | 10 ++++++++++ .../main/java/org/sonar/db/scim/ScimGroupDao.java | 4 ++++ .../java/org/sonar/db/scim/ScimGroupMapper.java | 2 ++ .../main/java/org/sonar/db/scim/ScimUserDao.java | 4 ++++ .../java/org/sonar/db/scim/ScimUserMapper.java | 2 ++ .../org/sonar/db/scim/ScimGroupMapper.xml | 4 ++++ .../org/sonar/db/scim/ScimUserMapper.xml | 4 ++++ .../java/org/sonar/db/scim/ScimGroupDaoTest.java | 10 ++++++++++ .../server/telemetry/TelemetryDataLoaderImpl.java | 2 +- .../telemetry/TelemetryDataLoaderImplTest.java | 15 ++++++++++----- 11 files changed, 52 insertions(+), 7 deletions(-) diff --git a/server/sonar-auth-saml/src/main/java/org/sonar/auth/saml/SamlSettings.java b/server/sonar-auth-saml/src/main/java/org/sonar/auth/saml/SamlSettings.java index 4f1a024a8df..39184848603 100644 --- a/server/sonar-auth-saml/src/main/java/org/sonar/auth/saml/SamlSettings.java +++ b/server/sonar-auth-saml/src/main/java/org/sonar/auth/saml/SamlSettings.java @@ -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() && diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/scim/ScimUserDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/scim/ScimUserDaoIT.java index 2fae3b3e518..b48dbb441d2 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/scim/ScimUserDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/scim/ScimUserDaoIT.java @@ -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(); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupDao.java index b6962213c8a..ae1a8be730e 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupDao.java @@ -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(); + } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupMapper.java index 391ec3faa5c..ec52527f856 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupMapper.java @@ -43,4 +43,6 @@ public interface ScimGroupMapper { void deleteByGroupUuid(@Param("groupUuid") String groupUuid); void deleteByScimUuid(@Param("scimUuid") String scimUuid); + + void deleteAll(); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserDao.java index 43827e664c2..c68481ae812 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserDao.java @@ -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(); + } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserMapper.java index c850448f0f3..8fe42ca6f90 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserMapper.java @@ -43,4 +43,6 @@ public interface ScimUserMapper { void deleteByUserUuid(@Param("userUuid") String userUuid); void deleteByScimUuid(@Param("scimUuid") String scimUuid); + + void deleteAll(); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimGroupMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimGroupMapper.xml index 17461b227cd..779940e91f1 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimGroupMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimGroupMapper.xml @@ -68,5 +68,9 @@ delete from scim_groups where scim_uuid = #{scimUuid, jdbcType=VARCHAR} + + delete from scim_groups + + diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimUserMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimUserMapper.xml index 44965cb925d..97a61c41d2e 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimUserMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimUserMapper.xml @@ -98,5 +98,9 @@ delete from scim_users where scim_uuid = #{scimUuid, jdbcType=VARCHAR} + + delete from scim_users + + diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/scim/ScimGroupDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/scim/ScimGroupDaoTest.java index a69b54e89e5..17377e56361 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/scim/ScimGroupDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/scim/ScimGroupDaoTest.java @@ -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(); + } } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java index 98251d34a7f..bf8dad42226 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java @@ -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); } } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java index e996981a909..d5cddf88838 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java @@ -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 getScimFeatureStatues() { - return Set.of(true, false); + public static Set getScimFeatureStatues() { + HashSet result = new HashSet<>(); + result.add("true"); + result.add("false"); + result.add(null); + return result; } } -- 2.39.5