Skip to content

Commit d876641

Browse files
shwstpprharikrishna-patnala
authored andcommitted
extensions: custom action entity access
1 parent e7a55a7 commit d876641

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

framework/extensions/src/main/java/org/apache/cloudstack/framework/extensions/manager/ExtensionsManagerImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
import com.cloud.serializer.GsonHelper;
130130
import com.cloud.storage.dao.VMTemplateDao;
131131
import com.cloud.user.Account;
132+
import com.cloud.user.AccountService;
132133
import com.cloud.utils.Pair;
133134
import com.cloud.utils.component.ManagerBase;
134135
import com.cloud.utils.component.PluggableService;
@@ -212,6 +213,9 @@ public class ExtensionsManagerImpl extends ManagerBase implements ExtensionsMana
212213
@Inject
213214
RoleService roleService;
214215

216+
@Inject
217+
AccountService accountService;
218+
215219
private ScheduledExecutorService extensionPathStateCheckExecutor;
216220

217221
protected String getDefaultExtensionRelativePath(String name) {
@@ -1354,6 +1358,7 @@ public CustomActionResultResponse runCustomAction(RunCustomActionCmd cmd) {
13541358
clusterId = host.getClusterId();
13551359
} else if (entity instanceof VirtualMachine) {
13561360
VirtualMachine virtualMachine = (VirtualMachine)entity;
1361+
accountService.checkAccess(caller, null, true, virtualMachine);
13571362
if (!Hypervisor.HypervisorType.External.equals(virtualMachine.getHypervisorType())) {
13581363
logger.error("Invalid {} specified as VM resource for running {}", entity, customActionVO);
13591364
throw new InvalidParameterValueException(error);

framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/manager/ExtensionsManagerImplTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import java.util.Map;
5050
import java.util.UUID;
5151

52+
import com.cloud.exception.PermissionDeniedException;
53+
import com.cloud.user.AccountService;
5254
import org.apache.cloudstack.acl.Role;
5355
import org.apache.cloudstack.acl.RoleService;
5456
import org.apache.cloudstack.acl.RoleType;
@@ -176,6 +178,8 @@ public class ExtensionsManagerImplTest {
176178
private VMTemplateDao templateDao;
177179
@Mock
178180
private RoleService roleService;
181+
@Mock
182+
private AccountService accountService;
179183

180184
@Before
181185
public void setUp() {
@@ -1640,6 +1644,35 @@ public void runCustomAction_ExecutionThrowsException() throws Exception {
16401644
}
16411645
}
16421646

1647+
@Test(expected = PermissionDeniedException.class)
1648+
public void runCustomAction_CheckAccessThrowsException() throws Exception {
1649+
RunCustomActionCmd cmd = mock(RunCustomActionCmd.class);
1650+
when(cmd.getCustomActionId()).thenReturn(1L);
1651+
when(cmd.getResourceId()).thenReturn("vm-123");
1652+
when(cmd.getParameters()).thenReturn(Map.of("param1", "value1"));
1653+
1654+
ExtensionCustomActionVO actionVO = mock(ExtensionCustomActionVO.class);
1655+
when(extensionCustomActionDao.findById(1L)).thenReturn(actionVO);
1656+
when(actionVO.isEnabled()).thenReturn(true);
1657+
when(actionVO.getResourceType()).thenReturn(ExtensionCustomAction.ResourceType.VirtualMachine);
1658+
when(actionVO.getAllowedRoleTypes()).thenReturn(RoleType.toCombinedMask(List.of(RoleType.Admin, RoleType.DomainAdmin, RoleType.User)));
1659+
1660+
ExtensionVO extensionVO = mock(ExtensionVO.class);
1661+
when(extensionDao.findById(anyLong())).thenReturn(extensionVO);
1662+
when(extensionVO.getState()).thenReturn(Extension.State.Enabled);
1663+
1664+
VirtualMachine vm = mock(VirtualMachine.class);
1665+
when(entityManager.findByUuid(eq(VirtualMachine.class), anyString())).thenReturn(vm);
1666+
doThrow(PermissionDeniedException.class).when(accountService).checkAccess(any(Account.class), eq(null), eq(true), eq(vm));
1667+
1668+
try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) {
1669+
mockCallerRole(RoleType.User);
1670+
CustomActionResultResponse result = extensionsManager.runCustomAction(cmd);
1671+
1672+
assertFalse(result.getSuccess());
1673+
}
1674+
}
1675+
16431676
@Test
16441677
public void createCustomActionResponse_SetsBasicFields() {
16451678
ExtensionCustomAction action = mock(ExtensionCustomAction.class);

0 commit comments

Comments
 (0)