Failed to update host password if username/password is not saved in db (#4359)

This commit is contained in:
Rakesh 2020-10-29 09:43:05 +01:00 committed by GitHub
parent b266c757d2
commit 8b994113ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3929,6 +3929,11 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
// get all the hosts in this cluster
final List<HostVO> hosts = _resourceMgr.listAllHostsInCluster(command.getClusterId());
String userNameWithoutSpaces = StringUtils.deleteWhitespace(command.getUsername());
if (StringUtils.isBlank(userNameWithoutSpaces)) {
throw new InvalidParameterValueException("Username should be non empty string");
}
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(final TransactionStatus status) {
@ -3938,7 +3943,12 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
}
// update password for this host
final DetailVO nv = _detailsDao.findDetail(h.getId(), ApiConstants.USERNAME);
if (nv.getValue().equals(command.getUsername())) {
if (nv == null) {
final DetailVO nvu = new DetailVO(h.getId(), ApiConstants.USERNAME, userNameWithoutSpaces);
_detailsDao.persist(nvu);
final DetailVO nvp = new DetailVO(h.getId(), ApiConstants.PASSWORD, DBEncryptionUtil.encrypt(command.getPassword()));
_detailsDao.persist(nvp);
} else if (nv.getValue().equals(userNameWithoutSpaces)) {
final DetailVO nvp = _detailsDao.findDetail(h.getId(), ApiConstants.PASSWORD);
nvp.setValue(DBEncryptionUtil.encrypt(command.getPassword()));
_detailsDao.persist(nvp);
@ -3986,6 +3996,12 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
if (!supportedHypervisors.contains(host.getHypervisorType())) {
throw new InvalidParameterValueException("This operation is not supported for this hypervisor type");
}
String userNameWithoutSpaces = StringUtils.deleteWhitespace(cmd.getUsername());
if (StringUtils.isBlank(userNameWithoutSpaces)) {
throw new InvalidParameterValueException("Username should be non empty string");
}
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(final TransactionStatus status) {
@ -3994,7 +4010,12 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
}
// update password for this host
final DetailVO nv = _detailsDao.findDetail(host.getId(), ApiConstants.USERNAME);
if (nv.getValue().equals(cmd.getUsername())) {
if (nv == null) {
final DetailVO nvu = new DetailVO(host.getId(), ApiConstants.USERNAME, userNameWithoutSpaces);
_detailsDao.persist(nvu);
final DetailVO nvp = new DetailVO(host.getId(), ApiConstants.PASSWORD, DBEncryptionUtil.encrypt(cmd.getPassword()));
_detailsDao.persist(nvp);
} else if (nv.getValue().equals(userNameWithoutSpaces)) {
final DetailVO nvp = _detailsDao.findDetail(host.getId(), ApiConstants.PASSWORD);
nvp.setValue(DBEncryptionUtil.encrypt(cmd.getPassword()));
_detailsDao.persist(nvp);