| 1 |
# Copyright (c) 2015 Canonical, Inc. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
import os |
| 17 |
from unittest import mock |
| 18 |
|
| 19 |
from neutron.agent.l3 import agent as neutron_l3_agent |
| 20 |
from neutron.agent.l3 import legacy_router |
| 21 |
from neutron.conf.agent.l3 import config as l3_config |
| 22 |
from neutron.tests.functional import base |
| 23 |
from neutron_lib import constants |
| 24 |
from oslo_config import cfg |
| 25 |
from oslo_utils import uuidutils |
| 26 |
|
| 27 |
from neutron_vpnaas.services.vpn import agent as vpn_agent |
| 28 |
from neutron_vpnaas.services.vpn.device_drivers import ipsec |
| 29 |
from neutron_vpnaas.services.vpn.device_drivers import strongswan_ipsec |
| 30 |
from neutron_vpnaas.tests.functional.common import test_scenario |
| 31 |
|
| 32 |
_uuid = uuidutils.generate_uuid |
| 33 |
FAKE_ROUTER_ID = _uuid() |
| 34 |
FAKE_IPSEC_SITE_CONNECTION1_ID = _uuid() |
| 35 |
FAKE_IPSEC_SITE_CONNECTION2_ID = _uuid() |
| 36 |
FAKE_IKE_POLICY = { |
| 37 |
'ike_version': 'v1', |
| 38 |
'encryption_algorithm': 'aes-128', |
| 39 |
'auth_algorithm': 'sha1', |
| 40 |
'pfs': 'group5' |
| 41 |
} |
| 42 |
|
| 43 |
FAKE_IPSEC_POLICY = { |
| 44 |
'encryption_algorithm': 'aes-128', |
| 45 |
'auth_algorithm': 'sha1', |
| 46 |
'pfs': 'group5' |
| 47 |
} |
| 48 |
|
| 49 |
FAKE_VPN_SERVICE = { |
| 50 |
'id': _uuid(), |
| 51 |
'router_id': FAKE_ROUTER_ID, |
| 52 |
'name': 'myvpn', |
| 53 |
'admin_state_up': True, |
| 54 |
'status': constants.PENDING_CREATE, |
| 55 |
'external_ip': '50.0.0.4', |
| 56 |
'subnet': {'cidr': '10.0.0.0/24'}, |
| 57 |
'ipsec_site_connections': [ |
| 58 |
{'peer_cidrs': ['20.0.0.0/24', |
| 59 |
'30.0.0.0/24'], |
| 60 |
'id': FAKE_IPSEC_SITE_CONNECTION1_ID, |
| 61 |
'external_ip': '50.0.0.4', |
| 62 |
'peer_address': '30.0.0.5', |
| 63 |
'peer_id': '30.0.0.5', |
| 64 |
'psk': 'password', |
| 65 |
'initiator': 'bi-directional', |
| 66 |
'ikepolicy': FAKE_IKE_POLICY, |
| 67 |
'ipsecpolicy': FAKE_IPSEC_POLICY, |
| 68 |
'status': constants.PENDING_CREATE}, |
| 69 |
{'peer_cidrs': ['40.0.0.0/24', |
| 70 |
'50.0.0.0/24'], |
| 71 |
'external_ip': '50.0.0.4', |
| 72 |
'peer_address': '50.0.0.5', |
| 73 |
'peer_id': '50.0.0.5', |
| 74 |
'psk': 'password', |
| 75 |
'id': FAKE_IPSEC_SITE_CONNECTION2_ID, |
| 76 |
'initiator': 'bi-directional', |
| 77 |
'ikepolicy': FAKE_IKE_POLICY, |
| 78 |
'ipsecpolicy': FAKE_IPSEC_POLICY, |
| 79 |
'status': constants.PENDING_CREATE}] |
| 80 |
} |
| 81 |
|
| 82 |
DESIRED_CONN_STATUS = {FAKE_IPSEC_SITE_CONNECTION1_ID: |
| 83 |
{'status': 'DOWN', |
| 84 |
'updated_pending_status': False}, |
| 85 |
FAKE_IPSEC_SITE_CONNECTION2_ID: |
| 86 |
{'status': 'DOWN', |
| 87 |
'updated_pending_status': False}} |
| 88 |
|
| 89 |
FAKE_IKE_POLICY2 = { |
| 90 |
'ike_version': 'v1', |
| 91 |
'encryption_algorithm': 'aes-256', |
| 92 |
'auth_algorithm': 'sha1', |
| 93 |
'pfs': 'group2', |
| 94 |
'lifetime_value': 1800 |
| 95 |
} |
| 96 |
|
| 97 |
FAKE_IPSEC_POLICY2 = { |
| 98 |
'encryption_algorithm': 'aes-256', |
| 99 |
'auth_algorithm': 'sha1', |
| 100 |
'pfs': 'group2', |
| 101 |
'transform_protocol': 'esp', |
| 102 |
'lifetime_value': 1800, |
| 103 |
'encapsulation_mode': 'tunnel' |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
class TestStrongSwanDeviceDriver(base.BaseSudoTestCase): |
| 108 |
|
| 109 |
"""Test the StrongSwan reference implementation of the device driver.""" |
| 110 |
|
| 111 |
def setUp(self): |
| 112 |
super().setUp() |
| 113 |
self.conf = cfg.CONF |
| 114 |
self.conf.register_opts(l3_config.OPTS) |
| 115 |
self.conf.register_opts(ipsec.ipsec_opts, 'ipsec') |
| 116 |
self.conf.register_opts(strongswan_ipsec.strongswan_opts, |
| 117 |
'strongswan') |
| 118 |
self.conf.set_override('state_path', '/tmp') |
| 119 |
|
| 120 |
ri_kwargs = {'router': {'id': FAKE_ROUTER_ID}, |
| 121 |
'agent_conf': self.conf, |
| 122 |
'interface_driver': mock.sentinel.interface_driver} |
| 123 |
self.router = legacy_router.LegacyRouter(router_id=FAKE_ROUTER_ID, |
| 124 |
agent=mock.Mock(), |
| 125 |
**ri_kwargs) |
| 126 |
self.router.router['distributed'] = False |
| 127 |
self.router_id = FAKE_VPN_SERVICE['router_id'] |
| 128 |
|
| 129 |
looping_call_p = mock.patch( |
| 130 |
'oslo_service.loopingcall.FixedIntervalLoopingCall') |
| 131 |
looping_call_p.start() |
| 132 |
|
| 133 |
vpn_service = mock.Mock() |
| 134 |
vpn_service.conf = self.conf |
| 135 |
self.driver = strongswan_ipsec.StrongSwanDriver( |
| 136 |
vpn_service, host=mock.sentinel.host) |
| 137 |
self.driver.routers[FAKE_ROUTER_ID] = self.router |
| 138 |
self.driver.agent_rpc = mock.Mock() |
| 139 |
self.driver._update_nat = mock.Mock() |
| 140 |
self.driver.agent_rpc.get_vpn_services_on_host.return_value = [ |
| 141 |
FAKE_VPN_SERVICE] |
| 142 |
self.addCleanup(self.driver.destroy_router, self.router_id) |
| 143 |
|
| 144 |
self.router.router_namespace.create() |
| 145 |
self.addCleanup(self.router.router_namespace.delete) |
| 146 |
|
| 147 |
def test_process_lifecycle(self): |
| 148 |
""" |
| 149 |
Lifecycle test that validates that the strongswan process could be |
| 150 |
launched, that a connection could be successfully initiated through |
| 151 |
it, and then that it could be terminated and clean up after itself. |
| 152 |
""" |
| 153 |
process = self.driver.ensure_process(self.router_id, |
| 154 |
FAKE_VPN_SERVICE) |
| 155 |
process.enable() |
| 156 |
self.assertTrue(process.active) |
| 157 |
self.assertIn(self.router_id, self.driver.processes) |
| 158 |
self.assertEqual(DESIRED_CONN_STATUS, process.connection_status) |
| 159 |
self.assertIsNotNone(process.namespace) |
| 160 |
|
| 161 |
conf_dir = os.path.join(self.conf.ipsec.config_base_dir, |
| 162 |
self.router_id) |
| 163 |
self.assertTrue(os.path.exists(conf_dir)) |
| 164 |
process.disable() |
| 165 |
self.assertFalse(process.active) |
| 166 |
self.assertFalse(process.connection_status) |
| 167 |
self.assertFalse(os.path.exists(conf_dir)) |
| 168 |
|
| 169 |
|
| 170 |
class TestStrongSwanScenario(test_scenario.TestIPSecBase): |
| 171 |
|
| 172 |
def setUp(self): |
| 173 |
super().setUp() |
| 174 |
self.conf.register_opts(strongswan_ipsec.strongswan_opts, |
| 175 |
'strongswan') |
| 176 |
VPNAAS_STRONGSWAN_DEVICE = ('neutron_vpnaas.services.vpn.' |
| 177 |
'device_drivers.strongswan_ipsec.' |
| 178 |
'StrongSwanDriver') |
| 179 |
cfg.CONF.set_override('vpn_device_driver', |
| 180 |
[VPNAAS_STRONGSWAN_DEVICE], |
| 181 |
'vpnagent') |
| 182 |
self.agent = neutron_l3_agent.L3NATAgentWithStateReport('agent1', |
| 183 |
self.conf) |
| 184 |
self.agent.init_host() |
| 185 |
self.vpn_agent = vpn_agent.L3WithVPNaaS(self.conf) |
| 186 |
vpn_service = mock.Mock() |
| 187 |
vpn_service.conf = self.conf |
| 188 |
self.driver = strongswan_ipsec.StrongSwanDriver( |
| 189 |
vpn_service, host=mock.sentinel.host) |
| 190 |
|
| 191 |
def _override_ikepolicy_for_site(self, site, ikepolicy): |
| 192 |
ipsec_connection = site.vpn_service['ipsec_site_connections'][0] |
| 193 |
ipsec_connection['ikepolicy'] = ikepolicy |
| 194 |
|
| 195 |
def _override_ipsecpolicy_for_site(self, site, ipsecpolicy): |
| 196 |
ipsec_connection = site.vpn_service['ipsec_site_connections'][0] |
| 197 |
ipsec_connection['ipsecpolicy'] = ipsecpolicy |
| 198 |
|
| 199 |
def _override_dpd_for_site(self, site, dpdaction, dpddelay, dpdtimeout): |
| 200 |
ipsec_connection = site.vpn_service['ipsec_site_connections'][0] |
| 201 |
ipsec_connection['dpd_action'] = dpdaction |
| 202 |
ipsec_connection['dpd_interval'] = dpddelay |
| 203 |
ipsec_connection['dpd_timeout'] = dpdtimeout |
| 204 |
|
| 205 |
def _override_auth_algorithm_for_site(self, site, auth): |
| 206 |
ipsec_connection = site.vpn_service['ipsec_site_connections'][0] |
| 207 |
ipsec_connection['ipsecpolicy']['auth_algorithm'] = auth |
| 208 |
ipsec_connection['ikepolicy']['auth_algorithm'] = auth |
| 209 |
|
| 210 |
def test_strongswan_connection_with_non_default_value(self): |
| 211 |
site1 = self.create_site(test_scenario.PUBLIC_NET[4], |
| 212 |
[self.private_nets[1]]) |
| 213 |
site2 = self.create_site(test_scenario.PUBLIC_NET[5], |
| 214 |
[self.private_nets[2]]) |
| 215 |
|
| 216 |
self.check_ping(site1, site2, success=False) |
| 217 |
self.check_ping(site2, site1, success=False) |
| 218 |
|
| 219 |
self.prepare_ipsec_site_connections(site1, site2) |
| 220 |
self._override_ikepolicy_for_site(site1, FAKE_IKE_POLICY2) |
| 221 |
self._override_ikepolicy_for_site(site2, FAKE_IKE_POLICY2) |
| 222 |
self._override_ipsecpolicy_for_site(site1, FAKE_IPSEC_POLICY2) |
| 223 |
self._override_ipsecpolicy_for_site(site2, FAKE_IPSEC_POLICY2) |
| 224 |
self._override_dpd_for_site(site1, 'hold', 60, 240) |
| 225 |
self._override_dpd_for_site(site2, 'hold', 60, 240) |
| 226 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 227 |
|
| 228 |
self.check_ping(site1, site2) |
| 229 |
self.check_ping(site2, site1) |
| 230 |
|
| 231 |
def _test_strongswan_connection_with_auth_algo(self, auth_algo): |
| 232 |
site1 = self.create_site(test_scenario.PUBLIC_NET[4], |
| 233 |
[self.private_nets[1]]) |
| 234 |
site2 = self.create_site(test_scenario.PUBLIC_NET[5], |
| 235 |
[self.private_nets[2]]) |
| 236 |
|
| 237 |
self.check_ping(site1, site2, success=False) |
| 238 |
self.check_ping(site2, site1, success=False) |
| 239 |
|
| 240 |
self.prepare_ipsec_site_connections(site1, site2) |
| 241 |
self._override_auth_algorithm_for_site(site1, auth_algo) |
| 242 |
self._override_auth_algorithm_for_site(site2, auth_algo) |
| 243 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 244 |
|
| 245 |
self.check_ping(site1, site2) |
| 246 |
self.check_ping(site2, site1) |
| 247 |
|
| 248 |
def test_strongswan_connection_with_sha256(self): |
| 249 |
self._test_strongswan_connection_with_auth_algo('sha256') |
| 250 |
|
| 251 |
def test_strongswan_connection_with_sha384(self): |
| 252 |
self._test_strongswan_connection_with_auth_algo('sha384') |
| 253 |
|
| 254 |
def test_strongswan_connection_with_sha512(self): |
| 255 |
self._test_strongswan_connection_with_auth_algo('sha512') |
| 256 |
|
| 257 |
def test_strongswan_connection_with_non_ascii_vpnservice_name(self): |
| 258 |
site1 = self.create_site(test_scenario.PUBLIC_NET[4], |
| 259 |
[self.private_nets[1]]) |
| 260 |
site2 = self.create_site(test_scenario.PUBLIC_NET[5], |
| 261 |
[self.private_nets[2]]) |
| 262 |
site1.vpn_service.update( |
| 263 |
{'name': test_scenario.NON_ASCII_VPNSERVICE_NAME}) |
| 264 |
|
| 265 |
self.check_ping(site1, site2, success=False) |
| 266 |
self.check_ping(site2, site1, success=False) |
| 267 |
|
| 268 |
self.prepare_ipsec_site_connections(site1, site2) |
| 269 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 270 |
|
| 271 |
self.check_ping(site1, site2) |
| 272 |
self.check_ping(site2, site1) |
| 273 |
|
| 274 |
def test_strongswan_connection_with_non_ascii_psk(self): |
| 275 |
site1 = self.create_site(test_scenario.PUBLIC_NET[4], |
| 276 |
[self.private_nets[1]]) |
| 277 |
site2 = self.create_site(test_scenario.PUBLIC_NET[5], |
| 278 |
[self.private_nets[2]]) |
| 279 |
|
| 280 |
self.check_ping(site1, site2, success=False) |
| 281 |
self.check_ping(site2, site1, success=False) |
| 282 |
|
| 283 |
self.prepare_ipsec_site_connections(site1, site2) |
| 284 |
self._update_ipsec_connection(site1, psk=test_scenario.NON_ASCII_PSK) |
| 285 |
self._update_ipsec_connection(site2, psk=test_scenario.NON_ASCII_PSK) |
| 286 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 287 |
|
| 288 |
self.check_ping(site1, site2) |
| 289 |
self.check_ping(site2, site1) |
| 290 |
|
| 291 |
def test_strongswan_connection_with_wrong_non_ascii_psk(self): |
| 292 |
site1 = self.create_site(test_scenario.PUBLIC_NET[4], |
| 293 |
[self.private_nets[1]]) |
| 294 |
site2 = self.create_site(test_scenario.PUBLIC_NET[5], |
| 295 |
[self.private_nets[2]]) |
| 296 |
|
| 297 |
self.check_ping(site1, site2, success=False) |
| 298 |
self.check_ping(site2, site1, success=False) |
| 299 |
|
| 300 |
self.prepare_ipsec_site_connections(site1, site2) |
| 301 |
self._update_ipsec_connection(site1, psk=test_scenario.NON_ASCII_PSK) |
| 302 |
self._update_ipsec_connection(site2, |
| 303 |
psk=test_scenario.NON_ASCII_PSK[:-1]) |
| 304 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 305 |
|
| 306 |
self.check_ping(site1, site2, success=False) |
| 307 |
self.check_ping(site2, site1, success=False) |