From 82b4add8238771d95773ca99f2f4beae4270a8cb Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Wed, 22 Jul 2026 17:15:28 +0200 Subject: [PATCH 1/7] conditional error reporting for vm move failure --- api/src/main/java/com/cloud/vm/UserVmService.java | 6 ++++++ .../apache/cloudstack/api/command/admin/vm/AssignVMCmd.java | 4 ++++ server/src/main/java/com/cloud/vm/UserVmManagerImpl.java | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/com/cloud/vm/UserVmService.java b/api/src/main/java/com/cloud/vm/UserVmService.java index dc9e8c1f0d8c..184fbd83c30b 100644 --- a/api/src/main/java/com/cloud/vm/UserVmService.java +++ b/api/src/main/java/com/cloud/vm/UserVmService.java @@ -40,6 +40,7 @@ import org.apache.cloudstack.api.command.user.vm.UpgradeVMCmd; import org.apache.cloudstack.api.command.user.vmgroup.CreateVMGroupCmd; import org.apache.cloudstack.api.command.user.vmgroup.DeleteVMGroupCmd; +import org.apache.cloudstack.framework.config.ConfigKey; import com.cloud.dc.DataCenter; import com.cloud.deploy.DeploymentPlanner; @@ -64,6 +65,11 @@ public interface UserVmService { + ConfigKey AllowExposingVmAssignFailureDetails = new ConfigKey<>("Advanced", Boolean.class, + "vm.assign.failure.detailed.message.enabled", "true", + "If true, the reason a VM ownership change (assignVirtualMachine) fails, e.g. remaining port forwarding rules or volume snapshots, is returned to the API caller. " + + "If false, only a generic error message referencing the Instance ID is returned, and the detailed reason is only available in the management server logs.", + true, ConfigKey.Scope.Global); /** * Destroys one virtual machine diff --git a/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java index e11d20d06466..dd726c339a88 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java @@ -36,6 +36,7 @@ import com.cloud.exception.InvalidParameterValueException; import com.cloud.user.Account; import com.cloud.uservm.UserVm; +import com.cloud.vm.UserVmService; import com.cloud.vm.VirtualMachine; @APICommand(name = "assignVirtualMachine", @@ -128,6 +129,9 @@ public void execute() { ApiErrorCode errorCode = e instanceof InvalidParameterValueException ? ApiErrorCode.PARAM_ERROR : ApiErrorCode.INTERNAL_ERROR; String msg = String.format("Failed to move Instance due to [%s].", getVmId()); logger.error(msg, e); + if (e instanceof InvalidParameterValueException && UserVmService.AllowExposingVmAssignFailureDetails.value()) { + msg = e.getMessage(); + } throw new ServerApiException(errorCode, msg); } } diff --git a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java index 54ba6f0e872b..470f60c5a0aa 100644 --- a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java @@ -8889,7 +8889,7 @@ public ConfigKey[] getConfigKeys() { return new ConfigKey[] {EnableDynamicallyScaleVm, AllowDiskOfferingChangeDuringScaleVm, AllowUserExpungeRecoverVm, VmIpFetchWaitInterval, VmIpFetchTrialMax, VmIpFetchThreadPoolMax, VmIpFetchTaskWorkers, AllowDeployVmIfGivenHostFails, EnableAdditionalVmConfig, DisplayVMOVFProperties, KvmAdditionalConfigAllowList, XenServerAdditionalConfigAllowList, VmwareAdditionalConfigAllowList, DestroyRootVolumeOnVmDestruction, - EnforceStrictResourceLimitHostTagCheck, StrictHostTags, AllowUserForceStopVm}; + EnforceStrictResourceLimitHostTagCheck, StrictHostTags, AllowUserForceStopVm, AllowExposingVmAssignFailureDetails}; } @Override From 94bae2c7104346bab169e42b0640d0fa350ff69d Mon Sep 17 00:00:00 2001 From: dahn Date: Tue, 4 Aug 2026 11:58:15 +0200 Subject: [PATCH 2/7] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- api/src/main/java/com/cloud/vm/UserVmService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/com/cloud/vm/UserVmService.java b/api/src/main/java/com/cloud/vm/UserVmService.java index 184fbd83c30b..13d38490bee2 100644 --- a/api/src/main/java/com/cloud/vm/UserVmService.java +++ b/api/src/main/java/com/cloud/vm/UserVmService.java @@ -66,7 +66,7 @@ public interface UserVmService { ConfigKey AllowExposingVmAssignFailureDetails = new ConfigKey<>("Advanced", Boolean.class, - "vm.assign.failure.detailed.message.enabled", "true", + "vm.assign.failure.detailed.message.enabled", "false", "If true, the reason a VM ownership change (assignVirtualMachine) fails, e.g. remaining port forwarding rules or volume snapshots, is returned to the API caller. " + "If false, only a generic error message referencing the Instance ID is returned, and the detailed reason is only available in the management server logs.", true, ConfigKey.Scope.Global); From cfaf294e5bbe0df837804d29e20368ddee344c18 Mon Sep 17 00:00:00 2001 From: dahn Date: Tue, 4 Aug 2026 12:00:41 +0200 Subject: [PATCH 3/7] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../apache/cloudstack/api/command/admin/vm/AssignVMCmd.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java index dd726c339a88..7ad17b634fef 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java @@ -127,10 +127,10 @@ public void execute() { setResponseObject(response); } catch (Exception e) { ApiErrorCode errorCode = e instanceof InvalidParameterValueException ? ApiErrorCode.PARAM_ERROR : ApiErrorCode.INTERNAL_ERROR; - String msg = String.format("Failed to move Instance due to [%s].", getVmId()); + String msg = String.format("Failed to move Instance [%s].", getVmId()); logger.error(msg, e); if (e instanceof InvalidParameterValueException && UserVmService.AllowExposingVmAssignFailureDetails.value()) { - msg = e.getMessage(); + msg = String.format("Failed to move Instance [%s]: %s", getVmId(), e.getMessage()); } throw new ServerApiException(errorCode, msg); } From 7b4d5c901aca1f8978da68121f179d378e7f1f6e Mon Sep 17 00:00:00 2001 From: dahn Date: Tue, 4 Aug 2026 19:31:16 +0200 Subject: [PATCH 4/7] Apply suggestion from @DaanHoogland --- .../org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java index 7ad17b634fef..c9ba04858e8a 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java @@ -129,7 +129,7 @@ public void execute() { ApiErrorCode errorCode = e instanceof InvalidParameterValueException ? ApiErrorCode.PARAM_ERROR : ApiErrorCode.INTERNAL_ERROR; String msg = String.format("Failed to move Instance [%s].", getVmId()); logger.error(msg, e); - if (e instanceof InvalidParameterValueException && UserVmService.AllowExposingVmAssignFailureDetails.value()) { + if (e instanceof InvalidParameterValueException && Boolean.TRUE.equals(UserVmService.AllowExposingVmAssignFailureDetails.value())) { msg = String.format("Failed to move Instance [%s]: %s", getVmId(), e.getMessage()); } throw new ServerApiException(errorCode, msg); From ad6abcc9e9c2b280002d812602bc08ab50352b99 Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Wed, 5 Aug 2026 17:02:35 +0200 Subject: [PATCH 5/7] tests --- .../api/command/admin/vm/AssignVMCmdTest.java | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 api/src/test/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmdTest.java diff --git a/api/src/test/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmdTest.java b/api/src/test/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmdTest.java new file mode 100644 index 000000000000..548cb9b56cda --- /dev/null +++ b/api/src/test/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmdTest.java @@ -0,0 +1,109 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.vm; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.framework.config.ConfigKey; +import org.apache.cloudstack.framework.config.impl.ConfigDepotImpl; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.vm.UserVmService; + +public class AssignVMCmdTest { + + private AssignVMCmd assignVMCmd; + private UserVmService userVmService; + private ConfigDepotImpl configDepot; + + @Before + public void setUp() { + assignVMCmd = new AssignVMCmd() { + @Override + public Long getVmId() { + return 1L; + } + }; + + userVmService = mock(UserVmService.class); + assignVMCmd._userVmService = userVmService; + + configDepot = mock(ConfigDepotImpl.class); + ConfigKey.init(configDepot); + } + + @After + public void tearDown() { + ConfigKey.init(null); + } + + private void setDetailedFailureMessageFlag(boolean enabled) { + when(configDepot.getConfigStringValue(UserVmService.AllowExposingVmAssignFailureDetails.key(), + ConfigKey.Scope.Global, null)).thenReturn(Boolean.toString(enabled)); + } + + @Test + public void testExecuteReturnsGenericMessageWhenDetailsDisabled() throws Exception { + setDetailedFailureMessageFlag(false); + when(userVmService.moveVmToUser(assignVMCmd)).thenThrow(new InvalidParameterValueException("account over resource limit")); + + try { + assignVMCmd.execute(); + fail("Expected a ServerApiException to be thrown"); + } catch (ServerApiException e) { + assertEquals(ApiErrorCode.PARAM_ERROR, e.getErrorCode()); + assertEquals("Failed to move Instance [1].", e.getDescription()); + } + } + + @Test + public void testExecuteReturnsDetailedMessageWhenDetailsEnabledAndParamInvalid() throws Exception { + setDetailedFailureMessageFlag(true); + when(userVmService.moveVmToUser(assignVMCmd)).thenThrow(new InvalidParameterValueException("account over resource limit")); + + try { + assignVMCmd.execute(); + fail("Expected a ServerApiException to be thrown"); + } catch (ServerApiException e) { + assertEquals(ApiErrorCode.PARAM_ERROR, e.getErrorCode()); + assertEquals("Failed to move Instance [1]: account over resource limit", e.getDescription()); + } + } + + @Test + public void testExecuteReturnsGenericMessageForNonParamExceptionEvenWhenDetailsEnabled() throws Exception { + setDetailedFailureMessageFlag(true); + when(userVmService.moveVmToUser(assignVMCmd)).thenThrow(new ResourceUnavailableException("host unreachable", AssignVMCmd.class, 1L)); + + try { + assignVMCmd.execute(); + fail("Expected a ServerApiException to be thrown"); + } catch (ServerApiException e) { + assertEquals(ApiErrorCode.INTERNAL_ERROR, e.getErrorCode()); + assertEquals("Failed to move Instance [1].", e.getDescription()); + } + } +} From cde62086b352d3ca24cafa19767e73ae065cf40a Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Thu, 12 Nov 2026 10:51:18 +0100 Subject: [PATCH 6/7] Use Instance UUID in assignVirtualMachine failure messages --- .../api/command/admin/vm/AssignVMCmd.java | 9 +++++++-- .../api/command/admin/vm/AssignVMCmdTest.java | 14 +++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java index c9ba04858e8a..6069bcd50105 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java @@ -94,6 +94,10 @@ public Long getVmId() { return virtualMachineId; } + private String getVmUUID() { + return _uuidMgr.getUuid(VirtualMachine.class, getVmId()); + } + public String getAccountName() { return accountName; } @@ -127,10 +131,11 @@ public void execute() { setResponseObject(response); } catch (Exception e) { ApiErrorCode errorCode = e instanceof InvalidParameterValueException ? ApiErrorCode.PARAM_ERROR : ApiErrorCode.INTERNAL_ERROR; - String msg = String.format("Failed to move Instance [%s].", getVmId()); + String vmUuid = getVmUUID(); + String msg = String.format("Failed to move Instance [%s].", vmUuid); logger.error(msg, e); if (e instanceof InvalidParameterValueException && Boolean.TRUE.equals(UserVmService.AllowExposingVmAssignFailureDetails.value())) { - msg = String.format("Failed to move Instance [%s]: %s", getVmId(), e.getMessage()); + msg = String.format("Failed to move Instance [%s]: %s", vmUuid, e.getMessage()); } throw new ServerApiException(errorCode, msg); } diff --git a/api/src/test/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmdTest.java b/api/src/test/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmdTest.java index 548cb9b56cda..695a3c313fc6 100644 --- a/api/src/test/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmdTest.java +++ b/api/src/test/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmdTest.java @@ -31,10 +31,14 @@ import com.cloud.exception.InvalidParameterValueException; import com.cloud.exception.ResourceUnavailableException; +import com.cloud.utils.db.UUIDManager; import com.cloud.vm.UserVmService; +import com.cloud.vm.VirtualMachine; public class AssignVMCmdTest { + private static final String VM_UUID = "8f6c1c9e-3a2b-4d5e-9f01-23456789abcd"; + private AssignVMCmd assignVMCmd; private UserVmService userVmService; private ConfigDepotImpl configDepot; @@ -51,6 +55,10 @@ public Long getVmId() { userVmService = mock(UserVmService.class); assignVMCmd._userVmService = userVmService; + UUIDManager uuidMgr = mock(UUIDManager.class); + when(uuidMgr.getUuid(VirtualMachine.class, 1L)).thenReturn(VM_UUID); + assignVMCmd._uuidMgr = uuidMgr; + configDepot = mock(ConfigDepotImpl.class); ConfigKey.init(configDepot); } @@ -75,7 +83,7 @@ public void testExecuteReturnsGenericMessageWhenDetailsDisabled() throws Excepti fail("Expected a ServerApiException to be thrown"); } catch (ServerApiException e) { assertEquals(ApiErrorCode.PARAM_ERROR, e.getErrorCode()); - assertEquals("Failed to move Instance [1].", e.getDescription()); + assertEquals("Failed to move Instance [" + VM_UUID + "].", e.getDescription()); } } @@ -89,7 +97,7 @@ public void testExecuteReturnsDetailedMessageWhenDetailsEnabledAndParamInvalid() fail("Expected a ServerApiException to be thrown"); } catch (ServerApiException e) { assertEquals(ApiErrorCode.PARAM_ERROR, e.getErrorCode()); - assertEquals("Failed to move Instance [1]: account over resource limit", e.getDescription()); + assertEquals("Failed to move Instance [" + VM_UUID + "]: account over resource limit", e.getDescription()); } } @@ -103,7 +111,7 @@ public void testExecuteReturnsGenericMessageForNonParamExceptionEvenWhenDetailsE fail("Expected a ServerApiException to be thrown"); } catch (ServerApiException e) { assertEquals(ApiErrorCode.INTERNAL_ERROR, e.getErrorCode()); - assertEquals("Failed to move Instance [1].", e.getDescription()); + assertEquals("Failed to move Instance [" + VM_UUID + "].", e.getDescription()); } } } From b3d4005921408ac0879cf2f0202ed5dc6bc10387 Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Tue, 1 Dec 2026 11:32:14 +0100 Subject: [PATCH 7/7] reuse expose.errors.to.user instead of a new setting --- .../configuration/ConfigurationService.java | 4 + .../main/java/com/cloud/vm/UserVmService.java | 6 -- .../api/command/admin/vm/AssignVMCmd.java | 10 ++- .../api/command/admin/vm/AssignVMCmdTest.java | 73 ++++++++++++------- .../ConfigurationManagerImpl.java | 2 +- .../java/com/cloud/vm/UserVmManagerImpl.java | 2 +- 6 files changed, 60 insertions(+), 37 deletions(-) diff --git a/api/src/main/java/com/cloud/configuration/ConfigurationService.java b/api/src/main/java/com/cloud/configuration/ConfigurationService.java index 97d4b42974b3..c128f0da7569 100644 --- a/api/src/main/java/com/cloud/configuration/ConfigurationService.java +++ b/api/src/main/java/com/cloud/configuration/ConfigurationService.java @@ -51,6 +51,7 @@ import org.apache.cloudstack.api.command.admin.zone.UpdateZoneCmd; import org.apache.cloudstack.api.command.user.network.ListNetworkOfferingsCmd; import org.apache.cloudstack.config.Configuration; +import org.apache.cloudstack.framework.config.ConfigKey; import org.apache.cloudstack.region.PortableIp; import org.apache.cloudstack.region.PortableIpRange; @@ -73,6 +74,9 @@ public interface ConfigurationService { + ConfigKey EXPOSE_ERRORS_TO_USER = new ConfigKey<>(Boolean.class, "expose.errors.to.user", ConfigKey.CATEGORY_ADVANCED, + "false", "If set to true, detailed error messages will be returned to all user roles. If false, detailed errors are only shown to admin users", true, ConfigKey.Scope.Global, null); + /** * Updates a configuration entry with a new value * diff --git a/api/src/main/java/com/cloud/vm/UserVmService.java b/api/src/main/java/com/cloud/vm/UserVmService.java index 13d38490bee2..dc9e8c1f0d8c 100644 --- a/api/src/main/java/com/cloud/vm/UserVmService.java +++ b/api/src/main/java/com/cloud/vm/UserVmService.java @@ -40,7 +40,6 @@ import org.apache.cloudstack.api.command.user.vm.UpgradeVMCmd; import org.apache.cloudstack.api.command.user.vmgroup.CreateVMGroupCmd; import org.apache.cloudstack.api.command.user.vmgroup.DeleteVMGroupCmd; -import org.apache.cloudstack.framework.config.ConfigKey; import com.cloud.dc.DataCenter; import com.cloud.deploy.DeploymentPlanner; @@ -65,11 +64,6 @@ public interface UserVmService { - ConfigKey AllowExposingVmAssignFailureDetails = new ConfigKey<>("Advanced", Boolean.class, - "vm.assign.failure.detailed.message.enabled", "false", - "If true, the reason a VM ownership change (assignVirtualMachine) fails, e.g. remaining port forwarding rules or volume snapshots, is returned to the API caller. " + - "If false, only a generic error message referencing the Instance ID is returned, and the detailed reason is only available in the management server logs.", - true, ConfigKey.Scope.Global); /** * Destroys one virtual machine diff --git a/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java index 6069bcd50105..63a88742f231 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java @@ -32,11 +32,12 @@ import org.apache.cloudstack.api.response.ProjectResponse; import org.apache.cloudstack.api.response.SecurityGroupResponse; import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; +import com.cloud.configuration.ConfigurationService; import com.cloud.exception.InvalidParameterValueException; import com.cloud.user.Account; import com.cloud.uservm.UserVm; -import com.cloud.vm.UserVmService; import com.cloud.vm.VirtualMachine; @APICommand(name = "assignVirtualMachine", @@ -94,6 +95,11 @@ public Long getVmId() { return virtualMachineId; } + private boolean isErrorDetailExposedToCaller() { + Account caller = CallContext.current().getCallingAccount(); + return (caller != null && caller.getType() == Account.Type.ADMIN) || Boolean.TRUE.equals(ConfigurationService.EXPOSE_ERRORS_TO_USER.value()); + } + private String getVmUUID() { return _uuidMgr.getUuid(VirtualMachine.class, getVmId()); } @@ -134,7 +140,7 @@ public void execute() { String vmUuid = getVmUUID(); String msg = String.format("Failed to move Instance [%s].", vmUuid); logger.error(msg, e); - if (e instanceof InvalidParameterValueException && Boolean.TRUE.equals(UserVmService.AllowExposingVmAssignFailureDetails.value())) { + if (e instanceof InvalidParameterValueException && isErrorDetailExposedToCaller()) { msg = String.format("Failed to move Instance [%s]: %s", vmUuid, e.getMessage()); } throw new ServerApiException(errorCode, msg); diff --git a/api/src/test/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmdTest.java b/api/src/test/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmdTest.java index 695a3c313fc6..7b90a563c139 100644 --- a/api/src/test/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmdTest.java +++ b/api/src/test/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmdTest.java @@ -23,14 +23,19 @@ import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.context.CallContext; import org.apache.cloudstack.framework.config.ConfigKey; import org.apache.cloudstack.framework.config.impl.ConfigDepotImpl; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import com.cloud.configuration.ConfigurationService; import com.cloud.exception.InvalidParameterValueException; import com.cloud.exception.ResourceUnavailableException; +import com.cloud.user.Account; import com.cloud.utils.db.UUIDManager; import com.cloud.vm.UserVmService; import com.cloud.vm.VirtualMachine; @@ -38,10 +43,14 @@ public class AssignVMCmdTest { private static final String VM_UUID = "8f6c1c9e-3a2b-4d5e-9f01-23456789abcd"; + private static final String GENERIC_MESSAGE = "Failed to move Instance [" + VM_UUID + "]."; + private static final String DETAILED_MESSAGE = "Failed to move Instance [" + VM_UUID + "]: account over resource limit"; private AssignVMCmd assignVMCmd; private UserVmService userVmService; private ConfigDepotImpl configDepot; + private Account caller; + private MockedStatic callContextMocked; @Before public void setUp() { @@ -61,57 +70,67 @@ public Long getVmId() { configDepot = mock(ConfigDepotImpl.class); ConfigKey.init(configDepot); + + caller = mock(Account.class); + when(caller.getType()).thenReturn(Account.Type.DOMAIN_ADMIN); + CallContext callContext = mock(CallContext.class); + when(callContext.getCallingAccount()).thenReturn(caller); + callContextMocked = Mockito.mockStatic(CallContext.class); + callContextMocked.when(CallContext::current).thenReturn(callContext); } @After public void tearDown() { + callContextMocked.close(); ConfigKey.init(null); } - private void setDetailedFailureMessageFlag(boolean enabled) { - when(configDepot.getConfigStringValue(UserVmService.AllowExposingVmAssignFailureDetails.key(), + private void setExposeErrorsToUser(boolean enabled) { + when(configDepot.getConfigStringValue(ConfigurationService.EXPOSE_ERRORS_TO_USER.key(), ConfigKey.Scope.Global, null)).thenReturn(Boolean.toString(enabled)); } - @Test - public void testExecuteReturnsGenericMessageWhenDetailsDisabled() throws Exception { - setDetailedFailureMessageFlag(false); - when(userVmService.moveVmToUser(assignVMCmd)).thenThrow(new InvalidParameterValueException("account over resource limit")); - + private void assertExecuteFails(ApiErrorCode expectedCode, String expectedMessage) { try { assignVMCmd.execute(); fail("Expected a ServerApiException to be thrown"); } catch (ServerApiException e) { - assertEquals(ApiErrorCode.PARAM_ERROR, e.getErrorCode()); - assertEquals("Failed to move Instance [" + VM_UUID + "].", e.getDescription()); + assertEquals(expectedCode, e.getErrorCode()); + assertEquals(expectedMessage, e.getDescription()); } } @Test - public void testExecuteReturnsDetailedMessageWhenDetailsEnabledAndParamInvalid() throws Exception { - setDetailedFailureMessageFlag(true); + public void testExecuteReturnsGenericMessageToNonRootAdminWhenExposeDisabled() throws Exception { + setExposeErrorsToUser(false); when(userVmService.moveVmToUser(assignVMCmd)).thenThrow(new InvalidParameterValueException("account over resource limit")); - try { - assignVMCmd.execute(); - fail("Expected a ServerApiException to be thrown"); - } catch (ServerApiException e) { - assertEquals(ApiErrorCode.PARAM_ERROR, e.getErrorCode()); - assertEquals("Failed to move Instance [" + VM_UUID + "]: account over resource limit", e.getDescription()); - } + assertExecuteFails(ApiErrorCode.PARAM_ERROR, GENERIC_MESSAGE); + } + + @Test + public void testExecuteReturnsDetailedMessageToNonRootAdminWhenExposeEnabled() throws Exception { + setExposeErrorsToUser(true); + when(userVmService.moveVmToUser(assignVMCmd)).thenThrow(new InvalidParameterValueException("account over resource limit")); + + assertExecuteFails(ApiErrorCode.PARAM_ERROR, DETAILED_MESSAGE); + } + + @Test + public void testExecuteReturnsDetailedMessageToRootAdminWhenExposeDisabled() throws Exception { + setExposeErrorsToUser(false); + when(caller.getType()).thenReturn(Account.Type.ADMIN); + when(userVmService.moveVmToUser(assignVMCmd)).thenThrow(new InvalidParameterValueException("account over resource limit")); + + assertExecuteFails(ApiErrorCode.PARAM_ERROR, DETAILED_MESSAGE); } @Test - public void testExecuteReturnsGenericMessageForNonParamExceptionEvenWhenDetailsEnabled() throws Exception { - setDetailedFailureMessageFlag(true); + public void testExecuteReturnsGenericMessageForNonParamExceptionEvenWhenExposeEnabled() throws Exception { + setExposeErrorsToUser(true); + when(caller.getType()).thenReturn(Account.Type.ADMIN); when(userVmService.moveVmToUser(assignVMCmd)).thenThrow(new ResourceUnavailableException("host unreachable", AssignVMCmd.class, 1L)); - try { - assignVMCmd.execute(); - fail("Expected a ServerApiException to be thrown"); - } catch (ServerApiException e) { - assertEquals(ApiErrorCode.INTERNAL_ERROR, e.getErrorCode()); - assertEquals("Failed to move Instance [" + VM_UUID + "].", e.getDescription()); - } + assertExecuteFails(ApiErrorCode.INTERNAL_ERROR, GENERIC_MESSAGE); } } diff --git a/server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java index 4d91c0f4f992..bcdfb0590417 100644 --- a/server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java +++ b/server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java @@ -8224,7 +8224,7 @@ public ConfigKey[] getConfigKeys() { BYTES_MAX_READ_LENGTH, BYTES_MAX_WRITE_LENGTH, ADD_HOST_ON_SERVICE_RESTART_KVM, SET_HOST_DOWN_TO_MAINTENANCE, VM_SERVICE_OFFERING_MAX_CPU_CORES, VM_SERVICE_OFFERING_MAX_RAM_SIZE, MIGRATE_VM_ACROSS_CLUSTERS, ENABLE_ACCOUNT_SETTINGS_FOR_DOMAIN, ENABLE_DOMAIN_SETTINGS_FOR_CHILD_DOMAIN, - ALLOW_DOMAIN_ADMINS_TO_CREATE_TAGGED_OFFERINGS, DELETE_QUERY_BATCH_SIZE, AllowNonRFC1918CompliantIPs + ALLOW_DOMAIN_ADMINS_TO_CREATE_TAGGED_OFFERINGS, EXPOSE_ERRORS_TO_USER, DELETE_QUERY_BATCH_SIZE, AllowNonRFC1918CompliantIPs }; } diff --git a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java index 470f60c5a0aa..54ba6f0e872b 100644 --- a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java @@ -8889,7 +8889,7 @@ public ConfigKey[] getConfigKeys() { return new ConfigKey[] {EnableDynamicallyScaleVm, AllowDiskOfferingChangeDuringScaleVm, AllowUserExpungeRecoverVm, VmIpFetchWaitInterval, VmIpFetchTrialMax, VmIpFetchThreadPoolMax, VmIpFetchTaskWorkers, AllowDeployVmIfGivenHostFails, EnableAdditionalVmConfig, DisplayVMOVFProperties, KvmAdditionalConfigAllowList, XenServerAdditionalConfigAllowList, VmwareAdditionalConfigAllowList, DestroyRootVolumeOnVmDestruction, - EnforceStrictResourceLimitHostTagCheck, StrictHostTags, AllowUserForceStopVm, AllowExposingVmAssignFailureDetails}; + EnforceStrictResourceLimitHostTagCheck, StrictHostTags, AllowUserForceStopVm}; } @Override