Bug 12897: Make updateHostPassword more robust. Allow only KVM's individual host password be changed and Xenserver cluster password be changed. For rest of the hypervisors throw an exception.

Reviewed-By: Abhi
This commit is contained in:
Nitin Mehta 2012-01-19 21:27:18 +05:30
parent 7fd7a4ae58
commit ee1971526c
2 changed files with 16 additions and 8 deletions

View File

@ -41,10 +41,10 @@ public class UpdateHostPasswordCmd extends BaseCmd {
// ////////////// API parameters /////////////////////
// ///////////////////////////////////////////////////
@Parameter(name=ApiConstants.HOST_ID, type=CommandType.LONG, description="the host ID")
@Parameter(name=ApiConstants.HOST_ID, type=CommandType.LONG, description="the host ID. Either this parameter, or clusterId has to be passed in")
private Long hostId;
@Parameter(name=ApiConstants.CLUSTER_ID, type=CommandType.LONG, description="the cluster ID for the host")
@Parameter(name=ApiConstants.CLUSTER_ID, type=CommandType.LONG, description="the cluster ID. Either this parameter, or hostId has to be passed in")
private Long clusterId;
@Parameter(name=ApiConstants.USERNAME, type=CommandType.STRING, required=true, description="the username for the host/cluster")

View File

@ -4757,22 +4757,30 @@ public class ManagementServerImpl implements ManagementServer {
@Override
@DB
public boolean updateHostPassword(UpdateHostPasswordCmd cmd) {
if (cmd.getClusterId() == null && cmd.getHostId() == null) {
if ( !((cmd.getClusterId() == null) ^ (cmd.getHostId() == null)) ) {//Using Xor operator here.
throw new InvalidParameterValueException("You should provide one of cluster id or a host id.");
} else if (cmd.getClusterId() == null) {
HostVO h = _hostDao.findById(cmd.getHostId());
if (h.getHypervisorType() == HypervisorType.XenServer) {
throw new InvalidParameterValueException("You should provide cluster id for Xenserver cluster.");
HostVO host = _hostDao.findById(cmd.getHostId());
if (host == null){
throw new InvalidParameterValueException("The hostId " +cmd.getHostId()+ " doesnt exist");
}if (host.getHypervisorType() != HypervisorType.KVM) {
throw new InvalidParameterValueException("This operation is not permitted for " + host.getHypervisorType() + " with the parameter hostId");
}
DetailVO nv = _detailsDao.findDetail(h.getId(), ApiConstants.USERNAME);
DetailVO nv = _detailsDao.findDetail(host.getId(), ApiConstants.USERNAME);
if (nv.getValue().equals(cmd.getUsername())) {
DetailVO nvp = new DetailVO(h.getId(), ApiConstants.PASSWORD, cmd.getPassword());
DetailVO nvp = new DetailVO(host.getId(), ApiConstants.PASSWORD, cmd.getPassword());
nvp.setValue(cmd.getPassword());
_detailsDao.persist(nvp);
} else {
throw new InvalidParameterValueException("The username is not under use by management server.");
}
} else {
ClusterVO cluster = _clusterDao.findById(cmd.getClusterId());
if (cluster == null){
throw new InvalidParameterValueException("The clusterId " +cmd.getClusterId()+ " doesnt exist");
}if (cluster.getHypervisorType() != HypervisorType.XenServer){
throw new InvalidParameterValueException("This operation is not permitted for " + cluster.getHypervisorType() + " with the parameter clusterId");
}
// get all the hosts in this cluster
List<HostVO> hosts = _hostDao.listByCluster(cmd.getClusterId());
Transaction txn = Transaction.currentTxn();