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/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..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,7 +32,9 @@ 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; @@ -93,6 +95,15 @@ 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()); + } + public String getAccountName() { return accountName; } @@ -126,8 +137,12 @@ 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 vmUuid = getVmUUID(); + String msg = String.format("Failed to move Instance [%s].", vmUuid); logger.error(msg, e); + 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 new file mode 100644 index 000000000000..7b90a563c139 --- /dev/null +++ b/api/src/test/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmdTest.java @@ -0,0 +1,136 @@ +// 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.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; + +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() { + assignVMCmd = new AssignVMCmd() { + @Override + public Long getVmId() { + return 1L; + } + }; + + 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); + + 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 setExposeErrorsToUser(boolean enabled) { + when(configDepot.getConfigStringValue(ConfigurationService.EXPOSE_ERRORS_TO_USER.key(), + ConfigKey.Scope.Global, null)).thenReturn(Boolean.toString(enabled)); + } + + private void assertExecuteFails(ApiErrorCode expectedCode, String expectedMessage) { + try { + assignVMCmd.execute(); + fail("Expected a ServerApiException to be thrown"); + } catch (ServerApiException e) { + assertEquals(expectedCode, e.getErrorCode()); + assertEquals(expectedMessage, e.getDescription()); + } + } + + @Test + public void testExecuteReturnsGenericMessageToNonRootAdminWhenExposeDisabled() throws Exception { + setExposeErrorsToUser(false); + when(userVmService.moveVmToUser(assignVMCmd)).thenThrow(new InvalidParameterValueException("account over resource limit")); + + 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 testExecuteReturnsGenericMessageForNonParamExceptionEvenWhenExposeEnabled() throws Exception { + setExposeErrorsToUser(true); + when(caller.getType()).thenReturn(Account.Type.ADMIN); + when(userVmService.moveVmToUser(assignVMCmd)).thenThrow(new ResourceUnavailableException("host unreachable", AssignVMCmd.class, 1L)); + + 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 }; }