]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10409 Update too long keys of permission templates
authorEric Hartmann <hartmann.eric@gmail.com>
Tue, 13 Feb 2018 17:43:15 +0000 (18:43 +0100)
committerEric Hartmann <hartmann.eric@gmail.Com>
Wed, 14 Feb 2018 16:45:56 +0000 (17:45 +0100)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/UpdatePermissionTooLongTemplateKeys.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/UpdatePermissionTooLongTemplateKeysTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/UpdatePermissionTooLongTemplateKeysTest/templates.sql [new file with mode: 0644]

index f72c75bbed56d6355ba49b30130b0144c7d9f91d..d96ad6af34e22338eb90be288de1eb5ac8fce9c2 100644 (file)
@@ -34,6 +34,7 @@ public class DbVersion67 implements DbVersion {
       .add(1835, "Populate WEBHOOK_DELIVERIES.ANALYSIS_UUID", PopulateAnalysisUuidColumnOnWebhookDeliveries.class)
       .add(1836, "Migrate 'previous_analysis' leak periods to 'previous_version'", MigratePreviousAnalysisToPreviousVersion.class)
       .add(1837, "Drop old licenses", DropOldLicenses.class)
+      .add(1838, "Update PERMISSION_TEMPLATE.KEYS", UpdatePermissionTooLongTemplateKeys.class)
     ;
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/UpdatePermissionTooLongTemplateKeys.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/UpdatePermissionTooLongTemplateKeys.java
new file mode 100644 (file)
index 0000000..3f9edeb
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.v67;
+
+import java.sql.SQLException;
+import org.sonar.core.util.UuidFactory;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.MsSql;
+import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
+import org.sonar.server.platform.db.migration.step.DataChange;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+
+/**
+ * SONAR-10409
+ *
+ * Update keys of PERMISSION_TEMPLATES that are longer than 40 characters
+ */
+public class UpdatePermissionTooLongTemplateKeys extends DataChange {
+
+  private final String lengthFunction;
+  private final UuidFactory uuidFactory;
+
+  public UpdatePermissionTooLongTemplateKeys(Database db, UuidFactory uuidFactory) {
+    super(db);
+    this.uuidFactory = uuidFactory;
+    if (db.getDialect().getId().equals(MsSql.ID)) {
+      lengthFunction = "len";
+    } else {
+      lengthFunction = "length";
+    }
+  }
+
+  @Override
+  protected void execute(Context context) throws SQLException {
+    MassUpdate massUpdate = context.prepareMassUpdate();
+    massUpdate.select("select kee from permission_templates where " + lengthFunction + "(kee) > ?").setInt(1, VarcharColumnDef.UUID_SIZE);
+    massUpdate.update("update permission_templates set kee=? where kee=?");
+    massUpdate.rowPluralName("permission templates");
+
+    massUpdate.execute((row, update) -> {
+      String oldKey = row.getString(1);
+      String newKey = uuidFactory.create();
+
+      update.setString(1, newKey);
+      update.setString(2, oldKey);
+      return true;
+    });
+  }
+}
index c418c66ef6243c161b03039e14b15166bff0ae51..a7b65e6c49e0262a60a469584fed1b10c33734cd 100644 (file)
@@ -36,7 +36,7 @@ public class DbVersion67Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 8);
+    verifyMigrationCount(underTest, 9);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/UpdatePermissionTooLongTemplateKeysTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/UpdatePermissionTooLongTemplateKeysTest.java
new file mode 100644 (file)
index 0000000..8e4e2c3
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.v67;
+
+import com.google.common.collect.ImmutableList;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.stream.IntStream;
+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 static java.util.stream.Collectors.toList;
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class UpdatePermissionTooLongTemplateKeysTest {
+  @Rule
+  public CoreDbTester dbTester = CoreDbTester.createForSchema(UpdatePermissionTooLongTemplateKeysTest.class, "templates.sql");
+
+  private static final Random RANDOM = new Random();
+
+  private final RecordingUuidFactory UUID_FACTORY = new RecordingUuidFactory();
+  private final UpdatePermissionTooLongTemplateKeys underTest = new UpdatePermissionTooLongTemplateKeys(dbTester.database(), UUID_FACTORY);
+
+  @Test
+  public void keys_whose_length_is_greater_than_40_should_be_updated() throws SQLException {
+    // Create 10 permission templates with keys' length greater or equals than 40
+    List<String> tooLongKeys = IntStream.range(0, 10)
+      .mapToObj(i -> insertTemplate(randomAlphanumeric(41 + RANDOM.nextInt(60))))
+      .collect(toList());
+
+    underTest.execute();
+
+    assertThat(UUID_FACTORY.getRecordedUuids()).hasSize(tooLongKeys.size());
+    List<String> kees = dbTester.select("select kee from permission_templates").stream()
+      .map(r -> (String) r.get("KEE")).collect(toList());
+
+    assertThat(kees).containsExactlyInAnyOrder(UUID_FACTORY.getRecordedUuids().toArray(new String[] {}));
+  }
+
+  @Test
+  public void keys_whose_length_is_lower_than_40_should_not_be_updated() throws SQLException {
+    // Create 10 permission templates with keys' length lower or equals than 40
+    List<String> correctKeys = IntStream.range(0, 10)
+      .mapToObj(i -> insertTemplate(randomAlphanumeric(RANDOM.nextInt(41))))
+      .collect(toList());
+
+    underTest.execute();
+
+    assertThat(UUID_FACTORY.getRecordedUuids()).hasSize(0);
+    List<String> kees = dbTester.select("select kee from permission_templates").stream()
+      .map(r -> (String) r.get("KEE")).collect(toList());
+
+    assertThat(kees).containsExactlyInAnyOrder(correctKeys.toArray(new String[] {}));
+  }
+
+  private String insertTemplate(String kee) {
+    dbTester.executeInsert(
+      "PERMISSION_TEMPLATES",
+      "NAME", randomAlphanumeric(50),
+      "ORGANIZATION_UUID", "uuid",
+      "KEE", kee);
+    return kee;
+  }
+
+  private static final class RecordingUuidFactory implements UuidFactory {
+    private final List<String> generatedUuids = new ArrayList<>();
+    private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
+
+    @Override
+    public String create() {
+      String uuid = uuidFactory.create();
+      generatedUuids.add(uuid);
+      return uuid;
+    }
+
+    public void clear() {
+      generatedUuids.clear();
+    }
+
+    public List<String> getRecordedUuids() {
+      return ImmutableList.copyOf(generatedUuids);
+    }
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/UpdatePermissionTooLongTemplateKeysTest/templates.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/UpdatePermissionTooLongTemplateKeysTest/templates.sql
new file mode 100644 (file)
index 0000000..e80e57d
--- /dev/null
@@ -0,0 +1,10 @@
+CREATE TABLE "PERMISSION_TEMPLATES" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "ORGANIZATION_UUID" VARCHAR(40) NOT NULL,
+  "NAME" VARCHAR(100) NOT NULL,
+  "KEE" VARCHAR(100) NOT NULL,
+  "DESCRIPTION" VARCHAR(4000),
+  "KEY_PATTERN" VARCHAR(500),
+  "CREATED_AT" TIMESTAMP,
+  "UPDATED_AT" TIMESTAMP
+);