|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.acl; |
| 18 | + |
| 19 | +import org.apache.cloudstack.acl.ControlledEntity; |
| 20 | +import org.apache.cloudstack.acl.SecurityChecker; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.mockito.InjectMocks; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.Mockito; |
| 26 | +import org.mockito.Spy; |
| 27 | +import org.mockito.junit.MockitoJUnitRunner; |
| 28 | + |
| 29 | +import com.cloud.domain.dao.DomainDao; |
| 30 | +import com.cloud.exception.PermissionDeniedException; |
| 31 | +import com.cloud.projects.ProjectManager; |
| 32 | +import com.cloud.user.Account; |
| 33 | +import com.cloud.user.AccountService; |
| 34 | +import com.cloud.user.AccountVO; |
| 35 | +import com.cloud.user.dao.AccountDao; |
| 36 | +import com.cloud.utils.Ternary; |
| 37 | + |
| 38 | +@RunWith(MockitoJUnitRunner.class) |
| 39 | +public class DomainCheckerTest { |
| 40 | + |
| 41 | + @Mock |
| 42 | + AccountService _accountService; |
| 43 | + @Mock |
| 44 | + AccountDao _accountDao; |
| 45 | + @Mock |
| 46 | + DomainDao _domainDao; |
| 47 | + @Mock |
| 48 | + ProjectManager _projectMgr; |
| 49 | + |
| 50 | + @Spy |
| 51 | + @InjectMocks |
| 52 | + DomainChecker domainChecker; |
| 53 | + |
| 54 | + private ControlledEntity getMockedEntity(long accountId) { |
| 55 | + ControlledEntity entity = Mockito.mock(Account.class); |
| 56 | + Mockito.when(entity.getAccountId()).thenReturn(accountId); |
| 57 | + Mockito.when(entity.getEntityType()).thenReturn((Class)Account.class); |
| 58 | + return entity; |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testRootAdminHasAccess() { |
| 63 | + Account rootAdmin = Mockito.mock(Account.class); |
| 64 | + Mockito.when(rootAdmin.getId()).thenReturn(1L); |
| 65 | + ControlledEntity entity = getMockedEntity(2L); |
| 66 | + Mockito.when(_accountService.isRootAdmin(rootAdmin.getId())).thenReturn(true); |
| 67 | + |
| 68 | + domainChecker.validateCallerHasAccessToEntityOwner(rootAdmin, entity, SecurityChecker.AccessType.ModifyProject); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testCallerIsOwner() { |
| 73 | + Account caller = Mockito.mock(Account.class); |
| 74 | + Mockito.when(caller.getId()).thenReturn(1L); |
| 75 | + ControlledEntity entity = getMockedEntity(1L); |
| 76 | + |
| 77 | + domainChecker.validateCallerHasAccessToEntityOwner(caller, entity, SecurityChecker.AccessType.ModifyProject); |
| 78 | + } |
| 79 | + |
| 80 | + @Test(expected = PermissionDeniedException.class) |
| 81 | + public void testOwnerNotFound() { |
| 82 | + Account caller = Mockito.mock(Account.class); |
| 83 | + Mockito.when(caller.getId()).thenReturn(1L); |
| 84 | + ControlledEntity entity = getMockedEntity(2L); |
| 85 | + Mockito.when(_accountDao.findById(entity.getAccountId())).thenReturn(null); |
| 86 | + |
| 87 | + domainChecker.validateCallerHasAccessToEntityOwner(caller, entity, SecurityChecker.AccessType.ModifyProject); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testDomainAdminHasAccess() { |
| 92 | + Account caller = Mockito.mock(Account.class); |
| 93 | + Mockito.when(caller.getId()).thenReturn(1L); |
| 94 | + Mockito.when(caller.getDomainId()).thenReturn(100L); |
| 95 | + Mockito.when(caller.getType()).thenReturn(Account.Type.DOMAIN_ADMIN); |
| 96 | + ControlledEntity entity = getMockedEntity(2L); |
| 97 | + AccountVO owner = Mockito.mock(AccountVO.class); |
| 98 | + Mockito.when(owner.getDomainId()).thenReturn(101L); |
| 99 | + Mockito.when(_accountDao.findById(entity.getAccountId())).thenReturn(owner); |
| 100 | + Mockito.when(_domainDao.isChildDomain(100L, 101L)).thenReturn(true); |
| 101 | + |
| 102 | + domainChecker.validateCallerHasAccessToEntityOwner(caller, entity, SecurityChecker.AccessType.ModifyProject); |
| 103 | + } |
| 104 | + |
| 105 | + private Ternary<Account, ControlledEntity, AccountVO> getProjectAccessCheckResources() { |
| 106 | + Account caller = Mockito.mock(Account.class); |
| 107 | + Mockito.when(caller.getId()).thenReturn(100L); |
| 108 | + Mockito.when(caller.getType()).thenReturn(Account.Type.PROJECT); |
| 109 | + ControlledEntity entity = getMockedEntity(2L); |
| 110 | + AccountVO projectAccount = Mockito.mock(AccountVO.class); |
| 111 | + Mockito.when(projectAccount.getId()).thenReturn(2L); |
| 112 | + Mockito.when(projectAccount.getType()).thenReturn(Account.Type.PROJECT); |
| 113 | + return new Ternary<>(caller, entity, projectAccount); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testProjectOwnerCanModify() { |
| 118 | + Ternary<Account, ControlledEntity, AccountVO> resources = getProjectAccessCheckResources(); |
| 119 | + Account caller = resources.first(); |
| 120 | + ControlledEntity entity = resources.second(); |
| 121 | + AccountVO projectAccount = resources.third(); |
| 122 | + Mockito.when(_accountDao.findById(entity.getAccountId())).thenReturn(projectAccount); |
| 123 | + Mockito.when(_projectMgr.canModifyProjectAccount(caller, projectAccount.getId())).thenReturn(true); |
| 124 | + Mockito.doReturn(true).when(domainChecker).checkOperationPermitted(caller, entity); |
| 125 | + |
| 126 | + domainChecker.validateCallerHasAccessToEntityOwner(caller, entity, SecurityChecker.AccessType.ModifyProject); |
| 127 | + } |
| 128 | + |
| 129 | + @Test(expected = PermissionDeniedException.class) |
| 130 | + public void testProjectOwnerCannotModify() { |
| 131 | + Ternary<Account, ControlledEntity, AccountVO> resources = getProjectAccessCheckResources(); |
| 132 | + Account caller = resources.first(); |
| 133 | + ControlledEntity entity = resources.second(); |
| 134 | + AccountVO projectAccount = resources.third(); |
| 135 | + Mockito.when(_accountDao.findById(entity.getAccountId())).thenReturn(projectAccount); |
| 136 | + Mockito.when(_projectMgr.canModifyProjectAccount(caller, projectAccount.getId())).thenReturn(false); |
| 137 | + |
| 138 | + domainChecker.validateCallerHasAccessToEntityOwner(caller, entity, SecurityChecker.AccessType.ModifyProject); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testProjectOwnerCanAccess() { |
| 143 | + Ternary<Account, ControlledEntity, AccountVO> resources = getProjectAccessCheckResources(); |
| 144 | + Account caller = resources.first(); |
| 145 | + ControlledEntity entity = resources.second(); |
| 146 | + AccountVO projectAccount = resources.third(); |
| 147 | + Mockito.when(_accountDao.findById(entity.getAccountId())).thenReturn(projectAccount); |
| 148 | + Mockito.when(_projectMgr.canAccessProjectAccount(caller, projectAccount.getId())).thenReturn(true); |
| 149 | + Mockito.doReturn(true).when(domainChecker).checkOperationPermitted(caller, entity); |
| 150 | + |
| 151 | + domainChecker.validateCallerHasAccessToEntityOwner(caller, entity, SecurityChecker.AccessType.ListEntry); |
| 152 | + } |
| 153 | + |
| 154 | + @Test(expected = PermissionDeniedException.class) |
| 155 | + public void testProjectOwnerCannotAccess() { |
| 156 | + Ternary<Account, ControlledEntity, AccountVO> resources = getProjectAccessCheckResources(); |
| 157 | + Account caller = resources.first(); |
| 158 | + ControlledEntity entity = resources.second(); |
| 159 | + AccountVO projectAccount = resources.third(); |
| 160 | + Mockito.when(_accountDao.findById(entity.getAccountId())).thenReturn(projectAccount); |
| 161 | + Mockito.when(_projectMgr.canAccessProjectAccount(caller, projectAccount.getId())).thenReturn(false); |
| 162 | + |
| 163 | + domainChecker.validateCallerHasAccessToEntityOwner(caller, entity, SecurityChecker.AccessType.ListEntry); |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments