aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-migration
diff options
context:
space:
mode:
authorJacek <jacek.poreda@sonarsource.com>2022-11-21 11:45:35 +0100
committersonartech <sonartech@sonarsource.com>2022-11-21 20:02:56 +0000
commit522c74b001f7f6f610978d8076c6403d995cb784 (patch)
treeba692b7118b6cfe295bde9eb14a485b99e0e03dc /server/sonar-db-migration
parent131c3a370a708720d631e986b914080543fae20d (diff)
downloadsonarqube-522c74b001f7f6f610978d8076c6403d995cb784.tar.gz
sonarqube-522c74b001f7f6f610978d8076c6403d995cb784.zip
MMF-2745 Default permission improvements
Diffstat (limited to 'server/sonar-db-migration')
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v00/PopulateInitialSchema.java4
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v98/DbVersion98.java1
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v98/UpsertSonarUsersDescription.java40
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v00/PopulateInitialSchemaTest.java6
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v98/UpsertSonarUsersDescriptionTest.java79
-rw-r--r--server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v98/UpsertSonarUsersDescriptionTest/schema.sql9
6 files changed, 134 insertions, 5 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v00/PopulateInitialSchema.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v00/PopulateInitialSchema.java
index 762a1c14213..c374e62fa84 100644
--- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v00/PopulateInitialSchema.java
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v00/PopulateInitialSchema.java
@@ -169,7 +169,7 @@ public class PopulateInitialSchema extends DataChange {
upsert
.setString(1, uuidFactory.create())
.setString(2, USERS_GROUP)
- .setString(3, "Any new users created will automatically join this group")
+ .setString(3, "Every authenticated user automatically belongs to this group")
.setDate(4, now)
.setDate(5, now)
.addBatch();
@@ -235,7 +235,7 @@ public class PopulateInitialSchema extends DataChange {
for (String anyoneRole : Arrays.asList("scan", "provisioning")) {
upsert
.setString(1, uuidFactory.create())
- .setString(2, null)
+ .setString(2, groups.getUserGroupUuid())
.setString(3, anyoneRole)
.addBatch();
}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v98/DbVersion98.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v98/DbVersion98.java
index f792305f9c7..ab5da5a55db 100644
--- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v98/DbVersion98.java
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v98/DbVersion98.java
@@ -30,6 +30,7 @@ public class DbVersion98 implements DbVersion {
.add(6701, "Drop live measure variation column", DropLiveMeasureVariationColumn.class)
.add(6702, "Move project measure variations to values", MoveProjectMeasureVariationToValue.class)
.add(6703, "Drop project measure variation column", DropProjectMeasureVariationColumn.class)
+ .add(6704, "Update sonar-users group description", UpsertSonarUsersDescription.class)
;
}
}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v98/UpsertSonarUsersDescription.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v98/UpsertSonarUsersDescription.java
new file mode 100644
index 00000000000..52b72475631
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v98/UpsertSonarUsersDescription.java
@@ -0,0 +1,40 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 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.db.migration.version.v98;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DataChange;
+import org.sonar.server.platform.db.migration.step.Upsert;
+
+public class UpsertSonarUsersDescription extends DataChange {
+
+ public UpsertSonarUsersDescription(Database db) {
+ super(db);
+ }
+
+ @Override
+ protected void execute(Context context) throws SQLException {
+ Upsert upsert = context.prepareUpsert("update groups set description = ? where name = 'sonar-users'");
+ upsert.setString(1, "Every authenticated user automatically belongs to this group");
+ upsert.execute();
+ upsert.commit();
+ }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v00/PopulateInitialSchemaTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v00/PopulateInitialSchemaTest.java
index ad8bc02f549..909159f0a09 100644
--- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v00/PopulateInitialSchemaTest.java
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v00/PopulateInitialSchemaTest.java
@@ -67,7 +67,7 @@ public class PopulateInitialSchemaTest {
underTest.execute();
verifyAdminUser();
- verifyGroup("sonar-users", "Any new users created will automatically join this group");
+ verifyGroup("sonar-users", "Every authenticated user automatically belongs to this group");
verifyGroup("sonar-administrators", "System administrators");
String qualityGateUuid = verifyQualityGate();
verifyInternalProperties();
@@ -204,7 +204,7 @@ public class PopulateInitialSchemaTest {
}
private void verifyRolesOfUsersGroup() {
- assertThat(selectRoles("sonar-users")).isEmpty();
+ assertThat(selectRoles("sonar-users")).containsOnly("provisioning", "scan");
}
private void verifyRolesOfAnyone() {
@@ -212,7 +212,7 @@ public class PopulateInitialSchemaTest {
"from group_roles gr where gr.group_uuid is null");
Stream<String> roles = rows.stream()
.map(row -> (String) row.get("role"));
- assertThat(roles).containsOnly("provisioning", "scan");
+ assertThat(roles).isEmpty();
}
private Stream<String> selectRoles(String groupName) {
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v98/UpsertSonarUsersDescriptionTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v98/UpsertSonarUsersDescriptionTest.java
new file mode 100644
index 00000000000..b894de21c6b
--- /dev/null
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v98/UpsertSonarUsersDescriptionTest.java
@@ -0,0 +1,79 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 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.db.migration.version.v98;
+
+import java.sql.SQLException;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.core.util.UuidFactory;
+import org.sonar.core.util.UuidFactoryFast;
+import org.sonar.db.CoreDbTester;
+import org.sonar.server.platform.db.migration.step.DataChange;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class UpsertSonarUsersDescriptionTest {
+
+ private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
+ public static final String OLD_DESCRIPTION = "Any new users created will automatically join this group";
+ public static final String NEW_DESCRIPTION = "Every authenticated user automatically belongs to this group";
+
+ @Rule
+ public CoreDbTester db = CoreDbTester.createForSchema(UpsertSonarUsersDescriptionTest.class, "schema.sql");
+
+ private final DataChange underTest = new UpsertSonarUsersDescription(db.database());
+
+ @Test
+ public void migration_populates_sonar_users_group_description() throws SQLException {
+ String uuid = insertSonarUsersGroupWithOldDescription();
+ underTest.execute();
+ assertSonarUsersGroupDescriptionIsUpsertedCorrectly(uuid);
+ }
+
+ @Test
+ public void migration_should_be_reentrant() throws SQLException {
+ String userUuid1 = insertSonarUsersGroupWithOldDescription();
+ underTest.execute();
+ // re-entrant
+ underTest.execute();
+ assertSonarUsersGroupDescriptionIsUpsertedCorrectly(userUuid1);
+ }
+
+ private void assertSonarUsersGroupDescriptionIsUpsertedCorrectly(String userUuid) {
+ String selectSql = String.format("select description from groups where uuid='%s'", userUuid);
+ assertThat(db.select(selectSql).stream().map(row -> row.get("DESCRIPTION")).collect(Collectors.toList()))
+ .containsExactlyInAnyOrder(UpsertSonarUsersDescriptionTest.NEW_DESCRIPTION);
+ }
+
+ private String insertSonarUsersGroupWithOldDescription() {
+ Map<String, Object> map = new HashMap<>();
+ String uuid = uuidFactory.create();
+ map.put("UUID", uuid);
+ map.put("NAME", "sonar-users");
+ map.put("DESCRIPTION", OLD_DESCRIPTION);
+ map.put("CREATED_AT", new Date());
+ db.executeInsert("groups", map);
+ return uuid;
+ }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v98/UpsertSonarUsersDescriptionTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v98/UpsertSonarUsersDescriptionTest/schema.sql
new file mode 100644
index 00000000000..6a9845ad643
--- /dev/null
+++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v98/UpsertSonarUsersDescriptionTest/schema.sql
@@ -0,0 +1,9 @@
+CREATE TABLE "GROUPS"(
+ "UUID" CHARACTER VARYING(40) NOT NULL,
+ "NAME" CHARACTER VARYING(500) NOT NULL,
+ "DESCRIPTION" CHARACTER VARYING(200),
+ "CREATED_AT" TIMESTAMP,
+ "UPDATED_AT" TIMESTAMP
+);
+ALTER TABLE "GROUPS" ADD CONSTRAINT "PK_GROUPS" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "UNIQ_GROUPS_NAME" ON "GROUPS"("NAME" NULLS FIRST);