Skip to content

Commit 3c38ed7

Browse files
server: allow user to list available IPs on shared networks (#7898)
This fixes #7817
1 parent 8ad1009 commit 3c38ed7

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

engine/components-api/src/main/java/com/cloud/network/IpAddressManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public interface IpAddressManager {
5656
"Set placement of vrouter ips in redundant mode in vpc tiers, this can be 3 value: `first` to use first ips in tiers, `last` to use last ips in tiers and `random` to take random ips in tiers.",
5757
true, ConfigKey.Scope.Account, null, null, null, null, null, ConfigKey.Kind.Select, "first,last,random");
5858

59+
ConfigKey<Boolean> AllowUserListAvailableIpsOnSharedNetwork = new ConfigKey<Boolean>("Advanced", Boolean.class, "allow.user.list.available.ips.on.shared.network", "false",
60+
"Determines whether users can list available IPs on shared networks",
61+
true, ConfigKey.Scope.Global);
62+
5963
/**
6064
* Assigns a new public ip address.
6165
*

server/src/main/java/com/cloud/network/IpAddressManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2342,7 +2342,7 @@ public String getConfigComponentName() {
23422342

23432343
@Override
23442344
public ConfigKey<?>[] getConfigKeys() {
2345-
return new ConfigKey<?>[] {UseSystemPublicIps, RulesContinueOnError, SystemVmPublicIpReservationModeStrictness, VrouterRedundantTiersPlacement};
2345+
return new ConfigKey<?>[] {UseSystemPublicIps, RulesContinueOnError, SystemVmPublicIpReservationModeStrictness, VrouterRedundantTiersPlacement, AllowUserListAvailableIpsOnSharedNetwork};
23462346
}
23472347

23482348
/**

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2323,6 +2323,7 @@ public Pair<List<? extends IpAddress>, Integer> searchForIPAddresses(final ListP
23232323
isAllocated = Boolean.TRUE;
23242324
}
23252325
}
2326+
boolean isAllocatedTemp = isAllocated;
23262327

23272328
VlanType vlanType = null;
23282329
if (forVirtualNetwork != null) {
@@ -2333,6 +2334,7 @@ public Pair<List<? extends IpAddress>, Integer> searchForIPAddresses(final ListP
23332334

23342335
final Account caller = getCaller();
23352336
List<IPAddressVO> addrs = new ArrayList<>();
2337+
NetworkVO network = null; // shared network
23362338

23372339
if (vlanType == VlanType.DirectAttached && networkId == null && ipId == null) { // only root admin can list public ips in all shared networks
23382340
if (caller.getType() != Account.Type.ADMIN) {
@@ -2341,7 +2343,6 @@ public Pair<List<? extends IpAddress>, Integer> searchForIPAddresses(final ListP
23412343
} else if (vlanType == VlanType.DirectAttached) {
23422344
// list public ip address on shared network
23432345
// access control. admin: all Ips, domain admin/user: all Ips in shared network in the domain/sub-domain/user
2344-
NetworkVO network = null;
23452346
if (networkId == null) {
23462347
IPAddressVO ip = _publicIpAddressDao.findById(ipId);
23472348
if (ip == null) {
@@ -2475,7 +2476,20 @@ public Pair<List<? extends IpAddress>, Integer> searchForIPAddresses(final ListP
24752476
for (IPAddressVO addr: freeAddrs) {
24762477
freeAddrIds.add(addr.getId());
24772478
}
2479+
} else if (vlanType == VlanType.DirectAttached && network != null && !isAllocatedTemp && isAllocated) {
2480+
if (caller.getType() != Account.Type.ADMIN && !IpAddressManager.AllowUserListAvailableIpsOnSharedNetwork.value()) {
2481+
s_logger.debug("Non-admin users are not allowed to list available IPs on shared networks");
2482+
} else {
2483+
final SearchBuilder<IPAddressVO> searchBuilder = _publicIpAddressDao.createSearchBuilder();
2484+
buildParameters(searchBuilder, cmd, false);
2485+
2486+
SearchCriteria<IPAddressVO> searchCriteria = searchBuilder.create();
2487+
setParameters(searchCriteria, cmd, vlanType, false);
2488+
searchCriteria.setParameters("state", IpAddress.State.Free.name());
2489+
addrs.addAll(_publicIpAddressDao.search(searchCriteria, searchFilter)); // Free IPs on shared network
2490+
}
24782491
}
2492+
24792493
if (freeAddrIds.size() > 0) {
24802494
final SearchBuilder<IPAddressVO> sb2 = _publicIpAddressDao.createSearchBuilder();
24812495
buildParameters(sb2, cmd, false);

0 commit comments

Comments
 (0)