Debian Codegraph @

neutron_tempest_plugin/fwaas/scenario/test_fwaas_v2.py

raw
1 # Copyright (c) 2016 Juniper Networks
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16
17 from oslo_log import log as logging
18 from tempest.common import utils
19 from tempest import config
20 from tempest.lib.common.utils import test_utils
21 from tempest.lib import decorators
22 from tempest.lib import exceptions as lib_exc
23
24 from neutron_tempest_plugin.fwaas.scenario import fwaas_v2_base as base
25
26
27 CONF = config.CONF
28 LOG = logging.getLogger(__name__)
29
30
31 class TestFWaaS_v2(base.FWaaSScenarioTest_V2):
32
33 """Config Requirement in tempest.conf:
34
35 - project_network_cidr_bits- specifies the subnet range for each network
36 - project_network_cidr
37 - public_network_id
38 """
39
40 @classmethod
41 def setup_credentials(cls):
42 # Create no default network resources; tests create their own topology.
43 cls.set_network_resources()
44 super(TestFWaaS_v2, cls).setup_credentials()
45
46 def setUp(self):
47 LOG.debug("Initializing FWaaSScenarioTest Setup")
48 super(TestFWaaS_v2, self).setUp()
49 required_exts = ['fwaas_v2', 'security-group', 'router']
50 # if self.router_insertion:
51 # required_exts.append('fwaasrouterinsertion')
52 for ext in required_exts:
53 if not utils.is_extension_enabled(ext, 'network'):
54 msg = "%s Extension not enabled." % ext
55 raise self.skipException(msg)
56 LOG.debug("FWaaSScenarioTest Setup done.")
57
58 def _create_server(self, network, security_group=None):
59 keys = self.create_keypair()
60 kwargs = {}
61 if security_group is not None:
62 kwargs['security_groups'] = [{'name': security_group['name']}]
63 server = self.create_server(
64 key_name=keys['name'],
65 networks=[{'uuid': network['id']}],
66 wait_until='ACTIVE',
67 **kwargs)
68 return server, keys
69
70 def _check_connectivity_between_internal_networks(
71 self, floating_ip1, keys1, network2, server2, should_connect=True):
72 internal_ips = (p['fixed_ips'][0]['ip_address'] for p in
73 self.os_admin.ports_client.list_ports(
74 tenant_id=server2['tenant_id'],
75 network_id=network2['id'])['ports']
76 if p['device_owner'].startswith('network'))
77 self._check_server_connectivity(
78 floating_ip1, keys1, internal_ips, should_connect)
79
80 def _check_server_connectivity(self, floating_ip, keys1, address_list,
81 should_connect=True):
82 ip_address = floating_ip['floating_ip_address']
83 private_key = keys1
84 ssh_source = self.get_remote_client(
85 ip_address, private_key=private_key)
86
87 for remote_ip in address_list:
88 if should_connect:
89 msg = ("Timed out waiting for %s to become "
90 "reachable") % remote_ip
91 else:
92 msg = "ip address %s is reachable" % remote_ip
93 try:
94 self.assertTrue(self._check_remote_connectivity
95 (ssh_source, remote_ip, should_connect),
96 msg)
97 except Exception:
98 LOG.exception("Unable to access %s via ssh to "
99 "floating-ip %s",
100 remote_ip, floating_ip)
101 raise
102
103 def _check_remote_connectivity(self, source, dest, should_succeed=True,
104 nic=None):
105 """check ping server via source ssh connection
106
107 :param source: RemoteClient: an ssh connection from which to ping
108 :param dest: and IP to ping against
109 :param should_succeed: boolean should ping succeed or not
110 :param nic: specific network interface to ping from
111 :returns: boolean -- should_succeed == ping
112 :returns: ping is false if ping failed
113 """
114 def ping_remote():
115 try:
116 source.ping_host(dest, nic=nic)
117 except lib_exc.SSHExecCommandFailed:
118 LOG.warning('Failed to ping IP: %s via a ssh connection '
119 'from: %s.', dest, source.ssh_client.host)
120 return not should_succeed
121 return should_succeed
122
123 return test_utils.call_until_true(ping_remote,
124 CONF.validation.ping_timeout,
125 1)
126
127 def _add_router_interface(self, router_id, subnet_id):
128 resp = self.routers_client.add_router_interface(
129 router_id, subnet_id=subnet_id)
130 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
131 self.routers_client.remove_router_interface, router_id,
132 subnet_id=subnet_id)
133 return resp
134
135 def _create_network_subnet(self, prefix="smoke-",
136 port_security_enabled=True):
137 network_prefix = "network-%s" % prefix
138 subnet_prefix = "subnet-%s" % prefix
139 network = self.create_network(
140 namestart=network_prefix,
141 port_security_enabled=port_security_enabled)
142 subnet = self.create_subnet(
143 network=network, namestart=subnet_prefix)
144 return network, subnet
145
146 def _create_test_server(self, network, security_group):
147 pub_network_id = CONF.network.public_network_id
148 server, keys = self._create_server(
149 network, security_group=security_group)
150 private_key = keys['private_key']
151 server_floating_ip = self.create_floating_ip(server, pub_network_id)
152 fixed_ip = list(server['addresses'].values())[0][0]['addr']
153 return server, private_key, fixed_ip, server_floating_ip
154
155 def _create_topology(self):
156 """Topology diagram:
157
158 +--------+ +-------------+
159 |"server"| | "subnet" |
160 | VM-1 +-------------+ "network-1" |
161 +--------+ +----+--------+
162 |
163 | router interface port
164 +----+-----+
165 | "router" |
166 +----+-----+
167 | router interface port
168 |
169 |
170 +--------+ +-------------+
171 |"server"| | "subnet" |
172 | VM-2 +-------------+ "network-2" |
173 +--------+ +----+--------+
174 """
175
176 LOG.debug('Starting Topology Creation')
177 resp = {}
178 # Create Network1 and Subnet1.
179 network1, subnet1 = self._create_network_subnet()
180 resp['network1'] = network1
181 resp['subnet1'] = subnet1
182
183 # Create Network2 and Subnet2.
184 network2, subnet2 = self._create_network_subnet()
185 resp['network2'] = network2
186 resp['subnet2'] = subnet2
187
188 # Create a router and attach Network1, Network2 and External Networks
189 # to it.
190 router = self._create_router(namestart='SCENARIO-TEST-ROUTER')
191 pub_network_id = CONF.network.public_network_id
192 kwargs = {'external_gateway_info': dict(network_id=pub_network_id)}
193 router = self.routers_client.update_router(
194 router['id'], **kwargs)['router']
195 router_id = router['id']
196 resp_add_intf = self._add_router_interface(
197 router_id, subnet_id=subnet1['id'])
198 router_portid_1 = resp_add_intf['port_id']
199 resp_add_intf = self._add_router_interface(
200 router_id, subnet_id=subnet2['id'])
201 router_portid_2 = resp_add_intf['port_id']
202 resp['router'] = router
203 resp['router_portid_1'] = router_portid_1
204 resp['router_portid_2'] = router_portid_2
205
206 # Create a VM on each of the network and assign it a floating IP.
207 security_group = self.create_security_group()
208 server1, private_key1, server_fixed_ip_1, server_floating_ip_1 = (
209 self._create_test_server(network1, security_group))
210 server2, private_key2, server_fixed_ip_2, server_floating_ip_2 = (
211 self._create_test_server(network2, security_group))
212 resp['server1'] = server1
213 resp['private_key1'] = private_key1
214 resp['server_fixed_ip_1'] = server_fixed_ip_1
215 resp['server_floating_ip_1'] = server_floating_ip_1
216 resp['server2'] = server2
217 resp['private_key2'] = private_key2
218 resp['server_fixed_ip_2'] = server_fixed_ip_2
219 resp['server_floating_ip_2'] = server_floating_ip_2
220
221 return resp
222
223 @decorators.idempotent_id('77fdf3ea-82c1-453d-bfec-f7efe335625d')
224 def test_icmp_reachability_scenarios(self):
225 topology = self._create_topology()
226 ssh_login = CONF.validation.image_ssh_user
227
228 self.check_vm_connectivity(
229 ip_address=topology['server_floating_ip_1']['floating_ip_address'],
230 username=ssh_login,
231 private_key=topology['private_key1'])
232 self.check_vm_connectivity(
233 ip_address=topology['server_floating_ip_2']['floating_ip_address'],
234 username=ssh_login,
235 private_key=topology['private_key2'])
236
237 # Scenario 1: Add allow ICMP rules between the two VMs.
238 fw_allow_icmp_rule = self.create_firewall_rule(action="allow",
239 protocol="icmp")
240 fw_allow_ssh_rule = self.create_firewall_rule(action="allow",
241 protocol="tcp",
242 destination_port=22)
243 fw_policy = self.create_firewall_policy(
244 firewall_rules=[fw_allow_icmp_rule['id'], fw_allow_ssh_rule['id']])
245 fw_group = self.create_firewall_group(
246 ports=[
247 topology['router_portid_1'],
248 topology['router_portid_2']],
249 ingress_firewall_policy_id=fw_policy['id'],
250 egress_firewall_policy_id=fw_policy['id'])
251 self._wait_firewall_group_ready(fw_group['id'])
252 LOG.debug('fw_allow_icmp_rule: %s\nfw_allow_ssh_rule: %s\n'
253 'fw_policy: %s\nfw_group: %s\n',
254 fw_allow_icmp_rule, fw_allow_ssh_rule, fw_policy, fw_group)
255
256 # Check the connectivity between VM1 and VM2. It should Pass.
257 self._check_server_connectivity(
258 topology['server_floating_ip_1'],
259 topology['private_key1'],
260 address_list=[topology['server_fixed_ip_2']],
261 should_connect=True)
262
263 # Scenario 2: Now remove the allow_icmp rule add a deny_icmp rule and
264 # check that ICMP gets blocked
265 fw_deny_icmp_rule = self.create_firewall_rule(action="deny",
266 protocol="icmp")
267 self.remove_firewall_rule_from_policy_and_wait(
268 firewall_group_id=fw_group['id'],
269 firewall_rule_id=fw_allow_icmp_rule['id'],
270 firewall_policy_id=fw_policy['id'])
271 self.insert_firewall_rule_in_policy_and_wait(
272 firewall_group_id=fw_group['id'],
273 firewall_rule_id=fw_deny_icmp_rule['id'],
274 firewall_policy_id=fw_policy['id'])
275 self._check_server_connectivity(
276 topology['server_floating_ip_1'],
277 topology['private_key1'],
278 address_list=[topology['server_fixed_ip_2']],
279 should_connect=False)
280
281 # Scenario 3: Create a rule allowing ICMP only from server_fixed_ip_1
282 # to server_fixed_ip_2 and check that traffic from opposite direction
283 # is blocked (for ovs driver where rules are stateful).
284 fw_allow_unidirectional_icmp_rule = self.create_firewall_rule(
285 action="allow", protocol="icmp",
286 source_ip_address=topology['server_fixed_ip_1'],
287 destination_ip_address=topology['server_fixed_ip_2'])
288 self.remove_firewall_rule_from_policy_and_wait(
289 firewall_group_id=fw_group['id'],
290 firewall_rule_id=fw_deny_icmp_rule['id'],
291 firewall_policy_id=fw_policy['id'])
292 self.insert_firewall_rule_in_policy_and_wait(
293 firewall_group_id=fw_group['id'],
294 firewall_rule_id=fw_allow_unidirectional_icmp_rule['id'],
295 firewall_policy_id=fw_policy['id'])
296
297 if CONF.fwaas.driver == 'ovn':
298 # NOTE(slaweq): OVN driver in FWaaS implements only stateless rules
299 # so allowing only unidirectional traffic is not enough as ICMP
300 # replies are still blocked and to make it working additional rule
301 # for the opposite direction is required also:
302 fw_allow_icmp_reply_rule = self.create_firewall_rule(
303 action="allow", protocol="icmp",
304 source_ip_address=topology['server_fixed_ip_2'],
305 destination_ip_address=topology['server_fixed_ip_1'])
306 self.insert_firewall_rule_in_policy_and_wait(
307 firewall_group_id=fw_group['id'],
308 firewall_rule_id=fw_allow_icmp_reply_rule['id'],
309 firewall_policy_id=fw_policy['id'])
310
311 self._check_server_connectivity(
312 topology['server_floating_ip_1'],
313 topology['private_key1'],
314 address_list=[topology['server_fixed_ip_2']],
315 should_connect=True)
316 self._check_server_connectivity(
317 topology['server_floating_ip_2'],
318 topology['private_key2'],
319 address_list=[topology['server_fixed_ip_1']],
320 should_connect=CONF.fwaas.driver == 'ovn')
321
322 # Disassociate ports of this firewall group for cleanup resources
323 self.update_firewall_group_and_wait(fw_group['id'], ports=[])
324
325 def _create_fip_topology(self):
326 """Create topology: network, subnet, router, VM with FIP.
327
328 VM is created without security group (network has
329 port_security_enabled=False).
330
331 +--------+ +-------------+
332 |"server"| | "subnet" |
333 | VM +-------------+ "network" |
334 +--------+ +----+--------+
335 |
336 | router interface port
337 +----+-----+
338 | "router"|
339 +----+-----+
340 |
341 | external gateway
342 |
343 [external network]
344 """
345 # No security group: network has port_security_enabled=False
346 network, subnet = self._create_network_subnet(
347 prefix='fwaas-ssh-fip-',
348 port_security_enabled=False)
349 router = self._create_router(namestart='fwaas-ssh-fip-router')
350 pub_network_id = CONF.network.public_network_id
351 router = self.routers_client.update_router(
352 router['id'],
353 external_gateway_info=dict(network_id=pub_network_id))['router']
354 resp = self._add_router_interface(
355 router['id'], subnet_id=subnet['id'])
356 router_port_id = resp['port_id']
357 server, keys = self._create_server(network)
358 floating_ip = self.create_floating_ip(server, pub_network_id)
359
360 return {
361 'server': server,
362 'private_key': keys['private_key'],
363 'floating_ip': floating_ip,
364 'router_port_id': router_port_id,
365 }
366
367 @decorators.idempotent_id('a8c2e1f4-9b3d-4f5a-8e6c-7d9f2b1a0c3e')
368 def test_ssh_via_fip_with_fwaas_rules(self):
369 """Test SSH access to VM with FIP controlled by FWaaS rules.
370
371 Verifies that:
372 1. FWaaS is enabled (skipped in setUp if not)
373 2. Baseline: VM reachable before firewall (SSH works)
374 3. Firewall group with empty policy denies all traffic (SSH blocked)
375 4. Adding allow SSH rules to ingress and egress policy permits SSH
376 """
377 topology = self._create_fip_topology()
378 fip_address = topology['floating_ip']['floating_ip_address']
379 ssh_login = CONF.validation.image_ssh_user
380 private_key = topology['private_key']
381
382 # Baseline: Ensure VM is reachable before applying firewall
383 self.check_vm_connectivity(
384 ip_address=fip_address,
385 username=ssh_login,
386 private_key=private_key)
387
388 # Phase 1: Attach firewall group with empty policy - SSH blocked
389 fw_policy = self.create_firewall_policy()
390 fw_group = self.create_firewall_group(
391 ports=[topology['router_port_id']],
392 ingress_firewall_policy_id=fw_policy['id'],
393 egress_firewall_policy_id=fw_policy['id'])
394 self.addCleanup(self.update_firewall_group_and_wait, fw_group['id'],
395 ports=[])
396 self._wait_firewall_group_ready(fw_group['id'])
397 LOG.debug('Firewall group with empty policy attached to router port')
398
399 self.check_connectivity(
400 ip_address=fip_address,
401 username=ssh_login,
402 private_key=private_key,
403 should_connect=False,
404 check_icmp=False,
405 check_ssh=True)
406
407 # Phase 2: Add allow SSH rules - ingress dport 22, egress sport 22
408 fw_allow_ssh_rule = self.create_firewall_rule(
409 action="allow", protocol="tcp", destination_port=22)
410 fw_allow_egress_ssh_rule = self.create_firewall_rule(
411 action="allow", protocol="tcp", source_port=22)
412 self.insert_firewall_rule_in_policy_and_wait(
413 firewall_group_id=fw_group['id'],
414 firewall_rule_id=fw_allow_ssh_rule['id'],
415 firewall_policy_id=fw_policy['id'])
416 self.insert_firewall_rule_in_policy_and_wait(
417 firewall_group_id=fw_group['id'],
418 firewall_rule_id=fw_allow_egress_ssh_rule['id'],
419 firewall_policy_id=fw_policy['id'])
420 LOG.debug('Added allow SSH rules to ingress and egress policy')
421
422 self.check_connectivity(
423 ip_address=fip_address,
424 username=ssh_login,
425 private_key=private_key,
426 should_connect=True,
427 check_icmp=False,
428 check_ssh=True)