From af6328816a4295b247d7abfc816ac8238100e22a Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Tue, 27 Apr 2021 16:28:27 -0300 Subject: [PATCH 1/7] Extract code to method and test it --- .../vpn/RemoteAccessVpnManagerImpl.java | 37 ++- .../vpn/RemoteAccessVpnManagerImplTest.java | 224 ++++++++++++++++++ 2 files changed, 248 insertions(+), 13 deletions(-) create mode 100644 server/src/test/java/com/cloud/network/vpn/RemoteAccessVpnManagerImplTest.java diff --git a/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java b/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java index 2030a5a4ee55..b316cf4f0f53 100644 --- a/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java +++ b/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java @@ -89,7 +89,9 @@ import com.cloud.utils.db.TransactionCallbackNoReturn; import com.cloud.utils.db.TransactionCallbackWithException; import com.cloud.utils.db.TransactionStatus; +import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; +import java.lang.reflect.InvocationTargetException; import org.apache.commons.collections.CollectionUtils; public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAccessVpnService, Configurable { @@ -190,16 +192,10 @@ public RemoteAccessVpn createRemoteAccessVpn(final long publicIpId, String ipRan if (ipRange == null) { ipRange = RemoteAccessVpnClientIpRange.valueIn(ipAddr.getAccountId()); } - final String[] range = ipRange.split("-"); - if (range.length != 2) { - throw new InvalidParameterValueException("Invalid ip range"); - } - if (!NetUtils.isValidIp4(range[0]) || !NetUtils.isValidIp4(range[1])) { - throw new InvalidParameterValueException("Invalid ip in range specification " + ipRange); - } - if (!NetUtils.validIpRange(range[0], range[1])) { - throw new InvalidParameterValueException("Invalid ip range " + ipRange); - } + + validateIpRange(ipRange, InvalidParameterValueException.class); + + String[] range = ipRange.split("-"); Pair cidr = null; @@ -267,15 +263,30 @@ private void validateRemoteAccessVpnConfiguration() throws ConfigurationExceptio throw new ConfigurationException("Remote Access VPN: IPSec preshared key length should be between 8 and 256"); } + validateIpRange(ipRange, ConfigurationException.class); + } + + protected void validateIpRange(String ipRange, Class exceptionClass) throws T { String[] range = ipRange.split("-"); + if (range.length != 2) { - throw new ConfigurationException("Remote Access VPN: Invalid ip range " + ipRange); + throwExceptionOnValidateIpRangeError(exceptionClass, String.format("IP range [%s] is an invalid IP range.", ipRange)); } + if (!NetUtils.isValidIp4(range[0]) || !NetUtils.isValidIp4(range[1])) { - throw new ConfigurationException("Remote Access VPN: Invalid ip in range specification " + ipRange); + throwExceptionOnValidateIpRangeError(exceptionClass, String.format("One or both IPs sets in the range [%s] are invalid IPs.", ipRange)); } + if (!NetUtils.validIpRange(range[0], range[1])) { - throw new ConfigurationException("Remote Access VPN: Invalid ip range " + ipRange); + throwExceptionOnValidateIpRangeError(exceptionClass, String.format("Range of IPs [%s] is invalid.", ipRange)); + } + } + + protected void throwExceptionOnValidateIpRangeError(Class exceptionClass, String errorMessage) throws T { + try { + throw exceptionClass.getConstructor(String.class).newInstance(errorMessage); + } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { + throw new CloudRuntimeException(String.format("Unexpected exception [%s] while throwing error [%s] on validateIpRange.", ex.getMessage(), errorMessage), ex); } } diff --git a/server/src/test/java/com/cloud/network/vpn/RemoteAccessVpnManagerImplTest.java b/server/src/test/java/com/cloud/network/vpn/RemoteAccessVpnManagerImplTest.java new file mode 100644 index 000000000000..fd9d1e0b8f6f --- /dev/null +++ b/server/src/test/java/com/cloud/network/vpn/RemoteAccessVpnManagerImplTest.java @@ -0,0 +1,224 @@ +/* + * Copyright 2021 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.cloud.network.vpn; + +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.net.NetUtils; +import java.lang.reflect.InvocationTargetException; +import javax.naming.ConfigurationException; +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(NetUtils.class) +public class RemoteAccessVpnManagerImplTest extends TestCase { + + Class expectedException = InvalidParameterValueException.class; + Class cloudRuntimeException = CloudRuntimeException.class; + + @Test + public void validateValidateIpRangeRangeLengthLessThan2MustThrowException(){ + String ipRange = "192.168.0.1"; + String expectedMessage = String.format("IP range [%s] is an invalid IP range.", ipRange); + + InvalidParameterValueException assertThrows = Assert.assertThrows(expectedMessage, expectedException, () -> { + new RemoteAccessVpnManagerImpl().validateIpRange(ipRange, expectedException); + }); + + assertEquals(expectedMessage, assertThrows.getMessage()); + } + + @Test + public void validateValidateIpRangeRangeLengthHigherThan2MustThrowException(){ + String ipRange = "192.168.0.1-192.168.0.31-192.168.0.63"; + String expectedMessage = String.format("IP range [%s] is an invalid IP range.", ipRange); + + InvalidParameterValueException assertThrows = Assert.assertThrows(expectedMessage, expectedException, () -> { + new RemoteAccessVpnManagerImpl().validateIpRange(ipRange, expectedException); + }); + + assertEquals(expectedMessage, assertThrows.getMessage()); + } + + @Test + public void validateValidateIpRangeFirstElementInvalidMustThrowException(){ + String ipRange = "192.168.0.400-192.168.0.255"; + String[] range = ipRange.split("-"); + String expectedMessage = String.format("One or both IPs sets in the range [%s] are invalid IPs.", ipRange); + + PowerMockito.mockStatic(NetUtils.class); + + PowerMockito.when(NetUtils.isValidIp4(range[0])).thenReturn(Boolean.FALSE); + PowerMockito.when(NetUtils.isValidIp4(range[1])).thenReturn(Boolean.TRUE); + + InvalidParameterValueException assertThrows = Assert.assertThrows(expectedMessage, expectedException, () -> { + new RemoteAccessVpnManagerImpl().validateIpRange(ipRange, expectedException); + }); + + assertEquals(expectedMessage, assertThrows.getMessage()); + } + + @Test + public void validateValidateIpRangeSecondElementInvalidMustThrowException(){ + String ipRange = "192.168.0.1-192.168.0.400"; + String[] range = ipRange.split("-"); + String expectedMessage = String.format("One or both IPs sets in the range [%s] are invalid IPs.", ipRange); + + PowerMockito.mockStatic(NetUtils.class); + + PowerMockito.when(NetUtils.isValidIp4(range[0])).thenReturn(Boolean.TRUE); + PowerMockito.when(NetUtils.isValidIp4(range[1])).thenReturn(Boolean.FALSE); + + InvalidParameterValueException assertThrows = Assert.assertThrows(expectedMessage, expectedException, () -> { + new RemoteAccessVpnManagerImpl().validateIpRange(ipRange, expectedException); + }); + + assertEquals(expectedMessage, assertThrows.getMessage()); + } + + @Test + public void validateValidateIpRangeBothElementsInvalidMustThrowException(){ + String ipRange = "192.168.0.256-192.168.0.300"; + String[] range = ipRange.split("-"); + String expectedMessage = String.format("One or both IPs sets in the range [%s] are invalid IPs.", ipRange); + + PowerMockito.mockStatic(NetUtils.class); + + PowerMockito.when(NetUtils.isValidIp4(range[0])).thenReturn(Boolean.FALSE); + PowerMockito.when(NetUtils.isValidIp4(range[1])).thenReturn(Boolean.FALSE); + + InvalidParameterValueException assertThrows = Assert.assertThrows(expectedMessage, expectedException, () -> { + new RemoteAccessVpnManagerImpl().validateIpRange(ipRange, expectedException); + }); + + assertEquals(expectedMessage, assertThrows.getMessage()); + } + + @Test + public void validateValidateIpRangeInvalidIpRangeMustThrowException(){ + String ipRange = "192.168.0.255-192.168.0.1"; + String[] range = ipRange.split("-"); + String expectedMessage = String.format("Range of IPs [%s] is invalid.", ipRange); + + PowerMockito.mockStatic(NetUtils.class); + + PowerMockito.when(NetUtils.isValidIp4(range[0])).thenReturn(Boolean.TRUE); + PowerMockito.when(NetUtils.isValidIp4(range[1])).thenReturn(Boolean.TRUE); + PowerMockito.when(NetUtils.validIpRange(range[0], range[1])).thenReturn(Boolean.FALSE); + + InvalidParameterValueException assertThrows = Assert.assertThrows(expectedMessage, expectedException, () -> { + new RemoteAccessVpnManagerImpl().validateIpRange(ipRange, expectedException); + }); + + assertEquals(expectedMessage, assertThrows.getMessage()); + } + + @Test + public void validateValidateIpRangeValidIpRangeMustValidate(){ + String ipRange = "192.168.0.1-192.168.0.255"; + String[] range = ipRange.split("-"); + + PowerMockito.mockStatic(NetUtils.class); + + PowerMockito.when(NetUtils.isValidIp4(range[0])).thenReturn(Boolean.TRUE); + PowerMockito.when(NetUtils.isValidIp4(range[1])).thenReturn(Boolean.TRUE); + PowerMockito.when(NetUtils.validIpRange(range[0], range[1])).thenReturn(Boolean.TRUE); + + new RemoteAccessVpnManagerImpl().validateIpRange(ipRange, expectedException); + } + + private void throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(Class exceptionToCatch){ + throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(exceptionToCatch, "Test"); + } + + private void throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(Class exceptionToCatch, String exceptionMessage){ + String errorMessage = "Test"; + String expectedMessage = String.format("Unexpected exception [%s] while throwing error [%s] on validateIpRange.", exceptionMessage, errorMessage); + + CloudRuntimeException assertThrows = Assert.assertThrows(expectedMessage, cloudRuntimeException, () -> { + new RemoteAccessVpnManagerImpl().throwExceptionOnValidateIpRangeError(exceptionToCatch, errorMessage); + }); + + assertEquals(expectedMessage, assertThrows.getMessage()); + } + + @Test + public void validateThrowExceptionOnValidateIpRangeErrorWhenNoSuchMethodExceptionThrowCloudRuntimeException(){ + Class exception = NoSuchMethodException.class; + throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(exception); + } + + @Test + public void validateThrowExceptionOnValidateIpRangeErrorWhenSecurityExceptionThrowCloudRuntimeException(){ + Class exception = SecurityException.class; + throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(exception); + } + + @Test + public void validateThrowExceptionOnValidateIpRangeErrorWhenInstantiationExceptionThrowCloudRuntimeException(){ + Class exception = InstantiationException.class; + throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(exception); + } + + @Test + public void validateThrowExceptionOnValidateIpRangeErrorWhenIllegalAccessExceptionThrowCloudRuntimeException(){ + Class exception = IllegalAccessException.class; + throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(exception); + } + + @Test + public void validateThrowExceptionOnValidateIpRangeErrorWhenIllegalArgumentExceptionThrowCloudRuntimeException(){ + Class exception = IllegalArgumentException.class; + throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(exception); + } + + @Test + public void validateThrowExceptionOnValidateIpRangeErrorWhenInvocationTargetExceptionThrowCloudRuntimeException(){ + Class exception = InvocationTargetException.class; + throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(exception, "java.lang.reflect.InvocationTargetException.(java.lang.String)"); + } + + @Test + public void validateThrowExceptionOnValidateIpRangeErrorWhenConfigurationExceptionThrowConfigurationException(){ + Class exception = ConfigurationException.class; + String expectedMessage = "Test"; + + ConfigurationException assertThrows = Assert.assertThrows(expectedMessage, exception, () -> { + new RemoteAccessVpnManagerImpl().throwExceptionOnValidateIpRangeError(exception, expectedMessage); + }); + + assertEquals(expectedMessage, assertThrows.getMessage()); + } + + @Test + public void validateThrowExceptionOnValidateIpRangeErrorWhenInvalidParameterValueExceptionThrowInvalidParameterValueException(){ + Class exception = InvalidParameterValueException.class; + String expectedMessage = "Test"; + + InvalidParameterValueException assertThrows = Assert.assertThrows(expectedMessage, exception, () -> { + new RemoteAccessVpnManagerImpl().throwExceptionOnValidateIpRangeError(exception, expectedMessage); + }); + + assertEquals(expectedMessage, assertThrows.getMessage()); + } +} From 944d5d93373a4288e4c899aea8d4cef295f62dcb Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Tue, 27 Apr 2021 16:37:51 -0300 Subject: [PATCH 2/7] Few refactors Use CollectionUtils to validate collections Use diamond inference when instantiate object Remove explicit unboxing --- .../vpn/RemoteAccessVpnManagerImpl.java | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java b/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java index b316cf4f0f53..afbb3a0ad9d8 100644 --- a/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java +++ b/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java @@ -143,8 +143,6 @@ public RemoteAccessVpn createRemoteAccessVpn(final long publicIpId, String ipRan CallContext ctx = CallContext.current(); final Account caller = ctx.getCallingAccount(); - Long networkId = null; - // make sure ip address exists final PublicIpAddress ipAddr = _networkMgr.getPublicIpAddress(publicIpId); if (ipAddr == null) { @@ -159,7 +157,7 @@ public RemoteAccessVpn createRemoteAccessVpn(final long publicIpId, String ipRan IPAddressVO ipAddress = _ipAddressDao.findById(publicIpId); - networkId = ipAddress.getAssociatedWithNetworkId(); + Long networkId = ipAddress.getAssociatedWithNetworkId(); if (networkId != null) { _networkMgr.checkIpForService(ipAddress, Service.Vpn, null); } @@ -258,8 +256,8 @@ private void validateRemoteAccessVpnConfiguration() throws ConfigurationExceptio s_logger.warn("Remote Access VPN global configuration missing client ip range -- ignoring"); return; } - Integer pskLength = _pskLength; - if (pskLength != null && (pskLength < 8 || pskLength > 256)) { + + if (_pskLength < 8 || _pskLength > 256) { throw new ConfigurationException("Remote Access VPN: IPSec preshared key length should be between 8 and 256"); } @@ -326,9 +324,9 @@ public boolean destroyRemoteAccessVpnForIp(long ipId, Account caller, final bool final List vpnFwRules = _rulesDao.listByIpAndPurpose(ipId, Purpose.Vpn); boolean applyFirewall = false; - final List fwRules = new ArrayList(); + final List fwRules = new ArrayList<>(); //if related firewall rule is created for the first vpn port, it would be created for the 2 other ports as well, so need to cleanup the backend - if (vpnFwRules.size() != 0 && _rulesDao.findByRelatedId(vpnFwRules.get(0).getId()) != null) { + if (CollectionUtils.isNotEmpty(vpnFwRules) && _rulesDao.findByRelatedId(vpnFwRules.get(0).getId()) != null) { applyFirewall = true; } @@ -370,7 +368,7 @@ public void doInTransactionWithoutResult(TransactionStatus status) { for (FirewallRule vpnFwRule : vpnFwRules) { _rulesDao.remove(vpnFwRule.getId()); s_logger.debug("Successfully removed firewall rule with ip id=" + vpnFwRule.getSourceIpAddressId() + " and port " + - vpnFwRule.getSourcePortStart().intValue() + " as a part of vpn cleanup"); + vpnFwRule.getSourcePortStart() + " as a part of vpn cleanup"); } } } @@ -548,13 +546,13 @@ public boolean applyVpnUsers(long vpnOwnerId, String userName) throws ResourceU String[] results = element.applyVpnUsers(vpn, users); if (results != null) { int indexUser = -1; - for (int i = 0; i < results.length; i++) { + for (String result : results) { indexUser ++; if (indexUser == users.size()) { indexUser = 0; // results on multiple VPC routers are combined in commit 13eb789, reset user index if one VR is done. } - s_logger.debug("VPN User " + users.get(indexUser) + (results[i] == null ? " is set on " : (" couldn't be set due to " + results[i]) + " on ") + vpn.getUuid()); - if (results[i] == null) { + s_logger.debug("VPN User " + users.get(indexUser) + (result == null ? " is set on " : (" couldn't be set due to " + result) + " on ") + vpn.getUuid()); + if (result == null) { if (finals[indexUser] == null) { finals[indexUser] = true; } @@ -615,9 +613,9 @@ public Pair, Integer> searchForVpnUsers(ListVpnUsersCmd Long id = cmd.getId(); String keyword = cmd.getKeyword(); Account caller = CallContext.current().getCallingAccount(); - List permittedAccounts = new ArrayList(); + List permittedAccounts = new ArrayList<>(); - Ternary domainIdRecursiveListProject = new Ternary(cmd.getDomainId(), cmd.isRecursive(), null); + Ternary domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null); _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false); Long domainId = domainIdRecursiveListProject.first(); Boolean isRecursive = domainIdRecursiveListProject.second(); @@ -651,7 +649,7 @@ public Pair, Integer> searchForVpnUsers(ListVpnUsersCmd } Pair, Integer> result = _vpnUsersDao.searchAndCount(sc, searchFilter); - return new Pair, Integer>(result.first(), result.second()); + return new Pair<>(result.first(), result.second()); } @Override @@ -659,7 +657,7 @@ public Pair, Integer> searchForRemoteAccessVpns( // do some parameter validation Account caller = CallContext.current().getCallingAccount(); Long ipAddressId = cmd.getPublicIpId(); - List permittedAccounts = new ArrayList(); + List permittedAccounts = new ArrayList<>(); Long vpnId = cmd.getId(); Long networkId = cmd.getNetworkId(); @@ -677,7 +675,7 @@ public Pair, Integer> searchForRemoteAccessVpns( _accountMgr.checkAccess(caller, null, true, publicIp); } - Ternary domainIdRecursiveListProject = new Ternary(cmd.getDomainId(), cmd.isRecursive(), null); + Ternary domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null); _accountMgr.buildACLSearchParameters(caller, null, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false); Long domainId = domainIdRecursiveListProject.first(); Boolean isRecursive = domainIdRecursiveListProject.second(); @@ -712,7 +710,7 @@ public Pair, Integer> searchForRemoteAccessVpns( } Pair, Integer> result = _remoteAccessVpnDao.searchAndCount(sc, filter); - return new Pair, Integer>(result.first(), result.second()); + return new Pair<>(result.first(), result.second()); } @Override @@ -751,7 +749,7 @@ public RemoteAccessVpn getRemoteAccessVpnById(long vpnId) { } public List getRemoteAccessVPNServiceProviders() { - List result = new ArrayList(); + List result = new ArrayList<>(); for (Iterator e = _vpnServiceProviders.iterator(); e.hasNext();) { result.add(e.next()); } From 1e7bedd1eb6a96b3853d3be2ea0505ca518b53c6 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Tue, 27 Apr 2021 16:43:51 -0300 Subject: [PATCH 3/7] Remove useless comments --- .../vpn/RemoteAccessVpnManagerImpl.java | 30 ++++--------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java b/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java index afbb3a0ad9d8..e873ca4d172f 100644 --- a/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java +++ b/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java @@ -143,7 +143,6 @@ public RemoteAccessVpn createRemoteAccessVpn(final long publicIpId, String ipRan CallContext ctx = CallContext.current(); final Account caller = ctx.getCallingAccount(); - // make sure ip address exists final PublicIpAddress ipAddr = _networkMgr.getPublicIpAddress(publicIpId); if (ipAddr == null) { throw new InvalidParameterValueException("Unable to create remote access vpn, invalid public IP address id" + publicIpId); @@ -163,10 +162,8 @@ public RemoteAccessVpn createRemoteAccessVpn(final long publicIpId, String ipRan } final Long vpcId = ipAddress.getVpcId(); - /* IP Address used for VPC must be the source NAT IP of whole VPC */ if (vpcId != null && ipAddress.isSourceNat()) { assert networkId == null; - // No firewall setting for VPC, it would be open internally openFirewall = false; } @@ -180,7 +177,6 @@ public RemoteAccessVpn createRemoteAccessVpn(final long publicIpId, String ipRan RemoteAccessVpnVO vpnVO = _remoteAccessVpnDao.findByPublicIpAddress(publicIpId); if (vpnVO != null) { - //if vpn is in Added state, return it to the api if (vpnVO.getState() == RemoteAccessVpn.State.Added) { return vpnVO; } @@ -197,35 +193,28 @@ public RemoteAccessVpn createRemoteAccessVpn(final long publicIpId, String ipRan Pair cidr = null; - // TODO: assumes one virtual network / domr per account per zone if (networkId != null) { vpnVO = _remoteAccessVpnDao.findByAccountAndNetwork(ipAddr.getAccountId(), networkId); if (vpnVO != null) { - //if vpn is in Added state, return it to the api if (vpnVO.getState() == RemoteAccessVpn.State.Added) { return vpnVO; } throw new InvalidParameterValueException("A Remote Access VPN already exists for this account"); } - //Verify that vpn service is enabled for the network Network network = _networkMgr.getNetwork(networkId); if (!_networkMgr.areServicesSupportedInNetwork(network.getId(), Service.Vpn)) { throw new InvalidParameterValueException("Vpn service is not supported in network id=" + ipAddr.getAssociatedWithNetworkId()); } cidr = NetUtils.getCidr(network.getCidr()); - } else { // Don't need to check VPC because there is only one IP(source NAT IP) available for VPN + } else { Vpc vpc = _vpcDao.findById(vpcId); cidr = NetUtils.getCidr(vpc.getCidr()); } - // FIXME: This check won't work for the case where the guest ip range - // changes depending on the vlan allocated. String[] guestIpRange = NetUtils.getIpRangeFromCidr(cidr.first(), cidr.second()); if (NetUtils.ipRangesOverlap(range[0], range[1], guestIpRange[0], guestIpRange[1])) { throw new InvalidParameterValueException("Invalid ip range: " + ipRange + " overlaps with guest ip range " + guestIpRange[0] + "-" + guestIpRange[1]); } - // TODO: check sufficient range - // TODO: check overlap with private and public ip ranges in datacenter long startIp = NetUtils.ip2Long(range[0]); final String newIpRange = NetUtils.long2Ip(++startIp) + "-" + range[1]; @@ -264,7 +253,7 @@ private void validateRemoteAccessVpnConfiguration() throws ConfigurationExceptio validateIpRange(ipRange, ConfigurationException.class); } - protected void validateIpRange(String ipRange, Class exceptionClass) throws T { + protected void validateIpRange(String ipRange, Class exceptionClass) throws T { String[] range = ipRange.split("-"); if (range.length != 2) { @@ -320,12 +309,11 @@ public boolean destroyRemoteAccessVpnForIp(long ipId, Account caller, final bool success = false; } finally { if (success|| forceCleanup) { - //Cleanup corresponding ports final List vpnFwRules = _rulesDao.listByIpAndPurpose(ipId, Purpose.Vpn); boolean applyFirewall = false; final List fwRules = new ArrayList<>(); - //if related firewall rule is created for the first vpn port, it would be created for the 2 other ports as well, so need to cleanup the backend + if (CollectionUtils.isNotEmpty(vpnFwRules) && _rulesDao.findByRelatedId(vpnFwRules.get(0).getId()) != null) { applyFirewall = true; } @@ -335,7 +323,6 @@ public boolean destroyRemoteAccessVpnForIp(long ipId, Account caller, final bool @Override public void doInTransactionWithoutResult(TransactionStatus status) { for (FirewallRule vpnFwRule : vpnFwRules) { - //don't apply on the backend yet; send all 3 rules in a banch _firewallMgr.revokeRelatedFirewallRule(vpnFwRule.getId(), false); fwRules.add(_rulesDao.findByRelatedId(vpnFwRule.getId())); } @@ -344,7 +331,6 @@ public void doInTransactionWithoutResult(TransactionStatus status) { } }); - //now apply vpn rules on the backend s_logger.debug("Reapplying firewall rules for ip id=" + ipId + " as a part of disable remote access vpn"); success = _firewallMgr.applyIngressFirewallRules(ipId, caller); } @@ -355,10 +341,9 @@ public void doInTransactionWithoutResult(TransactionStatus status) { @Override public void doInTransactionWithoutResult(TransactionStatus status) { _remoteAccessVpnDao.remove(vpn.getId()); - // Stop billing of VPN users when VPN is removed. VPN_User_ADD events will be generated when VPN is created again + List vpnUsers = _vpnUsersDao.listByAccount(vpn.getAccountId()); for (VpnUserVO user : vpnUsers) { - // VPN_USER_REMOVE event is already generated for users in Revoke state if (user.getState() != VpnUser.State.Revoke) { UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VPN_USER_REMOVE, user.getAccountId(), 0, user.getId(), user.getUsername(), user.getClass().getName(), user.getUuid()); @@ -403,7 +388,6 @@ public VpnUser doInTransaction(TransactionStatus status) { } _accountMgr.checkAccess(caller, null, true, owner); - //don't allow duplicated user names for the same account VpnUserVO vpnUser = _vpnUsersDao.findByAccountAndUsername(owner.getId(), username); if (vpnUser != null) { throw new InvalidParameterValueException("VPN User with name " + username + " is already added for account " + owner); @@ -495,7 +479,6 @@ public void doInTransactionWithoutResult(TransactionStatus status) { vpn.setState(RemoteAccessVpn.State.Running); _remoteAccessVpnDao.update(vpn.getId(), vpn); - // Start billing of existing VPN users in ADD and Active state List vpnUsers = _vpnUsersDao.listByAccount(vpn.getAccountId()); for (VpnUserVO user : vpnUsers) { if (user.getState() != VpnUser.State.Revoke) { @@ -528,7 +511,6 @@ public boolean applyVpnUsers(long vpnOwnerId, String userName) throws ResourceU List users = _vpnUsersDao.listByAccount(vpnOwnerId); - //If user is in Active state, we still have to resend them therefore their status has to be Add for (VpnUserVO user : users) { if (user.getState() == State.Active) { user.setState(State.Add); @@ -549,7 +531,7 @@ public boolean applyVpnUsers(long vpnOwnerId, String userName) throws ResourceU for (String result : results) { indexUser ++; if (indexUser == users.size()) { - indexUser = 0; // results on multiple VPC routers are combined in commit 13eb789, reset user index if one VR is done. + indexUser = 0; } s_logger.debug("VPN User " + users.get(indexUser) + (result == null ? " is set on " : (" couldn't be set due to " + result) + " on ") + vpn.getUuid()); if (result == null) { @@ -633,7 +615,6 @@ public Pair, Integer> searchForVpnUsers(ListVpnUsersCmd SearchCriteria sc = sb.create(); _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria); - //list only active users sc.setParameters("state", State.Active, State.Add); if(keyword != null){ @@ -654,7 +635,6 @@ public Pair, Integer> searchForVpnUsers(ListVpnUsersCmd @Override public Pair, Integer> searchForRemoteAccessVpns(ListRemoteAccessVpnsCmd cmd) { - // do some parameter validation Account caller = CallContext.current().getCallingAccount(); Long ipAddressId = cmd.getPublicIpId(); List permittedAccounts = new ArrayList<>(); From e49fddf0b31d8328cf6c5596761c7e69df57c870 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Tue, 27 Apr 2021 16:48:09 -0300 Subject: [PATCH 4/7] Improve logs on RemoteAccessVpnManagerImpl --- .../java/com/cloud/network/VpnUserVO.java | 2 +- .../main/java/com/cloud/user/AccountVO.java | 2 +- .../vpn/RemoteAccessVpnManagerImpl.java | 42 +++++++++++-------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/engine/schema/src/main/java/com/cloud/network/VpnUserVO.java b/engine/schema/src/main/java/com/cloud/network/VpnUserVO.java index d6c4b603529b..10f6bc00ae39 100644 --- a/engine/schema/src/main/java/com/cloud/network/VpnUserVO.java +++ b/engine/schema/src/main/java/com/cloud/network/VpnUserVO.java @@ -114,7 +114,7 @@ public long getDomainId() { @Override public String toString() { - return new StringBuilder("VpnUser[").append(id).append("-").append(username).append("-").append(accountId).append("]").toString(); + return String.format("VPN user {\"id\": %s, \"name\": \"%s\", \"uuid\": \"%s\", \"accountId\": %s}", id, username, uuid, accountId); } @Override diff --git a/engine/schema/src/main/java/com/cloud/user/AccountVO.java b/engine/schema/src/main/java/com/cloud/user/AccountVO.java index 983498902699..62204e823ae2 100644 --- a/engine/schema/src/main/java/com/cloud/user/AccountVO.java +++ b/engine/schema/src/main/java/com/cloud/user/AccountVO.java @@ -188,7 +188,7 @@ public long getAccountId() { @Override public String toString() { - return new StringBuilder("Acct[").append(uuid).append("-").append(accountName).append("]").toString(); + return String.format("Account {\"id\": %s, \"name\": \"%s\", \"uuid\": \"%s\"}", id, accountName, uuid); } @Override diff --git a/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java b/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java index e873ca4d172f..77238fd6d48d 100644 --- a/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java +++ b/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java @@ -92,6 +92,7 @@ import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; import java.lang.reflect.InvocationTargetException; +import java.util.stream.Collectors; import org.apache.commons.collections.CollectionUtils; public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAccessVpnService, Configurable { @@ -145,7 +146,7 @@ public RemoteAccessVpn createRemoteAccessVpn(final long publicIpId, String ipRan final PublicIpAddress ipAddr = _networkMgr.getPublicIpAddress(publicIpId); if (ipAddr == null) { - throw new InvalidParameterValueException("Unable to create remote access vpn, invalid public IP address id" + publicIpId); + throw new InvalidParameterValueException(String.format("Unable to create remote access VPN, invalid public IP address {\"id\": %s}.", publicIpId)); } _accountMgr.checkAccess(caller, null, true, ipAddr); @@ -180,7 +181,8 @@ public RemoteAccessVpn createRemoteAccessVpn(final long publicIpId, String ipRan if (vpnVO.getState() == RemoteAccessVpn.State.Added) { return vpnVO; } - throw new InvalidParameterValueException("A Remote Access VPN already exists for this public Ip address"); + + throw new InvalidParameterValueException(String.format("A remote Access VPN already exists for the public IP address [%s].", ipAddr.getAddress().toString())); } if (ipRange == null) { @@ -194,12 +196,14 @@ public RemoteAccessVpn createRemoteAccessVpn(final long publicIpId, String ipRan Pair cidr = null; if (networkId != null) { - vpnVO = _remoteAccessVpnDao.findByAccountAndNetwork(ipAddr.getAccountId(), networkId); + long ipAddressOwner = ipAddr.getAccountId(); + vpnVO = _remoteAccessVpnDao.findByAccountAndNetwork(ipAddressOwner, networkId); if (vpnVO != null) { if (vpnVO.getState() == RemoteAccessVpn.State.Added) { return vpnVO; } - throw new InvalidParameterValueException("A Remote Access VPN already exists for this account"); + + throw new InvalidParameterValueException(String.format("A remote access VPN already exists for the account [%s].", ipAddressOwner)); } Network network = _networkMgr.getNetwork(networkId); if (!_networkMgr.areServicesSupportedInNetwork(network.getId(), Service.Vpn)) { @@ -242,17 +246,17 @@ public RemoteAccessVpn doInTransaction(TransactionStatus status) throws NetworkR private void validateRemoteAccessVpnConfiguration() throws ConfigurationException { String ipRange = RemoteAccessVpnClientIpRange.value(); if (ipRange == null) { - s_logger.warn("Remote Access VPN global configuration missing client ip range -- ignoring"); + s_logger.warn(String.format("Remote access VPN configuration: Global configuration [%s] missing client IP range.", RemoteAccessVpnClientIpRange.key())); return; } if (_pskLength < 8 || _pskLength > 256) { - throw new ConfigurationException("Remote Access VPN: IPSec preshared key length should be between 8 and 256"); + throw new ConfigurationException(String.format("Remote access VPN configuration: IPSec preshared key length [%s] should be between 8 and 256.", _pskLength)); } validateIpRange(ipRange, ConfigurationException.class); } - + protected void validateIpRange(String ipRange, Class exceptionClass) throws T { String[] range = ipRange.split("-"); @@ -359,7 +363,8 @@ public void doInTransactionWithoutResult(TransactionStatus status) { } }); } catch (Exception ex) { - s_logger.warn("Unable to release the three vpn ports from the firewall rules", ex); + s_logger.warn(String.format("Unable to release the VPN ports from the firewall rules [%s] due to [%s]", fwRules.stream().map(rule -> + String.format("{\"ipId\": %s, \"port\": %s}", rule.getSourceIpAddressId(), rule.getSourcePortStart())).collect(Collectors.joining(", ")), ex.getMessage()), ex); } } } @@ -373,7 +378,7 @@ public VpnUser addVpnUser(final long vpnOwnerId, final String username, final St final Account caller = CallContext.current().getCallingAccount(); if (!username.matches("^[a-zA-Z0-9][a-zA-Z0-9@._-]{2,63}$")) { - throw new InvalidParameterValueException("Username has to be begin with an alphabet have 3-64 characters including alphabets, numbers and the set '@.-_'"); + throw new InvalidParameterValueException(String.format("Username [%s] is invalid. Username has to begin with an alphabet have 3-64 characters including alphabets, numbers and the set '@.-_'.", username)); } if (!password.matches("^[a-zA-Z0-9][a-zA-Z0-9@+=._-]{2,31}$")) { throw new InvalidParameterValueException("Password has to be 3-32 characters including alphabets, numbers and the set '@+=.-_'"); @@ -384,18 +389,18 @@ public VpnUser addVpnUser(final long vpnOwnerId, final String username, final St public VpnUser doInTransaction(TransactionStatus status) { Account owner = _accountDao.lockRow(vpnOwnerId, true); if (owner == null) { - throw new InvalidParameterValueException("Unable to add vpn user: Another operation active"); + throw new InvalidParameterValueException(String.format("Unable to add VPN user {\"id\": %s, \"username\": \"%s\"}: Another operation is active.", vpnOwnerId, username)); } _accountMgr.checkAccess(caller, null, true, owner); VpnUserVO vpnUser = _vpnUsersDao.findByAccountAndUsername(owner.getId(), username); if (vpnUser != null) { - throw new InvalidParameterValueException("VPN User with name " + username + " is already added for account " + owner); + throw new InvalidParameterValueException("VPN User with name " + username + " is already added for account " + owner); } long userCount = _vpnUsersDao.getVpnUserCount(owner.getId()); if (userCount >= _userLimit) { - throw new AccountLimitException("Cannot add more than " + _userLimit + " remote access vpn users"); + throw new AccountLimitException(String.format("Cannot add more than [%s] remote access VPN users to %s.", _userLimit, owner.toString())); } VpnUser user = _vpnUsersDao.persist(new VpnUserVO(vpnOwnerId, owner.getDomainId(), username, password)); @@ -412,7 +417,7 @@ public VpnUser doInTransaction(TransactionStatus status) { public boolean removeVpnUser(long vpnOwnerId, String username, Account caller) { final VpnUserVO user = _vpnUsersDao.findByAccountAndUsername(vpnOwnerId, username); if (user == null) { - throw new InvalidParameterValueException("Could not find vpn user " + username); + throw new InvalidParameterValueException(String.format("Could not find VPN user=[%s]. VPN owner id=[%s]", username, vpnOwnerId)); } _accountMgr.checkAccess(caller, null, true, user); @@ -499,11 +504,11 @@ public boolean applyVpnUsers(long vpnOwnerId, String userName) throws ResourceU Account owner = _accountDao.findById(vpnOwnerId); _accountMgr.checkAccess(caller, null, true, owner); - s_logger.debug("Applying vpn users for " + owner); + s_logger.debug(String.format("Applying VPN users for %s.", owner.toString())); List vpns = _remoteAccessVpnDao.findByAccount(vpnOwnerId); if (CollectionUtils.isEmpty(vpns)) { - s_logger.debug("There are no remote access vpns configured on this account " + owner +" to apply vpn user, failing add vpn user "); + 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())); return false; } @@ -545,8 +550,8 @@ public boolean applyVpnUsers(long vpnOwnerId, String userName) throws ResourceU } } } - } catch (Exception e) { - s_logger.warn("Unable to apply vpn users ", e); + } catch (ResourceUnavailableException e) { + s_logger.warn(String.format("Unable to apply VPN users [%s] due to [%s].", users.stream().map(user -> user.toString()).collect(Collectors.joining(", ")), e.getMessage()), e); success = false; vpnTemp = vpn; @@ -577,7 +582,8 @@ public void doInTransactionWithoutResult(TransactionStatus status) { } }); } - s_logger.warn("Failed to apply vpn for user " + user.getUsername() + ", accountId=" + user.getAccountId()); + + s_logger.warn(String.format("Failed to apply VPN for %s.", user.toString())); } } From 1b326623f1822f2876acca91443b0a75f2f0a313 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Fri, 30 Apr 2021 16:19:05 -0300 Subject: [PATCH 5/7] Change toString to guarantee backward compatibility --- engine/schema/src/main/java/com/cloud/user/AccountVO.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/schema/src/main/java/com/cloud/user/AccountVO.java b/engine/schema/src/main/java/com/cloud/user/AccountVO.java index 62204e823ae2..36b2508c1099 100644 --- a/engine/schema/src/main/java/com/cloud/user/AccountVO.java +++ b/engine/schema/src/main/java/com/cloud/user/AccountVO.java @@ -188,7 +188,7 @@ public long getAccountId() { @Override public String toString() { - return String.format("Account {\"id\": %s, \"name\": \"%s\", \"uuid\": \"%s\"}", id, accountName, uuid); + return String.format("Acct[%s-%s] -- Account {\"id\": %s, \"name\": \"%s\", \"uuid\": \"%s\"}", uuid, accountName, id, accountName, uuid); } @Override From 7a19ff4971976ed328c1984a004009599405a829 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador <38945620+GutoVeronezi@users.noreply.github.com> Date: Wed, 5 May 2021 15:49:01 -0300 Subject: [PATCH 6/7] Remove copyright line in license header --- .../com/cloud/network/vpn/RemoteAccessVpnManagerImplTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/server/src/test/java/com/cloud/network/vpn/RemoteAccessVpnManagerImplTest.java b/server/src/test/java/com/cloud/network/vpn/RemoteAccessVpnManagerImplTest.java index fd9d1e0b8f6f..5b0dd69835a2 100644 --- a/server/src/test/java/com/cloud/network/vpn/RemoteAccessVpnManagerImplTest.java +++ b/server/src/test/java/com/cloud/network/vpn/RemoteAccessVpnManagerImplTest.java @@ -1,6 +1,4 @@ /* - * Copyright 2021 The Apache Software Foundation. - * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at From 633156d1a4e93399efae74a3c126b6d5a0406263 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Mon, 10 May 2021 10:59:34 -0300 Subject: [PATCH 7/7] Rename methods --- .../vpn/RemoteAccessVpnManagerImpl.java | 10 ++--- .../vpn/RemoteAccessVpnManagerImplTest.java | 40 +++++++++---------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java b/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java index 77238fd6d48d..b923099b8847 100644 --- a/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java +++ b/server/src/main/java/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java @@ -261,19 +261,19 @@ protected void validateIpRange(String ipRange, Class ex String[] range = ipRange.split("-"); if (range.length != 2) { - throwExceptionOnValidateIpRangeError(exceptionClass, String.format("IP range [%s] is an invalid IP range.", ipRange)); + handleExceptionOnValidateIpRangeError(exceptionClass, String.format("IP range [%s] is an invalid IP range.", ipRange)); } if (!NetUtils.isValidIp4(range[0]) || !NetUtils.isValidIp4(range[1])) { - throwExceptionOnValidateIpRangeError(exceptionClass, String.format("One or both IPs sets in the range [%s] are invalid IPs.", ipRange)); + handleExceptionOnValidateIpRangeError(exceptionClass, String.format("One or both IPs sets in the range [%s] are invalid IPs.", ipRange)); } if (!NetUtils.validIpRange(range[0], range[1])) { - throwExceptionOnValidateIpRangeError(exceptionClass, String.format("Range of IPs [%s] is invalid.", ipRange)); + handleExceptionOnValidateIpRangeError(exceptionClass, String.format("Range of IPs [%s] is invalid.", ipRange)); } } - protected void throwExceptionOnValidateIpRangeError(Class exceptionClass, String errorMessage) throws T { + protected void handleExceptionOnValidateIpRangeError(Class exceptionClass, String errorMessage) throws T { try { throw exceptionClass.getConstructor(String.class).newInstance(errorMessage); } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { @@ -395,7 +395,7 @@ public VpnUser doInTransaction(TransactionStatus status) { VpnUserVO vpnUser = _vpnUsersDao.findByAccountAndUsername(owner.getId(), username); if (vpnUser != null) { - throw new InvalidParameterValueException("VPN User with name " + username + " is already added for account " + owner); + throw new InvalidParameterValueException("VPN User with name " + username + " is already added for account " + owner); } long userCount = _vpnUsersDao.getVpnUserCount(owner.getId()); diff --git a/server/src/test/java/com/cloud/network/vpn/RemoteAccessVpnManagerImplTest.java b/server/src/test/java/com/cloud/network/vpn/RemoteAccessVpnManagerImplTest.java index 5b0dd69835a2..cff95ae4f3df 100644 --- a/server/src/test/java/com/cloud/network/vpn/RemoteAccessVpnManagerImplTest.java +++ b/server/src/test/java/com/cloud/network/vpn/RemoteAccessVpnManagerImplTest.java @@ -145,76 +145,76 @@ public void validateValidateIpRangeValidIpRangeMustValidate(){ new RemoteAccessVpnManagerImpl().validateIpRange(ipRange, expectedException); } - private void throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(Class exceptionToCatch){ - throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(exceptionToCatch, "Test"); + private void handleExceptionOnValidateIpRangeErrorMustThrowCloudRuntimeException(Class exceptionToCatch){ + handleExceptionOnValidateIpRangeErrorMustThrowCloudRuntimeException(exceptionToCatch, "Test"); } - private void throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(Class exceptionToCatch, String exceptionMessage){ + private void handleExceptionOnValidateIpRangeErrorMustThrowCloudRuntimeException(Class exceptionToCatch, String exceptionMessage){ String errorMessage = "Test"; String expectedMessage = String.format("Unexpected exception [%s] while throwing error [%s] on validateIpRange.", exceptionMessage, errorMessage); CloudRuntimeException assertThrows = Assert.assertThrows(expectedMessage, cloudRuntimeException, () -> { - new RemoteAccessVpnManagerImpl().throwExceptionOnValidateIpRangeError(exceptionToCatch, errorMessage); + new RemoteAccessVpnManagerImpl().handleExceptionOnValidateIpRangeError(exceptionToCatch, errorMessage); }); assertEquals(expectedMessage, assertThrows.getMessage()); } @Test - public void validateThrowExceptionOnValidateIpRangeErrorWhenNoSuchMethodExceptionThrowCloudRuntimeException(){ + public void validateHandleExceptionOnValidateIpRangeErrorWhenNoSuchMethodExceptionThrowCloudRuntimeException(){ Class exception = NoSuchMethodException.class; - throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(exception); + handleExceptionOnValidateIpRangeErrorMustThrowCloudRuntimeException(exception); } @Test - public void validateThrowExceptionOnValidateIpRangeErrorWhenSecurityExceptionThrowCloudRuntimeException(){ + public void validateHandleExceptionOnValidateIpRangeErrorWhenSecurityExceptionThrowCloudRuntimeException(){ Class exception = SecurityException.class; - throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(exception); + handleExceptionOnValidateIpRangeErrorMustThrowCloudRuntimeException(exception); } @Test - public void validateThrowExceptionOnValidateIpRangeErrorWhenInstantiationExceptionThrowCloudRuntimeException(){ + public void validateHandleExceptionOnValidateIpRangeErrorWhenInstantiationExceptionThrowCloudRuntimeException(){ Class exception = InstantiationException.class; - throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(exception); + handleExceptionOnValidateIpRangeErrorMustThrowCloudRuntimeException(exception); } @Test - public void validateThrowExceptionOnValidateIpRangeErrorWhenIllegalAccessExceptionThrowCloudRuntimeException(){ + public void validateHandleExceptionOnValidateIpRangeErrorWhenIllegalAccessExceptionThrowCloudRuntimeException(){ Class exception = IllegalAccessException.class; - throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(exception); + handleExceptionOnValidateIpRangeErrorMustThrowCloudRuntimeException(exception); } @Test - public void validateThrowExceptionOnValidateIpRangeErrorWhenIllegalArgumentExceptionThrowCloudRuntimeException(){ + public void validateHandleExceptionOnValidateIpRangeErrorWhenIllegalArgumentExceptionThrowCloudRuntimeException(){ Class exception = IllegalArgumentException.class; - throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(exception); + handleExceptionOnValidateIpRangeErrorMustThrowCloudRuntimeException(exception); } @Test - public void validateThrowExceptionOnValidateIpRangeErrorWhenInvocationTargetExceptionThrowCloudRuntimeException(){ + public void validateHandleExceptionOnValidateIpRangeErrorWhenInvocationTargetExceptionThrowCloudRuntimeException(){ Class exception = InvocationTargetException.class; - throwExceptionOnValidateIpRangeMustThrowCloudRuntimeException(exception, "java.lang.reflect.InvocationTargetException.(java.lang.String)"); + handleExceptionOnValidateIpRangeErrorMustThrowCloudRuntimeException(exception, "java.lang.reflect.InvocationTargetException.(java.lang.String)"); } @Test - public void validateThrowExceptionOnValidateIpRangeErrorWhenConfigurationExceptionThrowConfigurationException(){ + public void validateHandleExceptionOnValidateIpRangeErrorWhenConfigurationExceptionThrowConfigurationException(){ Class exception = ConfigurationException.class; String expectedMessage = "Test"; ConfigurationException assertThrows = Assert.assertThrows(expectedMessage, exception, () -> { - new RemoteAccessVpnManagerImpl().throwExceptionOnValidateIpRangeError(exception, expectedMessage); + new RemoteAccessVpnManagerImpl().handleExceptionOnValidateIpRangeError(exception, expectedMessage); }); assertEquals(expectedMessage, assertThrows.getMessage()); } @Test - public void validateThrowExceptionOnValidateIpRangeErrorWhenInvalidParameterValueExceptionThrowInvalidParameterValueException(){ + public void validateHandleExceptionOnValidateIpRangeErrorWhenInvalidParameterValueExceptionThrowInvalidParameterValueException(){ Class exception = InvalidParameterValueException.class; String expectedMessage = "Test"; InvalidParameterValueException assertThrows = Assert.assertThrows(expectedMessage, exception, () -> { - new RemoteAccessVpnManagerImpl().throwExceptionOnValidateIpRangeError(exception, expectedMessage); + new RemoteAccessVpnManagerImpl().handleExceptionOnValidateIpRangeError(exception, expectedMessage); }); assertEquals(expectedMessage, assertThrows.getMessage());