Skip to content

Commit c72482d

Browse files
committed
address possible cidr adherence
1 parent 380ce1d commit c72482d

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@
174174
import org.apache.log4j.Logger;
175175

176176
import javax.inject.Inject;
177+
import java.net.InetAddress;
178+
import java.net.UnknownHostException;
177179
import java.util.ArrayList;
178180
import java.util.Arrays;
179181
import java.util.HashMap;
@@ -695,16 +697,30 @@ private void checkUnmanagedNicIpAndNetworkForImport(String instanceName, Unmanag
695697
}
696698
// If network is non L2, IP v4 is assigned and not set to auto-assign, check it is available for network
697699
if (!network.getGuestType().equals(Network.GuestType.L2) && ipAddresses != null && StringUtils.isNotEmpty(ipAddresses.getIp4Address()) && !ipAddresses.getIp4Address().equals("auto")) {
698-
List<String> usedIps = networkModel.getUsedIpsInNetwork(network);
699-
if (LOGGER.isDebugEnabled() && usedIps != null) {
700-
String s = usedIps.stream().collect(Collectors.joining(", "));
701-
LOGGER.debug(String.format("available IPs for network(ID: %s) are [%s]",network.getUuid(), s));
702-
}
703-
for (String usedIp : usedIps) {
704-
// only half witted support for ipv6 here :(
705-
if (usedIp.equals(ipAddresses.getIp4Address()) || usedIp.equals(ipAddresses.getIp6Address())) {
706-
throw new ServerApiException(ApiErrorCode.RESOURCE_IN_USE_ERROR, String.format("Cannot assign IP address %s to NIC(ID: %s) as it is in use in network(ID: %s)", ipAddresses.getIp4Address(), nic.getNicId(), network.getUuid()));
707-
}
700+
canIpBeUsedForNicInNetwork(nic, network, ipAddresses);
701+
}
702+
}
703+
704+
private void canIpBeUsedForNicInNetwork(UnmanagedInstanceTO.Nic nic, Network network, Network.IpAddresses ipAddresses) {
705+
List<String> usedIps = networkModel.getUsedIpsInNetwork(network);
706+
if (LOGGER.isDebugEnabled() && usedIps != null) {
707+
String s = usedIps.stream().collect(Collectors.joining(", "));
708+
LOGGER.debug(String.format("used IPs for network(ID: %s) to check against are [%s]", network.getUuid(), s));
709+
}
710+
for (String usedIp : usedIps) {
711+
// only half witted support for ipv6 here :(
712+
if (usedIp.equals(ipAddresses.getIp4Address()) || usedIp.equals(ipAddresses.getIp6Address())) {
713+
throw new ServerApiException(ApiErrorCode.RESOURCE_IN_USE_ERROR, String.format("Cannot assign IP address %s to NIC(ID: %s) as it is in use in network(ID: %s)", ipAddresses.getIp4Address(), nic.getNicId(), network.getUuid()));
714+
}
715+
}
716+
if (network.getCidr() != null) {
717+
String[] cidrs = Arrays.asList(network.getCidr()).toArray(new String[0]);
718+
try {
719+
NetUtils.isIpInCidrList(InetAddress.getByName(ipAddresses.getIp4Address()), cidrs);
720+
} catch (UnknownHostException e) {
721+
String msg = String.format("request an invalid IP address during import %s, ignoring", ipAddresses.getIp4Address());
722+
LOGGER.warn(msg);
723+
throw new ServerApiException(ApiErrorCode.MALFORMED_PARAMETER_ERROR, msg, e);
708724
}
709725
}
710726
}

0 commit comments

Comments
 (0)