From 451fa55e933c16dd9bafeb489b3777735b63a314 Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Wed, 25 Aug 2021 14:29:40 +0200 Subject: [PATCH] SONAR-15322 Allow SQ DB fit encrypted DevOps Platform values --- server/sonar-db-dao/src/schema/schema-sq.ddl | 4 +- ...tSecretColumnLengthOfAlmSettingsTable.java | 49 +++++++++++++++++ ...vateKeyColumnLengthOfAlmSettingsTable.java | 49 +++++++++++++++++ .../db/migration/version/v91/DbVersion91.java | 3 +- ...retColumnLengthOfAlmSettingsTableTest.java | 53 +++++++++++++++++++ ...KeyColumnLengthOfAlmSettingsTableTest.java | 53 +++++++++++++++++++ .../version/v91/DbVersion91Test.java | 2 +- .../schema.sql | 15 ++++++ .../schema.sql | 15 ++++++ .../almsettings/ws/UpdateGithubAction.java | 4 +- 10 files changed, 241 insertions(+), 6 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v91/AlterClientSecretColumnLengthOfAlmSettingsTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v91/AlterPrivateKeyColumnLengthOfAlmSettingsTable.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/AlterClientSecretColumnLengthOfAlmSettingsTableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/AlterPrivateKeyColumnLengthOfAlmSettingsTableTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v91/AlterClientSecretColumnLengthOfAlmSettingsTableTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v91/AlterPrivateKeyColumnLengthOfAlmSettingsTableTest/schema.sql diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index 88044297a20..ed1a1416676 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -52,12 +52,12 @@ CREATE TABLE "ALM_SETTINGS"( "KEE" VARCHAR(200) NOT NULL, "URL" VARCHAR(2000), "APP_ID" VARCHAR(80), - "PRIVATE_KEY" VARCHAR(2000), + "PRIVATE_KEY" VARCHAR(2500), "PAT" VARCHAR(2000), "UPDATED_AT" BIGINT NOT NULL, "CREATED_AT" BIGINT NOT NULL, "CLIENT_ID" VARCHAR(80), - "CLIENT_SECRET" VARCHAR(80) + "CLIENT_SECRET" VARCHAR(160) ); ALTER TABLE "ALM_SETTINGS" ADD CONSTRAINT "PK_ALM_SETTINGS" PRIMARY KEY("UUID"); CREATE UNIQUE INDEX "UNIQ_ALM_SETTINGS" ON "ALM_SETTINGS"("KEE"); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v91/AlterClientSecretColumnLengthOfAlmSettingsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v91/AlterClientSecretColumnLengthOfAlmSettingsTable.java new file mode 100644 index 00000000000..637bc4a1a40 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v91/AlterClientSecretColumnLengthOfAlmSettingsTable.java @@ -0,0 +1,49 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.v91; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class AlterClientSecretColumnLengthOfAlmSettingsTable extends DdlChange { + private static final String TABLE = "alm_settings"; + + private static final VarcharColumnDef columnDefinition = newVarcharColumnDefBuilder() + .setColumnName("client_secret") + .setIsNullable(true) + .setLimit(160) + .build(); + + public AlterClientSecretColumnLengthOfAlmSettingsTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AlterColumnsBuilder(getDialect(), TABLE) + .updateColumn(columnDefinition) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v91/AlterPrivateKeyColumnLengthOfAlmSettingsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v91/AlterPrivateKeyColumnLengthOfAlmSettingsTable.java new file mode 100644 index 00000000000..d891a35f37a --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v91/AlterPrivateKeyColumnLengthOfAlmSettingsTable.java @@ -0,0 +1,49 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.v91; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class AlterPrivateKeyColumnLengthOfAlmSettingsTable extends DdlChange { + private static final String TABLE = "alm_settings"; + + private static final VarcharColumnDef columnDefinition = newVarcharColumnDefBuilder() + .setColumnName("private_key") + .setIsNullable(true) + .setLimit(2500) + .build(); + + public AlterPrivateKeyColumnLengthOfAlmSettingsTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AlterColumnsBuilder(getDialect(), TABLE) + .updateColumn(columnDefinition) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v91/DbVersion91.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v91/DbVersion91.java index 01a3a721cff..cb77356c1dd 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v91/DbVersion91.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v91/DbVersion91.java @@ -34,6 +34,7 @@ public class DbVersion91 implements DbVersion { .add(6006, "Drop 'user_managed' column from 'metrics' table", DropUserManagedColumnFromMetricsTable.class) .add(6007, "Create Audit table", CreateAuditTable.class) .add(6008, "Add column 'removed' to 'plugins' table", AddColumnRemovedToPlugins.class) - ; + .add(6009, "Alter column 'client_secret' of 'alm_settings' table to length 160", AlterClientSecretColumnLengthOfAlmSettingsTable.class) + .add(6010, "Alter column 'private_key' of 'alm_settings' table to length 2500", AlterPrivateKeyColumnLengthOfAlmSettingsTable.class); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/AlterClientSecretColumnLengthOfAlmSettingsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/AlterClientSecretColumnLengthOfAlmSettingsTableTest.java new file mode 100644 index 00000000000..3fcd4a56e2d --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/AlterClientSecretColumnLengthOfAlmSettingsTableTest.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.v91; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static java.sql.Types.VARCHAR; + +public class AlterClientSecretColumnLengthOfAlmSettingsTableTest { + private final String TABLE = "alm_settings"; + private final String COLUMN = "client_secret"; + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(AlterClientSecretColumnLengthOfAlmSettingsTableTest.class, "schema.sql"); + + private DdlChange underTest = new AlterClientSecretColumnLengthOfAlmSettingsTable(db.database()); + + @Test + public void client_secret_column_is_not_null() throws SQLException { + underTest.execute(); + + db.assertColumnDefinition(TABLE, COLUMN, VARCHAR, 160, true); + } + + @Test + public void migration_is_reentrant() throws SQLException { + underTest.execute(); + underTest.execute(); + + db.assertColumnDefinition(TABLE, COLUMN, VARCHAR, 160, true); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/AlterPrivateKeyColumnLengthOfAlmSettingsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/AlterPrivateKeyColumnLengthOfAlmSettingsTableTest.java new file mode 100644 index 00000000000..e8ed6a3e70d --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/AlterPrivateKeyColumnLengthOfAlmSettingsTableTest.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.v91; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static java.sql.Types.VARCHAR; + +public class AlterPrivateKeyColumnLengthOfAlmSettingsTableTest { + private final String TABLE = "alm_settings"; + private final String COLUMN = "private_key"; + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(AlterPrivateKeyColumnLengthOfAlmSettingsTableTest.class, "schema.sql"); + + private DdlChange underTest = new AlterPrivateKeyColumnLengthOfAlmSettingsTable(db.database()); + + @Test + public void private_key_column_is_not_null() throws SQLException { + underTest.execute(); + + db.assertColumnDefinition(TABLE, COLUMN, VARCHAR, 2500, true); + } + + @Test + public void migration_is_reentrant() throws SQLException { + underTest.execute(); + underTest.execute(); + + db.assertColumnDefinition(TABLE, COLUMN, VARCHAR, 2500, true); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/DbVersion91Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/DbVersion91Test.java index b188978c960..6bdb2bc880a 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/DbVersion91Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/DbVersion91Test.java @@ -41,7 +41,7 @@ public class DbVersion91Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 8); + verifyMigrationCount(underTest, 10); } } diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v91/AlterClientSecretColumnLengthOfAlmSettingsTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v91/AlterClientSecretColumnLengthOfAlmSettingsTableTest/schema.sql new file mode 100644 index 00000000000..c54d773f69a --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v91/AlterClientSecretColumnLengthOfAlmSettingsTableTest/schema.sql @@ -0,0 +1,15 @@ +CREATE TABLE "ALM_SETTINGS"( + "UUID" VARCHAR(40) NOT NULL, + "ALM_ID" VARCHAR(40) NOT NULL, + "KEE" VARCHAR(200) NOT NULL, + "URL" VARCHAR(2000), + "APP_ID" VARCHAR(80), + "PRIVATE_KEY" VARCHAR(2000), + "PAT" VARCHAR(2000), + "UPDATED_AT" BIGINT NOT NULL, + "CREATED_AT" BIGINT NOT NULL, + "CLIENT_ID" VARCHAR(80), + "CLIENT_SECRET" VARCHAR(80) +); +ALTER TABLE "ALM_SETTINGS" ADD CONSTRAINT "PK_ALM_SETTINGS" PRIMARY KEY("UUID"); +CREATE UNIQUE INDEX "UNIQ_ALM_SETTINGS" ON "ALM_SETTINGS"("KEE"); \ No newline at end of file diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v91/AlterPrivateKeyColumnLengthOfAlmSettingsTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v91/AlterPrivateKeyColumnLengthOfAlmSettingsTableTest/schema.sql new file mode 100644 index 00000000000..c54d773f69a --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v91/AlterPrivateKeyColumnLengthOfAlmSettingsTableTest/schema.sql @@ -0,0 +1,15 @@ +CREATE TABLE "ALM_SETTINGS"( + "UUID" VARCHAR(40) NOT NULL, + "ALM_ID" VARCHAR(40) NOT NULL, + "KEE" VARCHAR(200) NOT NULL, + "URL" VARCHAR(2000), + "APP_ID" VARCHAR(80), + "PRIVATE_KEY" VARCHAR(2000), + "PAT" VARCHAR(2000), + "UPDATED_AT" BIGINT NOT NULL, + "CREATED_AT" BIGINT NOT NULL, + "CLIENT_ID" VARCHAR(80), + "CLIENT_SECRET" VARCHAR(80) +); +ALTER TABLE "ALM_SETTINGS" ADD CONSTRAINT "PK_ALM_SETTINGS" PRIMARY KEY("UUID"); +CREATE UNIQUE INDEX "UNIQ_ALM_SETTINGS" ON "ALM_SETTINGS"("KEE"); \ No newline at end of file diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateGithubAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateGithubAction.java index f9667cb1c9d..275b4f26f9f 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateGithubAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateGithubAction.java @@ -79,7 +79,7 @@ public class UpdateGithubAction implements AlmSettingsWsAction { .setDescription("GitHub API ID"); action.createParam(PARAM_PRIVATE_KEY) .setRequired(false) - .setMaximumLength(2000) + .setMaximumLength(2500) .setDescription("GitHub App private key"); action.createParam(PARAM_CLIENT_ID) .setRequired(true) @@ -87,7 +87,7 @@ public class UpdateGithubAction implements AlmSettingsWsAction { .setDescription("GitHub App Client ID"); action.createParam(PARAM_CLIENT_SECRET) .setRequired(false) - .setMaximumLength(80) + .setMaximumLength(160) .setDescription("GitHub App Client Secret"); } -- 2.39.5