| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
import collections |
| 14 |
import copy |
| 15 |
import functools |
| 16 |
from unittest import mock |
| 17 |
|
| 18 |
import netaddr |
| 19 |
from neutron.agent.common import ovs_lib |
| 20 |
from neutron.agent.l3 import l3_agent_extensions_manager as ext_manager |
| 21 |
from neutron.agent.l3 import namespaces as n_namespaces |
| 22 |
from neutron.agent.l3 import router_info |
| 23 |
from neutron.agent import l3_agent as l3_agent_main |
| 24 |
from neutron.agent.linux import external_process |
| 25 |
from neutron.agent.linux import interface |
| 26 |
from neutron.agent.linux import ip_lib |
| 27 |
from neutron.agent.linux import utils as linux_utils |
| 28 |
from neutron.common import config as common_config |
| 29 |
from neutron.common import utils as common_utils |
| 30 |
from neutron.conf.agent import common as agent_config |
| 31 |
from neutron.conf import common as conf_common |
| 32 |
from neutron.services.provider_configuration import serviceprovider_opts |
| 33 |
from neutron.tests.common import l3_test_common |
| 34 |
from neutron.tests.common import net_helpers |
| 35 |
from neutron.tests.functional.agent.l3 import framework |
| 36 |
from neutron_lib import constants |
| 37 |
from neutron_lib.utils import net as n_utils |
| 38 |
from oslo_config import cfg |
| 39 |
from oslo_log import log as logging |
| 40 |
from oslo_utils import uuidutils |
| 41 |
import testtools |
| 42 |
|
| 43 |
from neutron_vpnaas.services.vpn import agent as vpn_agent |
| 44 |
from neutron_vpnaas.services.vpn.agent import vpn_agent_opts |
| 45 |
from neutron_vpnaas.services.vpn.device_drivers import ipsec |
| 46 |
|
| 47 |
|
| 48 |
_uuid = uuidutils.generate_uuid |
| 49 |
FAKE_IKE_POLICY = { |
| 50 |
'auth_algorithm': 'sha1', |
| 51 |
"ike_version": "v1", |
| 52 |
'encryption_algorithm': 'aes-128', |
| 53 |
'pfs': 'group5', |
| 54 |
'phase1_negotiation_mode': 'main', |
| 55 |
'lifetime_units': 'seconds', |
| 56 |
'lifetime_value': 3600 |
| 57 |
} |
| 58 |
|
| 59 |
FAKE_IPSEC_POLICY = { |
| 60 |
"encapsulation_mode": "tunnel", |
| 61 |
"encryption_algorithm": "aes-128", |
| 62 |
"pfs": "group5", |
| 63 |
"lifetime_units": "seconds", |
| 64 |
"lifetime_value": 3600, |
| 65 |
"transform_protocol": "esp", |
| 66 |
"auth_algorithm": "sha1", |
| 67 |
} |
| 68 |
|
| 69 |
FAKE_VPN_SERVICE = { |
| 70 |
"id": _uuid(), |
| 71 |
"router_id": _uuid(), |
| 72 |
"status": constants.PENDING_CREATE, |
| 73 |
"admin_state_up": True, |
| 74 |
'external_ip': "172.24.4.8" |
| 75 |
} |
| 76 |
|
| 77 |
FAKE_IPSEC_CONNECTION = { |
| 78 |
"vpnservice_id": _uuid(), |
| 79 |
"status": "PENDING_CREATE", |
| 80 |
"psk": "969022489", |
| 81 |
"initiator": "bi-directional", |
| 82 |
"admin_state_up": True, |
| 83 |
"auth_mode": "psk", |
| 84 |
'external_ip': "172.24.4.8", |
| 85 |
"peer_cidrs": ["10.100.255.224/28"], |
| 86 |
"mtu": 1500, |
| 87 |
"dpd_action": "hold", |
| 88 |
"dpd_interval": 30, |
| 89 |
"dpd_timeout": 120, |
| 90 |
"route_mode": "static", |
| 91 |
"ikepolicy": FAKE_IKE_POLICY, |
| 92 |
"ipsecpolicy": FAKE_IPSEC_POLICY, |
| 93 |
"peer_address": "172.24.4.8", |
| 94 |
"peer_id": "172.24.4.8", |
| 95 |
"id": _uuid() |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
FAKE_IKE_POLICY_SHA256 = { |
| 100 |
'auth_algorithm': 'sha256', |
| 101 |
"ike_version": "v1", |
| 102 |
'encryption_algorithm': 'aes-128', |
| 103 |
'pfs': 'group5', |
| 104 |
'phase1_negotiation_mode': 'main', |
| 105 |
'lifetime_units': 'seconds', |
| 106 |
'lifetime_value': 3600 |
| 107 |
} |
| 108 |
|
| 109 |
FAKE_IPSEC_POLICY_SHA256 = { |
| 110 |
"encapsulation_mode": "tunnel", |
| 111 |
"encryption_algorithm": "aes-128", |
| 112 |
"pfs": "group5", |
| 113 |
"lifetime_units": "seconds", |
| 114 |
"lifetime_value": 3600, |
| 115 |
"transform_protocol": "esp", |
| 116 |
"auth_algorithm": "sha256", |
| 117 |
} |
| 118 |
|
| 119 |
FAKE_IPSEC_CONNECTION_SHA256 = { |
| 120 |
"vpnservice_id": _uuid(), |
| 121 |
"status": "PENDING_CREATE", |
| 122 |
"psk": "969022489", |
| 123 |
"initiator": "bi-directional", |
| 124 |
"admin_state_up": True, |
| 125 |
"auth_mode": "psk", |
| 126 |
'external_ip': "172.24.4.8", |
| 127 |
"peer_cidrs": ["10.100.255.224/28"], |
| 128 |
"mtu": 1500, |
| 129 |
"dpd_action": "hold", |
| 130 |
"dpd_interval": 30, |
| 131 |
"dpd_timeout": 120, |
| 132 |
"route_mode": "static", |
| 133 |
"ikepolicy": FAKE_IKE_POLICY_SHA256, |
| 134 |
"ipsecpolicy": FAKE_IPSEC_POLICY_SHA256, |
| 135 |
"peer_address": "172.24.4.8", |
| 136 |
"peer_id": "172.24.4.8", |
| 137 |
"id": _uuid() |
| 138 |
} |
| 139 |
|
| 140 |
PUBLIC_NET = netaddr.IPNetwork('19.4.4.0/24') |
| 141 |
PRIVATE_NET = netaddr.IPNetwork('35.4.0.0/16') |
| 142 |
FAKE_PUBLIC_SUBNET_ID = _uuid() |
| 143 |
FAKE_PRIVATE_SUBNET_ID = _uuid() |
| 144 |
|
| 145 |
FAKE_ROUTER = { |
| 146 |
'enable_snat': True, |
| 147 |
'gw_port': { |
| 148 |
'network_id': _uuid(), |
| 149 |
'subnets': [ |
| 150 |
{ |
| 151 |
'cidr': str(PUBLIC_NET), |
| 152 |
'gateway_ip': str(PUBLIC_NET[1]), |
| 153 |
'id': FAKE_PUBLIC_SUBNET_ID |
| 154 |
} |
| 155 |
], |
| 156 |
'fixed_ips': [ |
| 157 |
{ |
| 158 |
'subnet_id': FAKE_PUBLIC_SUBNET_ID, |
| 159 |
'prefixlen': PUBLIC_NET.prefixlen, |
| 160 |
} |
| 161 |
], |
| 162 |
}, |
| 163 |
'distributed': False, |
| 164 |
'_floatingips': [], |
| 165 |
'routes': [] |
| 166 |
} |
| 167 |
|
| 168 |
# It's a long name. |
| 169 |
NON_ASCII_VPNSERVICE_NAME = '\u9577\u3044\u540d\u524d\u3067\u3059' |
| 170 |
# I'm doing very well. |
| 171 |
NON_ASCII_PSK = '\u00e7a va tr\u00e9s bien' |
| 172 |
|
| 173 |
|
| 174 |
def get_ovs_bridge(br_name): |
| 175 |
return ovs_lib.OVSBridge(br_name) |
| 176 |
|
| 177 |
|
| 178 |
Vm = collections.namedtuple('Vm', ['namespace', 'port_ip']) |
| 179 |
|
| 180 |
|
| 181 |
class SiteInfo: |
| 182 |
|
| 183 |
"""Holds info on the router, ports, service, and connection.""" |
| 184 |
|
| 185 |
def __init__(self, public_net, private_nets): |
| 186 |
self.public_net = public_net |
| 187 |
self.private_nets = private_nets |
| 188 |
self.generate_router_info() |
| 189 |
self._prepare_vpn_service_info() |
| 190 |
|
| 191 |
def _get_random_mac(self): |
| 192 |
mac_base = cfg.CONF.base_mac.split(':') |
| 193 |
return n_utils.get_random_mac(mac_base) |
| 194 |
|
| 195 |
def _generate_private_interface_for_router(self, subnet): |
| 196 |
subnet_id = _uuid() |
| 197 |
return { |
| 198 |
'id': _uuid(), |
| 199 |
'admin_state_up': True, |
| 200 |
'network_id': _uuid(), |
| 201 |
'mtu': 1500, |
| 202 |
'mac_address': self._get_random_mac(), |
| 203 |
'subnets': [ |
| 204 |
{ |
| 205 |
'ipv6_ra_mode': None, |
| 206 |
'cidr': str(subnet), |
| 207 |
'gateway_ip': str(subnet[1]), |
| 208 |
'id': subnet_id, |
| 209 |
'ipv6_address_mode': None |
| 210 |
} |
| 211 |
], |
| 212 |
'fixed_ips': [ |
| 213 |
{ |
| 214 |
'subnet_id': subnet_id, |
| 215 |
'prefixlen': 24, |
| 216 |
'ip_address': str(subnet[4]) |
| 217 |
} |
| 218 |
] |
| 219 |
} |
| 220 |
|
| 221 |
def generate_router_info(self): |
| 222 |
self.info = copy.deepcopy(FAKE_ROUTER) |
| 223 |
self.info['id'] = _uuid() |
| 224 |
self.info['project_id'] = _uuid() |
| 225 |
self.info['_interfaces'] = [ |
| 226 |
self._generate_private_interface_for_router(subnet) |
| 227 |
for subnet in self.private_nets] |
| 228 |
self.info['gw_port']['id'] = _uuid() |
| 229 |
self.info['gw_port']['fixed_ips'][0]['ip_address'] = str( |
| 230 |
self.public_net) |
| 231 |
self.info['gw_port']['mac_address'] = self._get_random_mac() |
| 232 |
self.info['ha'] = False |
| 233 |
|
| 234 |
def _prepare_vpn_service_info(self): |
| 235 |
self.vpn_service = copy.deepcopy(FAKE_VPN_SERVICE) |
| 236 |
self.vpn_service.update({'id': _uuid(), |
| 237 |
'router_id': self.info['id'], |
| 238 |
'external_ip': str(self.public_net)}) |
| 239 |
|
| 240 |
def prepare_ipsec_conn_info(self, peer, connection=FAKE_IPSEC_CONNECTION, |
| 241 |
local_id=None, peer_id=None): |
| 242 |
ipsec_connection = copy.deepcopy(connection) |
| 243 |
local_cidrs = [str(s) for s in self.private_nets] |
| 244 |
peer_cidrs = [str(s) for s in peer.private_nets] |
| 245 |
ipsec_connection.update({ |
| 246 |
'id': _uuid(), |
| 247 |
'vpnservice_id': self.vpn_service['id'], |
| 248 |
'external_ip': self.vpn_service['external_ip'], |
| 249 |
'peer_cidrs': peer_cidrs, |
| 250 |
'peer_address': peer.vpn_service['external_ip'], |
| 251 |
'peer_id': peer.vpn_service['external_ip'], |
| 252 |
'local_cidrs': local_cidrs, |
| 253 |
'local_ip_vers': 4 |
| 254 |
}) |
| 255 |
if local_id: |
| 256 |
ipsec_connection['local_id'] = local_id |
| 257 |
if peer_id: |
| 258 |
ipsec_connection['peer_id'] = peer_id |
| 259 |
self.vpn_service['ipsec_site_connections'] = [ipsec_connection] |
| 260 |
|
| 261 |
|
| 262 |
class SiteInfoWithHaRouter(SiteInfo): |
| 263 |
|
| 264 |
def __init__(self, public_net, private_nets, host, failover_host): |
| 265 |
self.host = host |
| 266 |
self.failover_host = failover_host |
| 267 |
self.get_ns_name = mock.patch.object(n_namespaces.RouterNamespace, |
| 268 |
'_get_ns_name').start() |
| 269 |
super().__init__(public_net, private_nets) |
| 270 |
|
| 271 |
def generate_router_info(self): |
| 272 |
super().generate_router_info() |
| 273 |
self.info['ha'] = True |
| 274 |
self.info['ha_vr_id'] = 1 |
| 275 |
self.info[constants.HA_INTERFACE_KEY] = ( |
| 276 |
l3_test_common.get_ha_interface()) |
| 277 |
# Mock router namespace name, for when router is created |
| 278 |
self.get_ns_name.return_value = "qrouter-{}-{}".format( |
| 279 |
self.info['id'], self.host) |
| 280 |
|
| 281 |
def generate_backup_router_info(self): |
| 282 |
# Clone router info, using different HA interface (using same ID) |
| 283 |
info = copy.deepcopy(self.info) |
| 284 |
info[constants.HA_INTERFACE_KEY] = ( |
| 285 |
l3_test_common.get_ha_interface(ip='169.254.192.2', |
| 286 |
mac='22:22:22:22:22:22')) |
| 287 |
# Mock router namespace name, for when router is created |
| 288 |
self.get_ns_name.return_value = "qrouter-{}-{}".format( |
| 289 |
info['id'], self.failover_host) |
| 290 |
return info |
| 291 |
|
| 292 |
|
| 293 |
class TestIPSecBase(framework.L3AgentTestFramework): |
| 294 |
NESTED_NAMESPACE_SEPARATOR = '@' |
| 295 |
|
| 296 |
def setUp(self): |
| 297 |
super().setUp() |
| 298 |
common_config.register_common_config_options() |
| 299 |
mock.patch('neutron_vpnaas.services.vpn.device_drivers.ipsec.' |
| 300 |
'IPsecVpnDriverApi').start() |
| 301 |
# avoid report_status running periodically |
| 302 |
mock.patch('oslo_service.loopingcall.FixedIntervalLoopingCall').start() |
| 303 |
# Both the vpn agents try to use execute_rootwrap_daemon's socket |
| 304 |
# simultaneously during test cleanup, but execute_rootwrap_daemon has |
| 305 |
# limitations with simultaneous reads. So avoid using |
| 306 |
# root_helper_daemon and instead use root_helper |
| 307 |
# https://bugs.launchpad.net/neutron/+bug/1482622 |
| 308 |
cfg.CONF.set_override('root_helper_daemon', None, group='AGENT') |
| 309 |
self.vpn_agent = vpn_agent.L3WithVPNaaS(self.conf) |
| 310 |
self.driver = self.vpn_agent.device_drivers[0] |
| 311 |
self.driver.agent_rpc.get_vpn_services_on_host = mock.Mock( |
| 312 |
return_value=[]) |
| 313 |
self.driver.report_status = mock.Mock() |
| 314 |
|
| 315 |
self.private_nets = list(PRIVATE_NET.subnet(24)) |
| 316 |
|
| 317 |
def _connect_agents(self, agent1, agent2): |
| 318 |
"""Simulate both agents in the same host. |
| 319 |
|
| 320 |
For packet flow between resources connected to these two agents, |
| 321 |
agent's ovs bridges are connected through patch ports. |
| 322 |
""" |
| 323 |
br_int_1 = get_ovs_bridge(agent1.conf.OVS.integration_bridge) |
| 324 |
br_int_2 = get_ovs_bridge(agent2.conf.OVS.integration_bridge) |
| 325 |
net_helpers.create_patch_ports(br_int_1, br_int_2) |
| 326 |
|
| 327 |
def _get_config_opts(self): |
| 328 |
"""Register default config options""" |
| 329 |
config = cfg.ConfigOpts() |
| 330 |
config.register_opts(conf_common.core_opts) |
| 331 |
config.register_opts(conf_common.core_cli_opts) |
| 332 |
config.register_opts(serviceprovider_opts, 'service_providers') |
| 333 |
config.register_opts(vpn_agent_opts, 'vpnagent') |
| 334 |
config.register_opts(ipsec.ipsec_opts, 'ipsec') |
| 335 |
config.register_opts(ipsec.openswan_opts, 'openswan') |
| 336 |
|
| 337 |
logging.register_options(config) |
| 338 |
agent_config.register_process_monitor_opts(config) |
| 339 |
ext_manager.register_opts(config) |
| 340 |
return config |
| 341 |
|
| 342 |
def _configure_agent(self, host): |
| 343 |
"""Override specific config options""" |
| 344 |
config = self._get_config_opts() |
| 345 |
l3_agent_main.register_opts(config) |
| 346 |
cfg.CONF.set_override('debug', True) |
| 347 |
agent_config.setup_logging() |
| 348 |
config.set_override('extensions', ['vpnaas'], 'agent') |
| 349 |
config.set_override( |
| 350 |
'interface_driver', |
| 351 |
'neutron.agent.linux.interface.OVSInterfaceDriver') |
| 352 |
|
| 353 |
br_int = self.useFixture(net_helpers.OVSBridgeFixture()).bridge |
| 354 |
config.set_override('integration_bridge', br_int.br_name, 'OVS') |
| 355 |
|
| 356 |
temp_dir = self.get_new_temp_dir() |
| 357 |
get_temp_file_path = functools.partial(self.get_temp_file_path, |
| 358 |
root=temp_dir) |
| 359 |
config.set_override('state_path', temp_dir.path) |
| 360 |
config.set_override('metadata_proxy_socket', |
| 361 |
get_temp_file_path('metadata_proxy')) |
| 362 |
config.set_override('ha_confs_path', |
| 363 |
get_temp_file_path('ha_confs')) |
| 364 |
config.set_override('external_pids', |
| 365 |
get_temp_file_path('external/pids')) |
| 366 |
config.set_override('host', host) |
| 367 |
ipsec_config_base_dir = '{}/{}'.format(temp_dir.path, 'ipsec') |
| 368 |
config.set_override('config_base_dir', |
| 369 |
ipsec_config_base_dir, group='ipsec') |
| 370 |
|
| 371 |
# Assign ip address to br-int port because it is a gateway |
| 372 |
ex_port = ip_lib.IPDevice(br_int.br_name) |
| 373 |
ex_port.addr.add(str(PUBLIC_NET[1])) |
| 374 |
|
| 375 |
return config |
| 376 |
|
| 377 |
def _setup_failover_agent(self): |
| 378 |
self.failover_agent = self._configure_agent('agent2') |
| 379 |
self._connect_agents(self.vpn_agent, self.failover_agent) |
| 380 |
self.failover_driver = self.failover_agent.device_drivers[0] |
| 381 |
self.failover_driver.agent_rpc.get_vpn_services_on_host = ( |
| 382 |
mock.Mock(return_value=[])) |
| 383 |
self.failover_driver.report_status = mock.Mock() |
| 384 |
|
| 385 |
def create_router(self, agent, info): |
| 386 |
"""Create router for agent from router info.""" |
| 387 |
self.addCleanup(agent._safe_router_removed, info['id']) |
| 388 |
|
| 389 |
# Generate unique internal and external router device names using the |
| 390 |
# agent's hostname. This is to allow multiple HA router replicas to |
| 391 |
# co-exist on the same machine, otherwise they'd all use the same |
| 392 |
# device names and OVS would freak out(OVS won't allow a port with |
| 393 |
# same name connected to two bridges). |
| 394 |
def _append_suffix(dev_name): |
| 395 |
# If dev_name = 'xyz123' and the suffix is 'agent2' then the result |
| 396 |
# will be 'xy-nt2' |
| 397 |
return f"{dev_name[:-4]}-{agent.host[-3:]}" |
| 398 |
|
| 399 |
def get_internal_device_name(port_id): |
| 400 |
return _append_suffix( |
| 401 |
(n_namespaces.INTERNAL_DEV_PREFIX + port_id) |
| 402 |
[:interface.LinuxInterfaceDriver.DEV_NAME_LEN]) |
| 403 |
|
| 404 |
def get_external_device_name(port_id): |
| 405 |
return _append_suffix( |
| 406 |
(n_namespaces.EXTERNAL_DEV_PREFIX + port_id) |
| 407 |
[:interface.LinuxInterfaceDriver.DEV_NAME_LEN]) |
| 408 |
|
| 409 |
mock_get_internal_device_name = mock.patch.object( |
| 410 |
router_info.RouterInfo, 'get_internal_device_name').start() |
| 411 |
mock_get_internal_device_name.side_effect = get_internal_device_name |
| 412 |
mock_get_external_device_name = mock.patch.object( |
| 413 |
router_info.RouterInfo, 'get_external_device_name').start() |
| 414 |
mock_get_external_device_name.side_effect = get_external_device_name |
| 415 |
|
| 416 |
# NOTE(huntxu): with commit 88f5e11d8bf, neutron plugs new ports as |
| 417 |
# dead vlan(4095). During functional tests, all the ports are untagged. |
| 418 |
# So need to remove such tag during functional testing. |
| 419 |
original_plug_new = interface.OVSInterfaceDriver.plug_new |
| 420 |
|
| 421 |
def plug_new(self, *args, **kwargs): |
| 422 |
original_plug_new(self, *args, **kwargs) |
| 423 |
bridge = (kwargs.get('bridge') or args[4] or |
| 424 |
self.conf.OVS.integration_bridge) |
| 425 |
device_name = kwargs.get('device_name') or args[2] |
| 426 |
ovsbr = ovs_lib.OVSBridge(bridge) |
| 427 |
ovsbr.clear_db_attribute('Port', device_name, 'tag') |
| 428 |
|
| 429 |
with mock.patch( |
| 430 |
'neutron.agent.linux.interface.OVSInterfaceDriver.plug_new', |
| 431 |
autospec=True |
| 432 |
) as ovs_plug_new: |
| 433 |
ovs_plug_new.side_effect = plug_new |
| 434 |
agent._process_added_router(info) |
| 435 |
|
| 436 |
return agent.router_info[info['id']] |
| 437 |
|
| 438 |
def _port_first_ip_cidr(self, port): |
| 439 |
fixed_ip = port['fixed_ips'][0] |
| 440 |
return common_utils.ip_to_cidr(fixed_ip['ip_address'], |
| 441 |
fixed_ip['prefixlen']) |
| 442 |
|
| 443 |
def create_ports_for(self, site): |
| 444 |
"""Creates namespaces and ports for simulated VM. |
| 445 |
|
| 446 |
There will be a unique namespace for each port, which is representing |
| 447 |
a VM for the test. |
| 448 |
""" |
| 449 |
bridge = get_ovs_bridge(self.vpn_agent.conf.OVS.integration_bridge) |
| 450 |
site.vm = [] |
| 451 |
for internal_port in site.router.internal_ports: |
| 452 |
router_ip_cidr = self._port_first_ip_cidr(internal_port) |
| 453 |
port_ip_cidr = net_helpers.increment_ip_cidr(router_ip_cidr, 1) |
| 454 |
client_ns = self.useFixture( |
| 455 |
net_helpers.NamespaceFixture()).ip_wrapper |
| 456 |
namespace = client_ns.namespace |
| 457 |
port = self.useFixture( |
| 458 |
net_helpers.OVSPortFixture(bridge, namespace)).port |
| 459 |
port.addr.add(port_ip_cidr) |
| 460 |
port.route.add_gateway(router_ip_cidr.partition('/')[0]) |
| 461 |
site.vm.append(Vm(namespace, port_ip_cidr.partition('/')[0])) |
| 462 |
|
| 463 |
def create_site(self, public_net, private_nets, l3ha=False): |
| 464 |
"""Build router(s), namespaces, and ports for a site. |
| 465 |
|
| 466 |
For HA, we'll create a backup router and wait for both routers |
| 467 |
to be ready, so that we can test pings after failover. |
| 468 |
""" |
| 469 |
if l3ha: |
| 470 |
site = SiteInfoWithHaRouter(public_net, private_nets, |
| 471 |
self.agent.host, |
| 472 |
self.failover_agent.host) |
| 473 |
else: |
| 474 |
site = SiteInfo(public_net, private_nets) |
| 475 |
|
| 476 |
site.router = self.create_router(self.agent, site.info) |
| 477 |
if l3ha: |
| 478 |
backup_info = site.generate_backup_router_info() |
| 479 |
site.backup_router = self.create_router(self.failover_agent, |
| 480 |
backup_info) |
| 481 |
linux_utils.wait_until_true( |
| 482 |
lambda: site.router.ha_state in ('master', 'primary')) |
| 483 |
linux_utils.wait_until_true( |
| 484 |
lambda: site.backup_router.ha_state == 'backup') |
| 485 |
|
| 486 |
self.create_ports_for(site) |
| 487 |
return site |
| 488 |
|
| 489 |
def prepare_ipsec_site_connections(self, site1, site2): |
| 490 |
"""Builds info for connections in both directions in prep for sync.""" |
| 491 |
site1.prepare_ipsec_conn_info(site2) |
| 492 |
site2.prepare_ipsec_conn_info(site1) |
| 493 |
|
| 494 |
def prepare_ipsec_site_connections_sha256(self, site1, site2): |
| 495 |
"""Builds info for connections in both directions in prep for sync.""" |
| 496 |
site1.prepare_ipsec_conn_info(site2, |
| 497 |
FAKE_IPSEC_CONNECTION_SHA256) |
| 498 |
site2.prepare_ipsec_conn_info(site1, |
| 499 |
FAKE_IPSEC_CONNECTION_SHA256) |
| 500 |
|
| 501 |
def prepare_ipsec_site_connections_local_id(self, site1, site2): |
| 502 |
"""Builds info for connections in both directions in prep for sync.""" |
| 503 |
site1.prepare_ipsec_conn_info(site2, local_id='@site1.com', |
| 504 |
peer_id='@site2.com') |
| 505 |
site2.prepare_ipsec_conn_info(site1, local_id='@site2.com', |
| 506 |
peer_id='@site1.com') |
| 507 |
|
| 508 |
def sync_to_create_ipsec_connections(self, site1, site2): |
| 509 |
"""Perform a sync, so that connections are created.""" |
| 510 |
# Provide service info to sync |
| 511 |
self.driver.agent_rpc.get_vpn_services_on_host = mock.Mock( |
| 512 |
return_value=[site1.vpn_service, site2.vpn_service]) |
| 513 |
|
| 514 |
local_router_id = site1.router.router_id |
| 515 |
peer_router_id = site2.router.router_id |
| 516 |
self.driver.sync(mock.Mock(), [site1.router, |
| 517 |
site2.router]) |
| 518 |
self.agent._process_updated_router(site1.router.router) |
| 519 |
self.agent._process_updated_router(site2.router.router) |
| 520 |
self.addCleanup(self.driver._delete_vpn_processes, |
| 521 |
[local_router_id, peer_router_id], []) |
| 522 |
|
| 523 |
def sync_failover_agent(self, site): |
| 524 |
"""Perform a sync on failover agent associated w/backup router.""" |
| 525 |
self.failover_driver.agent_rpc.get_vpn_services_on_host = mock.Mock( |
| 526 |
return_value=[site.vpn_service]) |
| 527 |
self.failover_driver.sync(mock.Mock(), [site.router]) |
| 528 |
|
| 529 |
def check_ping(self, from_site, to_site, instance=0, success=True): |
| 530 |
if success: |
| 531 |
net_helpers.assert_ping(from_site.vm[instance].namespace, |
| 532 |
to_site.vm[instance].port_ip, |
| 533 |
timeout=8, count=4) |
| 534 |
else: |
| 535 |
net_helpers.assert_no_ping(from_site.vm[instance].namespace, |
| 536 |
to_site.vm[instance].port_ip, |
| 537 |
timeout=8, count=4) |
| 538 |
|
| 539 |
def _failover_ha_router(self, router1, router2): |
| 540 |
"""Cause a failover of HA router. |
| 541 |
|
| 542 |
Fail the agent1's HA router. Agent1's HA router will transition |
| 543 |
to backup and agent2's HA router will become master. Wait for |
| 544 |
the failover to complete. |
| 545 |
""" |
| 546 |
device_name = router1.get_ha_device_name() |
| 547 |
ha_device = ip_lib.IPDevice(device_name, router1.ns_name) |
| 548 |
ha_device.link.set_down() |
| 549 |
linux_utils.wait_until_true( |
| 550 |
lambda: router2.ha_state in ('master', 'primary')) |
| 551 |
linux_utils.wait_until_true(lambda: router1.ha_state == 'backup') |
| 552 |
|
| 553 |
def _ipsec_process_exists(self, conf, router, pid_files): |
| 554 |
"""Check if *Swan process has started up.""" |
| 555 |
for pid_file in pid_files: |
| 556 |
pm = external_process.ProcessManager( |
| 557 |
conf, |
| 558 |
"ipsec", |
| 559 |
router.ns_name, pid_file=pid_file) |
| 560 |
if pm.active: |
| 561 |
break |
| 562 |
return pm.active |
| 563 |
|
| 564 |
def _wait_for_ipsec_startup(self, router, driver, conf, should_run=True): |
| 565 |
"""Wait for new IPSec process on failover agent to start up.""" |
| 566 |
# check for both strongswan and openswan processes |
| 567 |
path = driver.processes[router.router_id].config_dir |
| 568 |
pid_files = ['%s/var/run/charon.pid' % path, |
| 569 |
'%s/var/run/pluto.pid' % path] |
| 570 |
linux_utils.wait_until_true( |
| 571 |
lambda: should_run == self._ipsec_process_exists( |
| 572 |
conf, router, pid_files)) |
| 573 |
|
| 574 |
@staticmethod |
| 575 |
def _update_vpnservice(site, **kwargs): |
| 576 |
site.vpn_service.update(kwargs) |
| 577 |
|
| 578 |
@staticmethod |
| 579 |
def _update_ipsec_connection(site, **kwargs): |
| 580 |
ipsec_connection = site.vpn_service['ipsec_site_connections'][0] |
| 581 |
ipsec_connection.update(kwargs) |
| 582 |
|
| 583 |
|
| 584 |
class TestIPSecScenario(TestIPSecBase): |
| 585 |
|
| 586 |
@testtools.skip('bug/1598466') |
| 587 |
def test_single_ipsec_connection(self): |
| 588 |
site1 = self.create_site(PUBLIC_NET[4], [self.private_nets[1]]) |
| 589 |
site2 = self.create_site(PUBLIC_NET[5], [self.private_nets[2]]) |
| 590 |
|
| 591 |
self.check_ping(site1, site2, success=False) |
| 592 |
self.check_ping(site2, site1, success=False) |
| 593 |
|
| 594 |
self.prepare_ipsec_site_connections(site1, site2) |
| 595 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 596 |
|
| 597 |
self.check_ping(site1, site2) |
| 598 |
self.check_ping(site2, site1) |
| 599 |
|
| 600 |
@testtools.skip('bug/1598466') |
| 601 |
def test_single_ipsec_connection_sha256(self): |
| 602 |
site1 = self.create_site(PUBLIC_NET[4], [self.private_nets[1]]) |
| 603 |
site2 = self.create_site(PUBLIC_NET[5], [self.private_nets[2]]) |
| 604 |
|
| 605 |
self.check_ping(site1, site2, success=False) |
| 606 |
self.check_ping(site2, site1, success=False) |
| 607 |
|
| 608 |
self.prepare_ipsec_site_connections_sha256(site1, site2) |
| 609 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 610 |
|
| 611 |
self.check_ping(site1, site2) |
| 612 |
self.check_ping(site2, site1) |
| 613 |
|
| 614 |
@testtools.skip('bug/1598466') |
| 615 |
def test_single_ipsec_connection_local_id(self): |
| 616 |
site1 = self.create_site(PUBLIC_NET[4], [self.private_nets[1]]) |
| 617 |
site2 = self.create_site(PUBLIC_NET[5], [self.private_nets[2]]) |
| 618 |
|
| 619 |
self.check_ping(site1, site2, success=False) |
| 620 |
self.check_ping(site2, site1, success=False) |
| 621 |
|
| 622 |
self.prepare_ipsec_site_connections_local_id(site1, site2) |
| 623 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 624 |
|
| 625 |
self.check_ping(site1, site2) |
| 626 |
self.check_ping(site2, site1) |
| 627 |
|
| 628 |
@testtools.skip('bug/1598466') |
| 629 |
def test_ipsec_site_connections_with_mulitple_subnets(self): |
| 630 |
"""Check with a pair of subnets on each end of connection.""" |
| 631 |
site1 = self.create_site(PUBLIC_NET[4], self.private_nets[1:3]) |
| 632 |
site2 = self.create_site(PUBLIC_NET[5], self.private_nets[3:5]) |
| 633 |
|
| 634 |
# Just check from each VM, not every combination |
| 635 |
for i in [0, 1]: |
| 636 |
self.check_ping(site1, site2, instance=i, success=False) |
| 637 |
self.check_ping(site2, site1, instance=i, success=False) |
| 638 |
|
| 639 |
self.prepare_ipsec_site_connections(site1, site2) |
| 640 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 641 |
|
| 642 |
for i in [0, 1]: |
| 643 |
self.check_ping(site1, site2, instance=i) |
| 644 |
self.check_ping(site2, site1, instance=i) |
| 645 |
|
| 646 |
@testtools.skip('bug/1598466') |
| 647 |
def test_ipsec_site_connections_with_l3ha_routers(self): |
| 648 |
"""Test ipsec site connection with HA routers. |
| 649 |
This test creates two agents. First agent will have Legacy and HA |
| 650 |
routers. Second agent will host only HA router. We setup ipsec |
| 651 |
connection between legacy and HA router. |
| 652 |
|
| 653 |
When HA router is created, agent1 will have master router and |
| 654 |
agent2 will have backup router. Ipsec connection will be established |
| 655 |
between legacy router and agent1's master HA router. |
| 656 |
|
| 657 |
Then we fail the agent1's master HA router. Agent1's HA router will |
| 658 |
transition to backup and agent2's HA router will become master. |
| 659 |
Now ipsec connection will be established between legacy router and |
| 660 |
agent2's master HA router |
| 661 |
""" |
| 662 |
|
| 663 |
self._setup_failover_agent() |
| 664 |
|
| 665 |
site1 = self.create_site(PUBLIC_NET[4], [self.private_nets[1]]) |
| 666 |
site2 = self.create_site(PUBLIC_NET[5], [self.private_nets[2]], |
| 667 |
l3ha=True) |
| 668 |
|
| 669 |
# No ipsec connection between legacy router and HA routers |
| 670 |
self.check_ping(site1, site2, 0, success=False) |
| 671 |
self.check_ping(site2, site1, 0, success=False) |
| 672 |
|
| 673 |
self.prepare_ipsec_site_connections(site1, site2) |
| 674 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 675 |
self.sync_failover_agent(site2) |
| 676 |
|
| 677 |
# Test ipsec connection between legacy router and agent2's HA router |
| 678 |
self.check_ping(site1, site2, 0) |
| 679 |
self.check_ping(site2, site1, 0) |
| 680 |
|
| 681 |
self._failover_ha_router(site2.router, site2.backup_router) |
| 682 |
self._wait_for_ipsec_startup(site2.backup_router, |
| 683 |
self.failover_driver, |
| 684 |
self.failover_agent.conf) |
| 685 |
|
| 686 |
# Test ipsec connection between legacy router and agent2's HA router |
| 687 |
self.check_ping(site1, site2, 0) |
| 688 |
self.check_ping(site2, site1, 0) |
| 689 |
|
| 690 |
@testtools.skip('bug/1598466') |
| 691 |
def _test_admin_state_up(self, update_method): |
| 692 |
# Create ipsec connection between two sites |
| 693 |
site1 = self.create_site(PUBLIC_NET[4], [self.private_nets[1]]) |
| 694 |
site2 = self.create_site(PUBLIC_NET[5], [self.private_nets[2]]) |
| 695 |
|
| 696 |
self.prepare_ipsec_site_connections(site1, site2) |
| 697 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 698 |
|
| 699 |
self.check_ping(site1, site2) |
| 700 |
self.check_ping(site2, site1) |
| 701 |
|
| 702 |
# Disable resource on one of the sites and check that |
| 703 |
# ping no longer passes. |
| 704 |
update_method(site1, admin_state_up=False) |
| 705 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 706 |
|
| 707 |
self.check_ping(site1, site2, 0, success=False) |
| 708 |
self.check_ping(site2, site1, 0, success=False) |
| 709 |
|
| 710 |
# Validate that ipsec process for the disabled site was terminated. |
| 711 |
self._wait_for_ipsec_startup(site1.router, self.driver, |
| 712 |
self.vpn_agent.conf, |
| 713 |
should_run=False) |
| 714 |
|
| 715 |
# Change admin_state_up of the disabled resource back to True and |
| 716 |
# check that everything works again. |
| 717 |
update_method(site1, admin_state_up=True) |
| 718 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 719 |
|
| 720 |
self.check_ping(site1, site2) |
| 721 |
self.check_ping(site2, site1) |
| 722 |
|
| 723 |
@testtools.skip('bug/1598466') |
| 724 |
def test_ipsec_site_connections_update_admin_state_up(self): |
| 725 |
"""Test updating admin_state_up of ipsec site connections.""" |
| 726 |
|
| 727 |
self._test_admin_state_up(self._update_ipsec_connection) |
| 728 |
|
| 729 |
@testtools.skip('bug/1598466') |
| 730 |
def test_vpnservice_update_admin_state_up(self): |
| 731 |
"""Test updating admin_state_up of a vpn service.""" |
| 732 |
|
| 733 |
self._test_admin_state_up(self._update_vpnservice) |