|
16 | 16 | // under the License. |
17 | 17 | package com.cloud.network.vpn; |
18 | 18 |
|
| 19 | +import java.lang.reflect.InvocationTargetException; |
19 | 20 | import java.util.ArrayList; |
20 | 21 | import java.util.Iterator; |
21 | 22 | import java.util.List; |
22 | 23 | import java.util.Map; |
| 24 | +import java.util.stream.Collectors; |
23 | 25 |
|
24 | 26 | import javax.inject.Inject; |
25 | 27 | import javax.naming.ConfigurationException; |
26 | 28 |
|
27 | | -import org.apache.log4j.Logger; |
28 | | - |
29 | 29 | import org.apache.cloudstack.acl.SecurityChecker.AccessType; |
30 | 30 | import org.apache.cloudstack.api.command.user.vpn.ListRemoteAccessVpnsCmd; |
31 | 31 | import org.apache.cloudstack.api.command.user.vpn.ListVpnUsersCmd; |
32 | 32 | import org.apache.cloudstack.context.CallContext; |
33 | 33 | import org.apache.cloudstack.framework.config.ConfigKey; |
34 | 34 | import org.apache.cloudstack.framework.config.Configurable; |
35 | 35 | import org.apache.cloudstack.framework.config.dao.ConfigurationDao; |
| 36 | +import org.apache.commons.collections.CollectionUtils; |
| 37 | +import org.apache.log4j.Logger; |
36 | 38 |
|
37 | 39 | import com.cloud.configuration.Config; |
38 | 40 | import com.cloud.domain.DomainVO; |
|
91 | 93 | import com.cloud.utils.db.TransactionStatus; |
92 | 94 | import com.cloud.utils.exception.CloudRuntimeException; |
93 | 95 | 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; |
97 | 96 |
|
98 | 97 | public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAccessVpnService, Configurable { |
99 | 98 | private final static Logger s_logger = Logger.getLogger(RemoteAccessVpnManagerImpl.class); |
@@ -138,6 +137,24 @@ public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAcc |
138 | 137 | int _pskLength; |
139 | 138 | SearchBuilder<RemoteAccessVpnVO> VpnSearch; |
140 | 139 |
|
| 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 | + |
141 | 158 | @Override |
142 | 159 | @DB |
143 | 160 | public RemoteAccessVpn createRemoteAccessVpn(final long publicIpId, String ipRange, boolean openFirewall, final Boolean forDisplay) throws NetworkRuleConflictException { |
@@ -499,19 +516,36 @@ public void doInTransactionWithoutResult(TransactionStatus status) { |
499 | 516 | } |
500 | 517 | } |
501 | 518 |
|
| 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 | + |
502 | 533 | @DB |
503 | 534 | @Override |
504 | | - public boolean applyVpnUsers(long vpnOwnerId, String userName) throws ResourceUnavailableException { |
| 535 | + public boolean applyVpnUsers(long vpnOwnerId, String userName, boolean forRemove) throws ResourceUnavailableException { |
505 | 536 | Account caller = CallContext.current().getCallingAccount(); |
506 | 537 | Account owner = _accountDao.findById(vpnOwnerId); |
507 | 538 | _accountMgr.checkAccess(caller, null, true, owner); |
508 | 539 |
|
509 | 540 | 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); |
511 | 542 |
|
512 | 543 | 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; |
515 | 549 | } |
516 | 550 |
|
517 | 551 | RemoteAccessVpnVO vpnTemp = null; |
@@ -597,6 +631,12 @@ public void doInTransactionWithoutResult(TransactionStatus status) { |
597 | 631 | return success; |
598 | 632 | } |
599 | 633 |
|
| 634 | + @DB |
| 635 | + @Override |
| 636 | + public boolean applyVpnUsers(long vpnOwnerId, String userName) throws ResourceUnavailableException { |
| 637 | + return applyVpnUsers(vpnOwnerId, userName, false); |
| 638 | + } |
| 639 | + |
600 | 640 | @Override |
601 | 641 | public Pair<List<? extends VpnUser>, Integer> searchForVpnUsers(ListVpnUsersCmd cmd) { |
602 | 642 | String username = cmd.getUsername(); |
|
0 commit comments