--- /dev/null
+#
+# SonarQube, open source software quality management tool.
+# Copyright (C) 2008-2014 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# SonarQube 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.
+#
+# SonarQube 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.
+#
+#
+# SonarQube 6.0
+# SONAR-7781
+#
+class RemoveUsersPasswordWhenNotLocal < ActiveRecord::Migration
+ def self.up
+ execute_java_migration('org.sonar.db.version.v60.RemoveUsersPasswordWhenNotLocal')
+ end
+end
public class DatabaseVersion {
- public static final int LAST_VERSION = 1_257;
+ public static final int LAST_VERSION = 1_258;
/**
* The minimum supported version which can be upgraded. Lower
import org.sonar.db.version.v60.PopulateUuidColumnsOfProjects;
import org.sonar.db.version.v60.PopulateUuidColumnsOfResourceIndex;
import org.sonar.db.version.v60.PopulateUuidPathColumnOnProjects;
+import org.sonar.db.version.v60.RemoveUsersPasswordWhenNotLocal;
public class MigrationStepModule extends Module {
@Override
// PROJECTS.UUID_PATH
AddUuidPathColumnToProjects.class,
PopulateUuidPathColumnOnProjects.class,
- MakeUuidPathColumnNotNullOnProjects.class);
+ MakeUuidPathColumnNotNullOnProjects.class,
+
+ RemoveUsersPasswordWhenNotLocal.class);
}
}
--- /dev/null
+/*
+ * 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.db.version.v60;
+
+import java.sql.SQLException;
+import org.sonar.api.utils.System2;
+import org.sonar.db.Database;
+import org.sonar.db.version.BaseDataChange;
+import org.sonar.db.version.MassUpdate;
+
+public class RemoveUsersPasswordWhenNotLocal extends BaseDataChange {
+
+ private final System2 system2;
+
+ public RemoveUsersPasswordWhenNotLocal(Database db, System2 system2) {
+ super(db);
+ this.system2 = system2;
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ long now = system2.now();
+ MassUpdate massUpdate = context.prepareMassUpdate();
+ massUpdate.select("SELECT u.id FROM users u WHERE (u.crypted_password IS NOT NULL OR u.salt IS NOT NULL) AND u.user_local=?").setBoolean(1, false);
+ massUpdate.update("UPDATE users SET crypted_password=null, salt=null, updated_at=? WHERE id=?");
+ massUpdate.rowPluralName("none local users with password");
+ massUpdate.execute((row, update) -> {
+ update.setLong(1, now);
+ update.setLong(2, row.getLong(1));
+ return true;
+ });
+ }
+}
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1255');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1256');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1257');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1258');
INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482');
ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
public void verify_count_of_added_MigrationStep_types() {
ComponentContainer container = new ComponentContainer();
new MigrationStepModule().configure(container);
- assertThat(container.size()).isEqualTo(113);
+ assertThat(container.size()).isEqualTo(114);
}
}
--- /dev/null
+/*
+ * 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.db.version.v60;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+import java.util.Map;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+import org.sonar.db.version.MigrationStep;
+
+public class RemoveUsersPasswordWhenNotLocalTest {
+
+ static long PAST_DATE = 1_000_000_000L;
+ static long NOW = 2_000_000_000L;
+
+ System2 system2 = mock(System2.class);
+
+ @Rule
+ public DbTester db = DbTester.createForSchema(System2.INSTANCE, RemoveUsersPasswordWhenNotLocalTest.class, "schema.sql");
+
+ private MigrationStep migration = new RemoveUsersPasswordWhenNotLocal(db.database(), system2);
+
+ @Before
+ public void setUp() throws Exception {
+ when(system2.now()).thenReturn(NOW);
+ }
+
+ @Test
+ public void update_not_local_user() throws Exception {
+ insertUserWithPassword("john", false);
+
+ migration.execute();
+
+ List<Map<String, Object>> rows = db.select("SELECT CRYPTED_PASSWORD,SALT,UPDATED_AT FROM users");
+ assertThat(rows).hasSize(1);
+ assertThat(rows.get(0).get("CRYPTED_PASSWORD")).isNull();
+ assertThat(rows.get(0).get("SALT")).isNull();
+ assertThat(rows.get(0).get("UPDATED_AT")).isEqualTo(NOW);
+ }
+
+ @Test
+ public void ignore_local_user() throws Exception {
+ insertUserWithPassword("john", true);
+
+ migration.execute();
+
+ List<Map<String, Object>> rows = db.select("SELECT CRYPTED_PASSWORD,SALT,UPDATED_AT FROM users");
+ assertThat(rows).hasSize(1);
+ assertThat(rows.get(0).get("CRYPTED_PASSWORD")).isNotNull();
+ assertThat(rows.get(0).get("SALT")).isNotNull();
+ assertThat(rows.get(0).get("UPDATED_AT")).isEqualTo(PAST_DATE);
+ }
+
+ @Test
+ public void ignore_already_migrated_user() throws Exception {
+ insertUserWithoutPasword("john", false);
+
+ migration.execute();
+
+ List<Map<String, Object>> rows = db.select("SELECT CRYPTED_PASSWORD,SALT,UPDATED_AT FROM users");
+ assertThat(rows).hasSize(1);
+ assertThat(rows.get(0).get("CRYPTED_PASSWORD")).isNull();
+ assertThat(rows.get(0).get("SALT")).isNull();
+ assertThat(rows.get(0).get("UPDATED_AT")).isEqualTo(PAST_DATE);
+ }
+
+ private void insertUserWithPassword(String login, boolean isLocal) {
+ db.executeInsert(
+ "users",
+ "LOGIN", login,
+ "NAME", login,
+ "USER_LOCAL", Boolean.toString(isLocal),
+ "CRYPTED_PASSWORD", "crypted_" + login,
+ "SALT", "salt" + login,
+ "CREATED_AT", Long.toString(PAST_DATE),
+ "UPDATED_AT", Long.toString(PAST_DATE));
+ }
+
+ private void insertUserWithoutPasword(String login, boolean isLocal) {
+ db.executeInsert(
+ "users",
+ "LOGIN", login,
+ "NAME", login,
+ "USER_LOCAL", Boolean.toString(isLocal),
+ "CREATED_AT", Long.toString(PAST_DATE),
+ "UPDATED_AT", Long.toString(PAST_DATE));
+ }
+
+}
--- /dev/null
+CREATE TABLE "USERS" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "LOGIN" VARCHAR(255),
+ "NAME" VARCHAR(200),
+ "EMAIL" VARCHAR(100),
+ "CRYPTED_PASSWORD" VARCHAR(40),
+ "SALT" VARCHAR(40),
+ "ACTIVE" BOOLEAN DEFAULT TRUE,
+ "SCM_ACCOUNTS" VARCHAR(4000),
+ "EXTERNAL_IDENTITY" VARCHAR(255),
+ "EXTERNAL_IDENTITY_PROVIDER" VARCHAR(100),
+ "USER_LOCAL" BOOLEAN,
+ "CREATED_AT" BIGINT,
+ "UPDATED_AT" BIGINT
+);