]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21139 setting nulls for clean_code_attribute of security hotspots rules
authorlukasz-jarocki-sonarsource <lukasz.jarocki@sonarsource.com>
Wed, 6 Dec 2023 08:59:43 +0000 (09:59 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 6 Dec 2023 20:02:46 +0000 (20:02 +0000)
server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/RemoveCleanCodeAttributeFromCustomHotspotRulesIT.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v104/DbVersion104.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v104/RemoveCleanCodeAttributeFromCustomHotspotRules.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/RuleCreator.java

diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/RemoveCleanCodeAttributeFromCustomHotspotRulesIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/RemoveCleanCodeAttributeFromCustomHotspotRulesIT.java
new file mode 100644 (file)
index 0000000..d4ebc2f
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * 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.db.migration.version.v104;
+
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.MigrationDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
+
+public class RemoveCleanCodeAttributeFromCustomHotspotRulesIT {
+
+  @Rule
+  public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(RemoveCleanCodeAttributeFromCustomHotspotRules.class);
+  private final RemoveCleanCodeAttributeFromCustomHotspotRules underTest = new RemoveCleanCodeAttributeFromCustomHotspotRules(db.database());
+
+  @Test
+  public void execute_whenRulesTableIsEmpty_shouldDoNothing() throws SQLException {
+    underTest.execute();
+
+    assertThat(db.select("select clean_code_attribute from rules")).isEmpty();
+  }
+
+  @Test
+  public void execute_whenCustomHotspotRuleExist_shouldRemoveCleanCodeAttributeOnlyFromHotspot() throws SQLException {
+    insertRule("custom_hotspot_rule", "4", "CONVENTIONAL");
+    insertRule("other_rule", "1", "ETHICAL");
+
+    underTest.execute();
+
+    List<Map<String, Object>> selectResult = db.select("select NAME, CLEAN_CODE_ATTRIBUTE, UPDATED_AT from RULES");
+
+    assertThat(selectResult)
+      .extracting(stringObjectMap -> stringObjectMap.get("NAME"), stringObjectMap -> stringObjectMap.get("CLEAN_CODE_ATTRIBUTE"))
+      .containsExactlyInAnyOrder(tuple("custom_hotspot_rule", null), tuple("other_rule", "ETHICAL"));
+
+    Optional<Object> updatedAtForHotspotRule = selectResult.stream().filter(map -> map.containsValue("custom_hotspot_rule"))
+      .map(map -> map.get("UPDATED_AT")).findFirst();
+    assertThat(updatedAtForHotspotRule.get()).isNotEqualTo(0L);
+
+    Optional<Object> updatedAtForOtherRule = selectResult.stream().filter(map -> map.containsValue("other_rule"))
+      .map(map -> map.get("UPDATED_AT")).findFirst();
+    assertThat(updatedAtForOtherRule).contains(0L);
+  }
+
+  @Test
+  public void execute_whenCustomHotspotRuleExist_isReentrant() throws SQLException {
+    insertRule("custom_hotspot_rule", "4", "CONVENTIONAL");
+    insertRule("other_rule", "1", "ETHICAL");
+
+    underTest.execute();
+    underTest.execute();
+
+    List<Map<String, Object>> selectResult = db.select("select NAME, CLEAN_CODE_ATTRIBUTE from RULES");
+
+    assertThat(selectResult)
+      .extracting(stringObjectMap -> stringObjectMap.get("NAME"), stringObjectMap -> stringObjectMap.get("CLEAN_CODE_ATTRIBUTE"))
+      .containsExactlyInAnyOrder(tuple("custom_hotspot_rule", null), tuple("other_rule", "ETHICAL"));
+  }
+
+  private void insertRule(String name, String ruleType, String cleanCodeAttribute) {
+    db.executeInsert("rules",
+      "PLUGIN_RULE_KEY", name,
+      "PLUGIN_NAME", name,
+      "SCOPE", name,
+      "NAME", name,
+      "IS_EXTERNAL", true,
+      "IS_AD_HOC", false,
+      "UUID", name,
+      "RULE_TYPE", ruleType,
+      "UPDATED_AT", 0L,
+      "CLEAN_CODE_ATTRIBUTE", cleanCodeAttribute);
+  }
+}
index 73f7742f355ed7454b17b13486392df64905751a..2db623a7e4931d08011aacee694716f266bce513 100644 (file)
@@ -50,6 +50,8 @@ public class DbVersion104 implements DbVersion {
       .add(10_4_006, "Add 'uuid' column to 'groups_users'", AddUuidColumnToGroupsUsers.class)
       .add(10_4_007, "Populate 'uuid' column in 'groups_users'", PopulateGroupsUsersUuid.class)
       .add(10_4_008, "Make 'uuid' column in 'groups_users' table non-nullable", MakeUuidInGroupsUsersNotNullable.class)
-      .add(10_4_009, "Create primary key on 'groups_users.uuid'", CreatePrimaryKeyOnGroupsUsersTable.class);
+      .add(10_4_009, "Create primary key on 'groups_users.uuid'", CreatePrimaryKeyOnGroupsUsersTable.class)
+      .add(10_4_010, "Set nulls in 'clean_code_attribute' column of 'rules' table for security hotspots",
+        RemoveCleanCodeAttributeFromCustomHotspotRules.class);
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v104/RemoveCleanCodeAttributeFromCustomHotspotRules.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v104/RemoveCleanCodeAttributeFromCustomHotspotRules.java
new file mode 100644 (file)
index 0000000..e31d479
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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.db.migration.version.v104;
+
+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.MassUpdate;
+
+public class RemoveCleanCodeAttributeFromCustomHotspotRules extends DataChange {
+
+  private static final String SELECT = """
+    SELECT uuid
+    FROM rules
+    WHERE rule_type = 4
+    AND clean_code_attribute IS NOT NULL
+    """;
+  private static final String UPDATE = """
+    UPDATE rules SET clean_code_attribute = NULL, updated_at = ? WHERE uuid = ?
+     """;
+
+  public RemoveCleanCodeAttributeFromCustomHotspotRules(Database db) {
+    super(db);
+  }
+
+  @Override
+  protected void execute(Context context) throws SQLException {
+    MassUpdate massUpdate = context.prepareMassUpdate();
+    Long updatedAt = System.currentTimeMillis();
+    massUpdate.select(SELECT);
+    massUpdate.update(UPDATE);
+    massUpdate.execute((row, update) -> {
+      String uuid = row.getString(1);
+      update.setLong(1, updatedAt);
+      update.setString(2, uuid);
+      return true;
+    });
+  }
+}
index c17f60d341b7a7b386d0458f9594cc367441bf62..4619e83411afb202e49c3faf1e44b8a2d5e3be30 100644 (file)
@@ -50,7 +50,6 @@ import org.sonar.db.rule.RuleParamDto;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.rule.index.RuleIndexer;
 import org.sonar.server.util.TypeValidations;
-import org.sonarqube.ws.Common;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.collect.Lists.newArrayList;