Skip to content

Commit c748b69

Browse files
authored
Fix NPE during public IP listing when a removed network or VPC ID is informed for associatenetworkid parameter (#12372)
1 parent cf71938 commit c748b69

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

server/src/main/java/com/cloud/server/ManagementServerImpl.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import javax.inject.Inject;
4545
import javax.naming.ConfigurationException;
4646

47+
import com.cloud.network.vpc.VpcVO;
4748
import org.apache.cloudstack.acl.ControlledEntity;
4849
import org.apache.cloudstack.acl.SecurityChecker;
4950
import org.apache.cloudstack.affinity.AffinityGroupProcessor;
@@ -2580,12 +2581,21 @@ public Pair<List<? extends IpAddress>, Integer> searchForIPAddresses(final ListP
25802581
}
25812582

25822583
if (associatedNetworkId != null) {
2583-
_accountMgr.checkAccess(caller, null, false, networkDao.findById(associatedNetworkId));
2584-
sc.setParameters("associatedNetworkIdEq", associatedNetworkId);
2584+
NetworkVO associatedNetwork = networkDao.findById(associatedNetworkId);
2585+
2586+
if (associatedNetwork != null) {
2587+
_accountMgr.checkAccess(caller, null, false, associatedNetwork);
2588+
sc.setParameters("associatedNetworkIdEq", associatedNetworkId);
2589+
}
25852590
}
2591+
25862592
if (vpcId != null) {
2587-
_accountMgr.checkAccess(caller, null, false, _vpcDao.findById(vpcId));
2588-
sc.setParameters("vpcId", vpcId);
2593+
VpcVO vpc = _vpcDao.findById(vpcId);
2594+
2595+
if (vpc != null) {
2596+
_accountMgr.checkAccess(caller, null, false, vpc);
2597+
sc.setParameters("vpcId", vpcId);
2598+
}
25892599
}
25902600

25912601
addrs = _publicIpAddressDao.search(sc, searchFilter); // Allocated
@@ -2602,13 +2612,16 @@ public Pair<List<? extends IpAddress>, Integer> searchForIPAddresses(final ListP
26022612
}
26032613
if (associatedNetworkId != null) {
26042614
NetworkVO guestNetwork = networkDao.findById(associatedNetworkId);
2605-
if (zoneId == null) {
2606-
zoneId = guestNetwork.getDataCenterId();
2607-
} else if (zoneId != guestNetwork.getDataCenterId()) {
2608-
InvalidParameterValueException ex = new InvalidParameterValueException("Please specify a valid associated network id in the specified zone.");
2609-
throw ex;
2615+
2616+
if (guestNetwork != null) {
2617+
if (zoneId == null) {
2618+
zoneId = guestNetwork.getDataCenterId();
2619+
} else if (zoneId != guestNetwork.getDataCenterId()) {
2620+
InvalidParameterValueException ex = new InvalidParameterValueException("Please specify a valid associated network id in the specified zone.");
2621+
throw ex;
2622+
}
2623+
owner = _accountDao.findById(guestNetwork.getAccountId());
26102624
}
2611-
owner = _accountDao.findById(guestNetwork.getAccountId());
26122625
}
26132626
List<DataCenterVO> dcList = new ArrayList<>();
26142627
if (zoneId == null){

0 commit comments

Comments
 (0)