From a7efc154cbe6f89074996ff9d96f59ebab4f208a Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Wed, 8 Feb 2017 16:36:36 +0100 Subject: [PATCH] SONAR-8755 make default organization guarded so that it can only be deleted by root --- .../db/migration/version/v63/DbVersion63.java | 4 +- .../v63/MakeDefaultOrganizationGuarded.java | 43 ++++++ .../version/v63/DbVersion63Test.java | 2 +- .../MakeDefaultOrganizationGuardedTest.java | 125 ++++++++++++++++++ .../organizations_and_internal_properties.sql | 24 ++++ .../org/sonar/db/version/rows-h2.sql | 3 +- 6 files changed, 198 insertions(+), 3 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/MakeDefaultOrganizationGuarded.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/MakeDefaultOrganizationGuardedTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v63/MakeDefaultOrganizationGuardedTest/organizations_and_internal_properties.sql diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63.java index 82fc9f8f641..21b4611f42f 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63.java @@ -44,6 +44,8 @@ public class DbVersion63 implements DbVersion { .add(1509, "Populate columns ORGANIZATIONS.DEFAULT_PERM_TEMPLATE_*", PopulateDefaultPermTemplateColumnsOfOrganizations.class) .add(1510, "Add ORGANIZATIONS.GUARDED", AddColumnGuardedToOrganizations.class) .add(1511, "Populate ORGANIZATIONS.GUARDED", PopulateColumnGuardedOfOrganizations.class) - .add(1512, "Make ORGANIZATIONS.GUARDED not nullable", MakeColumnGuardedOfOrganizationsNotNullable.class); + .add(1512, "Make ORGANIZATIONS.GUARDED not nullable", MakeColumnGuardedOfOrganizationsNotNullable.class) + .add(1513, "Make default organization guarded", MakeDefaultOrganizationGuarded.class) + ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/MakeDefaultOrganizationGuarded.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/MakeDefaultOrganizationGuarded.java new file mode 100644 index 00000000000..a88cf4fa71a --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/MakeDefaultOrganizationGuarded.java @@ -0,0 +1,43 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.v63; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; + +public class MakeDefaultOrganizationGuarded extends DataChange { + private final DefaultOrganizationUuid defaultOrganizationUuid; + + public MakeDefaultOrganizationGuarded(Database db, DefaultOrganizationUuid defaultOrganizationUuid) { + super(db); + this.defaultOrganizationUuid = defaultOrganizationUuid; + } + + @Override + protected void execute(Context context) throws SQLException { + String uuid = this.defaultOrganizationUuid.getAndCheck(context); + context.prepareUpsert("update organizations set guarded=? where uuid=?") + .setBoolean(1, true) + .setString(2, uuid) + .execute() + .commit(); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63Test.java index d9c06f27d62..51cd811a0b0 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63Test.java @@ -41,7 +41,7 @@ public class DbVersion63Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 13); + verifyMigrationCount(underTest, 14); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/MakeDefaultOrganizationGuardedTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/MakeDefaultOrganizationGuardedTest.java new file mode 100644 index 00000000000..40d28b4cbfe --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/MakeDefaultOrganizationGuardedTest.java @@ -0,0 +1,125 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.v63; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MakeDefaultOrganizationGuardedTest { + + private static final String TABLE_ORGANIZATIONS = "organizations"; + private static final String DEFAULT_ORGANIZATION_UUID = "def-org"; + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, MakeDefaultOrganizationGuardedTest.class, "organizations_and_internal_properties.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private MakeDefaultOrganizationGuarded underTest = new MakeDefaultOrganizationGuarded(db.database(), new DefaultOrganizationUuidImpl()); + + @Test + public void fails_with_ISE_when_no_default_organization_is_set() throws SQLException { + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Default organization uuid is missing"); + + underTest.execute(); + } + + @Test + public void fails_with_ISE_when_default_organization_does_not_exist_in_table_ORGANIZATIONS() throws SQLException { + insertDefaultOrganizationUuid("blabla"); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Default organization with uuid 'blabla' does not exist in table ORGANIZATIONS"); + + underTest.execute(); + } + + @Test + public void execute_sets_guarded_of_non_guarded_default_organization_to_true() throws Exception { + setupDefaultOrganization(); + assertThat(isDefaultOrganizationGuarded()).isFalse(); + + underTest.execute(); + + assertThat(isDefaultOrganizationGuarded()).isTrue(); + } + + @Test + public void execute_is_reentrant() throws Exception { + setupDefaultOrganization(); + + underTest.execute(); + + underTest.execute(); + + assertThat(isDefaultOrganizationGuarded()).isTrue(); + } + + private void setupDefaultOrganization() { + insertDefaultOrganizationUuid(DEFAULT_ORGANIZATION_UUID); + insertOrganization(DEFAULT_ORGANIZATION_UUID); + } + + private boolean isDefaultOrganizationGuarded() throws Exception { + try (Connection connection = db.openConnection(); + PreparedStatement selectGuardedForUuidStatement = createSelectGuardedForUuidStatement(connection, DEFAULT_ORGANIZATION_UUID); + ResultSet resultSet = selectGuardedForUuidStatement.executeQuery()) { + assertThat(resultSet.next()).isTrue(); + boolean res = resultSet.getBoolean(1); + assertThat(resultSet.next()).isFalse(); + return res; + } + } + + private static PreparedStatement createSelectGuardedForUuidStatement(Connection connection, String uuid) throws Exception { + PreparedStatement preparedStatement = connection.prepareStatement("select guarded from organizations where uuid=?"); + preparedStatement.setString(1, uuid); + return preparedStatement; + } + + private void insertOrganization(String uuid) { + db.executeInsert( + TABLE_ORGANIZATIONS, + "UUID", uuid, + "KEE", uuid, + "NAME", uuid, + "GUARDED", String.valueOf(false), + "CREATED_AT", "1000", + "UPDATED_AT", "1000"); + } + + private void insertDefaultOrganizationUuid(String defaultOrganizationUuid) { + db.executeInsert( + "INTERNAL_PROPERTIES", + "KEE", "organization.default", + "IS_EMPTY", "false", + "TEXT_VALUE", defaultOrganizationUuid); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v63/MakeDefaultOrganizationGuardedTest/organizations_and_internal_properties.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v63/MakeDefaultOrganizationGuardedTest/organizations_and_internal_properties.sql new file mode 100644 index 00000000000..8e67d9f9bfc --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v63/MakeDefaultOrganizationGuardedTest/organizations_and_internal_properties.sql @@ -0,0 +1,24 @@ +CREATE TABLE "ORGANIZATIONS" ( + "UUID" VARCHAR(40) NOT NULL PRIMARY KEY, + "KEE" VARCHAR(32) NOT NULL, + "NAME" VARCHAR(64) NOT NULL, + "DESCRIPTION" VARCHAR(256), + "URL" VARCHAR(256), + "AVATAR_URL" VARCHAR(256), + "GUARDED" BOOLEAN NOT NULL, + "DEFAULT_PERM_TEMPLATE_PROJECT" VARCHAR(40), + "DEFAULT_PERM_TEMPLATE_VIEW" VARCHAR(40), + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +CREATE UNIQUE INDEX "PK_ORGANIZATIONS" ON "ORGANIZATIONS" ("UUID"); +CREATE UNIQUE INDEX "ORGANIZATION_KEY" ON "ORGANIZATIONS" ("KEE"); + +CREATE TABLE "INTERNAL_PROPERTIES" ( + "KEE" VARCHAR(50) NOT NULL PRIMARY KEY, + "IS_EMPTY" BOOLEAN NOT NULL, + "TEXT_VALUE" VARCHAR(4000), + "CLOB_VALUE" CLOB, + "CREATED_AT" BIGINT +); +CREATE UNIQUE INDEX "UNIQ_INTERNAL_PROPERTIES" ON "INTERNAL_PROPERTIES" ("KEE"); diff --git a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql index 9ebdb68b1c9..0d00f5a933b 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql +++ b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql @@ -526,11 +526,12 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1509'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1510'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1511'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1512'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1513'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, IS_ROOT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', true, '1418215735482', '1418215735482'); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; -INSERT INTO ORGANIZATIONS (UUID, KEE, NAME, GUARDED, CREATED_AT, UPDATED_AT) VALUES ('AVdqnciQUUs7Zd3KPvFD', 'default-organization', 'Default Organization', false, '1474962596482', '1474962596482'); +INSERT INTO ORGANIZATIONS (UUID, KEE, NAME, GUARDED, CREATED_AT, UPDATED_AT) VALUES ('AVdqnciQUUs7Zd3KPvFD', 'default-organization', 'Default Organization', true, '1474962596482', '1474962596482'); INSERT INTO INTERNAL_PROPERTIES (KEE, IS_EMPTY, TEXT_VALUE, CREATED_AT) VALUES ('organization.default', false, 'AVdqnciQUUs7Zd3KPvFD', '1474962596482'); INSERT INTO GROUPS(ID, ORGANIZATION_UUID, NAME, DESCRIPTION, CREATED_AT, UPDATED_AT) VALUES (1, 'AVdqnciQUUs7Zd3KPvFD', 'sonar-administrators', 'System administrators', '2011-09-26 22:27:51.0', '2011-09-26 22:27:51.0'); -- 2.39.5