Skip to content

Commit a9accd3

Browse files
author
Jayapal
committed
CLOUDSTACK-6364 Added ip address validation
Also updated to assign vm primary ip to lb rule when vmid is passed virtualmachineid and vm id ip details in vmidipmap
1 parent 2ad98da commit a9accd3

2 files changed

Lines changed: 43 additions & 24 deletions

File tree

api/src/org/apache/cloudstack/api/command/user/loadbalancer/AssignToLoadBalancerRuleCmd.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.List;
2424
import java.util.Map;
2525

26+
import com.cloud.utils.net.NetUtils;
2627
import org.apache.log4j.Logger;
2728

2829
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
@@ -142,6 +143,11 @@ public Map<Long, List<String>> getVmIdIpListMap() {
142143
throw new InvalidParameterValueException("Unable to find virtual machine ID: " + vmId);
143144
}
144145

146+
//check wether the given ip is valid ip or not
147+
if (vmIp == null || !NetUtils.isValidIp(vmIp)) {
148+
throw new InvalidParameterValueException("Invalid ip address "+ vmIp +" passed in vmidipmap for " +
149+
"vmid " + vmId);
150+
}
145151
Long longVmId = lbvm.getId();
146152

147153
List<String> ipsList = null;

server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -986,20 +986,6 @@ public boolean assignToLoadBalancer(long loadBalancerId, List<Long> instanceIds,
986986
// check for conflict
987987
Set<Long> passedInstanceIds = vmIdIpMap.keySet();
988988
for (Long instanceId : passedInstanceIds) {
989-
if (existingVmIdIps.containsKey(instanceId)) {
990-
// now check for ip address
991-
List<String> mappedIps = existingVmIdIps.get(instanceId);
992-
List<String> newIps = vmIdIpMap.get(instanceId);
993-
994-
if (newIps != null) {
995-
for (String newIp: newIps) {
996-
if (mappedIps.contains(newIp)) {
997-
throw new InvalidParameterValueException("VM " + instanceId + " with " + newIp +" is already mapped to load balancer.");
998-
}
999-
}
1000-
}
1001-
}
1002-
1003989
UserVm vm = _vmDao.findById(instanceId);
1004990
if (vm == null || vm.getState() == State.Destroyed || vm.getState() == State.Expunging) {
1005991
InvalidParameterValueException ex = new InvalidParameterValueException("Invalid instance id specified");
@@ -1036,28 +1022,55 @@ public boolean assignToLoadBalancer(long loadBalancerId, List<Long> instanceIds,
10361022
}
10371023

10381024
String priIp = nicInSameNetwork.getIp4Address();
1025+
1026+
if (existingVmIdIps.containsKey(instanceId)) {
1027+
// now check for ip address
1028+
List<String> mappedIps = existingVmIdIps.get(instanceId);
1029+
List<String> newIps = vmIdIpMap.get(instanceId);
1030+
1031+
if (newIps == null) {
1032+
newIps = new ArrayList<String>();
1033+
newIps.add(priIp);
1034+
}
1035+
1036+
for (String newIp: newIps) {
1037+
if (mappedIps.contains(newIp)) {
1038+
throw new InvalidParameterValueException("VM " + instanceId + " with " + newIp +" is already mapped to load balancer.");
1039+
}
1040+
}
1041+
}
1042+
10391043
List<String> vmIpsList = vmIdIpMap.get(instanceId);
10401044
String vmLbIp = null;
10411045

1042-
if (vmIpsList == null) {
1043-
vmIpsList = new ArrayList<String>();
1044-
vmIpsList.add(priIp);
1045-
vmIdIpMap.put(instanceId, vmIpsList);
1046-
} else {
1047-
// skip the primary ip from vm secondary ip comparisions
1048-
if (vmIpsList.contains(priIp)) {
1049-
vmIpsList.remove(priIp);
1050-
}
1046+
if (vmIpsList != null) {
10511047

10521048
//check if the ips belongs to nic secondary ip
10531049
for (String ip: vmIpsList) {
1050+
// skip the primary ip from vm secondary ip comparisions
1051+
if (ip.equals(priIp)) {
1052+
continue;
1053+
}
10541054
if(_nicSecondaryIpDao.findByIp4AddressAndNicId(ip,nicInSameNetwork.getId()) == null) {
10551055
throw new InvalidParameterValueException("VM ip "+ ip + " specified does not belong to " +
10561056
"nic in network " + nicInSameNetwork.getNetworkId());
10571057
}
10581058
}
1059+
} else {
1060+
vmIpsList = new ArrayList<String>();
1061+
vmIpsList.add(priIp);
1062+
}
1063+
1064+
// when vm id is passed in instance ids and in vmidipmap
1065+
// assign for primary ip and ip passed in vmidipmap
1066+
if (instanceIds != null ) {
1067+
if (instanceIds.contains(instanceId)) {
1068+
vmIpsList.add(priIp);
1069+
}
10591070
}
10601071

1072+
vmIdIpMap.put(instanceId, vmIpsList);
1073+
10611074
if (s_logger.isDebugEnabled()) {
10621075
s_logger.debug("Adding " + vm + " to the load balancer pool");
10631076
}
@@ -1072,7 +1085,7 @@ public boolean assignToLoadBalancer(long loadBalancerId, List<Long> instanceIds,
10721085
public void doInTransactionWithoutResult(TransactionStatus status) {
10731086

10741087
for (Long vmId : vmIds) {
1075-
final List<String> lbVmIps = newMap.get(vmId);
1088+
final Set<String> lbVmIps = new HashSet<String>(newMap.get(vmId));
10761089
for (String vmIp: lbVmIps) {
10771090
LoadBalancerVMMapVO map = new LoadBalancerVMMapVO(loadBalancer.getId(), vmId, vmIp, false);
10781091
map = _lb2VmMapDao.persist(map);

0 commit comments

Comments
 (0)