diff options
28 files changed, 23 insertions, 1118 deletions
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java index 797891e1d96..d9b98480083 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java @@ -145,8 +145,6 @@ import org.sonar.server.rule.index.RuleIndexer; import org.sonar.server.setting.DatabaseSettingLoader; import org.sonar.server.setting.DatabaseSettingsEnabler; import org.sonar.server.setting.ThreadLocalSettings; -import org.sonar.server.user.index.UserIndex; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.util.OkHttpClientProvider; import org.sonar.server.util.Paths2Impl; import org.sonar.server.view.index.ViewIndex; @@ -381,9 +379,6 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { MetricFinder.class, UnanalyzedLanguageMetrics.class, - UserIndexer.class, - UserIndex.class, - // components, FavoriteUpdater.class, ProjectIndexersImpl.class, diff --git a/server/sonar-server-common/src/it/java/org/sonar/server/user/index/UserIndexIT.java b/server/sonar-server-common/src/it/java/org/sonar/server/user/index/UserIndexIT.java deleted file mode 100644 index e2d7afea84f..00000000000 --- a/server/sonar-server-common/src/it/java/org/sonar/server/user/index/UserIndexIT.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * 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.user.index; - -import java.util.Collections; -import java.util.List; -import java.util.Locale; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.api.utils.System2; -import org.sonar.core.util.Uuids; -import org.sonar.server.es.EsTester; -import org.sonar.server.es.SearchOptions; - -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; -import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.server.user.index.UserIndexDefinition.TYPE_USER; - -public class UserIndexIT { - - private static final String USER1_LOGIN = "user1"; - private static final String USER2_LOGIN = "user2"; - private static final String USER3_LOGIN = "user3"; - - @Rule - public EsTester es = EsTester.create(); - - private UserIndex underTest = new UserIndex(es.client(), System2.INSTANCE); - private UserQuery.Builder userQuery = UserQuery.builder(); - - @Test - public void getAtMostThreeActiveUsersForScmAccount_returns_the_users_with_specified_scm_account() { - UserDoc user1 = newUser("user1", asList("user_1", "u1")); - UserDoc user2 = newUser("user_with_same_email_as_user1", asList("user_2")).setEmail(user1.email()); - UserDoc user3 = newUser("inactive_user_with_same_scm_as_user1", user1.scmAccounts()).setActive(false); - es.putDocuments(TYPE_USER, user1); - es.putDocuments(TYPE_USER, user2); - es.putDocuments(TYPE_USER, user3); - - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount(user1.scmAccounts().get(0))).extractingResultOf("login").containsOnly(user1.login()); - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount(user1.login())).extractingResultOf("login").containsOnly(user1.login()); - - // both users share the same email - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount(user1.email())).extracting(UserDoc::login).containsOnly(user1.login(), user2.login()); - - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("")).isEmpty(); - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("unknown")).isEmpty(); - } - - @Test - public void getAtMostThreeActiveUsersForScmAccount_ignores_inactive_user() { - String scmAccount = "scmA"; - UserDoc user = newUser(USER1_LOGIN, singletonList(scmAccount)).setActive(false); - es.putDocuments(TYPE_USER, user); - - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount(user.login())).isEmpty(); - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount(scmAccount)).isEmpty(); - } - - @Test - public void getAtMostThreeActiveUsersForScmAccount_returns_maximum_three_users() { - String email = "user@mail.com"; - UserDoc user1 = newUser("user1", Collections.emptyList()).setEmail(email); - UserDoc user2 = newUser("user2", Collections.emptyList()).setEmail(email); - UserDoc user3 = newUser("user3", Collections.emptyList()).setEmail(email); - UserDoc user4 = newUser("user4", Collections.emptyList()).setEmail(email); - es.putDocuments(TYPE_USER, user1); - es.putDocuments(TYPE_USER, user2); - es.putDocuments(TYPE_USER, user3); - es.putDocuments(TYPE_USER, user4); - - // restrict results to 3 users - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount(email)).hasSize(3); - } - - @Test - public void getAtMostThreeActiveUsersForScmAccount_is_case_sensitive_for_login() { - UserDoc user = newUser("the_login", singletonList("John.Smith")); - es.putDocuments(TYPE_USER, user); - - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("the_login")).hasSize(1); - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("the_Login")).isEmpty(); - } - - @Test - public void getAtMostThreeActiveUsersForScmAccount_is_case_insensitive_for_email() { - UserDoc user = newUser("the_login", "the_EMAIL@corp.com", singletonList("John.Smith")); - es.putDocuments(TYPE_USER, user); - - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("the_EMAIL@corp.com")).hasSize(1); - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("the_email@corp.com")).hasSize(1); - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("email")).isEmpty(); - } - - @Test - public void getAtMostThreeActiveUsersForScmAccount_is_case_insensitive_for_scm_account() { - UserDoc user = newUser("the_login", singletonList("John.Smith")); - es.putDocuments(TYPE_USER, user); - - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("John.Smith")).hasSize(1); - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("JOHN.SMIth")).hasSize(1); - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("JOHN.SMITH")).hasSize(1); - assertThat(underTest.getAtMostThreeActiveUsersForScmAccount("JOHN")).isEmpty(); - } - - @Test - public void searchUsers() { - es.putDocuments(TYPE_USER, newUser(USER1_LOGIN, asList("user_1", "u1")).setEmail("email1")); - es.putDocuments(TYPE_USER, newUser(USER2_LOGIN, Collections.emptyList()).setEmail("email2")); - es.putDocuments(TYPE_USER, newUser(USER3_LOGIN, Collections.emptyList()).setEmail("email3").setActive(false)); - - assertThat(underTest.search(userQuery.build(), new SearchOptions()).getDocs()).hasSize(2); - assertThat(underTest.search(userQuery.setTextQuery("user").build(), new SearchOptions()).getDocs()).hasSize(2); - assertThat(underTest.search(userQuery.setTextQuery("ser").build(), new SearchOptions()).getDocs()).hasSize(2); - assertThat(underTest.search(userQuery.setTextQuery(USER1_LOGIN).build(), new SearchOptions()).getDocs()).hasSize(1); - assertThat(underTest.search(userQuery.setTextQuery(USER2_LOGIN).build(), new SearchOptions()).getDocs()).hasSize(1); - assertThat(underTest.search(userQuery.setTextQuery("mail").build(), new SearchOptions()).getDocs()).hasSize(2); - assertThat(underTest.search(userQuery.setTextQuery("EMAIL1").build(), new SearchOptions()).getDocs()).hasSize(1); - - // deactivated users - assertThat(underTest.search(userQuery.setActive(false).setTextQuery(null).build(), new SearchOptions()).getDocs()).hasSize(1); - assertThat(underTest.search(userQuery.setActive(false).setTextQuery("email3").build(), new SearchOptions()).getDocs()).hasSize(1); - } - - private static UserDoc newUser(String login, List<String> scmAccounts) { - return new UserDoc() - .setUuid(Uuids.createFast()) - .setLogin(login) - .setName(login.toUpperCase(Locale.ENGLISH)) - .setEmail(login + "@mail.com") - .setActive(true) - .setScmAccounts(scmAccounts); - } - - private static UserDoc newUser(String login, String email, List<String> scmAccounts) { - return new UserDoc() - .setUuid(Uuids.createFast()) - .setLogin(login) - .setName(login.toUpperCase(Locale.ENGLISH)) - .setEmail(email) - .setActive(true) - .setScmAccounts(scmAccounts); - } -} diff --git a/server/sonar-server-common/src/it/java/org/sonar/server/user/index/UserIndexerIT.java b/server/sonar-server-common/src/it/java/org/sonar/server/user/index/UserIndexerIT.java deleted file mode 100644 index 69d809ea6a5..00000000000 --- a/server/sonar-server-common/src/it/java/org/sonar/server/user/index/UserIndexerIT.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * 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.user.index; - -import java.util.HashSet; -import java.util.List; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.api.utils.System2; -import org.sonar.db.DbTester; -import org.sonar.db.user.UserDto; -import org.sonar.server.es.EsTester; - -import static java.util.Arrays.asList; -import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.server.user.index.UserIndexDefinition.TYPE_USER; - -public class UserIndexerIT { - - private final System2 system2 = System2.INSTANCE; - - @Rule - public DbTester db = DbTester.create(system2); - - @Rule - public EsTester es = EsTester.create(); - - private final UserIndexer underTest = new UserIndexer(db.getDbClient(), es.client()); - - @Test - public void index_nothing_on_startup() { - underTest.indexOnStartup(new HashSet<>()); - - assertThat(es.countDocuments(TYPE_USER)).isZero(); - } - - @Test - public void indexOnStartup_adds_all_users_to_index() { - UserDto user = db.users().insertUser(u -> u.setScmAccounts(asList("user_1", "u1"))); - - underTest.indexOnStartup(new HashSet<>()); - - List<UserDoc> docs = es.getDocuments(TYPE_USER, UserDoc.class); - assertThat(docs).hasSize(1); - UserDoc doc = docs.get(0); - assertThat(doc.uuid()).isEqualTo(user.getUuid()); - assertThat(doc.login()).isEqualTo(user.getLogin()); - assertThat(doc.name()).isEqualTo(user.getName()); - assertThat(doc.email()).isEqualTo(user.getEmail()); - assertThat(doc.active()).isEqualTo(user.isActive()); - assertThat(doc.scmAccounts()).isEqualTo(user.getSortedScmAccounts()); - } - - @Test - public void indexAll_adds_all_users_to_index() { - UserDto user = db.users().insertUser(u -> u.setScmAccounts(asList("user_1", "u1"))); - - underTest.indexAll(); - - List<UserDoc> docs = es.getDocuments(TYPE_USER, UserDoc.class); - assertThat(docs).hasSize(1); - UserDoc doc = docs.get(0); - assertThat(doc.uuid()).isEqualTo(user.getUuid()); - assertThat(doc.login()).isEqualTo(user.getLogin()); - assertThat(doc.name()).isEqualTo(user.getName()); - assertThat(doc.email()).isEqualTo(user.getEmail()); - assertThat(doc.active()).isEqualTo(user.isActive()); - assertThat(doc.scmAccounts()).isEqualTo(user.getSortedScmAccounts()); - } - - @Test - public void indexOnStartup_adds_all_users() { - UserDto user = db.users().insertUser(); - - underTest.indexOnStartup(new HashSet<>()); - - List<UserDoc> docs = es.getDocuments(TYPE_USER, UserDoc.class); - assertThat(docs).hasSize(1); - UserDoc doc = docs.get(0); - assertThat(doc.uuid()).isEqualTo(user.getUuid()); - assertThat(doc.login()).isEqualTo(user.getLogin()); - } - - @Test - public void commitAndIndex_single_user() { - UserDto user = db.users().insertUser(); - UserDto anotherUser = db.users().insertUser(); - - underTest.commitAndIndex(db.getSession(), user); - - List<UserDoc> docs = es.getDocuments(TYPE_USER, UserDoc.class); - assertThat(docs) - .extracting(UserDoc::uuid) - .containsExactlyInAnyOrder(user.getUuid()) - .doesNotContain(anotherUser.getUuid()); - } - - @Test - public void commitAndIndex_multiple_users() { - UserDto user1 = db.users().insertUser(); - UserDto user2 = db.users().insertUser(); - - underTest.commitAndIndex(db.getSession(), asList(user1, user2)); - - List<UserDoc> docs = es.getDocuments(TYPE_USER, UserDoc.class); - assertThat(docs) - .extracting(UserDoc::login) - .containsExactlyInAnyOrder( - user1.getLogin(), - user2.getLogin()); - assertThat(db.countRowsOfTable(db.getSession(), "users")).isEqualTo(2); - } -} diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserDoc.java b/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserDoc.java deleted file mode 100644 index ff8d0324f43..00000000000 --- a/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserDoc.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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.user.index; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import javax.annotation.Nullable; -import org.sonar.server.es.BaseDoc; - -import static org.sonar.server.user.index.UserIndexDefinition.TYPE_USER; - -public class UserDoc extends BaseDoc { - - public UserDoc(Map<String, Object> fields) { - super(TYPE_USER, fields); - } - - public UserDoc() { - this(new HashMap<>()); - } - - @Override - public String getId() { - return uuid(); - } - - public String uuid() { - return getField(UserIndexDefinition.FIELD_UUID); - } - - public String login() { - return getField(UserIndexDefinition.FIELD_LOGIN); - } - - public String name() { - return getField(UserIndexDefinition.FIELD_NAME); - } - - @Nullable - public String email() { - return getNullableField(UserIndexDefinition.FIELD_EMAIL); - } - - public boolean active() { - return getField(UserIndexDefinition.FIELD_ACTIVE); - } - - public List<String> scmAccounts() { - return getField(UserIndexDefinition.FIELD_SCM_ACCOUNTS); - } - - public UserDoc setUuid(@Nullable String s) { - setField(UserIndexDefinition.FIELD_UUID, s); - return this; - } - - public UserDoc setLogin(@Nullable String s) { - setField(UserIndexDefinition.FIELD_LOGIN, s); - return this; - } - - public UserDoc setName(@Nullable String s) { - setField(UserIndexDefinition.FIELD_NAME, s); - return this; - } - - public UserDoc setEmail(@Nullable String s) { - setField(UserIndexDefinition.FIELD_EMAIL, s); - return this; - } - - public UserDoc setActive(boolean b) { - setField(UserIndexDefinition.FIELD_ACTIVE, b); - return this; - } - - public UserDoc setScmAccounts(@Nullable List<String> s) { - setField(UserIndexDefinition.FIELD_SCM_ACCOUNTS, s); - return this; - } -} diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserIndex.java b/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserIndex.java deleted file mode 100644 index 4592822043f..00000000000 --- a/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserIndex.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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.user.index; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import org.apache.commons.lang.StringUtils; -import org.elasticsearch.action.search.SearchRequest; -import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.index.query.BoolQueryBuilder; -import org.elasticsearch.index.query.Operator; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.builder.SearchSourceBuilder; -import org.elasticsearch.search.sort.SortOrder; -import org.sonar.api.ce.ComputeEngineSide; -import org.sonar.api.server.ServerSide; -import org.sonar.api.utils.System2; -import org.sonar.server.es.EsClient; -import org.sonar.server.es.SearchOptions; -import org.sonar.server.es.SearchResult; - -import static org.elasticsearch.index.query.QueryBuilders.boolQuery; -import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; -import static org.elasticsearch.index.query.QueryBuilders.matchQuery; -import static org.elasticsearch.index.query.QueryBuilders.termQuery; -import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SORTABLE_ANALYZER; -import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.USER_SEARCH_GRAMS_ANALYZER; -import static org.sonar.server.user.index.UserIndexDefinition.FIELD_ACTIVE; -import static org.sonar.server.user.index.UserIndexDefinition.FIELD_EMAIL; -import static org.sonar.server.user.index.UserIndexDefinition.FIELD_LOGIN; -import static org.sonar.server.user.index.UserIndexDefinition.FIELD_NAME; -import static org.sonar.server.user.index.UserIndexDefinition.FIELD_SCM_ACCOUNTS; - -@ServerSide -@ComputeEngineSide -public class UserIndex { - - private final EsClient esClient; - private final System2 system2; - - public UserIndex(EsClient esClient, System2 system2) { - this.esClient = esClient; - this.system2 = system2; - } - - /** - * Returns the active users (at most 3) who are associated to the given SCM account. This method can be used - * to detect user conflicts. - */ - public List<UserDoc> getAtMostThreeActiveUsersForScmAccount(String scmAccount) { - List<UserDoc> result = new ArrayList<>(); - if (!StringUtils.isEmpty(scmAccount)) { - SearchResponse response = esClient.search(EsClient.prepareSearch(UserIndexDefinition.TYPE_USER) - .source(new SearchSourceBuilder() - .query(boolQuery().must(matchAllQuery()).filter( - boolQuery() - .must(termQuery(FIELD_ACTIVE, true)) - .should(termQuery(FIELD_LOGIN, scmAccount)) - .should(matchQuery(SORTABLE_ANALYZER.subField(FIELD_EMAIL), scmAccount)) - .should(matchQuery(SORTABLE_ANALYZER.subField(FIELD_SCM_ACCOUNTS), scmAccount)) - .minimumShouldMatch(1))) - .size(3))); - for (SearchHit hit : response.getHits().getHits()) { - result.add(new UserDoc(hit.getSourceAsMap())); - } - } - return result; - } - - public SearchResult<UserDoc> search(UserQuery userQuery, SearchOptions options) { - SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder() - .size(options.getLimit()) - .from(options.getOffset()) - .sort(FIELD_NAME, SortOrder.ASC); - - BoolQueryBuilder filter = boolQuery().must(termQuery(FIELD_ACTIVE, userQuery.isActive())); - QueryBuilder esQuery = matchAllQuery(); - Optional<String> textQuery = userQuery.getTextQuery(); - if (textQuery.isPresent()) { - esQuery = QueryBuilders.multiMatchQuery(textQuery.get(), - FIELD_LOGIN, - USER_SEARCH_GRAMS_ANALYZER.subField(FIELD_LOGIN), - FIELD_NAME, - USER_SEARCH_GRAMS_ANALYZER.subField(FIELD_NAME), - FIELD_EMAIL, - USER_SEARCH_GRAMS_ANALYZER.subField(FIELD_EMAIL)) - .operator(Operator.AND); - } - - SearchRequest request = EsClient.prepareSearch(UserIndexDefinition.TYPE_USER) - .source(searchSourceBuilder.query(boolQuery().must(esQuery).filter(filter))); - return new SearchResult<>(esClient.search(request), UserDoc::new, system2.getDefaultTimeZone().toZoneId()); - } - -} diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserIndexDefinition.java b/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserIndexDefinition.java deleted file mode 100644 index b2c1788b652..00000000000 --- a/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserIndexDefinition.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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.user.index; - -import org.sonar.api.config.Configuration; -import org.sonar.api.config.internal.MapSettings; -import org.sonar.server.es.Index; -import org.sonar.server.es.IndexDefinition; -import org.sonar.server.es.IndexType; -import org.sonar.server.es.IndexType.IndexMainType; -import org.sonar.server.es.newindex.NewRegularIndex; -import org.sonar.server.es.newindex.TypeMapping; - -import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SORTABLE_ANALYZER; -import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.USER_SEARCH_GRAMS_ANALYZER; -import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder; - -/** - * Definition of ES index "users", including settings and fields. - */ -public class UserIndexDefinition implements IndexDefinition { - - public static final Index DESCRIPTOR = Index.simple("users"); - public static final IndexMainType TYPE_USER = IndexType.main(DESCRIPTOR, "user"); - public static final String FIELD_UUID = "uuid"; - public static final String FIELD_LOGIN = "login"; - public static final String FIELD_NAME = "name"; - public static final String FIELD_EMAIL = "email"; - public static final String FIELD_ACTIVE = "active"; - public static final String FIELD_SCM_ACCOUNTS = "scmAccounts"; - - private final Configuration config; - - public UserIndexDefinition(Configuration config) { - this.config = config; - } - - public static UserIndexDefinition createForTest() { - return new UserIndexDefinition(new MapSettings().asConfig()); - } - - @Override - public void define(IndexDefinitionContext context) { - NewRegularIndex index = context.create( - DESCRIPTOR, - newBuilder(config) - .setDefaultNbOfShards(1) - .build()) - // all information is retrieved from the index, not only IDs - .setEnableSource(true); - - TypeMapping mapping = index.createTypeMapping(TYPE_USER); - mapping.keywordFieldBuilder(FIELD_UUID).disableNorms().build(); - mapping.keywordFieldBuilder(FIELD_LOGIN).addSubFields(USER_SEARCH_GRAMS_ANALYZER).build(); - mapping.keywordFieldBuilder(FIELD_NAME).addSubFields(USER_SEARCH_GRAMS_ANALYZER).build(); - mapping.keywordFieldBuilder(FIELD_EMAIL).addSubFields(USER_SEARCH_GRAMS_ANALYZER, SORTABLE_ANALYZER).build(); - mapping.createBooleanField(FIELD_ACTIVE); - mapping.keywordFieldBuilder(FIELD_SCM_ACCOUNTS).disableNorms().addSubFields(SORTABLE_ANALYZER).build(); - } -} diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserIndexer.java b/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserIndexer.java deleted file mode 100644 index 41142f7a36e..00000000000 --- a/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserIndexer.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * 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.user.index; - -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Maps; -import java.util.Collection; -import java.util.List; -import java.util.Set; -import org.elasticsearch.action.index.IndexRequest; -import org.sonar.core.util.stream.MoreCollectors; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.es.EsQueueDto; -import org.sonar.db.user.UserDto; -import org.sonar.server.es.BulkIndexer; -import org.sonar.server.es.BulkIndexer.Size; -import org.sonar.server.es.EsClient; -import org.sonar.server.es.IndexType; -import org.sonar.server.es.IndexingListener; -import org.sonar.server.es.IndexingResult; -import org.sonar.server.es.OneToOneResilientIndexingListener; -import org.sonar.server.es.ResilientIndexer; - -import static java.util.Collections.singletonList; -import static org.sonar.core.util.stream.MoreCollectors.toHashSet; -import static org.sonar.core.util.stream.MoreCollectors.toList; -import static org.sonar.server.user.index.UserIndexDefinition.TYPE_USER; - -public class UserIndexer implements ResilientIndexer { - private final DbClient dbClient; - private final EsClient esClient; - - public UserIndexer(DbClient dbClient, EsClient esClient) { - this.dbClient = dbClient; - this.esClient = esClient; - } - - @Override - public Set<IndexType> getIndexTypes() { - return ImmutableSet.of(TYPE_USER); - } - - @Override - public void indexOnStartup(Set<IndexType> uninitializedIndexTypes) { - indexAll(Size.LARGE); - } - - public void indexAll() { - indexAll(Size.REGULAR); - } - - private void indexAll(Size bulkSize) { - try (DbSession dbSession = dbClient.openSession(false)) { - BulkIndexer bulkIndexer = newBulkIndexer(bulkSize, IndexingListener.FAIL_ON_ERROR); - bulkIndexer.start(); - - dbClient.userDao().scrollAll(dbSession, - // only index requests, no deletion requests. - // Deactivated users are not deleted but updated. - u -> bulkIndexer.add(newIndexRequest(u))); - bulkIndexer.stop(); - } - } - - public void commitAndIndex(DbSession dbSession, UserDto user) { - commitAndIndex(dbSession, singletonList(user)); - } - - public void commitAndIndex(DbSession dbSession, Collection<UserDto> users) { - List<String> uuids = users.stream().map(UserDto::getUuid).collect(toList()); - List<EsQueueDto> items = uuids.stream() - .map(uuid -> EsQueueDto.create(TYPE_USER.format(), uuid)) - .collect(MoreCollectors.toArrayList()); - - dbClient.esQueueDao().insert(dbSession, items); - dbSession.commit(); - postCommit(dbSession, users.stream().map(UserDto::getLogin).collect(toList()), items); - } - - /** - * Entry point for Byteman tests. See directory tests/resilience. - * The parameter "logins" is used only by the Byteman script. - */ - private void postCommit(DbSession dbSession, Collection<String> logins, Collection<EsQueueDto> items) { - index(dbSession, items); - } - - /** - * @return the number of items that have been successfully indexed - */ - @Override - public IndexingResult index(DbSession dbSession, Collection<EsQueueDto> items) { - if (items.isEmpty()) { - return new IndexingResult(); - } - Set<String> uuids = items - .stream() - .map(EsQueueDto::getDocId) - .collect(toHashSet(items.size())); - - BulkIndexer bulkIndexer = newBulkIndexer(Size.REGULAR, new OneToOneResilientIndexingListener(dbClient, dbSession, items)); - bulkIndexer.start(); - - dbClient.userDao().scrollByUuids(dbSession, uuids, - // only index requests, no deletion requests. - // Deactivated users are not deleted but updated. - u -> { - uuids.remove(u.getUuid()); - bulkIndexer.add(newIndexRequest(u)); - }); - - // the remaining uuids reference rows that don't exist in db. They must - // be deleted from index. - uuids.forEach(uuid -> bulkIndexer.addDeletion(TYPE_USER, uuid)); - return bulkIndexer.stop(); - } - - private BulkIndexer newBulkIndexer(Size bulkSize, IndexingListener listener) { - return new BulkIndexer(esClient, TYPE_USER, bulkSize, listener); - } - - private static IndexRequest newIndexRequest(UserDto user) { - UserDoc doc = new UserDoc(Maps.newHashMapWithExpectedSize(8)); - // all the keys must be present, even if value is null - doc.setUuid(user.getUuid()); - doc.setLogin(user.getLogin()); - doc.setName(user.getName()); - doc.setEmail(user.getEmail()); - doc.setActive(user.isActive()); - doc.setScmAccounts(user.getSortedScmAccounts()); - - return doc.toIndexRequest(); - } - -} diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserQuery.java b/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserQuery.java deleted file mode 100644 index 6bdc696985e..00000000000 --- a/server/sonar-server-common/src/main/java/org/sonar/server/user/index/UserQuery.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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.user.index; - -import java.util.Optional; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; - -import static org.apache.commons.lang.StringUtils.isBlank; - -@Immutable -public class UserQuery { - private final String textQuery; - private final boolean active; - - private UserQuery(Builder builder) { - this.textQuery = builder.textQuery; - this.active = builder.active; - } - - public Optional<String> getTextQuery() { - return Optional.ofNullable(textQuery); - } - - public boolean isActive() { - return active; - } - - public static Builder builder() { - return new Builder(); - } - - public static class Builder { - private String textQuery; - private boolean active = true; - - private Builder() { - // enforce factory method - } - - public UserQuery build() { - return new UserQuery(this); - } - - public Builder setTextQuery(@Nullable String textQuery) { - this.textQuery = isBlank(textQuery) ? null : textQuery; - return this; - } - - public Builder setActive(boolean active) { - this.active = active; - return this; - } - } -} diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/user/index/package-info.java b/server/sonar-server-common/src/main/java/org/sonar/server/user/index/package-info.java deleted file mode 100644 index 399c31412b9..00000000000 --- a/server/sonar-server-common/src/main/java/org/sonar/server/user/index/package-info.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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. - */ -@ParametersAreNonnullByDefault -package org.sonar.server.user.index; - -import javax.annotation.ParametersAreNonnullByDefault; - diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/user/index/UserIndexDefinitionTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/user/index/UserIndexDefinitionTest.java deleted file mode 100644 index 34802196e1b..00000000000 --- a/server/sonar-server-common/src/test/java/org/sonar/server/user/index/UserIndexDefinitionTest.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.user.index; - -import org.junit.Test; -import org.sonar.api.config.internal.MapSettings; -import org.sonar.server.es.Index; -import org.sonar.server.es.IndexDefinition; -import org.sonar.server.es.IndexType; -import org.sonar.server.es.newindex.NewIndex; - -import static org.assertj.core.api.Assertions.assertThat; - -public class UserIndexDefinitionTest { - - private IndexDefinition.IndexDefinitionContext underTest = new IndexDefinition.IndexDefinitionContext(); - - @Test - public void define() { - UserIndexDefinition def = new UserIndexDefinition(new MapSettings().asConfig()); - def.define(underTest); - - assertThat(underTest.getIndices()).hasSize(1); - NewIndex index = underTest.getIndices().get("users"); - assertThat(index.getMainType()) - .isEqualTo(IndexType.main(Index.simple("users"), "user")); - assertThat(index.getRelationsStream()).isEmpty(); - - // no cluster by default - assertThat(index.getSetting("index.number_of_shards")).isEqualTo("1"); - assertThat(index.getSetting("index.number_of_replicas")).isEqualTo("0"); - } -} diff --git a/server/sonar-server-common/src/testFixtures/java/org/sonar/server/es/EsTester.java b/server/sonar-server-common/src/testFixtures/java/org/sonar/server/es/EsTester.java index 90a64349095..5b304813d71 100644 --- a/server/sonar-server-common/src/testFixtures/java/org/sonar/server/es/EsTester.java +++ b/server/sonar-server-common/src/testFixtures/java/org/sonar/server/es/EsTester.java @@ -96,7 +96,6 @@ import org.sonar.server.es.newindex.NewIndex; import org.sonar.server.issue.index.IssueIndexDefinition; import org.sonar.server.measure.index.ProjectMeasuresIndexDefinition; import org.sonar.server.rule.index.RuleIndexDefinition; -import org.sonar.server.user.index.UserIndexDefinition; import org.sonar.server.view.index.ViewIndexDefinition; import static com.google.common.base.Preconditions.checkArgument; @@ -152,7 +151,6 @@ public class EsTester extends ExternalResource { IssueIndexDefinition.createForTest(), ProjectMeasuresIndexDefinition.createForTest(), RuleIndexDefinition.createForTest(), - UserIndexDefinition.createForTest(), ViewIndexDefinition.createForTest()); CORE_INDICES_CREATED.set(true); diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserUpdater.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserUpdater.java index 366e681e201..8b062afa656 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserUpdater.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserUpdater.java @@ -27,7 +27,6 @@ import java.util.List; import java.util.Objects; import java.util.function.Consumer; import java.util.regex.Pattern; -import java.util.stream.Stream; import javax.annotation.Nullable; import javax.inject.Inject; import org.apache.commons.lang.math.RandomUtils; @@ -42,7 +41,6 @@ import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; import org.sonar.db.user.UserGroupDto; import org.sonar.server.authentication.CredentialsLocalAuthentication; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.usergroups.DefaultGroupFinder; import org.sonar.server.util.Validation; @@ -50,9 +48,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Strings.isNullOrEmpty; import static com.google.common.collect.Lists.newArrayList; import static java.lang.String.format; -import static java.util.Arrays.stream; import static java.util.Collections.emptyList; -import static java.util.stream.Stream.concat; import static org.sonar.api.CoreProperties.DEFAULT_ISSUE_ASSIGNEE; import static org.sonar.core.util.Slug.slugify; import static org.sonar.core.util.stream.MoreCollectors.toList; @@ -77,17 +73,15 @@ public class UserUpdater { private final NewUserNotifier newUserNotifier; private final DbClient dbClient; - private final UserIndexer userIndexer; private final DefaultGroupFinder defaultGroupFinder; private final AuditPersister auditPersister; private final CredentialsLocalAuthentication localAuthentication; @Inject - public UserUpdater(NewUserNotifier newUserNotifier, DbClient dbClient, UserIndexer userIndexer, DefaultGroupFinder defaultGroupFinder, Configuration config, + public UserUpdater(NewUserNotifier newUserNotifier, DbClient dbClient, DefaultGroupFinder defaultGroupFinder, Configuration config, AuditPersister auditPersister, CredentialsLocalAuthentication localAuthentication) { this.newUserNotifier = newUserNotifier; this.dbClient = dbClient; - this.userIndexer = userIndexer; this.defaultGroupFinder = defaultGroupFinder; this.auditPersister = auditPersister; this.localAuthentication = localAuthentication; @@ -138,7 +132,7 @@ public class UserUpdater { private UserDto commitUser(DbSession dbSession, UserDto userDto, Consumer<UserDto> beforeCommit, UserDto... otherUsersToIndex) { beforeCommit.accept(userDto); - userIndexer.commitAndIndex(dbSession, concat(Stream.of(userDto), stream(otherUsersToIndex)).collect(toList())); + dbSession.commit(); notifyNewUser(userDto.getLogin(), userDto.getName(), userDto.getEmail()); return userDto; } diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/HttpHeadersAuthenticationTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/HttpHeadersAuthenticationTest.java index 4e8a3d82fac..0e1975a2f4d 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/HttpHeadersAuthenticationTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/HttpHeadersAuthenticationTest.java @@ -44,7 +44,6 @@ import org.sonar.server.es.EsTester; import org.sonar.server.management.ManagedInstanceService; import org.sonar.server.user.NewUserNotifier; import org.sonar.server.user.UserUpdater; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.usergroups.DefaultGroupFinder; import static java.util.Arrays.stream; @@ -93,11 +92,9 @@ public class HttpHeadersAuthenticationTest { private final System2 system2 = mock(System2.class); private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient(), settings.asConfig()); - private final UserIndexer userIndexer = new UserIndexer(db.getDbClient(), es.client()); - private final DefaultGroupFinder defaultGroupFinder = new DefaultGroupFinder(db.getDbClient()); private final UserRegistrarImpl userIdentityAuthenticator = new UserRegistrarImpl(db.getDbClient(), - new UserUpdater(mock(NewUserNotifier.class), db.getDbClient(), userIndexer, defaultGroupFinder, settings.asConfig(), mock(AuditPersister.class), localAuthentication), + new UserUpdater(mock(NewUserNotifier.class), db.getDbClient(), defaultGroupFinder, settings.asConfig(), mock(AuditPersister.class), localAuthentication), defaultGroupFinder, mock(ManagedInstanceService.class)); private final HttpServletResponse response = mock(HttpServletResponse.class); private final JwtHttpHandler jwtHttpHandler = mock(JwtHttpHandler.class); diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/UserRegistrarImplTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/UserRegistrarImplTest.java index 070f9a6b330..ef0bc0b076c 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/UserRegistrarImplTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/UserRegistrarImplTest.java @@ -42,7 +42,6 @@ import org.sonar.server.es.EsTester; import org.sonar.server.management.ManagedInstanceService; import org.sonar.server.user.NewUserNotifier; import org.sonar.server.user.UserUpdater; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.usergroups.DefaultGroupFinder; import static java.util.Arrays.stream; @@ -86,12 +85,11 @@ public class UserRegistrarImplTest { @Rule public LogTester logTester = new LogTester(); - private final UserIndexer userIndexer = new UserIndexer(db.getDbClient(), es.client()); private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient(), settings.asConfig()); private final DefaultGroupFinder groupFinder = new DefaultGroupFinder(db.getDbClient()); private final AuditPersister auditPersister = mock(AuditPersister.class); private final ManagedInstanceService managedInstanceService = mock(ManagedInstanceService.class); - private final UserUpdater userUpdater = new UserUpdater(mock(NewUserNotifier.class), db.getDbClient(), userIndexer, groupFinder, settings.asConfig(), auditPersister, localAuthentication); + private final UserUpdater userUpdater = new UserUpdater(mock(NewUserNotifier.class), db.getDbClient(), groupFinder, settings.asConfig(), auditPersister, localAuthentication); private final UserRegistrarImpl underTest = new UserRegistrarImpl(db.getDbClient(), userUpdater, groupFinder, managedInstanceService); private GroupDto defaultGroup; diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterCreateTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterCreateTest.java index 059bd7eee30..6b90010163f 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterCreateTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterCreateTest.java @@ -25,7 +25,6 @@ import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.util.List; -import org.elasticsearch.search.SearchHit; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -43,8 +42,6 @@ import org.sonar.server.authentication.CredentialsLocalAuthentication; import org.sonar.server.authentication.CredentialsLocalAuthentication.HashMethod; import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.BadRequestException; -import org.sonar.server.user.index.UserIndexDefinition; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.usergroups.DefaultGroupFinder; import static java.util.Arrays.asList; @@ -52,7 +49,6 @@ import static java.util.Collections.singletonList; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.assertj.core.data.MapEntry.entry; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; @@ -76,10 +72,9 @@ public class UserUpdaterCreateTest { private final NewUserNotifier newUserNotifier = mock(NewUserNotifier.class); private final ArgumentCaptor<NewUserHandler.Context> newUserHandler = ArgumentCaptor.forClass(NewUserHandler.Context.class); private final DbSession session = db.getSession(); - private final UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); private final MapSettings settings = new MapSettings().setProperty("sonar.internal.pbkdf2.iterations", "1"); private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient(), settings.asConfig()); - private final UserUpdater underTest = new UserUpdater(newUserNotifier, dbClient, userIndexer, + private final UserUpdater underTest = new UserUpdater(newUserNotifier, dbClient, new DefaultGroupFinder(dbClient), settings.asConfig(), null, localAuthentication); @Test @@ -111,13 +106,6 @@ public class UserUpdaterCreateTest { .isEqualTo(dto.getUpdatedAt()); assertThat(dbClient.userDao().selectByLogin(session, "user").getUuid()).isEqualTo(dto.getUuid()); - List<SearchHit> indexUsers = es.getDocuments(UserIndexDefinition.TYPE_USER); - assertThat(indexUsers).hasSize(1); - assertThat(indexUsers.get(0).getSourceAsMap()) - .contains( - entry("login", "user"), - entry("name", "User"), - entry("email", "user@mail.com")); } @Test @@ -272,22 +260,6 @@ public class UserUpdaterCreateTest { } @Test - public void create_user_and_index_other_user() { - createDefaultGroup(); - UserDto otherUser = db.users().insertUser(); - - UserDto created = underTest.createAndCommit(session, NewUser.builder() - .setLogin("user") - .setName("User") - .setEmail("user@mail.com") - .setPassword("PASSWORD") - .build(), u -> { - }, otherUser); - - assertThat(es.getIds(UserIndexDefinition.TYPE_USER)).containsExactlyInAnyOrder(created.getUuid(), otherUser.getUuid()); - } - - @Test @UseDataProvider("loginWithAuthorizedSuffix") public void createAndCommit_should_createUserWithoutException_when_loginHasAuthorizedSuffix(String login) { createDefaultGroup(); @@ -302,7 +274,7 @@ public class UserUpdaterCreateTest { @DataProvider public static Object[][] loginWithAuthorizedSuffix() { - return new Object[][]{ + return new Object[][] { {"1Login"}, {"AnotherLogin"}, {"alogin"}, @@ -354,7 +326,7 @@ public class UserUpdaterCreateTest { @DataProvider public static Object[][] loginWithUnauthorizedSuffix() { - return new Object[][]{ + return new Object[][] { {".Toto"}, {"@Toto"}, {"-Tutu"}, diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterReactivateTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterReactivateTest.java index c5ecdee63a7..3eebdcf27a0 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterReactivateTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterReactivateTest.java @@ -34,8 +34,6 @@ import org.sonar.db.user.GroupTesting; import org.sonar.db.user.UserDto; import org.sonar.server.authentication.CredentialsLocalAuthentication; import org.sonar.server.authentication.CredentialsLocalAuthentication.HashMethod; -import org.sonar.server.es.EsTester; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.usergroups.DefaultGroupFinder; import static java.lang.String.format; @@ -53,18 +51,15 @@ public class UserUpdaterReactivateTest { private final System2 system2 = new AlwaysIncreasingSystem2(); @Rule - public EsTester es = EsTester.create(); - @Rule public DbTester db = DbTester.create(system2); private final DbClient dbClient = db.getDbClient(); private final NewUserNotifier newUserNotifier = mock(NewUserNotifier.class); private final DbSession session = db.getSession(); - private final UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); private final MapSettings settings = new MapSettings().setProperty("sonar.internal.pbkdf2.iterations", "1"); private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient(), settings.asConfig()); private final AuditPersister auditPersister = mock(AuditPersister.class); - private final UserUpdater underTest = new UserUpdater(newUserNotifier, dbClient, userIndexer, + private final UserUpdater underTest = new UserUpdater(newUserNotifier, dbClient, new DefaultGroupFinder(dbClient), settings.asConfig(), auditPersister, localAuthentication); diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterUpdateTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterUpdateTest.java index dfdc55fd4c5..2ab8be4eb7a 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterUpdateTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterUpdateTest.java @@ -20,9 +20,7 @@ package org.sonar.server.user; import com.google.common.collect.Multimap; -import java.util.List; import java.util.function.Consumer; -import org.elasticsearch.search.SearchHit; import org.junit.Rule; import org.junit.Test; import org.sonar.api.config.internal.MapSettings; @@ -38,10 +36,7 @@ import org.sonar.db.property.PropertyQuery; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; import org.sonar.server.authentication.CredentialsLocalAuthentication; -import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.BadRequestException; -import org.sonar.server.user.index.UserIndexDefinition; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.usergroups.DefaultGroupFinder; import static java.util.Arrays.asList; @@ -49,7 +44,6 @@ import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; -import static org.assertj.core.data.MapEntry.entry; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -69,18 +63,15 @@ public class UserUpdaterUpdateTest { private final System2 system2 = new AlwaysIncreasingSystem2(); @Rule - public EsTester es = EsTester.create(); - @Rule public DbTester db = DbTester.create(system2); private final DbClient dbClient = db.getDbClient(); private final NewUserNotifier newUserNotifier = mock(NewUserNotifier.class); private final DbSession session = db.getSession(); - private final UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); private final MapSettings settings = new MapSettings().setProperty("sonar.internal.pbkdf2.iterations", "1"); private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient(), settings.asConfig()); private final AuditPersister auditPersister = mock(AuditPersister.class); - private final UserUpdater underTest = new UserUpdater(newUserNotifier, dbClient, userIndexer, + private final UserUpdater underTest = new UserUpdater(newUserNotifier, dbClient, new DefaultGroupFinder(dbClient), settings.asConfig(), auditPersister, localAuthentication); @Test @@ -88,7 +79,6 @@ public class UserUpdaterUpdateTest { UserDto user = db.users().insertUser(newLocalUser(DEFAULT_LOGIN, "Marius", "marius@email.com") .setScmAccounts(asList("ma", "marius33"))); createDefaultGroup(); - userIndexer.indexAll(); underTest.updateAndCommit(session, user, new UpdateUser() .setName("Marius2") @@ -104,13 +94,6 @@ public class UserUpdaterUpdateTest { assertThat(updatedUser.getCreatedAt()).isEqualTo(user.getCreatedAt()); assertThat(updatedUser.getUpdatedAt()).isGreaterThan(user.getCreatedAt()); - List<SearchHit> indexUsers = es.getDocuments(UserIndexDefinition.TYPE_USER); - assertThat(indexUsers).hasSize(1); - assertThat(indexUsers.get(0).getSourceAsMap()) - .contains( - entry("login", DEFAULT_LOGIN), - entry("name", "Marius2"), - entry("email", "marius2@mail.com")); verify(auditPersister, never()).updateUserPassword(any(), any()); } @@ -219,22 +202,6 @@ public class UserUpdaterUpdateTest { } @Test - public void update_index_when_updating_user_login() { - UserDto oldUser = db.users().insertUser(); - createDefaultGroup(); - userIndexer.indexAll(); - - underTest.updateAndCommit(session, oldUser, new UpdateUser() - .setLogin("new_login"), u -> { - }); - - List<SearchHit> indexUsers = es.getDocuments(UserIndexDefinition.TYPE_USER); - assertThat(indexUsers).hasSize(1); - assertThat(indexUsers.get(0).getSourceAsMap()) - .contains(entry("login", "new_login")); - } - - @Test public void update_default_assignee_when_updating_login() { createDefaultGroup(); UserDto oldUser = db.users().insertUser(); @@ -248,7 +215,6 @@ public class UserUpdaterUpdateTest { new PropertyDto().setKey(DEFAULT_ISSUE_ASSIGNEE).setValue(oldUser.getLogin()).setComponentUuid(project2.uuid())); db.properties().insertProperties(oldUser.getLogin(), anotherProject.getKey(), anotherProject.name(), anotherProject.qualifier(), new PropertyDto().setKey(DEFAULT_ISSUE_ASSIGNEE).setValue("another login").setComponentUuid(anotherProject.uuid())); - userIndexer.indexAll(); underTest.updateAndCommit(session, oldUser, new UpdateUser() .setLogin("new_login"), u -> { @@ -486,23 +452,6 @@ public class UserUpdaterUpdateTest { } @Test - public void update_user_and_index_other_user() { - createDefaultGroup(); - UserDto user = db.users().insertUser(newLocalUser(DEFAULT_LOGIN, "Marius", "marius@email.com") - .setScmAccounts(asList("ma", "marius33"))); - UserDto otherUser = db.users().insertUser(); - - underTest.updateAndCommit(session, user, new UpdateUser() - .setName("Marius2") - .setEmail("marius2@mail.com") - .setPassword("password2") - .setScmAccounts(asList("ma2")), u -> { - }, otherUser); - - assertThat(es.getIds(UserIndexDefinition.TYPE_USER)).containsExactlyInAnyOrder(user.getUuid(), otherUser.getUuid()); - } - - @Test public void fail_to_set_null_password_when_local_user() { UserDto user = db.users().insertUser(newLocalUser(DEFAULT_LOGIN, "Marius", "marius@email.com")); createDefaultGroup(); diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/es/RecoveryIndexerTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/es/RecoveryIndexerTest.java index 1731d796002..024f4613c20 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/es/RecoveryIndexerTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/es/RecoveryIndexerTest.java @@ -45,7 +45,6 @@ import org.sonar.db.DbTester; import org.sonar.db.es.EsQueueDto; import org.sonar.server.es.IndexType.IndexMainType; import org.sonar.server.rule.index.RuleIndexer; -import org.sonar.server.user.index.UserIndexer; import static java.util.stream.IntStream.rangeClosed; import static org.assertj.core.api.Assertions.assertThat; @@ -397,9 +396,8 @@ public class RecoveryIndexerTest { } private RecoveryIndexer newRecoveryIndexer() { - UserIndexer userIndexer = new UserIndexer(db.getDbClient(), es.client()); RuleIndexer ruleIndexer = new RuleIndexer(es.client(), db.getDbClient()); - return newRecoveryIndexer(userIndexer, ruleIndexer); + return newRecoveryIndexer(ruleIndexer); } private RecoveryIndexer newRecoveryIndexer(ResilientIndexer... indexers) { diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/AnonymizeActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/AnonymizeActionIT.java index 2257f33c79c..a03f4399b57 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/AnonymizeActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/AnonymizeActionIT.java @@ -22,8 +22,6 @@ package org.sonar.server.user.ws; import java.util.List; import java.util.Optional; import javax.annotation.Nullable; -import org.elasticsearch.search.SearchHits; -import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.Rule; import org.junit.Test; import org.sonar.api.impl.utils.AlwaysIncreasingSystem2; @@ -32,17 +30,11 @@ import org.sonar.db.DbClient; import org.sonar.db.DbTester; import org.sonar.db.user.UserDto; import org.sonar.db.user.UserQuery; -import org.sonar.server.es.EsClient; -import org.sonar.server.es.EsTester; -import org.sonar.server.es.EsUtils; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.user.ExternalIdentity; -import org.sonar.server.user.index.UserDoc; -import org.sonar.server.user.index.UserIndexDefinition; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; @@ -50,11 +42,7 @@ import org.sonar.server.ws.WsActionTester; import static java.util.Collections.emptyList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.elasticsearch.index.query.QueryBuilders.boolQuery; -import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.sonar.db.permission.GlobalPermission.ADMINISTER; -import static org.sonar.server.user.index.UserIndexDefinition.FIELD_ACTIVE; -import static org.sonar.server.user.index.UserIndexDefinition.FIELD_UUID; public class AnonymizeActionIT { private final System2 system2 = new AlwaysIncreasingSystem2(); @@ -62,14 +50,11 @@ public class AnonymizeActionIT { @Rule public DbTester db = DbTester.create(system2); @Rule - public EsTester es = EsTester.create(); - @Rule public UserSessionRule userSession = UserSessionRule.standalone(); private final DbClient dbClient = db.getDbClient(); - private final UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); private final UserAnonymizer userAnonymizer = new UserAnonymizer(db.getDbClient()); - private final WsActionTester ws = new WsActionTester(new AnonymizeAction(dbClient, userIndexer, userSession, userAnonymizer)); + private final WsActionTester ws = new WsActionTester(new AnonymizeAction(dbClient, userSession, userAnonymizer)); @Test public void anonymize_user() { @@ -87,7 +72,6 @@ public class AnonymizeActionIT { TestResponse response = anonymize(user.getLogin()); verifyThatUserIsAnonymized(user.getUuid()); - verifyThatUserIsAnonymizedOnEs(user.getUuid()); assertThat(response.getInput()).isEmpty(); } @@ -174,19 +158,6 @@ public class AnonymizeActionIT { return request.execute(); } - private void verifyThatUserIsAnonymizedOnEs(String uuid) { - SearchHits hits = es.client().search(EsClient.prepareSearch(UserIndexDefinition.TYPE_USER) - .source(new SearchSourceBuilder() - .query(boolQuery() - .must(termQuery(FIELD_UUID, uuid)) - .must(termQuery(FIELD_ACTIVE, "false"))))) - .getHits(); - List<UserDoc> userDocs = EsUtils.convertToDocs(hits, UserDoc::new); - assertThat(userDocs).hasSize(1); - assertThat(userDocs.get(0).login()).startsWith("sq-removed-"); - assertThat(userDocs.get(0).name()).startsWith("sq-removed-"); - } - private void verifyThatUserIsAnonymized(String uuid) { List<UserDto> users = dbClient.userDao().selectUsers(db.getSession(), UserQuery.builder().isActive(false).build()); assertThat(users).hasSize(1); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/ChangePasswordActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/ChangePasswordActionIT.java index 541119f52c7..a1fdd23020d 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/ChangePasswordActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/ChangePasswordActionIT.java @@ -40,14 +40,11 @@ import org.sonar.db.user.SessionTokenDto; import org.sonar.db.user.UserDto; import org.sonar.server.authentication.CredentialsLocalAuthentication; import org.sonar.server.authentication.JwtHttpHandler; -import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.user.NewUserNotifier; import org.sonar.server.user.UserUpdater; -import org.sonar.server.user.index.UserIndexDefinition; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.usergroups.DefaultGroupFinder; import org.sonar.server.ws.ServletFilterHandler; @@ -74,8 +71,6 @@ public class ChangePasswordActionIT { @Rule public DbTester db = DbTester.create(); @Rule - public EsTester es = EsTester.createCustom(UserIndexDefinition.createForTest()); - @Rule public UserSessionRule userSessionRule = UserSessionRule.standalone().logIn(); private final ArgumentCaptor<UserDto> userDtoCaptor = ArgumentCaptor.forClass(UserDto.class); @@ -88,7 +83,7 @@ public class ChangePasswordActionIT { private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient(), settings.asConfig()); private final UserUpdater userUpdater = new UserUpdater(mock(NewUserNotifier.class), db.getDbClient(), - new UserIndexer(db.getDbClient(), es.client()), new DefaultGroupFinder(db.getDbClient()), + new DefaultGroupFinder(db.getDbClient()), new MapSettings().asConfig(), new NoOpAuditPersister(), localAuthentication); private final JwtHttpHandler jwtHttpHandler = mock(JwtHttpHandler.class); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/CreateActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/CreateActionIT.java index 3f885ffefb4..5af7f5a8c5e 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/CreateActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/CreateActionIT.java @@ -21,7 +21,6 @@ package org.sonar.server.user.ws; import java.util.List; import java.util.Optional; -import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -34,8 +33,6 @@ import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; import org.sonar.server.authentication.CredentialsLocalAuthentication; -import org.sonar.server.es.EsClient; -import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.UnauthorizedException; @@ -43,8 +40,6 @@ import org.sonar.server.management.ManagedInstanceChecker; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.user.NewUserNotifier; import org.sonar.server.user.UserUpdater; -import org.sonar.server.user.index.UserIndexDefinition; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.user.ws.CreateAction.CreateRequest; import org.sonar.server.usergroups.DefaultGroupFinder; import org.sonar.server.ws.TestRequest; @@ -57,17 +52,11 @@ import static java.util.Collections.singletonList; import static java.util.Optional.ofNullable; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.elasticsearch.index.query.QueryBuilders.boolQuery; -import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.sonar.db.user.UserTesting.newUserDto; -import static org.sonar.server.user.index.UserIndexDefinition.FIELD_EMAIL; -import static org.sonar.server.user.index.UserIndexDefinition.FIELD_LOGIN; -import static org.sonar.server.user.index.UserIndexDefinition.FIELD_NAME; -import static org.sonar.server.user.index.UserIndexDefinition.FIELD_SCM_ACCOUNTS; public class CreateActionIT { @@ -77,17 +66,14 @@ public class CreateActionIT { @Rule public DbTester db = DbTester.create(system2); @Rule - public EsTester es = EsTester.create(); - @Rule public UserSessionRule userSessionRule = UserSessionRule.standalone(); - private final UserIndexer userIndexer = new UserIndexer(db.getDbClient(), es.client()); private GroupDto defaultGroup; private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient(), settings.asConfig()); private final ManagedInstanceChecker managedInstanceChecker = mock(ManagedInstanceChecker.class); private final WsActionTester tester = new WsActionTester(new CreateAction(db.getDbClient(), new UserUpdater(mock(NewUserNotifier.class), - db.getDbClient(), userIndexer, new DefaultGroupFinder(db.getDbClient()), settings.asConfig(), new NoOpAuditPersister(), localAuthentication), userSessionRule, managedInstanceChecker)); + db.getDbClient(), new DefaultGroupFinder(db.getDbClient()), settings.asConfig(), new NoOpAuditPersister(), localAuthentication), userSessionRule, managedInstanceChecker)); @Before public void setUp() { @@ -110,16 +96,6 @@ public class CreateActionIT { .extracting(User::getLogin, User::getName, User::getEmail, User::getScmAccountsList, User::getLocal) .containsOnly("john", "John", "john@email.com", singletonList("jn"), true); - // exists in index - assertThat(es.client().search(EsClient.prepareSearch(UserIndexDefinition.TYPE_USER) - .source(new SearchSourceBuilder() - .query(boolQuery() - .must(termQuery(FIELD_LOGIN, "john")) - .must(termQuery(FIELD_NAME, "John")) - .must(termQuery(FIELD_EMAIL, "john@email.com")) - .must(termQuery(FIELD_SCM_ACCOUNTS, "jn"))))) - .getHits().getHits()).hasSize(1); - // exists in db Optional<UserDto> dbUser = db.users().selectUserByLogin("john"); assertThat(dbUser).isPresent(); @@ -229,7 +205,6 @@ public class CreateActionIT { logInAsSystemAdministrator(); db.users().insertUser(newUserDto("john", "John", "john@email.com").setActive(false)); - userIndexer.indexAll(); call(CreateRequest.builder() .setLogin("john") diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/DeactivateActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/DeactivateActionIT.java index 74c0b98f237..6ed266dfa16 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/DeactivateActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/DeactivateActionIT.java @@ -21,7 +21,6 @@ package org.sonar.server.user.ws; import java.util.Optional; import javax.annotation.Nullable; -import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.Rule; import org.junit.Test; import org.sonar.api.impl.utils.AlwaysIncreasingSystem2; @@ -45,8 +44,6 @@ import org.sonar.db.user.GroupDto; import org.sonar.db.user.SessionTokenDto; import org.sonar.db.user.UserDismissedMessageDto; import org.sonar.db.user.UserDto; -import org.sonar.server.es.EsClient; -import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; @@ -54,8 +51,6 @@ import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.management.ManagedInstanceChecker; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.user.ExternalIdentity; -import org.sonar.server.user.index.UserIndexDefinition; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; @@ -63,15 +58,11 @@ import org.sonar.server.ws.WsActionTester; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.elasticsearch.index.query.QueryBuilders.boolQuery; -import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.sonar.db.property.PropertyTesting.newUserPropertyDto; -import static org.sonar.server.user.index.UserIndexDefinition.FIELD_ACTIVE; -import static org.sonar.server.user.index.UserIndexDefinition.FIELD_UUID; import static org.sonar.test.JsonAssert.assertJson; public class DeactivateActionIT { @@ -81,15 +72,12 @@ public class DeactivateActionIT { @Rule public DbTester db = DbTester.create(system2); @Rule - public EsTester es = EsTester.create(); - @Rule public UserSessionRule userSession = UserSessionRule.standalone(); private final DbClient dbClient = db.getDbClient(); - private final UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); private final DbSession dbSession = db.getSession(); private final UserAnonymizer userAnonymizer = new UserAnonymizer(db.getDbClient(), () -> "anonymized"); - private final UserDeactivator userDeactivator = new UserDeactivator(dbClient, userIndexer, userSession, userAnonymizer); + private final UserDeactivator userDeactivator = new UserDeactivator(dbClient, userSession, userAnonymizer); private final ManagedInstanceChecker managedInstanceChecker = mock(ManagedInstanceChecker.class); private final WsActionTester ws = new WsActionTester(new DeactivateAction(dbClient, userSession, new UserJsonWriter(userSession), userDeactivator, managedInstanceChecker)); @@ -106,12 +94,6 @@ public class DeactivateActionIT { deactivate(user.getLogin()); verifyThatUserIsDeactivated(user.getLogin()); - assertThat(es.client().search(EsClient.prepareSearch(UserIndexDefinition.TYPE_USER) - .source(new SearchSourceBuilder() - .query(boolQuery() - .must(termQuery(FIELD_UUID, user.getUuid())) - .must(termQuery(FIELD_ACTIVE, "false"))))) - .getHits().getHits()).hasSize(1); } @Test @@ -128,12 +110,6 @@ public class DeactivateActionIT { verifyThatUserIsDeactivated("anonymized"); verifyThatUserIsAnomymized("anonymized"); - assertThat(es.client().search(EsClient.prepareSearch(UserIndexDefinition.TYPE_USER) - .source(new SearchSourceBuilder() - .query(boolQuery() - .must(termQuery(FIELD_UUID, user.getUuid())) - .must(termQuery(FIELD_ACTIVE, "false"))))) - .getHits().getHits()).hasSize(1); } @Test diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/UpdateActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/UpdateActionIT.java index d3827636087..cf0f8bfee69 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/UpdateActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/UpdateActionIT.java @@ -32,7 +32,6 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.user.UserDto; import org.sonar.server.authentication.CredentialsLocalAuthentication; -import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; @@ -41,7 +40,6 @@ import org.sonar.server.management.ManagedInstanceChecker; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.user.NewUserNotifier; import org.sonar.server.user.UserUpdater; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.usergroups.DefaultGroupFinder; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; @@ -64,17 +62,14 @@ public class UpdateActionIT { @Rule public DbTester db = DbTester.create(system2); @Rule - public EsTester es = EsTester.create(); - @Rule public UserSessionRule userSession = UserSessionRule.standalone().logIn().setSystemAdministrator(); private final DbClient dbClient = db.getDbClient(); private final DbSession dbSession = db.getSession(); - private final UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient(), settings.asConfig()); private final ManagedInstanceChecker managedInstanceChecker = mock(ManagedInstanceChecker.class); private final WsActionTester ws = new WsActionTester(new UpdateAction( - new UserUpdater(mock(NewUserNotifier.class), dbClient, userIndexer, new DefaultGroupFinder(db.getDbClient()), settings.asConfig(), null, localAuthentication), + new UserUpdater(mock(NewUserNotifier.class), dbClient, new DefaultGroupFinder(db.getDbClient()), settings.asConfig(), null, localAuthentication), userSession, new UserJsonWriter(userSession), dbClient, managedInstanceChecker)); @Before @@ -338,6 +333,6 @@ public class UpdateActionIT { .setExternalLogin("jo") .setExternalIdentityProvider("sonarqube"); dbClient.userDao().insert(dbSession, userDto); - userIndexer.commitAndIndex(dbSession, userDto); + dbSession.commit(); } } diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/UpdateIdentityProviderActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/UpdateIdentityProviderActionIT.java index e5271b48c26..125339b82ba 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/UpdateIdentityProviderActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/UpdateIdentityProviderActionIT.java @@ -29,7 +29,6 @@ import org.sonar.db.user.UserDto; import org.sonar.server.authentication.CredentialsLocalAuthentication; import org.sonar.server.authentication.IdentityProviderRepositoryRule; import org.sonar.server.authentication.TestIdentityProvider; -import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; @@ -38,7 +37,6 @@ import org.sonar.server.management.ManagedInstanceChecker; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.user.NewUserNotifier; import org.sonar.server.user.UserUpdater; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.usergroups.DefaultGroupFinder; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; @@ -63,20 +61,17 @@ public class UpdateIdentityProviderActionIT { @Rule public DbTester db = DbTester.create(); @Rule - public EsTester es = EsTester.create(); - @Rule public UserSessionRule userSession = UserSessionRule.standalone().logIn().setSystemAdministrator(); private final MapSettings settings = new MapSettings().setProperty("sonar.internal.pbkdf2.iterations", "1"); private final DbClient dbClient = db.getDbClient(); private final DbSession dbSession = db.getSession(); - private final UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(dbClient, settings.asConfig()); private final ManagedInstanceChecker managedInstanceChecker = mock(ManagedInstanceChecker.class); private final WsActionTester underTest = new WsActionTester(new UpdateIdentityProviderAction(dbClient, identityProviderRepository, - new UserUpdater(mock(NewUserNotifier.class), dbClient, userIndexer, new DefaultGroupFinder(db.getDbClient()), settings.asConfig(), null, localAuthentication), + new UserUpdater(mock(NewUserNotifier.class), dbClient, new DefaultGroupFinder(db.getDbClient()), settings.asConfig(), null, localAuthentication), userSession, managedInstanceChecker)); @Test @@ -261,7 +256,7 @@ public class UpdateIdentityProviderActionIT { .setExternalLogin(externalLogin) .setExternalIdentityProvider(externalIdentityProvider); dbClient.userDao().insert(dbSession, userDto); - userIndexer.commitAndIndex(dbSession, userDto); + dbSession.commit(); } } diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/UpdateLoginActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/UpdateLoginActionIT.java index d3609f45760..ef372d567d0 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/UpdateLoginActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/UpdateLoginActionIT.java @@ -26,7 +26,6 @@ import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; import org.sonar.db.user.UserDto; -import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; @@ -35,7 +34,6 @@ import org.sonar.server.management.ManagedInstanceChecker; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.user.NewUserNotifier; import org.sonar.server.user.UserUpdater; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; @@ -56,13 +54,11 @@ public class UpdateLoginActionIT { @Rule public DbTester db = DbTester.create(system2); @Rule - public EsTester es = EsTester.create(); - @Rule public UserSessionRule userSession = UserSessionRule.standalone().logIn().setSystemAdministrator(); private final ManagedInstanceChecker managedInstanceChecker = mock(ManagedInstanceChecker.class); private final WsActionTester ws = new WsActionTester(new UpdateLoginAction(db.getDbClient(), userSession, - new UserUpdater(mock(NewUserNotifier.class), db.getDbClient(), new UserIndexer(db.getDbClient(), es.client()), null, null, null, null), managedInstanceChecker)); + new UserUpdater(mock(NewUserNotifier.class), db.getDbClient(), null, null, null, null), managedInstanceChecker)); @Test public void update_login_from_sonarqube_account_when_user_is_local() { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/AnonymizeAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/AnonymizeAction.java index 9e61155036c..d26ffd9eeeb 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/AnonymizeAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/AnonymizeAction.java @@ -27,7 +27,6 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.user.UserDto; import org.sonar.server.user.UserSession; -import org.sonar.server.user.index.UserIndexer; import static org.sonar.server.exceptions.NotFoundException.checkFound; @@ -35,14 +34,12 @@ public class AnonymizeAction implements UsersWsAction { private static final String PARAM_LOGIN = "login"; private final DbClient dbClient; - private final UserIndexer userIndexer; private final UserSession userSession; private final UserAnonymizer userAnonymizer; - public AnonymizeAction(DbClient dbClient, UserIndexer userIndexer, UserSession userSession, UserAnonymizer userAnonymizer) { + public AnonymizeAction(DbClient dbClient, UserSession userSession, UserAnonymizer userAnonymizer) { this.userAnonymizer = userAnonymizer; this.dbClient = dbClient; - this.userIndexer = userIndexer; this.userSession = userSession; } @@ -74,7 +71,7 @@ public class AnonymizeAction implements UsersWsAction { userAnonymizer.anonymize(dbSession, user); dbClient.userDao().update(dbSession, user); - userIndexer.commitAndIndex(dbSession, user); + dbSession.commit(); } response.noContent(); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/UserDeactivator.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/UserDeactivator.java index 45126469955..6c1f438b1ab 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/UserDeactivator.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/UserDeactivator.java @@ -24,7 +24,6 @@ import org.sonar.db.DbSession; import org.sonar.db.property.PropertyQuery; import org.sonar.db.user.UserDto; import org.sonar.server.user.UserSession; -import org.sonar.server.user.index.UserIndexer; import static org.sonar.api.CoreProperties.DEFAULT_ISSUE_ASSIGNEE; import static org.sonar.db.permission.GlobalPermission.ADMINISTER; @@ -33,13 +32,11 @@ import static org.sonar.server.exceptions.NotFoundException.checkFound; public class UserDeactivator { private final DbClient dbClient; - private final UserIndexer userIndexer; private final UserSession userSession; private final UserAnonymizer userAnonymizer; - public UserDeactivator(DbClient dbClient, UserIndexer userIndexer, UserSession userSession, UserAnonymizer userAnonymizer) { + public UserDeactivator(DbClient dbClient, UserSession userSession, UserAnonymizer userAnonymizer) { this.dbClient = dbClient; - this.userIndexer = userIndexer; this.userSession = userSession; this.userAnonymizer = userAnonymizer; } @@ -98,6 +95,6 @@ public class UserDeactivator { private void deactivateUser(DbSession dbSession, UserDto user) { dbClient.userDao().deactivateUser(dbSession, user); - userIndexer.commitAndIndex(dbSession, user); + dbSession.commit(); } } diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 10fe7bc7554..7590e9108b0 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -246,9 +246,6 @@ import org.sonar.server.user.NewUserNotifier; import org.sonar.server.user.SecurityRealmFactory; import org.sonar.server.user.UserSessionFactoryImpl; import org.sonar.server.user.UserUpdater; -import org.sonar.server.user.index.UserIndex; -import org.sonar.server.user.index.UserIndexDefinition; -import org.sonar.server.user.index.UserIndexer; import org.sonar.server.user.ws.UsersWsModule; import org.sonar.server.usergroups.DefaultGroupFinder; import org.sonar.server.usergroups.ws.UserGroupsModule; @@ -397,9 +394,6 @@ public class PlatformLevel4 extends PlatformLevel { UserSessionFactoryImpl.class, SecurityRealmFactory.class, NewUserNotifier.class, - UserIndexDefinition.class, - UserIndexer.class, - UserIndex.class, UserUpdater.class, new UsersWsModule(), new UserTokenModule(), |