3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.permission.index;
22 import java.util.Collection;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.utils.System2;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.component.ProjectData;
29 import org.sonar.db.entity.EntityDto;
30 import org.sonar.db.es.EsQueueDto;
31 import org.sonar.db.portfolio.PortfolioDto;
32 import org.sonar.db.project.ProjectDto;
33 import org.sonar.db.user.GroupDto;
34 import org.sonar.db.user.UserDto;
35 import org.sonar.server.es.EsTester;
36 import org.sonar.server.es.IndexType;
37 import org.sonar.server.es.IndexType.IndexMainType;
38 import org.sonar.server.es.Indexers.EntityEvent;
39 import org.sonar.server.es.IndexingResult;
40 import org.sonar.server.tester.UserSessionRule;
42 import static java.util.Arrays.asList;
43 import static java.util.Collections.singletonList;
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.sonar.api.resources.Qualifiers.PROJECT;
46 import static org.sonar.api.web.UserRole.ADMIN;
47 import static org.sonar.api.web.UserRole.USER;
48 import static org.sonar.server.es.Indexers.EntityEvent.PERMISSION_CHANGE;
49 import static org.sonar.server.permission.index.IndexAuthorizationConstants.TYPE_AUTHORIZATION;
51 public class PermissionIndexerTest {
53 private static final IndexMainType INDEX_TYPE_FOO_AUTH = IndexType.main(FooIndexDefinition.DESCRIPTOR, TYPE_AUTHORIZATION);
56 public DbTester db = DbTester.create(System2.INSTANCE);
58 public EsTester es = EsTester.createCustom(new FooIndexDefinition());
60 public UserSessionRule userSession = UserSessionRule.standalone();
62 private FooIndex fooIndex = new FooIndex(es.client(), new WebAuthorizationTypeSupport(userSession));
63 private FooIndexer fooIndexer = new FooIndexer(es.client(), db.getDbClient());
64 private PermissionIndexer underTest = new PermissionIndexer(db.getDbClient(), es.client(), fooIndexer);
67 public void indexOnStartup_grants_access_to_any_user_and_to_group_Anyone_on_public_projects() {
68 ProjectDto project = createAndIndexPublicProject();
69 UserDto user1 = db.users().insertUser();
70 UserDto user2 = db.users().insertUser();
74 verifyAnyoneAuthorized(project);
75 verifyAuthorized(project, user1);
76 verifyAuthorized(project, user2);
80 public void indexAll_grants_access_to_any_user_and_to_group_Anyone_on_public_projects() {
81 ProjectDto project = createAndIndexPublicProject();
82 UserDto user1 = db.users().insertUser();
83 UserDto user2 = db.users().insertUser();
85 underTest.indexAll(underTest.getIndexTypes());
87 verifyAnyoneAuthorized(project);
88 verifyAuthorized(project, user1);
89 verifyAuthorized(project, user2);
93 public void deletion_resilience_will_deindex_projects() {
94 ProjectDto project1 = createUnindexedPublicProject();
95 ProjectDto project2 = createUnindexedPublicProject();
96 // UserDto user1 = db.users().insertUser();
98 assertThat(es.countDocuments(INDEX_TYPE_FOO_AUTH)).isEqualTo(2);
100 // Simulate a indexation issue
101 db.getDbClient().purgeDao().deleteProject(db.getSession(), project1.getUuid(), PROJECT, project1.getName(), project1.getKey());
102 underTest.prepareForRecoveryOnEntityEvent(db.getSession(), asList(project1.getUuid()), EntityEvent.DELETION);
103 assertThat(db.countRowsOfTable(db.getSession(), "es_queue")).isOne();
104 Collection<EsQueueDto> esQueueDtos = db.getDbClient().esQueueDao().selectForRecovery(db.getSession(), Long.MAX_VALUE, 2);
106 underTest.index(db.getSession(), esQueueDtos);
108 assertThat(db.countRowsOfTable(db.getSession(), "es_queue")).isZero();
109 assertThat(es.countDocuments(INDEX_TYPE_FOO_AUTH)).isOne();
113 public void indexOnStartup_grants_access_to_user() {
114 ProjectDto project = createAndIndexPrivateProject();
115 UserDto user1 = db.users().insertUser();
116 UserDto user2 = db.users().insertUser();
117 db.users().insertProjectPermissionOnUser(user1, USER, project);
118 db.users().insertProjectPermissionOnUser(user2, ADMIN, project);
123 verifyAnyoneNotAuthorized(project);
126 verifyAuthorized(project, user1);
128 // user2 has not access (only USER permission is accepted)
129 verifyNotAuthorized(project, user2);
133 public void indexOnStartup_grants_access_to_group_on_private_project() {
134 ProjectDto project = createAndIndexPrivateProject();
135 UserDto user1 = db.users().insertUser();
136 UserDto user2 = db.users().insertUser();
137 UserDto user3 = db.users().insertUser();
138 GroupDto group1 = db.users().insertGroup();
139 GroupDto group2 = db.users().insertGroup();
140 db.users().insertEntityPermissionOnGroup(group1, USER, project);
141 db.users().insertEntityPermissionOnGroup(group2, ADMIN, project);
146 verifyAnyoneNotAuthorized(project);
149 verifyAuthorized(project, user1, group1);
151 // group2 has not access (only USER permission is accepted)
152 verifyNotAuthorized(project, user2, group2);
154 // user3 is not in any group
155 verifyNotAuthorized(project, user3);
159 public void indexOnStartup_grants_access_to_user_and_group() {
160 ProjectDto project = createAndIndexPrivateProject();
161 UserDto user1 = db.users().insertUser();
162 UserDto user2 = db.users().insertUser();
163 GroupDto group = db.users().insertGroup();
164 db.users().insertMember(group, user2);
165 db.users().insertProjectPermissionOnUser(user1, USER, project);
166 db.users().insertEntityPermissionOnGroup(group, USER, project);
171 verifyAnyoneNotAuthorized(project);
174 verifyAuthorized(project, user1);
176 // has access through group
177 verifyAuthorized(project, user1, group);
180 verifyNotAuthorized(project, user2);
184 public void indexOnStartup_does_not_grant_access_to_anybody_on_private_project() {
185 ProjectDto project = createAndIndexPrivateProject();
186 UserDto user = db.users().insertUser();
187 GroupDto group = db.users().insertGroup();
191 verifyAnyoneNotAuthorized(project);
192 verifyNotAuthorized(project, user);
193 verifyNotAuthorized(project, user, group);
197 public void indexOnStartup_grants_access_to_anybody_on_public_project() {
198 ProjectDto project = createAndIndexPublicProject();
199 UserDto user = db.users().insertUser();
200 GroupDto group = db.users().insertGroup();
204 verifyAnyoneAuthorized(project);
205 verifyAuthorized(project, user);
206 verifyAuthorized(project, user, group);
210 public void indexOnStartup_grants_access_to_anybody_on_view() {
211 PortfolioDto view = createAndIndexPortfolio();
212 UserDto user = db.users().insertUser();
213 GroupDto group = db.users().insertGroup();
217 verifyAnyoneAuthorized(view);
218 verifyAuthorized(view, user);
219 verifyAuthorized(view, user, group);
223 public void indexOnStartup_grants_access_on_many_projects() {
224 UserDto user1 = db.users().insertUser();
225 UserDto user2 = db.users().insertUser();
226 ProjectDto project = null;
227 for (int i = 0; i < 10; i++) {
228 project = createAndIndexPrivateProject();
229 db.users().insertProjectPermissionOnUser(user1, USER, project);
234 verifyAnyoneNotAuthorized(project);
235 verifyAuthorized(project, user1);
236 verifyNotAuthorized(project, user2);
240 public void public_projects_are_visible_to_anybody() {
241 ProjectDto projectOnOrg1 = createAndIndexPublicProject();
242 UserDto user = db.users().insertUser();
246 verifyAnyoneAuthorized(projectOnOrg1);
247 verifyAuthorized(projectOnOrg1, user);
251 public void permissions_are_not_updated_on_project_tags_update() {
252 ProjectDto project = createAndIndexPublicProject();
254 indexPermissions(project, EntityEvent.PROJECT_TAGS_UPDATE);
256 assertThatAuthIndexHasSize(0);
257 verifyAnyoneNotAuthorized(project);
261 public void permissions_are_not_updated_on_project_key_update() {
262 ProjectDto project = createAndIndexPublicProject();
264 indexPermissions(project, EntityEvent.PROJECT_TAGS_UPDATE);
266 assertThatAuthIndexHasSize(0);
267 verifyAnyoneNotAuthorized(project);
271 public void index_permissions_on_project_creation() {
272 ProjectDto project = createAndIndexPrivateProject();
273 UserDto user = db.users().insertUser();
274 db.users().insertProjectPermissionOnUser(user, USER, project);
276 indexPermissions(project, EntityEvent.CREATION);
278 assertThatAuthIndexHasSize(1);
279 verifyAuthorized(project, user);
283 public void index_permissions_on_permission_change() {
284 ProjectDto project = createAndIndexPrivateProject();
285 UserDto user1 = db.users().insertUser();
286 UserDto user2 = db.users().insertUser();
287 db.users().insertProjectPermissionOnUser(user1, USER, project);
288 indexPermissions(project, EntityEvent.CREATION);
289 verifyAuthorized(project, user1);
290 verifyNotAuthorized(project, user2);
292 db.users().insertProjectPermissionOnUser(user2, USER, project);
293 indexPermissions(project, PERMISSION_CHANGE);
295 verifyAuthorized(project, user1);
296 verifyAuthorized(project, user1);
300 public void delete_permissions_on_project_deletion() {
301 ProjectDto project = createAndIndexPrivateProject();
302 UserDto user = db.users().insertUser();
303 db.users().insertProjectPermissionOnUser(user, USER, project);
304 indexPermissions(project, EntityEvent.CREATION);
305 verifyAuthorized(project, user);
307 db.getDbClient().purgeDao().deleteProject(db.getSession(), project.getUuid(), PROJECT, project.getUuid(), project.getKey());
308 indexPermissions(project, EntityEvent.DELETION);
310 verifyNotAuthorized(project, user);
311 assertThatAuthIndexHasSize(0);
315 public void errors_during_indexing_are_recovered() {
316 ProjectDto project = createAndIndexPublicProject();
317 es.lockWrites(INDEX_TYPE_FOO_AUTH);
319 IndexingResult result = indexPermissions(project, PERMISSION_CHANGE);
320 assertThat(result.getTotal()).isOne();
321 assertThat(result.getFailures()).isOne();
323 // index is still read-only, fail to recover
325 assertThat(result.getTotal()).isOne();
326 assertThat(result.getFailures()).isOne();
327 assertThatAuthIndexHasSize(0);
328 assertThatEsQueueTableHasSize(1);
330 es.unlockWrites(INDEX_TYPE_FOO_AUTH);
333 assertThat(result.getTotal()).isOne();
334 assertThat(result.getFailures()).isZero();
335 verifyAnyoneAuthorized(project);
336 assertThatEsQueueTableHasSize(0);
339 private void assertThatAuthIndexHasSize(int expectedSize) {
340 assertThat(es.countDocuments(FooIndexDefinition.TYPE_AUTHORIZATION)).isEqualTo(expectedSize);
343 private void indexOnStartup() {
344 underTest.indexOnStartup(underTest.getIndexTypes());
347 private void verifyAuthorized(EntityDto entity, UserDto user) {
349 verifyAuthorized(entity, true);
352 private void verifyAuthorized(EntityDto entity, UserDto user, GroupDto group) {
353 logIn(user).setGroups(group);
354 verifyAuthorized(entity, true);
357 private void verifyNotAuthorized(EntityDto entity, UserDto user) {
359 verifyAuthorized(entity, false);
362 private void verifyNotAuthorized(EntityDto entity, UserDto user, GroupDto group) {
363 logIn(user).setGroups(group);
364 verifyAuthorized(entity, false);
367 private void verifyAnyoneAuthorized(EntityDto entity) {
368 userSession.anonymous();
369 verifyAuthorized(entity, true);
372 private void verifyAnyoneNotAuthorized(EntityDto entity) {
373 userSession.anonymous();
374 verifyAuthorized(entity, false);
377 private void verifyAuthorized(EntityDto entity, boolean expectedAccess) {
378 assertThat(fooIndex.hasAccessToProject(entity.getUuid())).isEqualTo(expectedAccess);
381 private UserSessionRule logIn(UserDto u) {
382 userSession.logIn(u);
386 private IndexingResult indexPermissions(EntityDto entity, EntityEvent cause) {
387 DbSession dbSession = db.getSession();
388 Collection<EsQueueDto> items = underTest.prepareForRecoveryOnEntityEvent(dbSession, singletonList(entity.getUuid()), cause);
390 return underTest.index(dbSession, items);
393 private ProjectDto createUnindexedPublicProject() {
394 return db.components().insertPublicProject().getProjectDto();
397 private ProjectDto createAndIndexPrivateProject() {
398 ProjectData project = db.components().insertPrivateProject();
399 fooIndexer.indexOnAnalysis(project.getMainBranchDto().getUuid());
400 return project.getProjectDto();
403 private ProjectDto createAndIndexPublicProject() {
404 ProjectData project = db.components().insertPublicProject();
405 fooIndexer.indexOnAnalysis(project.getMainBranchDto().getUuid());
406 return project.getProjectDto();
409 private PortfolioDto createAndIndexPortfolio() {
410 PortfolioDto view = db.components().insertPublicPortfolioDto();
411 fooIndexer.indexOnAnalysis(view.getUuid());
415 private IndexingResult recover() {
416 Collection<EsQueueDto> items = db.getDbClient().esQueueDao().selectForRecovery(db.getSession(), System.currentTimeMillis() + 1_000L, 10);
417 return underTest.index(db.getSession(), items);
420 private void assertThatEsQueueTableHasSize(int expectedSize) {
421 assertThat(db.countRowsOfTable("es_queue")).isEqualTo(expectedSize);