Skip to content

Commit f88f934

Browse files
authored
api, server: fix add-remove vpn user without vpn owner (apache#5850)
* api, server: fix add-remove vpn user without vpn owner Fixes apache#5711 ACS should not add a new user in Add state when the owner account does not have VPN access. While removing VPN user ACS should not fail completely when owner account ahs no VPN. * change , fixes * remove unused method Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 640118c commit f88f934

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

api/src/main/java/com/cloud/network/vpn/RemoteAccessVpnService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public interface RemoteAccessVpnService {
4343

4444
List<? extends VpnUser> listVpnUsers(long vpnOwnerId, String userName);
4545

46+
boolean applyVpnUsers(long vpnOwnerId, String userName, boolean forRemove) throws ResourceUnavailableException;
47+
4648
boolean applyVpnUsers(long vpnOwnerId, String userName) throws ResourceUnavailableException;
4749

4850
Pair<List<? extends RemoteAccessVpn>, Integer> searchForRemoteAccessVpns(ListRemoteAccessVpnsCmd cmd);

api/src/main/java/org/apache/cloudstack/api/command/user/vpn/RemoveVpnUserCmd.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,8 @@ public void execute() {
120120
}
121121

122122
boolean appliedVpnUsers = false;
123-
124123
try {
125-
appliedVpnUsers = _ravService.applyVpnUsers(ownerId, userName);
124+
appliedVpnUsers = _ravService.applyVpnUsers(ownerId, userName, true);
126125
} catch (ResourceUnavailableException ex) {
127126
String errorMessage = String.format("Failed to refresh VPN user=[%s] due to resource unavailable. VPN owner id=[%s].", userName, ownerId);
128127
s_logger.error(errorMessage, ex);

server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,25 @@
1616
// under the License.
1717
package com.cloud.network.vpn;
1818

19+
import java.lang.reflect.InvocationTargetException;
1920
import java.util.ArrayList;
2021
import java.util.Iterator;
2122
import java.util.List;
2223
import java.util.Map;
24+
import java.util.stream.Collectors;
2325

2426
import javax.inject.Inject;
2527
import javax.naming.ConfigurationException;
2628

27-
import org.apache.log4j.Logger;
28-
2929
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
3030
import org.apache.cloudstack.api.command.user.vpn.ListRemoteAccessVpnsCmd;
3131
import org.apache.cloudstack.api.command.user.vpn.ListVpnUsersCmd;
3232
import org.apache.cloudstack.context.CallContext;
3333
import org.apache.cloudstack.framework.config.ConfigKey;
3434
import org.apache.cloudstack.framework.config.Configurable;
3535
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
36+
import org.apache.commons.collections.CollectionUtils;
37+
import org.apache.log4j.Logger;
3638

3739
import com.cloud.configuration.Config;
3840
import com.cloud.domain.DomainVO;
@@ -91,9 +93,6 @@
9193
import com.cloud.utils.db.TransactionStatus;
9294
import com.cloud.utils.exception.CloudRuntimeException;
9395
import com.cloud.utils.net.NetUtils;
94-
import java.lang.reflect.InvocationTargetException;
95-
import java.util.stream.Collectors;
96-
import org.apache.commons.collections.CollectionUtils;
9796

9897
public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAccessVpnService, Configurable {
9998
private final static Logger s_logger = Logger.getLogger(RemoteAccessVpnManagerImpl.class);
@@ -138,6 +137,24 @@ public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAcc
138137
int _pskLength;
139138
SearchBuilder<RemoteAccessVpnVO> VpnSearch;
140139

140+
private List<RemoteAccessVpnVO> getValidRemoteAccessVpnForAccount(long accountId) {
141+
List<RemoteAccessVpnVO> vpns = _remoteAccessVpnDao.findByAccount(accountId);
142+
if (CollectionUtils.isNotEmpty(vpns)) {
143+
List<RemoteAccessVpnVO> validVpns = new ArrayList<>();
144+
for (RemoteAccessVpnVO vpn : vpns) {
145+
if (vpn.getNetworkId() != null) {
146+
Network network = _networkMgr.getNetwork(vpn.getNetworkId());
147+
if (!Network.State.Implemented.equals(network.getState())) {
148+
continue;
149+
}
150+
}
151+
validVpns.add(vpn);
152+
}
153+
vpns = validVpns;
154+
}
155+
return vpns;
156+
}
157+
141158
@Override
142159
@DB
143160
public RemoteAccessVpn createRemoteAccessVpn(final long publicIpId, String ipRange, boolean openFirewall, final Boolean forDisplay) throws NetworkRuleConflictException {
@@ -499,19 +516,36 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
499516
}
500517
}
501518

519+
@DB
520+
private boolean removeVpnUserWithoutRemoteAccessVpn(long vpnOwnerId, String userName) {
521+
VpnUserVO vpnUser = _vpnUsersDao.findByAccountAndUsername(vpnOwnerId, userName);
522+
if (vpnUser == null) {
523+
s_logger.error(String.format("VPN user not found with ownerId: %d and username: %s", vpnOwnerId, userName));
524+
return false;
525+
}
526+
if (!State.Revoke.equals(vpnUser.getState())) {
527+
s_logger.error(String.format("VPN user with ownerId: %d and username: %s is not in revoked state, current state: %s", vpnOwnerId, userName, vpnUser.getState()));
528+
return false;
529+
}
530+
return _vpnUsersDao.remove(vpnUser.getId());
531+
}
532+
502533
@DB
503534
@Override
504-
public boolean applyVpnUsers(long vpnOwnerId, String userName) throws ResourceUnavailableException {
535+
public boolean applyVpnUsers(long vpnOwnerId, String userName, boolean forRemove) throws ResourceUnavailableException {
505536
Account caller = CallContext.current().getCallingAccount();
506537
Account owner = _accountDao.findById(vpnOwnerId);
507538
_accountMgr.checkAccess(caller, null, true, owner);
508539

509540
s_logger.debug(String.format("Applying VPN users for %s.", owner.toString()));
510-
List<RemoteAccessVpnVO> vpns = _remoteAccessVpnDao.findByAccount(vpnOwnerId);
541+
List<RemoteAccessVpnVO> vpns = getValidRemoteAccessVpnForAccount(vpnOwnerId);
511542

512543
if (CollectionUtils.isEmpty(vpns)) {
513-
s_logger.debug(String.format("Unable to add VPN user due to there are no remote access VPNs configured on %s to apply VPN user.", owner.toString()));
514-
return false;
544+
if (forRemove) {
545+
return removeVpnUserWithoutRemoteAccessVpn(vpnOwnerId, userName);
546+
}
547+
s_logger.warn(String.format("Unable to apply VPN user due to there are no remote access VPNs configured on %s to apply VPN user.", owner.toString()));
548+
return true;
515549
}
516550

517551
RemoteAccessVpnVO vpnTemp = null;
@@ -597,6 +631,12 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
597631
return success;
598632
}
599633

634+
@DB
635+
@Override
636+
public boolean applyVpnUsers(long vpnOwnerId, String userName) throws ResourceUnavailableException {
637+
return applyVpnUsers(vpnOwnerId, userName, false);
638+
}
639+
600640
@Override
601641
public Pair<List<? extends VpnUser>, Integer> searchForVpnUsers(ListVpnUsersCmd cmd) {
602642
String username = cmd.getUsername();

0 commit comments

Comments
 (0)