| 1 |
# Copyright 2013, Nachi Ueno, NTT I3, 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 |
import copy |
| 16 |
import difflib |
| 17 |
import io |
| 18 |
import os |
| 19 |
import socket |
| 20 |
import time |
| 21 |
from unittest import mock |
| 22 |
|
| 23 |
import netaddr |
| 24 |
from neutron.agent.l3 import dvr_edge_router |
| 25 |
from neutron.agent.l3 import dvr_snat_ns |
| 26 |
from neutron.agent.l3 import legacy_router |
| 27 |
from neutron.agent.l3 import router_info as l3_router_info |
| 28 |
from neutron.agent.linux import iptables_manager |
| 29 |
from neutron.conf.agent.l3 import config as l3_config |
| 30 |
from neutron_lib import constants |
| 31 |
from neutron_lib import context |
| 32 |
from neutron_lib.exceptions import vpn as vpn_exception |
| 33 |
from oslo_config import cfg |
| 34 |
from oslo_utils import uuidutils |
| 35 |
|
| 36 |
from neutron_vpnaas.services.vpn.device_drivers import ipsec as openswan_ipsec |
| 37 |
from neutron_vpnaas.services.vpn.device_drivers import libreswan_ipsec |
| 38 |
from neutron_vpnaas.services.vpn.device_drivers import strongswan_ipsec |
| 39 |
from neutron_vpnaas.tests import base |
| 40 |
|
| 41 |
# Note: process_id == router_id == vpnservice_id |
| 42 |
|
| 43 |
_uuid = uuidutils.generate_uuid |
| 44 |
FAKE_UUID = _uuid() |
| 45 |
FAKE_HOST = 'fake_host' |
| 46 |
FAKE_ROUTER_ID = FAKE_UUID |
| 47 |
FAKE_VPNSERVICE_ID = FAKE_UUID |
| 48 |
FAKE_PROCESS_ID = FAKE_UUID |
| 49 |
FAKE_IPSEC_SITE_CONNECTION1_ID = _uuid() |
| 50 |
FAKE_IPSEC_SITE_CONNECTION2_ID = _uuid() |
| 51 |
FAKE_IKE_POLICY = { |
| 52 |
'ike_version': 'v1', |
| 53 |
'encryption_algorithm': 'aes-128', |
| 54 |
'auth_algorithm': 'sha1', |
| 55 |
'pfs': 'group5', |
| 56 |
'lifetime_value': 3600 |
| 57 |
} |
| 58 |
|
| 59 |
FAKE_IPSEC_POLICY = { |
| 60 |
'encryption_algorithm': 'aes-128', |
| 61 |
'auth_algorithm': 'sha1', |
| 62 |
'pfs': 'group5', |
| 63 |
'transform_protocol': 'esp', |
| 64 |
'lifetime_value': 3600, |
| 65 |
'encapsulation_mode': 'tunnel' |
| 66 |
} |
| 67 |
|
| 68 |
FAKE_VPN_SERVICE = { |
| 69 |
'id': FAKE_VPNSERVICE_ID, |
| 70 |
'router_id': FAKE_ROUTER_ID, |
| 71 |
'name': 'myvpn', |
| 72 |
'admin_state_up': True, |
| 73 |
'status': constants.PENDING_CREATE, |
| 74 |
'external_ip': '60.0.0.4', |
| 75 |
'ipsec_site_connections': [ |
| 76 |
{'peer_cidrs': ['20.0.0.0/24', |
| 77 |
'30.0.0.0/24'], |
| 78 |
'local_cidrs': ['10.0.0.0/24'], |
| 79 |
'local_ip_vers': 4, |
| 80 |
'admin_state_up': True, |
| 81 |
'id': FAKE_IPSEC_SITE_CONNECTION1_ID, |
| 82 |
'external_ip': '60.0.0.4', |
| 83 |
'local_id': '60.0.0.4', |
| 84 |
'peer_address': '60.0.0.5', |
| 85 |
'mtu': 1500, |
| 86 |
'peer_id': '60.0.0.5', |
| 87 |
'psk': 'password', |
| 88 |
'initiator': 'bi-directional', |
| 89 |
'ikepolicy': FAKE_IKE_POLICY, |
| 90 |
'ipsecpolicy': FAKE_IPSEC_POLICY, |
| 91 |
'dpd_action': 'hold', |
| 92 |
'dpd_interval': 30, |
| 93 |
'dpd_timeout': 120, |
| 94 |
'status': constants.PENDING_CREATE}, |
| 95 |
{'peer_cidrs': ['40.0.0.0/24', |
| 96 |
'50.0.0.0/24'], |
| 97 |
'local_cidrs': ['11.0.0.0/24'], |
| 98 |
'local_ip_vers': 4, |
| 99 |
'admin_state_up': True, |
| 100 |
'external_ip': '60.0.0.4', |
| 101 |
'local_id': '60.0.0.4', |
| 102 |
'peer_address': '60.0.0.6', |
| 103 |
'peer_id': '60.0.0.6', |
| 104 |
'mtu': 1500, |
| 105 |
'psk': 'password', |
| 106 |
'id': FAKE_IPSEC_SITE_CONNECTION2_ID, |
| 107 |
'initiator': 'bi-directional', |
| 108 |
'ikepolicy': FAKE_IKE_POLICY, |
| 109 |
'ipsecpolicy': FAKE_IPSEC_POLICY, |
| 110 |
'dpd_action': 'hold', |
| 111 |
'dpd_interval': 30, |
| 112 |
'dpd_timeout': 120, |
| 113 |
'status': constants.PENDING_CREATE}] |
| 114 |
} |
| 115 |
|
| 116 |
AUTH_ESP = '''esp |
| 117 |
# [encryption_algorithm]-[auth_algorithm];[pfs] |
| 118 |
phase2alg=aes128-sha1;modp1536''' |
| 119 |
|
| 120 |
AUTH_AH = '''ah |
| 121 |
# AH protocol does not support encryption |
| 122 |
# [auth_algorithm];[pfs] |
| 123 |
phase2alg=sha1;modp1536''' |
| 124 |
|
| 125 |
OPENSWAN_CONNECTION_DETAILS = '''# rightsubnet=networkA/netmaskA, networkB/netmaskB (IKEv2 only) |
| 126 |
# [mtu] |
| 127 |
mtu=1500 |
| 128 |
# [dpd_action] |
| 129 |
dpdaction=%(dpd_action)s |
| 130 |
# [dpd_interval] |
| 131 |
dpddelay=%(dpd_delay)s |
| 132 |
# [dpd_timeout] |
| 133 |
dpdtimeout=%(dpd_timeout)s |
| 134 |
# [auth_mode] |
| 135 |
authby=secret |
| 136 |
###################### |
| 137 |
# IKEPolicy params |
| 138 |
###################### |
| 139 |
#ike version |
| 140 |
ikev2=never |
| 141 |
# [encryption_algorithm]-[auth_algorithm];[pfs] |
| 142 |
ike=aes128-sha1;modp1536 |
| 143 |
# [lifetime_value] |
| 144 |
ikelifetime=%(ike_lifetime)ss |
| 145 |
# NOTE: it looks lifetime_units=kilobytes can't be enforced \ |
| 146 |
(could be seconds, hours, days...) |
| 147 |
########################## |
| 148 |
# IPsecPolicys params |
| 149 |
########################## |
| 150 |
# [transform_protocol] |
| 151 |
phase2=%(auth_mode)s |
| 152 |
# [encapsulation_mode] |
| 153 |
type=%(encapsulation_mode)s |
| 154 |
# [lifetime_value] |
| 155 |
lifetime=%(life_time)ss |
| 156 |
# lifebytes=100000 if lifetime_units=kilobytes (IKEv2 only) |
| 157 |
''' # noqa: E501 |
| 158 |
|
| 159 |
IPV4_NEXT_HOP = '''# NOTE: a default route is required for %defaultroute to work... |
| 160 |
leftnexthop=%defaultroute |
| 161 |
rightnexthop=%defaultroute''' # noqa: E501 |
| 162 |
|
| 163 |
IPV6_NEXT_HOP = '''# To recognize the given IP addresses in this config |
| 164 |
# as IPv6 addresses by pluto whack. Default is ipv4 |
| 165 |
connaddrfamily=ipv6 |
| 166 |
# openswan can't process defaultroute for ipv6. |
| 167 |
# Assign gateway address as leftnexthop |
| 168 |
leftnexthop=%s |
| 169 |
# rightnexthop is not mandatory for ipsec, so no need in ipv6.''' |
| 170 |
|
| 171 |
EXPECTED_OPENSWAN_CONF = """ |
| 172 |
# Configuration for %(vpnservice_id)s |
| 173 |
config setup |
| 174 |
nat_traversal=yes |
| 175 |
virtual_private=%(virtual_privates)s |
| 176 |
conn %%default |
| 177 |
keylife=60m |
| 178 |
keyingtries=%%forever |
| 179 |
conn %(conn1_id)s |
| 180 |
%(next_hop)s |
| 181 |
left=%(left)s |
| 182 |
leftid=%(leftid)s |
| 183 |
auto=start |
| 184 |
# NOTE:REQUIRED |
| 185 |
# [subnet] |
| 186 |
leftsubnet%(local_cidrs1)s |
| 187 |
# [updown] |
| 188 |
# What "updown" script to run to adjust routing and/or firewalling when |
| 189 |
# the status of the connection changes (default "ipsec _updown"). |
| 190 |
# "--route yes" allows to specify such routing options as mtu and metric. |
| 191 |
leftupdown="ipsec _updown --route yes" |
| 192 |
###################### |
| 193 |
# ipsec_site_connections |
| 194 |
###################### |
| 195 |
# [peer_address] |
| 196 |
right=%(right1)s |
| 197 |
# [peer_id] |
| 198 |
rightid=%(right1)s |
| 199 |
# [peer_cidrs] |
| 200 |
rightsubnets={ %(peer_cidrs1)s } |
| 201 |
%(conn_details)sconn %(conn2_id)s |
| 202 |
%(next_hop)s |
| 203 |
left=%(left)s |
| 204 |
leftid=%(leftid)s |
| 205 |
auto=start |
| 206 |
# NOTE:REQUIRED |
| 207 |
# [subnet] |
| 208 |
leftsubnet%(local_cidrs2)s |
| 209 |
# [updown] |
| 210 |
# What "updown" script to run to adjust routing and/or firewalling when |
| 211 |
# the status of the connection changes (default "ipsec _updown"). |
| 212 |
# "--route yes" allows to specify such routing options as mtu and metric. |
| 213 |
leftupdown="ipsec _updown --route yes" |
| 214 |
###################### |
| 215 |
# ipsec_site_connections |
| 216 |
###################### |
| 217 |
# [peer_address] |
| 218 |
right=%(right2)s |
| 219 |
# [peer_id] |
| 220 |
rightid=%(right2)s |
| 221 |
# [peer_cidrs] |
| 222 |
rightsubnets={ %(peer_cidrs2)s } |
| 223 |
%(conn_details)s |
| 224 |
""" |
| 225 |
|
| 226 |
STRONGSWAN_AUTH_ESP = 'esp=aes128-sha1-modp1536' |
| 227 |
|
| 228 |
STRONGSWAN_AUTH_AH = 'ah=sha1-modp1536' |
| 229 |
|
| 230 |
EXPECTED_IPSEC_OPENSWAN_SECRET_CONF = ''' |
| 231 |
# Configuration for %s |
| 232 |
60.0.0.4 60.0.0.5 : PSK 0scGFzc3dvcmQ= |
| 233 |
60.0.0.4 60.0.0.6 : PSK 0scGFzc3dvcmQ=''' % FAKE_VPNSERVICE_ID |
| 234 |
|
| 235 |
EXPECTED_IPSEC_STRONGSWAN_CONF = ''' |
| 236 |
# Configuration for %(vpnservice_id)s |
| 237 |
config setup |
| 238 |
|
| 239 |
conn %%default |
| 240 |
keylife=20m |
| 241 |
rekeymargin=3m |
| 242 |
keyingtries=1 |
| 243 |
authby=psk |
| 244 |
mobike=no |
| 245 |
|
| 246 |
conn %(conn1_id)s |
| 247 |
keyexchange=ikev1 |
| 248 |
left=%(left)s |
| 249 |
leftsubnet=%(local_cidrs1)s |
| 250 |
leftid=%(leftid)s |
| 251 |
leftfirewall=yes |
| 252 |
right=%(right1)s |
| 253 |
rightsubnet=%(peer_cidrs1)s |
| 254 |
rightid=%(right1)s |
| 255 |
auto=route |
| 256 |
dpdaction=%(dpd_action)s |
| 257 |
dpddelay=%(dpd_delay)ss |
| 258 |
dpdtimeout=%(dpd_timeout)ss |
| 259 |
ike=%(ike_encryption_algorithm)s-%(ike_auth_algorithm)s-%(ike_pfs)s |
| 260 |
ikelifetime=%(ike_lifetime)ss |
| 261 |
%(auth_mode)s |
| 262 |
lifetime=%(life_time)ss |
| 263 |
type=%(encapsulation_mode)s |
| 264 |
|
| 265 |
conn %(conn2_id)s |
| 266 |
keyexchange=ikev1 |
| 267 |
left=%(left)s |
| 268 |
leftsubnet=%(local_cidrs2)s |
| 269 |
leftid=%(leftid)s |
| 270 |
leftfirewall=yes |
| 271 |
right=%(right2)s |
| 272 |
rightsubnet=%(peer_cidrs2)s |
| 273 |
rightid=%(right2)s |
| 274 |
auto=route |
| 275 |
dpdaction=%(dpd_action)s |
| 276 |
dpddelay=%(dpd_delay)ss |
| 277 |
dpdtimeout=%(dpd_timeout)ss |
| 278 |
ike=%(ike_encryption_algorithm)s-%(ike_auth_algorithm)s-%(ike_pfs)s |
| 279 |
ikelifetime=%(ike_lifetime)ss |
| 280 |
%(auth_mode)s |
| 281 |
lifetime=%(life_time)ss |
| 282 |
type=%(encapsulation_mode)s |
| 283 |
''' |
| 284 |
|
| 285 |
EXPECTED_STRONGSWAN_DEFAULT_CONF = ''' |
| 286 |
charon { |
| 287 |
load_modular = yes |
| 288 |
plugins { |
| 289 |
include strongswan.d/charon/*.conf |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
include strongswan.d/*.conf |
| 294 |
''' |
| 295 |
|
| 296 |
EXPECTED_IPSEC_STRONGSWAN_SECRET_CONF = ''' |
| 297 |
# Configuration for %s |
| 298 |
60.0.0.4 60.0.0.5 : PSK 0scGFzc3dvcmQ= |
| 299 |
|
| 300 |
60.0.0.4 60.0.0.6 : PSK 0scGFzc3dvcmQ= |
| 301 |
''' % FAKE_VPNSERVICE_ID |
| 302 |
|
| 303 |
PLUTO_ACTIVE_STATUS = """000 "{conn_id}/0x1": erouted;\n |
| 304 |
000 #4: "{conn_id}/0x1":500 STATE_QUICK_R2 (IPsec SA established); \ |
| 305 |
newest IPSEC;""".format( |
| 306 |
conn_id=FAKE_IPSEC_SITE_CONNECTION2_ID) |
| 307 |
PLUTO_ACTIVE_STATUS_IKEV2 = """000 "{conn_id}/0x1": erouted;\n |
| 308 |
000 #4: "{conn_id}/0x1":500 STATE_PARENT_R2 (PARENT SA established); \ |
| 309 |
newest IPSEC;""".format( |
| 310 |
conn_id=FAKE_IPSEC_SITE_CONNECTION2_ID) |
| 311 |
PLUTO_MULTIPLE_SUBNETS_ESTABLISHED_STATUS = """000 "{conn_id1}/1x1": erouted;\n |
| 312 |
000 #4: "{conn_id1}/1x1":500 STATE_QUICK_R2 (IPsec SA established); \ |
| 313 |
newest IPSEC;\n |
| 314 |
000 "{conn_id2}/2x1": erouted;\n |
| 315 |
000 #4: "{conn_id2}/2x1":500 STATE_QUICK_R2 (IPsec SA established); \ |
| 316 |
newest IPSEC;\n""".format( # noqa: E501 |
| 317 |
conn_id1=FAKE_IPSEC_SITE_CONNECTION1_ID, |
| 318 |
conn_id2=FAKE_IPSEC_SITE_CONNECTION2_ID) |
| 319 |
PLUTO_ACTIVE_NO_IPSEC_SA_STATUS = """000 "{conn_id}/0x1": erouted;\n |
| 320 |
000 #258: "{conn_id}/0x1":500 STATE_MAIN_R2 |
| 321 |
(sent MR2, expecting MI3);""".format( |
| 322 |
conn_id=FAKE_IPSEC_SITE_CONNECTION2_ID) |
| 323 |
PLUTO_DOWN_STATUS = "000 \"%(conn_id)s/0x1\": unrouted;" % { |
| 324 |
'conn_id': FAKE_IPSEC_SITE_CONNECTION2_ID} |
| 325 |
|
| 326 |
CHARON_ACTIVE_STATUS = "%(conn_id)s{1}: INSTALLED, TUNNEL" % { |
| 327 |
'conn_id': FAKE_IPSEC_SITE_CONNECTION2_ID} |
| 328 |
CHARON_DOWN_STATUS = "%(conn_id)s{1}: ROUTED, TUNNEL" % { |
| 329 |
'conn_id': FAKE_IPSEC_SITE_CONNECTION2_ID} |
| 330 |
|
| 331 |
NOT_RUNNING_STATUS = "Command: ['ipsec', 'status'] Exit code: 3 Stdout:" |
| 332 |
|
| 333 |
|
| 334 |
class BaseIPsecDeviceDriver(base.BaseTestCase): |
| 335 |
def setUp(self, driver=openswan_ipsec.OpenSwanDriver, |
| 336 |
ipsec_process=openswan_ipsec.OpenSwanProcess, |
| 337 |
vpnservice=FAKE_VPN_SERVICE): |
| 338 |
super().setUp() |
| 339 |
for klass in [ |
| 340 |
'neutron_lib.rpc.Connection', |
| 341 |
'oslo_service.loopingcall.FixedIntervalLoopingCall' |
| 342 |
]: |
| 343 |
mock.patch(klass).start() |
| 344 |
self._execute = mock.patch.object(ipsec_process, '_execute').start() |
| 345 |
self.agent = mock.Mock() |
| 346 |
self.conf = cfg.CONF |
| 347 |
l3_config.register_l3_agent_config_opts(l3_config.OPTS, self.conf) |
| 348 |
self.agent.conf = self.conf |
| 349 |
self.driver = driver( |
| 350 |
self.agent, |
| 351 |
FAKE_HOST) |
| 352 |
self.driver.agent_rpc = mock.Mock() |
| 353 |
self.ri_kwargs = {'router': {'id': FAKE_ROUTER_ID, 'ha': False}, |
| 354 |
'agent_conf': self.conf, |
| 355 |
'interface_driver': mock.sentinel.interface_driver} |
| 356 |
self.iptables = mock.Mock() |
| 357 |
self.apply_mock = mock.Mock() |
| 358 |
self.vpnservice = copy.deepcopy(vpnservice) |
| 359 |
self.context = context.get_admin_context() |
| 360 |
ipsec_process._get_strongswan_piddir = mock.Mock( |
| 361 |
return_value="/var/run") |
| 362 |
|
| 363 |
@staticmethod |
| 364 |
def generate_diff(a, b): |
| 365 |
"""Generates unified diff of a and b.""" |
| 366 |
a, b = list(a.splitlines(True)), list(b.splitlines(True)) |
| 367 |
diff = difflib.unified_diff(a, b, fromfile="expected", |
| 368 |
tofile="actual") |
| 369 |
return diff |
| 370 |
|
| 371 |
def modify_config_for_test(self, overrides): |
| 372 |
"""Revise service/connection settings to test variations. |
| 373 |
|
| 374 |
Must update service, so that dialect mappings occur for any changes |
| 375 |
that are made. |
| 376 |
""" |
| 377 |
ipsec_auth_protocol = overrides.get('ipsec_auth') |
| 378 |
if ipsec_auth_protocol: |
| 379 |
auth_proto = {'transform_protocol': ipsec_auth_protocol} |
| 380 |
for conn in self.vpnservice['ipsec_site_connections']: |
| 381 |
conn['ipsecpolicy'].update(auth_proto) |
| 382 |
|
| 383 |
local_cidrs = overrides.get('local_cidrs') |
| 384 |
if local_cidrs: |
| 385 |
for i, conn in enumerate( |
| 386 |
self.vpnservice['ipsec_site_connections']): |
| 387 |
conn['local_cidrs'] = local_cidrs[i] |
| 388 |
|
| 389 |
local_ip_version = overrides.get('local_ip_vers', 4) |
| 390 |
for conn in self.vpnservice['ipsec_site_connections']: |
| 391 |
conn['local_ip_vers'] = local_ip_version |
| 392 |
|
| 393 |
peer_cidrs = overrides.get('peer_cidrs') |
| 394 |
if peer_cidrs: |
| 395 |
for i, conn in enumerate( |
| 396 |
self.vpnservice['ipsec_site_connections']): |
| 397 |
conn['peer_cidrs'] = peer_cidrs[i] |
| 398 |
|
| 399 |
peers = overrides.get('peers') |
| 400 |
if peers: |
| 401 |
for i, conn in enumerate( |
| 402 |
self.vpnservice['ipsec_site_connections']): |
| 403 |
conn['peer_id'] = peers[i] |
| 404 |
conn['peer_address'] = peers[i] |
| 405 |
|
| 406 |
local_ip = overrides.get('local') |
| 407 |
local_id = overrides.get('local_id') |
| 408 |
if local_ip: |
| 409 |
for conn in self.vpnservice['ipsec_site_connections']: |
| 410 |
conn['external_ip'] = local_ip |
| 411 |
conn['local_id'] = local_ip |
| 412 |
if local_id: |
| 413 |
conn['local_id'] = local_id |
| 414 |
|
| 415 |
def check_config_file(self, expected, actual): |
| 416 |
expected = expected.strip() |
| 417 |
actual = actual.strip() |
| 418 |
res_diff = self.generate_diff(expected, actual) |
| 419 |
self.assertEqual(expected, actual, message=''.join(res_diff)) |
| 420 |
|
| 421 |
def _test_ipsec_connection_config(self, info): |
| 422 |
"""Check config file string for service/connection. |
| 423 |
|
| 424 |
Calls test specific method to create (and override as needed) the |
| 425 |
expected config file string, generates the config using the test's |
| 426 |
IPSec template, and then compares the results. |
| 427 |
""" |
| 428 |
|
| 429 |
expected = self.build_ipsec_expected_config_for_test(info) |
| 430 |
actual = self.process._gen_config_content(self.ipsec_template, |
| 431 |
self.vpnservice) |
| 432 |
self.check_config_file(expected, actual) |
| 433 |
|
| 434 |
|
| 435 |
class IPSecDeviceLegacy(BaseIPsecDeviceDriver): |
| 436 |
|
| 437 |
def setUp(self, driver=openswan_ipsec.OpenSwanDriver, |
| 438 |
ipsec_process=openswan_ipsec.OpenSwanProcess): |
| 439 |
super().setUp(driver, ipsec_process) |
| 440 |
self._make_router_info_for_test() |
| 441 |
|
| 442 |
def _make_router_info_for_test(self): |
| 443 |
self.router_info = legacy_router.LegacyRouter(router_id=FAKE_ROUTER_ID, |
| 444 |
agent=self.agent, |
| 445 |
**self.ri_kwargs) |
| 446 |
self.router_info.router['distributed'] = False |
| 447 |
self.router_info.iptables_manager.ipv4['nat'] = self.iptables |
| 448 |
self.router_info.iptables_manager.apply = self.apply_mock |
| 449 |
self.driver.routers[FAKE_ROUTER_ID] = self.router_info |
| 450 |
|
| 451 |
def _test_vpnservice_updated(self, expected_param, **kwargs): |
| 452 |
with mock.patch.object(self.driver, 'sync') as sync: |
| 453 |
context = mock.Mock() |
| 454 |
self.driver.vpnservice_updated(context, **kwargs) |
| 455 |
sync.assert_called_once_with(context, expected_param) |
| 456 |
|
| 457 |
def test_vpnservice_updated(self): |
| 458 |
self._test_vpnservice_updated([]) |
| 459 |
|
| 460 |
def test_vpnservice_updated_with_router_info(self): |
| 461 |
kwargs = {'router': self.router_info} |
| 462 |
self._test_vpnservice_updated([self.router_info], **kwargs) |
| 463 |
|
| 464 |
def test_create_router(self): |
| 465 |
process = mock.Mock(openswan_ipsec.OpenSwanProcess) |
| 466 |
process.vpnservice = self.vpnservice |
| 467 |
self.driver.processes = { |
| 468 |
FAKE_ROUTER_ID: process} |
| 469 |
self.driver.create_router(self.router_info) |
| 470 |
self._test_ensure_nat_rules() |
| 471 |
process.enable.assert_called_once_with() |
| 472 |
|
| 473 |
def test_destroy_router(self): |
| 474 |
process_id = _uuid() |
| 475 |
process = mock.Mock() |
| 476 |
process.vpnservice = self.vpnservice |
| 477 |
self.driver.processes = { |
| 478 |
process_id: process} |
| 479 |
self.driver.destroy_router(process_id) |
| 480 |
process.disable.assert_called_once_with() |
| 481 |
self.assertNotIn(process_id, self.driver.processes) |
| 482 |
|
| 483 |
def _test_ensure_nat_rules(self): |
| 484 |
self.router_info.iptables_manager.ipv4['nat'].assert_has_calls([ |
| 485 |
mock.call.clear_rules_by_tag('vpnaas'), |
| 486 |
mock.call.add_rule( |
| 487 |
'POSTROUTING', |
| 488 |
'-s 10.0.0.0/24 -d 20.0.0.0/24 -m policy ' |
| 489 |
'--dir out --pol ipsec -j ACCEPT ', |
| 490 |
top=True, |
| 491 |
tag='vpnaas'), |
| 492 |
mock.call.add_rule( |
| 493 |
'POSTROUTING', |
| 494 |
'-s 10.0.0.0/24 -d 30.0.0.0/24 -m policy ' |
| 495 |
'--dir out --pol ipsec -j ACCEPT ', |
| 496 |
top=True, |
| 497 |
tag='vpnaas'), |
| 498 |
mock.call.add_rule( |
| 499 |
'POSTROUTING', |
| 500 |
'-s 11.0.0.0/24 -d 40.0.0.0/24 -m policy ' |
| 501 |
'--dir out --pol ipsec -j ACCEPT ', |
| 502 |
top=True, |
| 503 |
tag='vpnaas'), |
| 504 |
mock.call.add_rule( |
| 505 |
'POSTROUTING', |
| 506 |
'-s 11.0.0.0/24 -d 50.0.0.0/24 -m policy ' |
| 507 |
'--dir out --pol ipsec -j ACCEPT ', |
| 508 |
top=True, |
| 509 |
tag='vpnaas') |
| 510 |
]) |
| 511 |
self.router_info.iptables_manager.apply.assert_called_once_with() |
| 512 |
|
| 513 |
def _test_ensure_nat_rules_with_multiple_locals(self): |
| 514 |
self.router_info.iptables_manager.ipv4['nat'].assert_has_calls([ |
| 515 |
mock.call.clear_rules_by_tag('vpnaas'), |
| 516 |
mock.call.add_rule( |
| 517 |
'POSTROUTING', |
| 518 |
'-s 10.0.0.0/24 -d 20.0.0.0/24 -m policy ' |
| 519 |
'--dir out --pol ipsec -j ACCEPT ', |
| 520 |
top=True, |
| 521 |
tag='vpnaas'), |
| 522 |
mock.call.add_rule( |
| 523 |
'POSTROUTING', |
| 524 |
'-s 10.0.0.0/24 -d 30.0.0.0/24 -m policy ' |
| 525 |
'--dir out --pol ipsec -j ACCEPT ', |
| 526 |
top=True, |
| 527 |
tag='vpnaas'), |
| 528 |
mock.call.add_rule( |
| 529 |
'POSTROUTING', |
| 530 |
'-s 11.0.0.0/24 -d 20.0.0.0/24 -m policy ' |
| 531 |
'--dir out --pol ipsec -j ACCEPT ', |
| 532 |
top=True, |
| 533 |
tag='vpnaas'), |
| 534 |
mock.call.add_rule( |
| 535 |
'POSTROUTING', |
| 536 |
'-s 11.0.0.0/24 -d 30.0.0.0/24 -m policy ' |
| 537 |
'--dir out --pol ipsec -j ACCEPT ', |
| 538 |
top=True, |
| 539 |
tag='vpnaas'), |
| 540 |
mock.call.add_rule( |
| 541 |
'POSTROUTING', |
| 542 |
'-s 12.0.0.0/24 -d 40.0.0.0/24 -m policy ' |
| 543 |
'--dir out --pol ipsec -j ACCEPT ', |
| 544 |
top=True, |
| 545 |
tag='vpnaas'), |
| 546 |
mock.call.add_rule( |
| 547 |
'POSTROUTING', |
| 548 |
'-s 12.0.0.0/24 -d 50.0.0.0/24 -m policy ' |
| 549 |
'--dir out --pol ipsec -j ACCEPT ', |
| 550 |
top=True, |
| 551 |
tag='vpnaas'), |
| 552 |
mock.call.add_rule( |
| 553 |
'POSTROUTING', |
| 554 |
'-s 13.0.0.0/24 -d 40.0.0.0/24 -m policy ' |
| 555 |
'--dir out --pol ipsec -j ACCEPT ', |
| 556 |
top=True, |
| 557 |
tag='vpnaas'), |
| 558 |
mock.call.add_rule( |
| 559 |
'POSTROUTING', |
| 560 |
'-s 13.0.0.0/24 -d 50.0.0.0/24 -m policy ' |
| 561 |
'--dir out --pol ipsec -j ACCEPT ', |
| 562 |
top=True, |
| 563 |
tag='vpnaas') |
| 564 |
]) |
| 565 |
self.router_info.iptables_manager.apply.assert_called_once_with() |
| 566 |
|
| 567 |
def test_sync(self): |
| 568 |
fake_vpn_service = FAKE_VPN_SERVICE |
| 569 |
self.driver.agent_rpc.get_vpn_services_on_host.return_value = [ |
| 570 |
fake_vpn_service] |
| 571 |
context = mock.Mock() |
| 572 |
self.driver._sync_vpn_processes = mock.Mock() |
| 573 |
self.driver._delete_vpn_processes = mock.Mock() |
| 574 |
self.driver._cleanup_stale_vpn_processes = mock.Mock() |
| 575 |
sync_router_ids = [fake_vpn_service['router_id']] |
| 576 |
self.driver.sync(context, [self.router_info]) |
| 577 |
self.driver._sync_vpn_processes.assert_called_once_with( |
| 578 |
[fake_vpn_service], sync_router_ids) |
| 579 |
self.driver._delete_vpn_processes.assert_called_once_with( |
| 580 |
sync_router_ids, sync_router_ids) |
| 581 |
self.driver._cleanup_stale_vpn_processes.assert_called_once_with( |
| 582 |
sync_router_ids) |
| 583 |
|
| 584 |
def test__sync_vpn_processes_new_vpn_service(self): |
| 585 |
new_vpnservice = self.vpnservice |
| 586 |
router_id = new_vpnservice['router_id'] |
| 587 |
self.driver.processes = {} |
| 588 |
with mock.patch.object(self.driver, 'ensure_process') as ensure_p: |
| 589 |
ensure_p.side_effect = self.fake_ensure_process |
| 590 |
self.driver._sync_vpn_processes([new_vpnservice], router_id) |
| 591 |
self._test_ensure_nat_rules() |
| 592 |
self.driver.processes[router_id].update.assert_called_once_with() |
| 593 |
|
| 594 |
def test__sync_with_dict_and_router_info(self): |
| 595 |
"""Verify sync handles mixed router_information types correctly.""" |
| 596 |
|
| 597 |
router_id_1 = 'router_1' |
| 598 |
router_id_2 = 'router_2' |
| 599 |
|
| 600 |
# A proper RouterInfo object |
| 601 |
router_info_1 = mock.Mock(spec=l3_router_info.RouterInfo) |
| 602 |
router_info_1.router_id = router_id_1 |
| 603 |
|
| 604 |
# A dictionary, as sometimes received via RPC |
| 605 |
router_info_2_dict = {'id': router_id_2} |
| 606 |
|
| 607 |
router_information = [router_info_1, router_info_2_dict] |
| 608 |
|
| 609 |
self.driver.agent_rpc.get_vpn_services_on_host.return_value = [] |
| 610 |
self.driver._sync_vpn_processes = mock.Mock() |
| 611 |
self.driver._delete_vpn_processes = mock.Mock() |
| 612 |
self.driver._cleanup_stale_vpn_processes = mock.Mock() |
| 613 |
self.driver.report_status = mock.Mock() |
| 614 |
|
| 615 |
self.driver.sync(self.context, router_information) |
| 616 |
|
| 617 |
self.assertIn(router_id_1, self.driver.routers) |
| 618 |
self.assertEqual(router_info_1, self.driver.routers[router_id_1]) |
| 619 |
|
| 620 |
self.assertNotIn(router_id_2, self.driver.routers) |
| 621 |
|
| 622 |
self.driver._sync_vpn_processes.assert_called_once() |
| 623 |
_vpn_services, sync_router_ids = \ |
| 624 |
self.driver._sync_vpn_processes.call_args[0] |
| 625 |
self.assertIn(router_id_1, sync_router_ids) |
| 626 |
self.assertIn(router_id_2, sync_router_ids) |
| 627 |
self.assertEqual(2, len(sync_router_ids)) |
| 628 |
|
| 629 |
def test_ensure_nat_rules_with_multiple_local_subnets(self): |
| 630 |
"""Ensure that add nat rule combinations are correct.""" |
| 631 |
overrides = {'local_cidrs': [['10.0.0.0/24', '11.0.0.0/24'], |
| 632 |
['12.0.0.0/24', '13.0.0.0/24']]} |
| 633 |
self.modify_config_for_test(overrides) |
| 634 |
self.driver.ensure_nat_rules(self.vpnservice) |
| 635 |
self._test_ensure_nat_rules_with_multiple_locals() |
| 636 |
|
| 637 |
def test__sync_vpn_processes_router_with_no_vpn(self): |
| 638 |
"""Test _sync_vpn_processes with a router not hosting vpnservice. |
| 639 |
|
| 640 |
This test case tests that when a router which doesn't host |
| 641 |
vpn services is updated, sync_vpn_processes doesn't restart/update |
| 642 |
the existing vpnservice processes. |
| 643 |
""" |
| 644 |
process = mock.Mock() |
| 645 |
process.vpnservice = self.vpnservice |
| 646 |
process.connection_status = {} |
| 647 |
self.driver.processes = { |
| 648 |
self.vpnservice['router_id']: process} |
| 649 |
router_id_no_vpn = _uuid() |
| 650 |
with mock.patch.object(self.driver, 'ensure_process') as ensure_p: |
| 651 |
self.driver._sync_vpn_processes([self.vpnservice], |
| 652 |
[router_id_no_vpn]) |
| 653 |
self.assertEqual(0, ensure_p.call_count) |
| 654 |
|
| 655 |
def test__sync_vpn_processes_router_with_no_vpn_and_no_vpn_services(self): |
| 656 |
"""No vpn services running and router not hosting vpn svc.""" |
| 657 |
router_id_no_vpn = _uuid() |
| 658 |
self.driver.process_status_cache = {} |
| 659 |
self.driver.processes = {} |
| 660 |
with mock.patch.object(self.driver, 'ensure_process') as ensure_p: |
| 661 |
ensure_p.side_effect = self.fake_ensure_process |
| 662 |
self.driver._sync_vpn_processes([], [router_id_no_vpn]) |
| 663 |
self.assertEqual(0, ensure_p.call_count) |
| 664 |
|
| 665 |
def test__sync_vpn_processes_router_with_no_vpn_agent_restarted(self): |
| 666 |
"""Test for the router not hosting vpnservice and agent restarted. |
| 667 |
|
| 668 |
This test case tests that when a non vpnservice hosted router |
| 669 |
is updated, _sync_vpn_processes restart/update the existing vpnservices |
| 670 |
which are not yet stored in driver.processes. |
| 671 |
""" |
| 672 |
self.driver.process_status_cache = {} |
| 673 |
self.driver.processes = {} |
| 674 |
with mock.patch.object(self.driver, 'ensure_process') as ensure_p: |
| 675 |
ensure_p.side_effect = self.fake_ensure_process |
| 676 |
self.driver._sync_vpn_processes( |
| 677 |
[self.vpnservice], |
| 678 |
[FAKE_ROUTER_ID] |
| 679 |
) |
| 680 |
self._test_ensure_nat_rules() |
| 681 |
self.driver.processes[ |
| 682 |
FAKE_ROUTER_ID |
| 683 |
].update.assert_called_once_with() |
| 684 |
|
| 685 |
def test_delete_vpn_processes(self): |
| 686 |
router_id_no_vpn = _uuid() |
| 687 |
vpn_service_router_id = _uuid() |
| 688 |
with mock.patch.object(self.driver, |
| 689 |
'destroy_process') as (fake_destroy_process): |
| 690 |
self.driver._delete_vpn_processes([router_id_no_vpn], |
| 691 |
[vpn_service_router_id]) |
| 692 |
fake_destroy_process.assert_has_calls( |
| 693 |
[mock.call(router_id_no_vpn)]) |
| 694 |
|
| 695 |
# test that _delete_vpn_processes doesn't delete the |
| 696 |
# the valid vpn processes |
| 697 |
with mock.patch.object(self.driver, |
| 698 |
'destroy_process') as fake_destroy_process: |
| 699 |
self.driver._delete_vpn_processes([vpn_service_router_id], |
| 700 |
[vpn_service_router_id]) |
| 701 |
self.assertFalse(fake_destroy_process.called) |
| 702 |
|
| 703 |
def test_cleanup_stale_vpn_processes(self): |
| 704 |
stale_vpn_service = {'router_id': _uuid()} |
| 705 |
active_vpn_service = {'router_id': _uuid()} |
| 706 |
self.driver.processes = { |
| 707 |
stale_vpn_service['router_id']: stale_vpn_service, |
| 708 |
active_vpn_service['router_id']: active_vpn_service} |
| 709 |
with mock.patch.object(self.driver, 'destroy_process') as destroy_p: |
| 710 |
self.driver._cleanup_stale_vpn_processes( |
| 711 |
[active_vpn_service['router_id']]) |
| 712 |
destroy_p.assert_has_calls( |
| 713 |
[mock.call(stale_vpn_service['router_id'])]) |
| 714 |
|
| 715 |
def fake_ensure_process(self, process_id, vpnservice=None): |
| 716 |
process = self.driver.processes.get(process_id) |
| 717 |
if not process: |
| 718 |
process = mock.Mock() |
| 719 |
process.vpnservice = self.vpnservice |
| 720 |
process.connection_status = {} |
| 721 |
process.status = constants.ACTIVE |
| 722 |
process.updated_pending_status = True |
| 723 |
self.driver.processes[process_id] = process |
| 724 |
elif vpnservice: |
| 725 |
process.vpnservice = vpnservice |
| 726 |
process.update_vpnservice(vpnservice) |
| 727 |
return process |
| 728 |
|
| 729 |
def fake_destroy_router(self, process_id): |
| 730 |
process = self.driver.processes.get(process_id) |
| 731 |
if process: |
| 732 |
del self.driver.processes[process_id] |
| 733 |
|
| 734 |
# TODO(crohmann): Add test cases for HARouter and different ha_states |
| 735 |
# @ddt [(False, None),(True, 'primary'), (True, 'standby')] |
| 736 |
def test_sync_update_vpnservice(self): |
| 737 |
with mock.patch.object(self.driver, |
| 738 |
'ensure_process') as ensure_process: |
| 739 |
ensure_process.side_effect = self.fake_ensure_process |
| 740 |
new_vpn_service = self.vpnservice |
| 741 |
updated_vpn_service = copy.deepcopy(new_vpn_service) |
| 742 |
updated_vpn_service['ipsec_site_connections'][1].update( |
| 743 |
{'peer_cidrs': ['60.0.0.0/24', '70.0.0.0/24']}) |
| 744 |
context = mock.Mock() |
| 745 |
self.driver.process_status_cache = {} |
| 746 |
self.driver.agent_rpc.get_vpn_services_on_host.return_value = [ |
| 747 |
new_vpn_service] |
| 748 |
self.driver.sync(context, [self.router_info]) |
| 749 |
process = self.driver.processes[FAKE_ROUTER_ID] |
| 750 |
self.assertEqual(new_vpn_service, process.vpnservice) |
| 751 |
self.driver.agent_rpc.get_vpn_services_on_host.return_value = [ |
| 752 |
updated_vpn_service] |
| 753 |
self.driver.sync(context, [self.router_info]) |
| 754 |
process = self.driver.processes[FAKE_ROUTER_ID] |
| 755 |
process.update_vpnservice.assert_called_once_with( |
| 756 |
updated_vpn_service) |
| 757 |
self.assertEqual(updated_vpn_service, process.vpnservice) |
| 758 |
|
| 759 |
def test_sync_removed(self): |
| 760 |
self.driver.agent_rpc.get_vpn_services_on_host.return_value = [] |
| 761 |
context = mock.Mock() |
| 762 |
process_id = _uuid() |
| 763 |
process = mock.Mock() |
| 764 |
process.vpnservice = self.vpnservice |
| 765 |
self.driver.processes = { |
| 766 |
process_id: process} |
| 767 |
self.driver.sync(context, []) |
| 768 |
process.disable.assert_called_once_with() |
| 769 |
self.assertNotIn(process_id, self.driver.processes) |
| 770 |
|
| 771 |
def test_sync_removed_router(self): |
| 772 |
self.driver.agent_rpc.get_vpn_services_on_host.return_value = [] |
| 773 |
context = mock.Mock() |
| 774 |
process_id = _uuid() |
| 775 |
ri = self.router_info |
| 776 |
ri.router_id = process_id |
| 777 |
ri.router['id'] = process_id |
| 778 |
self.driver.sync(context, [self.router_info]) |
| 779 |
self.assertNotIn(process_id, self.driver.processes) |
| 780 |
|
| 781 |
def test_status_updated_on_connection_admin_down(self): |
| 782 |
self.driver.process_status_cache = { |
| 783 |
'1': { |
| 784 |
'status': constants.ACTIVE, |
| 785 |
'id': 123, |
| 786 |
'updated_pending_status': False, |
| 787 |
'ipsec_site_connections': { |
| 788 |
'10': { |
| 789 |
'status': constants.ACTIVE, |
| 790 |
'updated_pending_status': False, |
| 791 |
}, |
| 792 |
'20': { |
| 793 |
'status': constants.ACTIVE, |
| 794 |
'updated_pending_status': False, |
| 795 |
} |
| 796 |
} |
| 797 |
} |
| 798 |
} |
| 799 |
# Simulate that there is no longer status for connection '20' |
| 800 |
# e.g. connection admin down |
| 801 |
new_status = { |
| 802 |
'ipsec_site_connections': { |
| 803 |
'10': { |
| 804 |
'status': constants.ACTIVE, |
| 805 |
'updated_pending_status': False |
| 806 |
} |
| 807 |
} |
| 808 |
} |
| 809 |
self.driver.update_downed_connections('1', new_status) |
| 810 |
existing_conn = new_status['ipsec_site_connections'].get('10') |
| 811 |
self.assertIsNotNone(existing_conn) |
| 812 |
self.assertEqual(constants.ACTIVE, existing_conn['status']) |
| 813 |
missing_conn = new_status['ipsec_site_connections'].get('20') |
| 814 |
self.assertIsNotNone(missing_conn) |
| 815 |
self.assertEqual(constants.DOWN, missing_conn['status']) |
| 816 |
|
| 817 |
def test_status_updated_on_service_admin_down(self): |
| 818 |
self.driver.process_status_cache = { |
| 819 |
'1': { |
| 820 |
'status': constants.ACTIVE, |
| 821 |
'id': 123, |
| 822 |
'updated_pending_status': False, |
| 823 |
'ipsec_site_connections': { |
| 824 |
'10': { |
| 825 |
'status': constants.ACTIVE, |
| 826 |
'updated_pending_status': False, |
| 827 |
}, |
| 828 |
'20': { |
| 829 |
'status': constants.ACTIVE, |
| 830 |
'updated_pending_status': False, |
| 831 |
} |
| 832 |
} |
| 833 |
} |
| 834 |
} |
| 835 |
# Simulate that there are no connections now |
| 836 |
new_status = { |
| 837 |
'ipsec_site_connections': {} |
| 838 |
} |
| 839 |
self.driver.update_downed_connections('1', new_status) |
| 840 |
missing_conn = new_status['ipsec_site_connections'].get('10') |
| 841 |
self.assertIsNotNone(missing_conn) |
| 842 |
self.assertEqual(constants.DOWN, missing_conn['status']) |
| 843 |
missing_conn = new_status['ipsec_site_connections'].get('20') |
| 844 |
self.assertIsNotNone(missing_conn) |
| 845 |
self.assertEqual(constants.DOWN, missing_conn['status']) |
| 846 |
|
| 847 |
def _test_status_handling_for_downed_connection(self, down_status): |
| 848 |
"""Test status handling for downed connection.""" |
| 849 |
router_id = self.router_info.router_id |
| 850 |
connection_id = FAKE_IPSEC_SITE_CONNECTION2_ID |
| 851 |
self.driver.ensure_process(router_id, self.vpnservice) |
| 852 |
self._execute.return_value = down_status |
| 853 |
self.driver.report_status(mock.Mock()) |
| 854 |
process_status = self.driver.process_status_cache[router_id] |
| 855 |
ipsec_site_conn = process_status['ipsec_site_connections'] |
| 856 |
self.assertEqual(constants.ACTIVE, process_status['status']) |
| 857 |
self.assertEqual(constants.DOWN, |
| 858 |
ipsec_site_conn[connection_id]['status']) |
| 859 |
|
| 860 |
def _test_status_handling_for_active_connection(self, active_status): |
| 861 |
"""Test status handling for active connection.""" |
| 862 |
router_id = self.router_info.router_id |
| 863 |
connection_id = FAKE_IPSEC_SITE_CONNECTION2_ID |
| 864 |
self.driver.ensure_process(router_id, self.vpnservice) |
| 865 |
self._execute.return_value = active_status |
| 866 |
self.driver.report_status(mock.Mock()) |
| 867 |
process_status = self.driver.process_status_cache[ |
| 868 |
router_id] |
| 869 |
ipsec_site_conn = process_status['ipsec_site_connections'] |
| 870 |
self.assertEqual(constants.ACTIVE, process_status['status']) |
| 871 |
self.assertEqual(constants.ACTIVE, |
| 872 |
ipsec_site_conn[connection_id]['status']) |
| 873 |
|
| 874 |
def _test_status_handling_for_ike_v2_active_connection(self, |
| 875 |
active_status): |
| 876 |
"""Test status handling for active connection.""" |
| 877 |
router_id = self.router_info.router_id |
| 878 |
connection_id = FAKE_IPSEC_SITE_CONNECTION2_ID |
| 879 |
ike_policy = {'ike_version': 'v2', |
| 880 |
'encryption_algorithm': 'aes-128', |
| 881 |
'auth_algorithm': 'sha1', |
| 882 |
'pfs': 'group5', |
| 883 |
'lifetime_value': 3600} |
| 884 |
vpn_service = FAKE_VPN_SERVICE |
| 885 |
for isc in vpn_service["ipsec_site_connections"]: |
| 886 |
isc['ikepolicy'] = ike_policy |
| 887 |
self.driver.ensure_process(router_id, vpn_service) |
| 888 |
self._execute.return_value = active_status |
| 889 |
self.driver.report_status(mock.Mock()) |
| 890 |
process_status = self.driver.process_status_cache[ |
| 891 |
router_id] |
| 892 |
ipsec_site_conn = process_status['ipsec_site_connections'] |
| 893 |
self.assertEqual(constants.ACTIVE, process_status['status']) |
| 894 |
self.assertEqual(constants.ACTIVE, |
| 895 |
ipsec_site_conn[connection_id]['status']) |
| 896 |
|
| 897 |
def _test_connection_names_handling_for_multiple_subnets(self, |
| 898 |
active_status): |
| 899 |
"""Test connection names handling for multiple subnets.""" |
| 900 |
router_id = self.router_info.router_id |
| 901 |
process = self.driver.ensure_process(router_id, self.vpnservice) |
| 902 |
self._execute.return_value = active_status |
| 903 |
names = process.get_established_connections() |
| 904 |
self.assertEqual(2, len(names)) |
| 905 |
|
| 906 |
def _test_status_handling_for_deleted_connection(self, |
| 907 |
not_running_status): |
| 908 |
"""Test status handling for deleted connection.""" |
| 909 |
router_id = self.router_info.router_id |
| 910 |
self.driver.ensure_process(router_id, self.vpnservice) |
| 911 |
self._execute.return_value = not_running_status |
| 912 |
self.driver.report_status(mock.Mock()) |
| 913 |
process_status = self.driver.process_status_cache[router_id] |
| 914 |
ipsec_site_conn = process_status['ipsec_site_connections'] |
| 915 |
self.assertEqual(constants.DOWN, process_status['status']) |
| 916 |
self.assertFalse(ipsec_site_conn) |
| 917 |
|
| 918 |
def _test_parse_connection_status(self, not_running_status, |
| 919 |
active_status, down_status): |
| 920 |
"""Test the status of ipsec-site-connection is parsed correctly.""" |
| 921 |
router_id = self.router_info.router_id |
| 922 |
process = self.driver.ensure_process(router_id, self.vpnservice) |
| 923 |
self._execute.return_value = not_running_status |
| 924 |
self.assertFalse(process.active) |
| 925 |
# An empty return value to simulate that the process |
| 926 |
# does not have any status to report. |
| 927 |
self._execute.return_value = '' |
| 928 |
self.assertFalse(process.active) |
| 929 |
self._execute.return_value = active_status |
| 930 |
self.assertTrue(process.active) |
| 931 |
self._execute.return_value = down_status |
| 932 |
self.assertTrue(process.active) |
| 933 |
|
| 934 |
def test_get_namespace_for_router(self): |
| 935 |
namespace = self.driver.get_namespace(FAKE_ROUTER_ID) |
| 936 |
self.assertEqual('qrouter-' + FAKE_ROUTER_ID, namespace) |
| 937 |
|
| 938 |
def test_fail_getting_namespace_for_unknown_router(self): |
| 939 |
self.assertFalse(self.driver.get_namespace('bogus_id')) |
| 940 |
|
| 941 |
|
| 942 |
class IPSecDeviceDVR(BaseIPsecDeviceDriver): |
| 943 |
|
| 944 |
def setUp(self, driver=openswan_ipsec.OpenSwanDriver, |
| 945 |
ipsec_process=openswan_ipsec.OpenSwanProcess): |
| 946 |
super().setUp(driver, ipsec_process) |
| 947 |
mock.patch.object(dvr_snat_ns.SnatNamespace, 'create').start() |
| 948 |
self._make_dvr_edge_router_info_for_test() |
| 949 |
|
| 950 |
def _make_dvr_edge_router_info_for_test(self): |
| 951 |
router_info = dvr_edge_router.DvrEdgeRouter(mock.sentinel.agent, |
| 952 |
mock.sentinel.myhost, |
| 953 |
FAKE_ROUTER_ID, |
| 954 |
**self.ri_kwargs) |
| 955 |
router_info.router['distributed'] = True |
| 956 |
router_info.snat_namespace = dvr_snat_ns.SnatNamespace( |
| 957 |
router_info.router['id'], |
| 958 |
mock.sentinel.agent, |
| 959 |
self.driver, |
| 960 |
mock.ANY |
| 961 |
) |
| 962 |
router_info.snat_namespace.create() |
| 963 |
router_info.snat_iptables_manager = iptables_manager.IptablesManager( |
| 964 |
namespace='snat-' + FAKE_ROUTER_ID, use_ipv6=mock.ANY) |
| 965 |
router_info.snat_iptables_manager.ipv4['nat'] = self.iptables |
| 966 |
router_info.snat_iptables_manager.apply = self.apply_mock |
| 967 |
self.driver.routers[FAKE_ROUTER_ID] = router_info |
| 968 |
|
| 969 |
def test_sync_dvr(self): |
| 970 |
fake_vpn_service = FAKE_VPN_SERVICE |
| 971 |
self.driver.agent_rpc.get_vpn_services_on_host.return_value = [ |
| 972 |
fake_vpn_service] |
| 973 |
context = mock.Mock() |
| 974 |
self.driver._sync_vpn_processes = mock.Mock() |
| 975 |
self.driver._delete_vpn_processes = mock.Mock() |
| 976 |
self.driver._cleanup_stale_vpn_processes = mock.Mock() |
| 977 |
sync_router_ids = [fake_vpn_service['router_id']] |
| 978 |
with mock.patch.object(self.driver, |
| 979 |
'get_process_status_cache') as process_status: |
| 980 |
self.driver.sync(context, [self.driver.routers[FAKE_ROUTER_ID]]) |
| 981 |
self.driver._sync_vpn_processes.assert_called_once_with( |
| 982 |
[fake_vpn_service], sync_router_ids) |
| 983 |
self.driver._delete_vpn_processes.assert_called_once_with( |
| 984 |
sync_router_ids, sync_router_ids) |
| 985 |
self.driver._cleanup_stale_vpn_processes.assert_called_once_with( |
| 986 |
sync_router_ids) |
| 987 |
self.assertEqual(0, process_status.call_count) |
| 988 |
|
| 989 |
def test_get_namespace_for_dvr_edge_router(self): |
| 990 |
namespace = self.driver.get_namespace(FAKE_ROUTER_ID) |
| 991 |
self.assertEqual('snat-' + FAKE_ROUTER_ID, namespace) |
| 992 |
|
| 993 |
def test_ensure_nat_rules_with_dvr_edge_router(self): |
| 994 |
self.driver.ensure_nat_rules(FAKE_VPN_SERVICE) |
| 995 |
self.apply_mock.assert_called_once_with() |
| 996 |
|
| 997 |
|
| 998 |
class TestOpenSwanConfigGeneration(BaseIPsecDeviceDriver): |
| 999 |
|
| 1000 |
"""Verify that configuration files are generated correctly. |
| 1001 |
|
| 1002 |
Besides the normal translation of some settings, when creating the config |
| 1003 |
file, the generated file can also vary based on the following |
| 1004 |
special conditions: |
| 1005 |
|
| 1006 |
- IPv6 versus IPv4 |
| 1007 |
- Multiple left subnets versus a single left subnet |
| 1008 |
- IPSec policy using AH transform |
| 1009 |
|
| 1010 |
The tests will focus on these variations. |
| 1011 |
""" |
| 1012 |
|
| 1013 |
def setUp(self, driver=openswan_ipsec.OpenSwanDriver, |
| 1014 |
ipsec_process=openswan_ipsec.OpenSwanProcess): |
| 1015 |
super().setUp(driver, ipsec_process, vpnservice=FAKE_VPN_SERVICE) |
| 1016 |
self.conf.register_opts(openswan_ipsec.openswan_opts, 'openswan') |
| 1017 |
self.conf.set_override('state_path', '/tmp') |
| 1018 |
self.ipsec_template = self.conf.openswan.ipsec_config_template |
| 1019 |
self.process = ipsec_process(self.conf, |
| 1020 |
'foo-process-id', |
| 1021 |
self.vpnservice, |
| 1022 |
mock.ANY) |
| 1023 |
|
| 1024 |
def build_ipsec_expected_config_for_test(self, info): |
| 1025 |
"""Modify OpenSwan ipsec expected config files for test variations.""" |
| 1026 |
auth_mode = info.get('ipsec_auth', AUTH_ESP) |
| 1027 |
conn_details = OPENSWAN_CONNECTION_DETAILS % { |
| 1028 |
'auth_mode': auth_mode, |
| 1029 |
'dpd_action': 'hold', |
| 1030 |
'dpd_delay': 30, |
| 1031 |
'dpd_timeout': 120, |
| 1032 |
'ike_lifetime': 3600, |
| 1033 |
'life_time': 3600, |
| 1034 |
'encapsulation_mode': 'tunnel'} |
| 1035 |
virtual_privates = [] |
| 1036 |
# Convert local CIDRs into assignment strings. IF more than one, |
| 1037 |
# pluralize the attribute name and enclose in brackets. |
| 1038 |
cidrs = info.get('local_cidrs', [['10.0.0.0/24'], ['11.0.0.0/24']]) |
| 1039 |
local_cidrs = [] |
| 1040 |
for cidr in cidrs: |
| 1041 |
if len(cidr) == 2: |
| 1042 |
local_cidrs.append("s={ %s }" % ' '.join(cidr)) |
| 1043 |
else: |
| 1044 |
local_cidrs.append("=%s" % cidr[0]) |
| 1045 |
for net in cidr: |
| 1046 |
version = netaddr.IPNetwork(net).version |
| 1047 |
virtual_privates.append('%v{}:{}'.format(version, net)) |
| 1048 |
# Convert peer CIDRs into space separated strings |
| 1049 |
cidrs = info.get('peer_cidrs', [['20.0.0.0/24', '30.0.0.0/24'], |
| 1050 |
['40.0.0.0/24', '50.0.0.0/24']]) |
| 1051 |
for cidr in cidrs: |
| 1052 |
for net in cidr: |
| 1053 |
version = netaddr.IPNetwork(net).version |
| 1054 |
virtual_privates.append('%v{}:{}'.format(version, net)) |
| 1055 |
peer_cidrs = [' '.join(cidr) for cidr in cidrs] |
| 1056 |
local_ip = info.get('local', '60.0.0.4') |
| 1057 |
local_id = info.get('local_id') |
| 1058 |
leftid = local_ip |
| 1059 |
if local_id: |
| 1060 |
leftid = local_id |
| 1061 |
version = info.get('local_ip_vers', 4) |
| 1062 |
next_hop = IPV4_NEXT_HOP if version == 4 else IPV6_NEXT_HOP % local_ip |
| 1063 |
peer_ips = info.get('peers', ['60.0.0.5', '60.0.0.6']) |
| 1064 |
virtual_privates.sort() |
| 1065 |
return EXPECTED_OPENSWAN_CONF % { |
| 1066 |
'vpnservice_id': FAKE_VPNSERVICE_ID, |
| 1067 |
'virtual_privates': ','.join(virtual_privates), |
| 1068 |
'next_hop': next_hop, |
| 1069 |
'local_cidrs1': local_cidrs[0], 'local_cidrs2': local_cidrs[1], |
| 1070 |
'local_ver': version, |
| 1071 |
'peer_cidrs1': peer_cidrs[0], 'peer_cidrs2': peer_cidrs[1], |
| 1072 |
'left': local_ip, |
| 1073 |
'leftid': leftid, |
| 1074 |
'right1': peer_ips[0], 'right2': peer_ips[1], |
| 1075 |
'conn1_id': FAKE_IPSEC_SITE_CONNECTION1_ID, |
| 1076 |
'conn2_id': FAKE_IPSEC_SITE_CONNECTION2_ID, |
| 1077 |
'conn_details': conn_details} |
| 1078 |
|
| 1079 |
def test_connections_with_esp_transform_protocol(self): |
| 1080 |
"""Test config file with IPSec policy using ESP.""" |
| 1081 |
self._test_ipsec_connection_config({}) |
| 1082 |
|
| 1083 |
def test_connections_with_ah_transform_protocol(self): |
| 1084 |
"""Test config file with IPSec policy using ESP.""" |
| 1085 |
overrides = {'ipsec_auth': 'ah'} |
| 1086 |
self.modify_config_for_test(overrides) |
| 1087 |
self.process.update_vpnservice(self.vpnservice) |
| 1088 |
info = {'ipsec_auth': AUTH_AH} |
| 1089 |
self._test_ipsec_connection_config(info) |
| 1090 |
|
| 1091 |
def test_connections_with_multiple_left_subnets(self): |
| 1092 |
"""Test multiple local subnets. |
| 1093 |
|
| 1094 |
The configure uses the 'leftsubnets' attribute, instead of the |
| 1095 |
'leftsubnet' attribute. |
| 1096 |
""" |
| 1097 |
|
| 1098 |
overrides = {'local_cidrs': [['10.0.0.0/24', '11.0.0.0/24'], |
| 1099 |
['12.0.0.0/24', '13.0.0.0/24']]} |
| 1100 |
self.modify_config_for_test(overrides) |
| 1101 |
self.process.update_vpnservice(self.vpnservice) |
| 1102 |
self._test_ipsec_connection_config(overrides) |
| 1103 |
|
| 1104 |
def test_config_files_with_ipv6_addresses(self): |
| 1105 |
"""Test creating config files using IPv6 addressing.""" |
| 1106 |
overrides = {'local_cidrs': [['2002:0a00::/48'], ['2002:0b00::/48']], |
| 1107 |
'local_ip_vers': 6, |
| 1108 |
'peer_cidrs': [['2002:1400::/48', '2002:1e00::/48'], |
| 1109 |
['2002:2800::/48', '2002:3200::/48']], |
| 1110 |
'local': '2002:3c00:0004::', |
| 1111 |
'peers': ['2002:3c00:0005::', '2002:3c00:0006::'], |
| 1112 |
'local_id': '2002:3c00:0004::'} |
| 1113 |
self.modify_config_for_test(overrides) |
| 1114 |
self.process.update_vpnservice(self.vpnservice) |
| 1115 |
self._test_ipsec_connection_config(overrides) |
| 1116 |
|
| 1117 |
def test_config_files_with_ipv6_addresses_without_local_id(self): |
| 1118 |
"""Test creating config files using IPv6 addressing.""" |
| 1119 |
overrides = {'local_cidrs': [['2002:0a00::/48'], ['2002:0b00::/48']], |
| 1120 |
'local_ip_vers': 6, |
| 1121 |
'peer_cidrs': [['2002:1400::/48', '2002:1e00::/48'], |
| 1122 |
['2002:2800::/48', '2002:3200::/48']], |
| 1123 |
'local': '2002:3c00:0004::', |
| 1124 |
'peers': ['2002:3c00:0005::', '2002:3c00:0006::']} |
| 1125 |
self.modify_config_for_test(overrides) |
| 1126 |
self.process.update_vpnservice(self.vpnservice) |
| 1127 |
self._test_ipsec_connection_config(overrides) |
| 1128 |
|
| 1129 |
def test_secrets_config_file(self): |
| 1130 |
expected = EXPECTED_IPSEC_OPENSWAN_SECRET_CONF |
| 1131 |
actual = self.process._gen_config_content( |
| 1132 |
self.conf.openswan.ipsec_secret_template, self.vpnservice) |
| 1133 |
self.check_config_file(expected, actual) |
| 1134 |
|
| 1135 |
|
| 1136 |
class IPsecStrongswanConfigGeneration(BaseIPsecDeviceDriver): |
| 1137 |
|
| 1138 |
def setUp(self, driver=strongswan_ipsec.StrongSwanDriver, |
| 1139 |
ipsec_process=strongswan_ipsec.StrongSwanProcess): |
| 1140 |
super().setUp( |
| 1141 |
driver, ipsec_process, vpnservice=FAKE_VPN_SERVICE) |
| 1142 |
self.conf.register_opts(strongswan_ipsec.strongswan_opts, |
| 1143 |
'strongswan') |
| 1144 |
self.conf.set_override('state_path', '/tmp') |
| 1145 |
self.ipsec_template = self.conf.strongswan.ipsec_config_template |
| 1146 |
self.process = strongswan_ipsec.StrongSwanProcess(self.conf, |
| 1147 |
'foo-process-id', |
| 1148 |
self.vpnservice, |
| 1149 |
mock.ANY) |
| 1150 |
|
| 1151 |
def build_ipsec_expected_config_for_test(self, info): |
| 1152 |
cidrs = info.get('local_cidrs', [['10.0.0.0/24'], ['11.0.0.0/24']]) |
| 1153 |
local_cidrs = [','.join(cidr) for cidr in cidrs] |
| 1154 |
cidrs = info.get('peer_cidrs', [['20.0.0.0/24', '30.0.0.0/24'], |
| 1155 |
['40.0.0.0/24', '50.0.0.0/24']]) |
| 1156 |
peer_cidrs = [','.join(cidr) for cidr in cidrs] |
| 1157 |
local_ip = info.get('local', '60.0.0.4') |
| 1158 |
local_id = info.get('local_id') |
| 1159 |
leftid = local_ip |
| 1160 |
if local_id: |
| 1161 |
leftid = local_id |
| 1162 |
peer_ips = info.get('peers', ['60.0.0.5', '60.0.0.6']) |
| 1163 |
auth_mode = info.get('ipsec_auth', STRONGSWAN_AUTH_ESP) |
| 1164 |
return EXPECTED_IPSEC_STRONGSWAN_CONF % { |
| 1165 |
'vpnservice_id': FAKE_VPNSERVICE_ID, |
| 1166 |
'local_cidrs1': local_cidrs[0], 'local_cidrs2': local_cidrs[1], |
| 1167 |
'peer_cidrs1': peer_cidrs[0], 'peer_cidrs2': peer_cidrs[1], |
| 1168 |
'left': local_ip, |
| 1169 |
'leftid': leftid, |
| 1170 |
'right1': peer_ips[0], 'right2': peer_ips[1], |
| 1171 |
'dpd_action': 'hold', |
| 1172 |
'dpd_delay': 30, |
| 1173 |
'dpd_timeout': 120, |
| 1174 |
'ike_encryption_algorithm': 'aes128', |
| 1175 |
'ike_auth_algorithm': 'sha1', |
| 1176 |
'ike_pfs': 'modp1536', |
| 1177 |
'ike_lifetime': 3600, |
| 1178 |
'life_time': 3600, |
| 1179 |
'auth_mode': auth_mode, |
| 1180 |
'encapsulation_mode': 'tunnel', |
| 1181 |
'conn1_id': FAKE_IPSEC_SITE_CONNECTION1_ID, |
| 1182 |
'conn2_id': FAKE_IPSEC_SITE_CONNECTION2_ID} |
| 1183 |
|
| 1184 |
def test_ipsec_config_file_with_esp(self): |
| 1185 |
self._test_ipsec_connection_config({}) |
| 1186 |
|
| 1187 |
def test_ipsec_config_file_with_ah(self): |
| 1188 |
overrides = {'ipsec_auth': 'ah'} |
| 1189 |
self.modify_config_for_test(overrides) |
| 1190 |
self.process.update_vpnservice(self.vpnservice) |
| 1191 |
info = {'ipsec_auth': STRONGSWAN_AUTH_AH} |
| 1192 |
self._test_ipsec_connection_config(info) |
| 1193 |
|
| 1194 |
def test_ipsec_config_file_for_v6(self): |
| 1195 |
overrides = {'local_cidrs': [['2002:0a00::/48'], ['2002:0b00::/48']], |
| 1196 |
'peer_cidrs': [['2002:1400::/48', '2002:1e00::/48'], |
| 1197 |
['2002:2800::/48', '2002:3200::/48']], |
| 1198 |
'local': '2002:3c00:0004::', |
| 1199 |
'peers': ['2002:3c00:0005::', '2002:3c00:0006::'], |
| 1200 |
'local_id': '2002:3c00:0004::'} |
| 1201 |
self.modify_config_for_test(overrides) |
| 1202 |
self.process.update_vpnservice(self.vpnservice) |
| 1203 |
self._test_ipsec_connection_config(overrides) |
| 1204 |
|
| 1205 |
def test_ipsec_config_file_for_v6_without_local_id(self): |
| 1206 |
overrides = {'local_cidrs': [['2002:0a00::/48'], ['2002:0b00::/48']], |
| 1207 |
'peer_cidrs': [['2002:1400::/48', '2002:1e00::/48'], |
| 1208 |
['2002:2800::/48', '2002:3200::/48']], |
| 1209 |
'local': '2002:3c00:0004::', |
| 1210 |
'peers': ['2002:3c00:0005::', '2002:3c00:0006::']} |
| 1211 |
self.modify_config_for_test(overrides) |
| 1212 |
self.process.update_vpnservice(self.vpnservice) |
| 1213 |
self._test_ipsec_connection_config(overrides) |
| 1214 |
|
| 1215 |
def test_strongswan_default_config_file(self): |
| 1216 |
expected = EXPECTED_STRONGSWAN_DEFAULT_CONF |
| 1217 |
actual = self.process._gen_config_content( |
| 1218 |
self.conf.strongswan.strongswan_config_template, self.vpnservice) |
| 1219 |
self.check_config_file(expected, actual) |
| 1220 |
|
| 1221 |
def test_secrets_config_file(self): |
| 1222 |
expected = EXPECTED_IPSEC_STRONGSWAN_SECRET_CONF |
| 1223 |
actual = self.process._gen_config_content( |
| 1224 |
self.conf.strongswan.ipsec_secret_template, self.vpnservice) |
| 1225 |
self.check_config_file(expected, actual) |
| 1226 |
|
| 1227 |
|
| 1228 |
class TestLibreSwanConfigGeneration(TestOpenSwanConfigGeneration): |
| 1229 |
def setUp(self, driver=libreswan_ipsec.LibreSwanDriver, |
| 1230 |
ipsec_process=libreswan_ipsec.LibreSwanProcess): |
| 1231 |
super().setUp(driver=driver, ipsec_process=ipsec_process) |
| 1232 |
|
| 1233 |
def build_ipsec_expected_config_for_test(self, info): |
| 1234 |
expected = super().build_ipsec_expected_config_for_test(info) |
| 1235 |
return expected.replace(' nat_traversal=yes\n', '') |
| 1236 |
|
| 1237 |
|
| 1238 |
class TestOpenSwanProcess(IPSecDeviceLegacy): |
| 1239 |
|
| 1240 |
_test_timeout = 1 |
| 1241 |
_test_backoff = 2 |
| 1242 |
_test_retries = 5 |
| 1243 |
|
| 1244 |
def setUp(self, driver=openswan_ipsec.OpenSwanDriver, |
| 1245 |
ipsec_process=openswan_ipsec.OpenSwanProcess): |
| 1246 |
super().setUp(driver, ipsec_process) |
| 1247 |
self.conf.register_opts(openswan_ipsec.openswan_opts, |
| 1248 |
'openswan') |
| 1249 |
self.conf.set_override('state_path', '/tmp') |
| 1250 |
cfg.CONF.register_opts(openswan_ipsec.pluto_opts, |
| 1251 |
'pluto') |
| 1252 |
cfg.CONF.set_override('shutdown_check_timeout', self._test_timeout, |
| 1253 |
group='pluto') |
| 1254 |
cfg.CONF.set_override('shutdown_check_back_off', self._test_backoff, |
| 1255 |
group='pluto') |
| 1256 |
cfg.CONF.set_override('shutdown_check_retries', self._test_retries, |
| 1257 |
group='pluto') |
| 1258 |
self.addCleanup(cfg.CONF.reset) |
| 1259 |
|
| 1260 |
self.os_remove = mock.patch('os.remove').start() |
| 1261 |
|
| 1262 |
self.process = openswan_ipsec.OpenSwanProcess(self.conf, |
| 1263 |
'foo-process-id', |
| 1264 |
self.vpnservice, |
| 1265 |
mock.ANY) |
| 1266 |
|
| 1267 |
def test__resolve_fqdn(self): |
| 1268 |
with mock.patch.object(socket, 'getaddrinfo') as mock_getaddr_info: |
| 1269 |
mock_getaddr_info.return_value = [(2, 1, 6, '', |
| 1270 |
('172.168.1.2', 0))] |
| 1271 |
resolved_ip_addr = self.process._resolve_fqdn('fqdn.foo.addr') |
| 1272 |
self.assertEqual('172.168.1.2', resolved_ip_addr) |
| 1273 |
|
| 1274 |
def _test_get_nexthop_helper(self, address, _resolve_fqdn_side_effect, |
| 1275 |
expected_ip_cmd, expected_nexthop): |
| 1276 |
with mock.patch.object(self.process, |
| 1277 |
'_resolve_fqdn') as fake_resolve_fqdn: |
| 1278 |
fake_resolve_fqdn.side_effect = _resolve_fqdn_side_effect |
| 1279 |
|
| 1280 |
returned_next_hop = self.process._get_nexthop(address, |
| 1281 |
'fake-conn-id') |
| 1282 |
_resolve_fqdn_expected_call_count = ( |
| 1283 |
1 if _resolve_fqdn_side_effect else 0) |
| 1284 |
|
| 1285 |
self.assertEqual(_resolve_fqdn_expected_call_count, |
| 1286 |
fake_resolve_fqdn.call_count) |
| 1287 |
self._execute.assert_called_once_with(expected_ip_cmd) |
| 1288 |
self.assertEqual(expected_nexthop, returned_next_hop) |
| 1289 |
|
| 1290 |
def test__get_nexthop_peer_addr_is_ipaddr(self): |
| 1291 |
gw_addr = '10.0.0.1' |
| 1292 |
self._execute.return_value = '172.168.1.2 via %s' % gw_addr |
| 1293 |
peer_address = '172.168.1.2' |
| 1294 |
expected_ip_cmd = ['ip', 'route', 'get', peer_address] |
| 1295 |
self._test_get_nexthop_helper(peer_address, None, |
| 1296 |
expected_ip_cmd, gw_addr) |
| 1297 |
|
| 1298 |
def test__get_nexthop_peer_addr_is_valid_fqdn(self): |
| 1299 |
peer_address = 'foo.peer.addr' |
| 1300 |
expected_ip_cmd = ['ip', 'route', 'get', '172.168.1.2'] |
| 1301 |
gw_addr = '10.0.0.1' |
| 1302 |
self._execute.return_value = '172.168.1.2 via %s' % gw_addr |
| 1303 |
|
| 1304 |
def _fake_resolve_fqdn(address): |
| 1305 |
return '172.168.1.2' |
| 1306 |
|
| 1307 |
self._test_get_nexthop_helper(peer_address, _fake_resolve_fqdn, |
| 1308 |
expected_ip_cmd, gw_addr) |
| 1309 |
|
| 1310 |
def test__get_nexthop_gw_not_present(self): |
| 1311 |
peer_address = '172.168.1.2' |
| 1312 |
expected_ip_cmd = ['ip', 'route', 'get', '172.168.1.2'] |
| 1313 |
self._execute.return_value = ' ' |
| 1314 |
|
| 1315 |
self._test_get_nexthop_helper(peer_address, None, |
| 1316 |
expected_ip_cmd, peer_address) |
| 1317 |
|
| 1318 |
def test__get_nexthop_fqdn_peer_addr_is_not_resolved(self): |
| 1319 |
self.process.connection_status = {} |
| 1320 |
expected_connection_status_dict = ( |
| 1321 |
{'fake-conn-id': {'status': constants.ERROR, |
| 1322 |
'updated_pending_status': True}}) |
| 1323 |
|
| 1324 |
self.assertRaises(vpn_exception.VPNPeerAddressNotResolved, |
| 1325 |
self.process._get_nexthop, 'foo.peer.addr.', |
| 1326 |
'fake-conn-id') |
| 1327 |
self.assertEqual(expected_connection_status_dict, |
| 1328 |
self.process.connection_status) |
| 1329 |
|
| 1330 |
self.process.connection_status = ( |
| 1331 |
{'fake-conn-id': {'status': constants.PENDING_CREATE, |
| 1332 |
'updated_pending_status': False}}) |
| 1333 |
|
| 1334 |
self.assertRaises(vpn_exception.VPNPeerAddressNotResolved, |
| 1335 |
self.process._get_nexthop, 'foo.peer.addr.', |
| 1336 |
'fake-conn-id') |
| 1337 |
self.assertEqual(expected_connection_status_dict, |
| 1338 |
self.process.connection_status) |
| 1339 |
|
| 1340 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.' |
| 1341 |
'ipsec.OpenSwanProcess._get_nexthop', |
| 1342 |
return_value='172.168.1.2') |
| 1343 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.' |
| 1344 |
'ipsec.OpenSwanProcess._cleanup_control_files') |
| 1345 |
def test_no_cleanups(self, cleanup_mock, hop_mock): |
| 1346 |
# Not an "awesome test" but more of a check box item. Basically, |
| 1347 |
# what happens if we didn't need to clean up any files. |
| 1348 |
with mock.patch.object(self.process, |
| 1349 |
'_process_running', |
| 1350 |
return_value=True) as query_mock: |
| 1351 |
self.process.start() |
| 1352 |
self.assertEqual(1, query_mock.call_count) |
| 1353 |
|
| 1354 |
# This is really what is being tested here. If process is |
| 1355 |
# running, we shouldn't attempt a cleanup. |
| 1356 |
self.assertFalse(cleanup_mock.called) |
| 1357 |
|
| 1358 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.' |
| 1359 |
'ipsec.OpenSwanProcess._get_nexthop', |
| 1360 |
return_value='172.168.1.2') |
| 1361 |
@mock.patch('os.path.exists', return_value=True) |
| 1362 |
def test_cleanup_files(self, exists_mock, hop_mock): |
| 1363 |
# Tests the 'bones' of things really and kind of check-box-item-bogus |
| 1364 |
# test - this really needs exercising through a higher level test. |
| 1365 |
with mock.patch.object(self.process, |
| 1366 |
'_process_running', |
| 1367 |
return_value=False) as query_mock: |
| 1368 |
fake_path = '/fake/path/run' |
| 1369 |
self.process.pid_path = fake_path |
| 1370 |
self.process.pid_file = '%s.pid' % fake_path |
| 1371 |
self.process.start() |
| 1372 |
self.assertEqual(1, query_mock.call_count) |
| 1373 |
self.assertEqual(2, self.os_remove.call_count) |
| 1374 |
self.os_remove.assert_has_calls([mock.call('%s.pid' % fake_path), |
| 1375 |
mock.call('%s.ctl' % fake_path)]) |
| 1376 |
|
| 1377 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.' |
| 1378 |
'ipsec.OpenSwanProcess._get_nexthop', |
| 1379 |
return_value='172.168.1.2') |
| 1380 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.' |
| 1381 |
'ipsec.OpenSwanProcess._process_running', |
| 1382 |
return_value=False) |
| 1383 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.' |
| 1384 |
'ipsec.OpenSwanProcess._cleanup_control_files') |
| 1385 |
@mock.patch.object(time, 'sleep') |
| 1386 |
def test_restart_process_not_running(self, sleep_mock, cleanup_mock, |
| 1387 |
query_mock, hop_mock): |
| 1388 |
self.process.restart() |
| 1389 |
|
| 1390 |
# Really what is being tested - retry configuration exists and that |
| 1391 |
# we do the right things when process check is false. |
| 1392 |
self.assertTrue(query_mock.called) |
| 1393 |
self.assertTrue(cleanup_mock.called) |
| 1394 |
self.assertFalse(sleep_mock.called) |
| 1395 |
|
| 1396 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.' |
| 1397 |
'ipsec.OpenSwanProcess._get_nexthop', |
| 1398 |
return_value='172.168.1.2') |
| 1399 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.' |
| 1400 |
'ipsec.OpenSwanProcess._process_running', |
| 1401 |
return_value=True) |
| 1402 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.' |
| 1403 |
'ipsec.OpenSwanProcess._cleanup_control_files') |
| 1404 |
@mock.patch.object(time, 'sleep') |
| 1405 |
def test_restart_process_doesnt_stop(self, sleep_mock, cleanup_mock, |
| 1406 |
query_mock, hop_mock): |
| 1407 |
self.process.restart() |
| 1408 |
|
| 1409 |
# Really what is being tested - retry configuration exists and that |
| 1410 |
# we do the right things when process check is True. |
| 1411 |
self.assertEqual(self._test_retries + 1, query_mock.call_count) |
| 1412 |
self.assertFalse(cleanup_mock.called) |
| 1413 |
self.assertEqual(self._test_retries, sleep_mock.call_count) |
| 1414 |
calls = [mock.call(1), mock.call(2), mock.call(4), |
| 1415 |
mock.call(8), mock.call(16)] |
| 1416 |
sleep_mock.assert_has_calls(calls) |
| 1417 |
|
| 1418 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.' |
| 1419 |
'ipsec.OpenSwanProcess._get_nexthop', |
| 1420 |
return_value='172.168.1.2') |
| 1421 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.' |
| 1422 |
'ipsec.OpenSwanProcess._process_running', |
| 1423 |
side_effect=[True, True, False, False]) |
| 1424 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.' |
| 1425 |
'ipsec.OpenSwanProcess._cleanup_control_files') |
| 1426 |
@mock.patch.object(time, 'sleep') |
| 1427 |
def test_restart_process_retry_until_stop(self, sleep_mock, cleanup_mock, |
| 1428 |
query_mock, hop_mock): |
| 1429 |
self.process.restart() |
| 1430 |
|
| 1431 |
# Really what is being tested - retry configuration exists and that |
| 1432 |
# we do the right things when process check is True a few times and |
| 1433 |
# then returns False. |
| 1434 |
self.assertEqual(4, query_mock.call_count) |
| 1435 |
self.assertTrue(cleanup_mock.called) |
| 1436 |
self.assertEqual(2, sleep_mock.call_count) |
| 1437 |
|
| 1438 |
def test_process_running_no_pid(self): |
| 1439 |
with mock.patch('os.path.exists', return_value=False): |
| 1440 |
self.assertFalse( |
| 1441 |
self.process._process_running()) |
| 1442 |
|
| 1443 |
# open() is used elsewhere, so we need to inject a mocked open into the |
| 1444 |
# module to be tested. |
| 1445 |
@mock.patch('os.path.exists', return_value=True) |
| 1446 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.ipsec.open', |
| 1447 |
create=True, |
| 1448 |
side_effect=IOError) |
| 1449 |
def test_process_running_open_failure(self, mock_open, mock_exists): |
| 1450 |
self.assertFalse(self.process._process_running()) |
| 1451 |
self.assertTrue(mock_exists.called) |
| 1452 |
self.assertTrue(mock_open.called) |
| 1453 |
|
| 1454 |
@mock.patch('os.path.exists', return_value=True) |
| 1455 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.ipsec.open', |
| 1456 |
create=True, |
| 1457 |
side_effect=[io.StringIO('invalid'), |
| 1458 |
IOError]) |
| 1459 |
def test_process_running_bogus_pid(self, mock_open, mock_exists): |
| 1460 |
with mock.patch.object(openswan_ipsec.LOG, 'error'): |
| 1461 |
self.assertFalse(self.process._process_running()) |
| 1462 |
self.assertTrue(mock_exists.called) |
| 1463 |
self.assertEqual(2, mock_open.call_count) |
| 1464 |
|
| 1465 |
@mock.patch('os.path.exists', return_value=True) |
| 1466 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.ipsec.open', |
| 1467 |
create=True, |
| 1468 |
side_effect=[io.StringIO('134'), io.StringIO('')]) |
| 1469 |
def test_process_running_no_cmdline(self, mock_open, mock_exists): |
| 1470 |
with mock.patch.object(openswan_ipsec.LOG, 'error') as log_mock: |
| 1471 |
self.assertFalse(self.process._process_running()) |
| 1472 |
self.assertFalse(log_mock.called) |
| 1473 |
self.assertEqual(2, mock_open.call_count) |
| 1474 |
|
| 1475 |
@mock.patch('os.path.exists', return_value=True) |
| 1476 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.ipsec.open', |
| 1477 |
create=True, |
| 1478 |
side_effect=[io.StringIO('134'), io.StringIO('ps ax')]) |
| 1479 |
def test_process_running_cmdline_mismatch(self, mock_open, mock_exists): |
| 1480 |
with mock.patch.object(openswan_ipsec.LOG, 'error') as log_mock: |
| 1481 |
self.assertFalse(self.process._process_running()) |
| 1482 |
self.assertFalse(log_mock.called) |
| 1483 |
self.assertEqual(2, mock_open.call_count) |
| 1484 |
|
| 1485 |
@mock.patch('os.path.exists', return_value=True) |
| 1486 |
@mock.patch('neutron_vpnaas.services.vpn.device_drivers.ipsec.open', |
| 1487 |
create=True, |
| 1488 |
side_effect=[io.StringIO('134'), |
| 1489 |
io.StringIO('/usr/libexec/ipsec/pluto -ctlbase' |
| 1490 |
'/some/foo/path')]) |
| 1491 |
def test_process_running_cmdline_match(self, mock_open, mock_exists): |
| 1492 |
self.process.pid_path = '/some/foo/path' |
| 1493 |
with mock.patch.object(openswan_ipsec.LOG, 'error') as log_mock: |
| 1494 |
self.assertTrue(self.process._process_running()) |
| 1495 |
self.assertTrue(log_mock.called) |
| 1496 |
|
| 1497 |
def test_status_handling_for_downed_connection(self): |
| 1498 |
"""Test status handling for downed connection.""" |
| 1499 |
self._test_status_handling_for_downed_connection(PLUTO_DOWN_STATUS) |
| 1500 |
|
| 1501 |
def test_status_handling_for_connection_with_no_ipsec_sa(self): |
| 1502 |
"""Test status handling for downed connection.""" |
| 1503 |
self._test_status_handling_for_downed_connection( |
| 1504 |
PLUTO_ACTIVE_NO_IPSEC_SA_STATUS) |
| 1505 |
|
| 1506 |
def test_status_handling_for_active_connection(self): |
| 1507 |
"""Test status handling for active connection.""" |
| 1508 |
self._test_status_handling_for_active_connection(PLUTO_ACTIVE_STATUS) |
| 1509 |
|
| 1510 |
def test_status_handling_for_ike_v2_active_connection(self): |
| 1511 |
"""Test status handling for active connection.""" |
| 1512 |
self._test_status_handling_for_ike_v2_active_connection( |
| 1513 |
PLUTO_ACTIVE_STATUS_IKEV2) |
| 1514 |
|
| 1515 |
def test_status_handling_for_deleted_connection(self): |
| 1516 |
"""Test status handling for deleted connection.""" |
| 1517 |
self._test_status_handling_for_deleted_connection(NOT_RUNNING_STATUS) |
| 1518 |
|
| 1519 |
def test_connection_names_handling_for_multiple_subnets(self): |
| 1520 |
"""Test connection names handling for multiple subnets.""" |
| 1521 |
self._test_connection_names_handling_for_multiple_subnets( |
| 1522 |
PLUTO_MULTIPLE_SUBNETS_ESTABLISHED_STATUS) |
| 1523 |
|
| 1524 |
def test_parse_connection_status(self): |
| 1525 |
"""Test the status of ipsec-site-connection parsed correctly.""" |
| 1526 |
self._test_parse_connection_status(NOT_RUNNING_STATUS, |
| 1527 |
PLUTO_ACTIVE_STATUS, |
| 1528 |
PLUTO_DOWN_STATUS) |
| 1529 |
|
| 1530 |
|
| 1531 |
class TestLibreSwanProcess(base.BaseTestCase): |
| 1532 |
|
| 1533 |
def setUp(self): |
| 1534 |
super().setUp() |
| 1535 |
self.vpnservice = copy.deepcopy(FAKE_VPN_SERVICE) |
| 1536 |
|
| 1537 |
self.ipsec_process = libreswan_ipsec.LibreSwanProcess(cfg.CONF, |
| 1538 |
'foo-process-id', |
| 1539 |
self.vpnservice, |
| 1540 |
mock.ANY) |
| 1541 |
|
| 1542 |
@mock.patch('os.path.exists', return_value=True) |
| 1543 |
def test_ensure_configs_on_restart(self, exists_mock): |
| 1544 |
openswan_ipsec.OpenSwanProcess.ensure_configs = mock.Mock() |
| 1545 |
with mock.patch.object( |
| 1546 |
self.ipsec_process, '_execute' |
| 1547 |
) as fake_execute, mock.patch.object( |
| 1548 |
self.ipsec_process, '_ipsec_execute' |
| 1549 |
) as fake_ipsec_execute, mock.patch.object( |
| 1550 |
self.ipsec_process, '_ensure_needed_files' |
| 1551 |
) as fake_ensure_needed_files: |
| 1552 |
self.ipsec_process.ensure_configs() |
| 1553 |
|
| 1554 |
expected = [mock.call(['rm', '-f', |
| 1555 |
self.ipsec_process._get_config_filename( |
| 1556 |
'ipsec.secrets')]), |
| 1557 |
mock.call(['chown', '--from=%s' % os.getuid(), |
| 1558 |
'root:root', |
| 1559 |
self.ipsec_process._get_config_filename( |
| 1560 |
'ipsec.secrets')]), |
| 1561 |
mock.call(['chown', '--from=%s' % os.getuid(), |
| 1562 |
'root:root', self.ipsec_process.log_dir])] |
| 1563 |
fake_execute.assert_has_calls(expected) |
| 1564 |
self.assertEqual(3, fake_execute.call_count) |
| 1565 |
|
| 1566 |
expected = [mock.call(['_stackmanager', 'start']), |
| 1567 |
mock.call(['checknss'])] |
| 1568 |
fake_ipsec_execute.assert_has_calls(expected) |
| 1569 |
self.assertEqual(2, fake_ipsec_execute.call_count) |
| 1570 |
|
| 1571 |
self.assertTrue(fake_ensure_needed_files.called) |
| 1572 |
self.assertTrue(exists_mock.called) |
| 1573 |
|
| 1574 |
@mock.patch('os.path.exists', return_value=False) |
| 1575 |
def test_ensure_configs(self, exists_mock): |
| 1576 |
openswan_ipsec.OpenSwanProcess.ensure_configs = mock.Mock() |
| 1577 |
with mock.patch.object( |
| 1578 |
self.ipsec_process, '_execute' |
| 1579 |
) as fake_execute, mock.patch.object( |
| 1580 |
self.ipsec_process, '_ipsec_execute' |
| 1581 |
) as fake_ipsec_execute, mock.patch.object( |
| 1582 |
self.ipsec_process, '_ensure_needed_files' |
| 1583 |
) as fake_ensure_needed_files: |
| 1584 |
self.ipsec_process.ensure_configs() |
| 1585 |
|
| 1586 |
expected = [mock.call(['chown', '--from=%s' % os.getuid(), |
| 1587 |
'root:root', |
| 1588 |
self.ipsec_process._get_config_filename( |
| 1589 |
'ipsec.secrets')]), |
| 1590 |
mock.call(['chown', '--from=%s' % os.getuid(), |
| 1591 |
'root:root', self.ipsec_process.log_dir])] |
| 1592 |
fake_execute.assert_has_calls(expected) |
| 1593 |
self.assertEqual(2, fake_execute.call_count) |
| 1594 |
|
| 1595 |
expected = [mock.call(['_stackmanager', 'start']), |
| 1596 |
mock.call(['checknss'])] |
| 1597 |
fake_ipsec_execute.assert_has_calls(expected) |
| 1598 |
self.assertEqual(2, fake_ipsec_execute.call_count) |
| 1599 |
|
| 1600 |
self.assertTrue(fake_ensure_needed_files.called) |
| 1601 |
self.assertTrue(exists_mock.called) |
| 1602 |
|
| 1603 |
exists_mock.reset_mock() |
| 1604 |
|
| 1605 |
with mock.patch.object( |
| 1606 |
self.ipsec_process, '_execute' |
| 1607 |
) as fake_execute, mock.patch.object( |
| 1608 |
self.ipsec_process, '_ipsec_execute' |
| 1609 |
) as fake_ipsec_execute, mock.patch.object( |
| 1610 |
self.ipsec_process, '_ensure_needed_files' |
| 1611 |
) as fake_ensure_needed_files: |
| 1612 |
fake_ipsec_execute.side_effect = [None, RuntimeError, None] |
| 1613 |
self.ipsec_process.ensure_configs() |
| 1614 |
|
| 1615 |
expected = [mock.call(['chown', '--from=%s' % os.getuid(), |
| 1616 |
'root:root', |
| 1617 |
self.ipsec_process._get_config_filename( |
| 1618 |
'ipsec.secrets')]), |
| 1619 |
mock.call(['chown', '--from=%s' % os.getuid(), |
| 1620 |
'root:root', self.ipsec_process.log_dir])] |
| 1621 |
fake_execute.assert_has_calls(expected) |
| 1622 |
self.assertEqual(2, fake_execute.call_count) |
| 1623 |
|
| 1624 |
expected = [mock.call(['_stackmanager', 'start']), |
| 1625 |
mock.call(['checknss']), |
| 1626 |
mock.call(['initnss'])] |
| 1627 |
self.assertEqual(3, fake_ipsec_execute.call_count) |
| 1628 |
fake_ipsec_execute.assert_has_calls(expected) |
| 1629 |
|
| 1630 |
self.assertTrue(fake_ensure_needed_files.called) |
| 1631 |
self.assertTrue(exists_mock.called) |
| 1632 |
|
| 1633 |
|
| 1634 |
class IPsecStrongswanDeviceDriverLegacy(IPSecDeviceLegacy): |
| 1635 |
|
| 1636 |
def setUp(self, driver=strongswan_ipsec.StrongSwanDriver, |
| 1637 |
ipsec_process=strongswan_ipsec.StrongSwanProcess): |
| 1638 |
super().setUp(driver, ipsec_process) |
| 1639 |
self.conf.register_opts(strongswan_ipsec.strongswan_opts, |
| 1640 |
'strongswan') |
| 1641 |
self.conf.set_override('state_path', '/tmp') |
| 1642 |
self.driver.agent_rpc.get_vpn_services_on_host.return_value = [ |
| 1643 |
self.vpnservice] |
| 1644 |
|
| 1645 |
def test_status_handling_for_downed_connection(self): |
| 1646 |
"""Test status handling for downed connection.""" |
| 1647 |
self._test_status_handling_for_downed_connection(CHARON_DOWN_STATUS) |
| 1648 |
|
| 1649 |
def test_status_handling_for_active_connection(self): |
| 1650 |
"""Test status handling for active connection.""" |
| 1651 |
self._test_status_handling_for_active_connection(CHARON_ACTIVE_STATUS) |
| 1652 |
|
| 1653 |
def test_status_handling_for_deleted_connection(self): |
| 1654 |
"""Test status handling for deleted connection.""" |
| 1655 |
self._test_status_handling_for_deleted_connection(NOT_RUNNING_STATUS) |
| 1656 |
|
| 1657 |
def test_parse_connection_status(self): |
| 1658 |
"""Test the status of ipsec-site-connection parsed correctly.""" |
| 1659 |
self._test_parse_connection_status(NOT_RUNNING_STATUS, |
| 1660 |
CHARON_ACTIVE_STATUS, |
| 1661 |
CHARON_DOWN_STATUS) |
| 1662 |
|
| 1663 |
|
| 1664 |
class IPsecStrongswanDeviceDriverDVR(IPSecDeviceDVR): |
| 1665 |
def setUp(self, driver=strongswan_ipsec.StrongSwanDriver, |
| 1666 |
ipsec_process=strongswan_ipsec.StrongSwanProcess): |
| 1667 |
super().setUp(driver, ipsec_process) |