From 55bef2b069c941b134e283fdf629cd3f974c2c8b Mon Sep 17 00:00:00 2001 From: Stephan Krug Date: Fri, 13 Oct 2023 10:44:34 -0300 Subject: [PATCH] Add option to define password during password reset (#6863) Co-authored-by: Stephan Krug Co-authored-by: Gabriel Co-authored-by: GaOrtiga <49285692+GaOrtiga@users.noreply.github.com> Co-authored-by: dahn --- .../api/command/user/vm/ResetVMPasswordCmd.java | 13 ++++++++++--- ui/src/config/section/compute.js | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/org/apache/cloudstack/api/command/user/vm/ResetVMPasswordCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/user/vm/ResetVMPasswordCmd.java index e275a988cd15..1cf4c929b32d 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/user/vm/ResetVMPasswordCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/user/vm/ResetVMPasswordCmd.java @@ -16,6 +16,7 @@ // under the License. package org.apache.cloudstack.api.command.user.vm; +import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.apache.cloudstack.acl.SecurityChecker.AccessType; @@ -56,8 +57,7 @@ public class ResetVMPasswordCmd extends BaseAsyncCmd implements UserCmd { required=true, description="The ID of the virtual machine") private Long id; - // unexposed parameter needed for serializing/deserializing the command - @Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, expose=false) + @Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, description="The new password of the virtual machine. If null, a random password will be generated for the VM.", since="4.19.0") protected String password; @@ -118,7 +118,14 @@ public Long getApiResourceId() { @Override public void execute() throws ResourceUnavailableException, InsufficientCapacityException { - password = _mgr.generateRandomPassword(); + password = getPassword(); + UserVm vm = _responseGenerator.findUserVmById(getId()); + if (StringUtils.isBlank(password)) { + password = _mgr.generateRandomPassword(); + s_logger.debug(String.format("Resetting VM [%s] password to a randomly generated password.", vm.getUuid())); + } else { + s_logger.debug(String.format("Resetting VM [%s] password to password defined by user.", vm.getUuid())); + } CallContext.current().setEventDetails("Vm Id: " + getId()); UserVm result = _userVmService.resetVMPassword(this, password); if (result != null){ diff --git a/ui/src/config/section/compute.js b/ui/src/config/section/compute.js index e09fab347920..8b30cce877de 100644 --- a/ui/src/config/section/compute.js +++ b/ui/src/config/section/compute.js @@ -368,6 +368,7 @@ export default { label: 'label.action.reset.password', message: 'message.action.instance.reset.password', dataView: true, + args: ['password'], show: (record) => { return ['Stopped'].includes(record.state) && record.passwordenabled }, response: (result) => { return {