blob: 18bd764086f11d7228dbf251a2ca13e16d5d3f06 [file] [log] [blame]
Yair Fried4d7efa62013-11-17 17:12:29 +02001# Copyright 2013 Red Hat, 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
Andrea Frittolif9cde7e2014-02-18 09:57:04 +000016from tempest import clients
Fei Long Wangd39431f2015-05-14 11:30:48 +120017from tempest.common.utils import data_utils
Matthew Treinish6c072292014-01-29 19:15:52 +000018from tempest import config
Yair Fried4d7efa62013-11-17 17:12:29 +020019from tempest.scenario import manager
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090020from tempest import test
Yair Fried4d7efa62013-11-17 17:12:29 +020021
Matthew Treinish6c072292014-01-29 19:15:52 +000022CONF = config.CONF
23
Yair Fried4d7efa62013-11-17 17:12:29 +020024
Andrea Frittoli4971fc82014-09-25 10:22:20 +010025class TestSecurityGroupsBasicOps(manager.NetworkScenarioTest):
Yair Fried4d7efa62013-11-17 17:12:29 +020026
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +000027 """The test suite for security groups
28
Yair Fried4d7efa62013-11-17 17:12:29 +020029 This test suite assumes that Nova has been configured to
30 boot VM's with Neutron-managed networking, and attempts to
31 verify cross tenant connectivity as follows
32
33 ssh:
34 in order to overcome "ip namespace", each tenant has an "access point"
35 VM with floating-ip open to incoming ssh connection allowing network
36 commands (ping/ssh) to be executed from within the
37 tenant-network-namespace
38 Tempest host performs key-based authentication to the ssh server via
39 floating IP address
40
41 connectivity test is done by pinging destination server via source server
42 ssh connection.
43 success - ping returns
44 failure - ping_timeout reached
45
46 setup:
Yair Friedbf2e2c42014-01-28 12:06:38 +020047 for primary tenant:
Yair Fried4d7efa62013-11-17 17:12:29 +020048 1. create a network&subnet
49 2. create a router (if public router isn't configured)
50 3. connect tenant network to public network via router
51 4. create an access point:
52 a. a security group open to incoming ssh connection
53 b. a VM with a floating ip
54 5. create a general empty security group (same as "default", but
55 without rules allowing in-tenant traffic)
Yair Fried4d7efa62013-11-17 17:12:29 +020056
57 tests:
58 1. _verify_network_details
59 2. _verify_mac_addr: for each access point verify that
60 (subnet, fix_ip, mac address) are as defined in the port list
61 3. _test_in_tenant_block: test that in-tenant traffic is disabled
62 without rules allowing it
63 4. _test_in_tenant_allow: test that in-tenant traffic is enabled
64 once an appropriate rule has been created
65 5. _test_cross_tenant_block: test that cross-tenant traffic is disabled
66 without a rule allowing it on destination tenant
67 6. _test_cross_tenant_allow:
68 * test that cross-tenant traffic is enabled once an appropriate
69 rule has been created on destination tenant.
70 * test that reverse traffic is still blocked
Fei Long Wang50131ee2015-02-02 16:58:24 +130071 * test than reverse traffic is enabled once an appropriate rule has
Yair Fried4d7efa62013-11-17 17:12:29 +020072 been created on source tenant
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -080073 7._test_port_update_new_security_group:
74 * test that traffic is blocked with default security group
75 * test that traffic is enabled after updating port with new security
76 group having appropriate rule
prdsilva8b733ad2014-12-09 02:54:49 -080077 8. _test_multiple_security_groups: test multiple security groups can be
78 associated with the vm
Yair Fried4d7efa62013-11-17 17:12:29 +020079
80 assumptions:
Yair Friedbf2e2c42014-01-28 12:06:38 +020081 1. alt_tenant/user existed and is different from primary_tenant/user
Yair Fried4d7efa62013-11-17 17:12:29 +020082 2. Public network is defined and reachable from the Tempest host
83 3. Public router can either be:
84 * defined, in which case all tenants networks can connect directly
85 to it, and cross tenant check will be done on the private IP of the
86 destination tenant
87 or
Fei Long Wang50131ee2015-02-02 16:58:24 +130088 * not defined (empty string), in which case each tenant will have
Yair Fried4d7efa62013-11-17 17:12:29 +020089 its own router connected to the public network
90 """
91
Andrea Frittolib21de6c2015-02-06 20:12:38 +000092 credentials = ['primary', 'alt', 'admin']
93
Joe Gordon28788b42015-02-25 12:42:37 -080094 class TenantProperties(object):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +000095 """helper class to save tenant details
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +000096
Yair Fried4d7efa62013-11-17 17:12:29 +020097 id
98 credentials
99 network
100 subnet
101 security groups
102 servers
103 access point
Yair Friedbf2e2c42014-01-28 12:06:38 +0200104 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200105
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000106 def __init__(self, credentials):
Yair Frieddb6c9e92014-08-06 08:53:13 +0300107 self.manager = clients.Manager(credentials)
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000108 # Credentials from manager are filled with both names and IDs
109 self.creds = self.manager.credentials
Yair Fried4d7efa62013-11-17 17:12:29 +0200110 self.network = None
111 self.subnet = None
112 self.router = None
113 self.security_groups = {}
114 self.servers = list()
115
Yair Friedbf2e2c42014-01-28 12:06:38 +0200116 def set_network(self, network, subnet, router):
Yair Fried4d7efa62013-11-17 17:12:29 +0200117 self.network = network
118 self.subnet = subnet
119 self.router = router
120
Yair Fried4d7efa62013-11-17 17:12:29 +0200121 @classmethod
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000122 def skip_checks(cls):
123 super(TestSecurityGroupsBasicOps, cls).skip_checks()
Yair Frieddb6c9e92014-08-06 08:53:13 +0300124 if CONF.baremetal.driver_enabled:
125 msg = ('Not currently supported by baremetal.')
Yair Frieddb6c9e92014-08-06 08:53:13 +0300126 raise cls.skipException(msg)
Itzik Brown06952672015-03-29 12:38:58 +0300127 if CONF.network.port_vnic_type in ['direct', 'macvtap']:
128 msg = ('Not currently supported when using vnic_type'
129 ' direct or macvtap')
130 raise cls.skipException(msg)
Matthew Treinish6c072292014-01-29 19:15:52 +0000131 if not (CONF.network.tenant_networks_reachable or
132 CONF.network.public_network_id):
Yair Fried4d7efa62013-11-17 17:12:29 +0200133 msg = ('Either tenant_networks_reachable must be "true", or '
134 'public_network_id must be defined.')
Yair Fried4d7efa62013-11-17 17:12:29 +0200135 raise cls.skipException(msg)
Bence Romsics41f3f852016-01-11 13:48:23 +0100136 if not test.is_extension_enabled('security-group', 'network'):
137 msg = "security-group extension not enabled."
138 raise cls.skipException(msg)
Yair Fried4d7efa62013-11-17 17:12:29 +0200139
140 @classmethod
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000141 def setup_credentials(cls):
Yair Fried764610a2014-04-07 12:17:05 +0300142 # Create no network resources for these tests.
143 cls.set_network_resources()
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000144 super(TestSecurityGroupsBasicOps, cls).setup_credentials()
Yair Fried4d7efa62013-11-17 17:12:29 +0200145 # TODO(mnewby) Consider looking up entities as needed instead
146 # of storing them as collections on the class.
Yair Fried79b0a912014-10-20 11:15:37 +0300147
Yair Fried79b0a912014-10-20 11:15:37 +0300148 # Credentials from the manager are filled with both IDs and Names
149 cls.alt_creds = cls.alt_manager.credentials
150
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000151 @classmethod
152 def resource_setup(cls):
153 super(TestSecurityGroupsBasicOps, cls).resource_setup()
Yair Fried4d7efa62013-11-17 17:12:29 +0200154 cls.floating_ips = {}
155 cls.tenants = {}
Andrea Frittolib21de6c2015-02-06 20:12:38 +0000156 creds = cls.manager.credentials
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000157 cls.primary_tenant = cls.TenantProperties(creds)
158 cls.alt_tenant = cls.TenantProperties(cls.alt_creds)
Yair Friedbf2e2c42014-01-28 12:06:38 +0200159 for tenant in [cls.primary_tenant, cls.alt_tenant]:
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000160 cls.tenants[tenant.creds.tenant_id] = tenant
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000161
Yair Friedbf2e2c42014-01-28 12:06:38 +0200162 cls.floating_ip_access = not CONF.network.public_router_id
Yair Fried4d7efa62013-11-17 17:12:29 +0200163
Yair Friedbf2e2c42014-01-28 12:06:38 +0200164 def setUp(self):
165 super(TestSecurityGroupsBasicOps, self).setUp()
166 self._deploy_tenant(self.primary_tenant)
167 self._verify_network_details(self.primary_tenant)
168 self._verify_mac_addr(self.primary_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200169
Yair Frieddb6c9e92014-08-06 08:53:13 +0300170 def _create_tenant_keypairs(self, tenant):
171 keypair = self.create_keypair(tenant.manager.keypairs_client)
172 tenant.keypair = keypair
Yair Fried4d7efa62013-11-17 17:12:29 +0200173
174 def _create_tenant_security_groups(self, tenant):
Yair Fried4d7efa62013-11-17 17:12:29 +0200175 access_sg = self._create_empty_security_group(
176 namestart='secgroup_access-',
Yair Frieddb6c9e92014-08-06 08:53:13 +0300177 tenant_id=tenant.creds.tenant_id,
John Warrenf9606e92015-12-10 12:12:42 -0500178 client=tenant.manager.security_groups_client
Yair Fried4d7efa62013-11-17 17:12:29 +0200179 )
Yair Friedbf2e2c42014-01-28 12:06:38 +0200180
Yair Fried4d7efa62013-11-17 17:12:29 +0200181 # don't use default secgroup since it allows in-tenant traffic
182 def_sg = self._create_empty_security_group(
183 namestart='secgroup_general-',
Yair Frieddb6c9e92014-08-06 08:53:13 +0300184 tenant_id=tenant.creds.tenant_id,
John Warrenf9606e92015-12-10 12:12:42 -0500185 client=tenant.manager.security_groups_client
Yair Fried4d7efa62013-11-17 17:12:29 +0200186 )
187 tenant.security_groups.update(access=access_sg, default=def_sg)
188 ssh_rule = dict(
189 protocol='tcp',
190 port_range_min=22,
191 port_range_max=22,
192 direction='ingress',
193 )
John Warren456d9ae2016-01-12 15:36:33 -0500194 sec_group_rules_client = tenant.manager.security_group_rules_client
195 self._create_security_group_rule(
196 secgroup=access_sg,
197 sec_group_rules_client=sec_group_rules_client,
198 **ssh_rule)
Yair Fried4d7efa62013-11-17 17:12:29 +0200199
200 def _verify_network_details(self, tenant):
201 # Checks that we see the newly created network/subnet/router via
202 # checking the result of list_[networks,routers,subnets]
203 # Check that (router, subnet) couple exist in port_list
204 seen_nets = self._list_networks()
205 seen_names = [n['name'] for n in seen_nets]
206 seen_ids = [n['id'] for n in seen_nets]
207
208 self.assertIn(tenant.network.name, seen_names)
209 self.assertIn(tenant.network.id, seen_ids)
210
211 seen_subnets = [(n['id'], n['cidr'], n['network_id'])
212 for n in self._list_subnets()]
213 mysubnet = (tenant.subnet.id, tenant.subnet.cidr, tenant.network.id)
214 self.assertIn(mysubnet, seen_subnets)
215
216 seen_routers = self._list_routers()
217 seen_router_ids = [n['id'] for n in seen_routers]
218 seen_router_names = [n['name'] for n in seen_routers]
219
220 self.assertIn(tenant.router.name, seen_router_names)
221 self.assertIn(tenant.router.id, seen_router_ids)
222
223 myport = (tenant.router.id, tenant.subnet.id)
224 router_ports = [(i['device_id'], i['fixed_ips'][0]['subnet_id']) for i
Yair Frieddb6c9e92014-08-06 08:53:13 +0300225 in self._list_ports()
armando-migliacciobcfbbeb2014-08-11 18:33:47 -0700226 if self._is_router_port(i)]
Yair Fried4d7efa62013-11-17 17:12:29 +0200227
228 self.assertIn(myport, router_ports)
229
armando-migliacciobcfbbeb2014-08-11 18:33:47 -0700230 def _is_router_port(self, port):
231 """Return True if port is a router interface."""
232 # NOTE(armando-migliaccio): match device owner for both centralized
233 # and distributed routers; 'device_owner' is "" by default.
234 return port['device_owner'].startswith('network:router_interface')
235
Yair Fried4d7efa62013-11-17 17:12:29 +0200236 def _create_server(self, name, tenant, security_groups=None):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000237 """creates a server and assigns to security group"""
Yair Fried4d7efa62013-11-17 17:12:29 +0200238 if security_groups is None:
Yair Frieddb6c9e92014-08-06 08:53:13 +0300239 security_groups = [tenant.security_groups['default']]
Ken'ichi Ohmichi1b3461e2014-12-02 03:41:07 +0000240 security_groups_names = [{'name': s['name']} for s in security_groups]
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +0000241 server = self.create_server(
242 name=name,
lanoux5fc14522015-09-21 08:17:35 +0000243 networks=[{'uuid': tenant.network.id}],
244 key_name=tenant.keypair['name'],
245 security_groups=security_groups_names,
246 wait_until='ACTIVE',
247 clients=tenant.manager)
Claudiu Belufaa98912014-09-01 16:50:28 +0300248 self.assertEqual(
249 sorted([s['name'] for s in security_groups]),
250 sorted([s['name'] for s in server['security_groups']]))
251 return server
Yair Fried4d7efa62013-11-17 17:12:29 +0200252
253 def _create_tenant_servers(self, tenant, num=1):
254 for i in range(num):
Ken'ichi Ohmichi6ded8df2015-03-23 02:00:19 +0000255 name = 'server-{tenant}-gen-{num}'.format(
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000256 tenant=tenant.creds.tenant_name,
Yair Fried4d7efa62013-11-17 17:12:29 +0200257 num=i
258 )
259 name = data_utils.rand_name(name)
260 server = self._create_server(name, tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200261 tenant.servers.append(server)
262
263 def _set_access_point(self, tenant):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000264 # creates a server in a secgroup with rule allowing external ssh
265 # in order to access tenant internal network
266 # workaround ip namespace
Yair Frieddb6c9e92014-08-06 08:53:13 +0300267 secgroups = tenant.security_groups.values()
Ken'ichi Ohmichi6ded8df2015-03-23 02:00:19 +0000268 name = 'server-{tenant}-access_point'.format(
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000269 tenant=tenant.creds.tenant_name)
Yair Fried4d7efa62013-11-17 17:12:29 +0200270 name = data_utils.rand_name(name)
271 server = self._create_server(name, tenant,
272 security_groups=secgroups)
Yair Fried4d7efa62013-11-17 17:12:29 +0200273 tenant.access_point = server
Yair Frieddb6c9e92014-08-06 08:53:13 +0300274 self._assign_floating_ips(tenant, server)
Yair Fried4d7efa62013-11-17 17:12:29 +0200275
Yair Frieddb6c9e92014-08-06 08:53:13 +0300276 def _assign_floating_ips(self, tenant, server):
Matthew Treinish6c072292014-01-29 19:15:52 +0000277 public_network_id = CONF.network.public_network_id
Yair Friedae0e73d2014-11-24 11:56:26 +0200278 floating_ip = self.create_floating_ip(
Yair Frieddb6c9e92014-08-06 08:53:13 +0300279 server, public_network_id,
John Warrenfbf2a892015-11-17 12:36:14 -0500280 client=tenant.manager.floating_ips_client)
Yair Frieddb6c9e92014-08-06 08:53:13 +0300281 self.floating_ips.setdefault(server['id'], floating_ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200282
283 def _create_tenant_network(self, tenant):
Yair Frieddb6c9e92014-08-06 08:53:13 +0300284 network, subnet, router = self.create_networks(
John Warren94d8faf2015-09-15 12:22:24 -0400285 client=tenant.manager.network_client,
John Warren3961acd2015-10-02 14:38:53 -0400286 networks_client=tenant.manager.networks_client,
287 subnets_client=tenant.manager.subnets_client)
Yair Friedbf2e2c42014-01-28 12:06:38 +0200288 tenant.set_network(network, subnet, router)
Yair Fried4d7efa62013-11-17 17:12:29 +0200289
Yair Fried4d7efa62013-11-17 17:12:29 +0200290 def _deploy_tenant(self, tenant_or_id):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000291 """creates:
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000292
Yair Fried4d7efa62013-11-17 17:12:29 +0200293 network
294 subnet
295 router (if public not defined)
296 access security group
297 access-point server
Yair Fried4d7efa62013-11-17 17:12:29 +0200298 """
299 if not isinstance(tenant_or_id, self.TenantProperties):
300 tenant = self.tenants[tenant_or_id]
Yair Fried4d7efa62013-11-17 17:12:29 +0200301 else:
302 tenant = tenant_or_id
Yair Frieddb6c9e92014-08-06 08:53:13 +0300303 self._create_tenant_keypairs(tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200304 self._create_tenant_network(tenant)
305 self._create_tenant_security_groups(tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200306 self._set_access_point(tenant)
307
308 def _get_server_ip(self, server, floating=False):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000309 """returns the ip (floating/internal) of a server"""
Yair Fried4d7efa62013-11-17 17:12:29 +0200310 if floating:
Yair Frieddb6c9e92014-08-06 08:53:13 +0300311 server_ip = self.floating_ips[server['id']].floating_ip_address
Yair Fried4d7efa62013-11-17 17:12:29 +0200312 else:
armando-migliacciod03f2642014-02-21 19:55:50 -0800313 server_ip = None
Yair Frieddb6c9e92014-08-06 08:53:13 +0300314 network_name = self.tenants[server['tenant_id']].network.name
315 if network_name in server['addresses']:
316 server_ip = server['addresses'][network_name][0]['addr']
armando-migliacciod03f2642014-02-21 19:55:50 -0800317 return server_ip
Yair Fried4d7efa62013-11-17 17:12:29 +0200318
319 def _connect_to_access_point(self, tenant):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000320 """create ssh connection to tenant access point"""
Yair Fried4d7efa62013-11-17 17:12:29 +0200321 access_point_ssh = \
Yair Frieddb6c9e92014-08-06 08:53:13 +0300322 self.floating_ips[tenant.access_point['id']].floating_ip_address
323 private_key = tenant.keypair['private_key']
Jordan Pittierbbb17122016-01-26 17:10:55 +0100324 access_point_ssh = self.get_remote_client(
325 access_point_ssh, private_key=private_key)
Yair Fried4d7efa62013-11-17 17:12:29 +0200326 return access_point_ssh
327
Yair Fried4d7efa62013-11-17 17:12:29 +0200328 def _check_connectivity(self, access_point, ip, should_succeed=True):
329 if should_succeed:
330 msg = "Timed out waiting for %s to become reachable" % ip
331 else:
Yair Fried4d7efa62013-11-17 17:12:29 +0200332 msg = "%s is reachable" % ip
Matthew Treinish53483132014-12-09 18:50:06 -0500333 self.assertTrue(self._check_remote_connectivity(access_point, ip,
334 should_succeed), msg)
Yair Fried4d7efa62013-11-17 17:12:29 +0200335
336 def _test_in_tenant_block(self, tenant):
337 access_point_ssh = self._connect_to_access_point(tenant)
338 for server in tenant.servers:
339 self._check_connectivity(access_point=access_point_ssh,
340 ip=self._get_server_ip(server),
341 should_succeed=False)
342
343 def _test_in_tenant_allow(self, tenant):
344 ruleset = dict(
345 protocol='icmp',
346 remote_group_id=tenant.security_groups['default'].id,
347 direction='ingress'
348 )
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000349 self._create_security_group_rule(
Yair Fried4d7efa62013-11-17 17:12:29 +0200350 secgroup=tenant.security_groups['default'],
351 **ruleset
352 )
353 access_point_ssh = self._connect_to_access_point(tenant)
354 for server in tenant.servers:
355 self._check_connectivity(access_point=access_point_ssh,
356 ip=self._get_server_ip(server))
Yair Fried4d7efa62013-11-17 17:12:29 +0200357
358 def _test_cross_tenant_block(self, source_tenant, dest_tenant):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000359 # if public router isn't defined, then dest_tenant access is via
360 # floating-ip
Yair Fried4d7efa62013-11-17 17:12:29 +0200361 access_point_ssh = self._connect_to_access_point(source_tenant)
362 ip = self._get_server_ip(dest_tenant.access_point,
363 floating=self.floating_ip_access)
364 self._check_connectivity(access_point=access_point_ssh, ip=ip,
365 should_succeed=False)
366
367 def _test_cross_tenant_allow(self, source_tenant, dest_tenant):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000368 """check for each direction:
369
Yair Fried4d7efa62013-11-17 17:12:29 +0200370 creating rule for tenant incoming traffic enables only 1way traffic
Yair Friedbf2e2c42014-01-28 12:06:38 +0200371 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200372 ruleset = dict(
373 protocol='icmp',
374 direction='ingress'
375 )
John Warren456d9ae2016-01-12 15:36:33 -0500376 sec_group_rules_client = (
377 dest_tenant.manager.security_group_rules_client)
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000378 self._create_security_group_rule(
Yair Fried4d7efa62013-11-17 17:12:29 +0200379 secgroup=dest_tenant.security_groups['default'],
John Warren456d9ae2016-01-12 15:36:33 -0500380 sec_group_rules_client=sec_group_rules_client,
Yair Fried4d7efa62013-11-17 17:12:29 +0200381 **ruleset
382 )
Yair Friedbf2e2c42014-01-28 12:06:38 +0200383 access_point_ssh = self._connect_to_access_point(source_tenant)
384 ip = self._get_server_ip(dest_tenant.access_point,
385 floating=self.floating_ip_access)
386 self._check_connectivity(access_point_ssh, ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200387
Yair Friedbf2e2c42014-01-28 12:06:38 +0200388 # test that reverse traffic is still blocked
389 self._test_cross_tenant_block(dest_tenant, source_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200390
Yair Friedbf2e2c42014-01-28 12:06:38 +0200391 # allow reverse traffic and check
John Warren456d9ae2016-01-12 15:36:33 -0500392 sec_group_rules_client = (
393 source_tenant.manager.security_group_rules_client)
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000394 self._create_security_group_rule(
Yair Friedbf2e2c42014-01-28 12:06:38 +0200395 secgroup=source_tenant.security_groups['default'],
John Warren456d9ae2016-01-12 15:36:33 -0500396 sec_group_rules_client=sec_group_rules_client,
Yair Friedbf2e2c42014-01-28 12:06:38 +0200397 **ruleset
398 )
Yair Fried4d7efa62013-11-17 17:12:29 +0200399
Yair Friedbf2e2c42014-01-28 12:06:38 +0200400 access_point_ssh_2 = self._connect_to_access_point(dest_tenant)
401 ip = self._get_server_ip(source_tenant.access_point,
402 floating=self.floating_ip_access)
403 self._check_connectivity(access_point_ssh_2, ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200404
405 def _verify_mac_addr(self, tenant):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000406 """Verify that VM has the same ip, mac as listed in port"""
407
Yair Fried4d7efa62013-11-17 17:12:29 +0200408 access_point_ssh = self._connect_to_access_point(tenant)
409 mac_addr = access_point_ssh.get_mac_address()
410 mac_addr = mac_addr.strip().lower()
Henry Gessau78ab4b02014-03-31 15:10:13 -0400411 # Get the fixed_ips and mac_address fields of all ports. Select
412 # only those two columns to reduce the size of the response.
Yair Frieddb6c9e92014-08-06 08:53:13 +0300413 port_list = self._list_ports(fields=['fixed_ips', 'mac_address'])
Yair Fried4d7efa62013-11-17 17:12:29 +0200414 port_detail_list = [
415 (port['fixed_ips'][0]['subnet_id'],
416 port['fixed_ips'][0]['ip_address'],
Henry Gessau78ab4b02014-03-31 15:10:13 -0400417 port['mac_address'].lower())
418 for port in port_list if port['fixed_ips']
Yair Fried4d7efa62013-11-17 17:12:29 +0200419 ]
420 server_ip = self._get_server_ip(tenant.access_point)
421 subnet_id = tenant.subnet.id
422 self.assertIn((subnet_id, server_ip, mac_addr), port_detail_list)
423
Chris Hoge7579c1a2015-02-26 14:12:15 -0800424 @test.idempotent_id('e79f879e-debb-440c-a7e4-efeda05b6848')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900425 @test.services('compute', 'network')
Yair Fried4d7efa62013-11-17 17:12:29 +0200426 def test_cross_tenant_traffic(self):
Andrea Frittoli (andreaf)1f342412015-05-12 16:37:19 +0100427 if not self.credentials_provider.is_multi_tenant():
Yair Fried79b0a912014-10-20 11:15:37 +0300428 raise self.skipException("No secondary tenant defined")
Nachi Ueno26b4c972014-01-17 06:15:13 -0800429 try:
Yair Friedbf2e2c42014-01-28 12:06:38 +0200430 # deploy new tenant
431 self._deploy_tenant(self.alt_tenant)
432 self._verify_network_details(self.alt_tenant)
433 self._verify_mac_addr(self.alt_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200434
Nachi Ueno26b4c972014-01-17 06:15:13 -0800435 # cross tenant check
Yair Friedbf2e2c42014-01-28 12:06:38 +0200436 source_tenant = self.primary_tenant
Nachi Ueno26b4c972014-01-17 06:15:13 -0800437 dest_tenant = self.alt_tenant
438 self._test_cross_tenant_block(source_tenant, dest_tenant)
439 self._test_cross_tenant_allow(source_tenant, dest_tenant)
440 except Exception:
Yair Friedbf2e2c42014-01-28 12:06:38 +0200441 for tenant in self.tenants.values():
442 self._log_console_output(servers=tenant.servers)
443 raise
444
Chris Hoge7579c1a2015-02-26 14:12:15 -0800445 @test.idempotent_id('63163892-bbf6-4249-aa12-d5ea1f8f421b')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900446 @test.services('compute', 'network')
Yair Friedbf2e2c42014-01-28 12:06:38 +0200447 def test_in_tenant_traffic(self):
448 try:
449 self._create_tenant_servers(self.primary_tenant, num=1)
450
451 # in-tenant check
452 self._test_in_tenant_block(self.primary_tenant)
453 self._test_in_tenant_allow(self.primary_tenant)
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800454 except Exception:
455 for tenant in self.tenants.values():
456 self._log_console_output(servers=tenant.servers)
457 raise
Yair Friedbf2e2c42014-01-28 12:06:38 +0200458
Chris Hoge7579c1a2015-02-26 14:12:15 -0800459 @test.idempotent_id('f4d556d7-1526-42ad-bafb-6bebf48568f6')
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800460 @test.services('compute', 'network')
461 def test_port_update_new_security_group(self):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000462 """Verifies the traffic after updating the vm port
463
464 With new security group having appropriate rule.
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800465 """
466 new_tenant = self.primary_tenant
467
468 # Create empty security group and add icmp rule in it
469 new_sg = self._create_empty_security_group(
470 namestart='secgroup_new-',
471 tenant_id=new_tenant.creds.tenant_id,
John Warrenf9606e92015-12-10 12:12:42 -0500472 client=new_tenant.manager.security_groups_client)
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800473 icmp_rule = dict(
474 protocol='icmp',
475 direction='ingress',
476 )
John Warren456d9ae2016-01-12 15:36:33 -0500477 sec_group_rules_client = new_tenant.manager.security_group_rules_client
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800478 self._create_security_group_rule(
479 secgroup=new_sg,
John Warren456d9ae2016-01-12 15:36:33 -0500480 sec_group_rules_client=sec_group_rules_client,
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800481 **icmp_rule)
482 new_tenant.security_groups.update(new_sg=new_sg)
483
484 # Create server with default security group
Ken'ichi Ohmichi6ded8df2015-03-23 02:00:19 +0000485 name = 'server-{tenant}-gen-1'.format(
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800486 tenant=new_tenant.creds.tenant_name
487 )
488 name = data_utils.rand_name(name)
489 server = self._create_server(name, new_tenant)
490
491 # Check connectivity failure with default security group
492 try:
493 access_point_ssh = self._connect_to_access_point(new_tenant)
494 self._check_connectivity(access_point=access_point_ssh,
495 ip=self._get_server_ip(server),
496 should_succeed=False)
497 server_id = server['id']
498 port_id = self._list_ports(device_id=server_id)[0]['id']
499
500 # update port with new security group and check connectivity
John Warren49c0fe52015-10-22 12:35:54 -0400501 self.ports_client.update_port(port_id, security_groups=[
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800502 new_tenant.security_groups['new_sg'].id])
503 self._check_connectivity(
504 access_point=access_point_ssh,
505 ip=self._get_server_ip(server))
Yair Friedbf2e2c42014-01-28 12:06:38 +0200506 except Exception:
507 for tenant in self.tenants.values():
508 self._log_console_output(servers=tenant.servers)
Nachi Ueno26b4c972014-01-17 06:15:13 -0800509 raise
prdsilva8b733ad2014-12-09 02:54:49 -0800510
Chris Hoge7579c1a2015-02-26 14:12:15 -0800511 @test.idempotent_id('d2f77418-fcc4-439d-b935-72eca704e293')
prdsilva8b733ad2014-12-09 02:54:49 -0800512 @test.services('compute', 'network')
513 def test_multiple_security_groups(self):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000514 """Verify multiple security groups and checks that rules
515
prdsilva8b733ad2014-12-09 02:54:49 -0800516 provided in the both the groups is applied onto VM
517 """
518 tenant = self.primary_tenant
519 ip = self._get_server_ip(tenant.access_point,
520 floating=self.floating_ip_access)
lanoux283273b2015-12-04 03:01:54 -0800521 ssh_login = CONF.validation.image_ssh_user
prdsilva8b733ad2014-12-09 02:54:49 -0800522 private_key = tenant.keypair['private_key']
523 self.check_vm_connectivity(ip,
524 should_connect=False)
525 ruleset = dict(
526 protocol='icmp',
527 direction='ingress'
528 )
529 self._create_security_group_rule(
530 secgroup=tenant.security_groups['default'],
531 **ruleset
532 )
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000533 # NOTE: Vm now has 2 security groups one with ssh rule(
534 # already added in setUp() method),and other with icmp rule
535 # (added in the above step).The check_vm_connectivity tests
536 # -that vm ping test is successful
537 # -ssh to vm is successful
prdsilva8b733ad2014-12-09 02:54:49 -0800538 self.check_vm_connectivity(ip,
539 username=ssh_login,
540 private_key=private_key,
541 should_connect=True)
YAMAMOTO Takashi51e04082015-09-08 18:44:23 +0900542
543 @test.requires_ext(service='network', extension='port-security')
544 @test.idempotent_id('7c811dcc-263b-49a3-92d2-1b4d8405f50c')
545 @test.services('compute', 'network')
546 def test_port_security_disable_security_group(self):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000547 """Verify the default security group rules is disabled."""
YAMAMOTO Takashi51e04082015-09-08 18:44:23 +0900548 new_tenant = self.primary_tenant
549
550 # Create server
551 name = 'server-{tenant}-gen-1'.format(
552 tenant=new_tenant.creds.tenant_name
553 )
554 name = data_utils.rand_name(name)
555 server = self._create_server(name, new_tenant)
556
557 access_point_ssh = self._connect_to_access_point(new_tenant)
558 server_id = server['id']
559 port_id = self._list_ports(device_id=server_id)[0]['id']
560
561 # Flip the port's port security and check connectivity
562 try:
John Warren49c0fe52015-10-22 12:35:54 -0400563 self.ports_client.update_port(port_id,
564 port_security_enabled=True,
565 security_groups=[])
YAMAMOTO Takashi51e04082015-09-08 18:44:23 +0900566 self._check_connectivity(access_point=access_point_ssh,
567 ip=self._get_server_ip(server),
568 should_succeed=False)
569
John Warren49c0fe52015-10-22 12:35:54 -0400570 self.ports_client.update_port(port_id,
571 port_security_enabled=False,
572 security_groups=[])
YAMAMOTO Takashi51e04082015-09-08 18:44:23 +0900573 self._check_connectivity(
574 access_point=access_point_ssh,
575 ip=self._get_server_ip(server))
576 except Exception:
577 for tenant in self.tenants.values():
578 self._log_console_output(servers=tenant.servers)
579 raise