blob: 32f5d9fa1a84b3b0e36c70d481151769cdff9b34 [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.
Yair Friedca5cfb52016-01-04 15:41:55 +020015from oslo_log import log
Markus Zoeller156b5da2016-07-11 18:10:31 +020016import testtools
Yair Fried4d7efa62013-11-17 17:12:29 +020017
Andrea Frittolif9cde7e2014-02-18 09:57:04 +000018from tempest import clients
Fei Long Wangd39431f2015-05-14 11:30:48 +120019from tempest.common.utils import data_utils
Attila Fazekasd7e08a62016-10-07 13:05:05 +020020from tempest.common.utils import net_info
Matthew Treinish6c072292014-01-29 19:15:52 +000021from tempest import config
Yair Fried4d7efa62013-11-17 17:12:29 +020022from tempest.scenario import manager
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090023from tempest import test
Yair Fried4d7efa62013-11-17 17:12:29 +020024
Matthew Treinish6c072292014-01-29 19:15:52 +000025CONF = config.CONF
Yair Friedca5cfb52016-01-04 15:41:55 +020026LOG = log.getLogger(__name__)
Matthew Treinish6c072292014-01-29 19:15:52 +000027
Yair Fried4d7efa62013-11-17 17:12:29 +020028
Andrea Frittoli4971fc82014-09-25 10:22:20 +010029class TestSecurityGroupsBasicOps(manager.NetworkScenarioTest):
Yair Fried4d7efa62013-11-17 17:12:29 +020030
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +000031 """The test suite for security groups
32
Yair Fried4d7efa62013-11-17 17:12:29 +020033 This test suite assumes that Nova has been configured to
34 boot VM's with Neutron-managed networking, and attempts to
35 verify cross tenant connectivity as follows
36
37 ssh:
38 in order to overcome "ip namespace", each tenant has an "access point"
39 VM with floating-ip open to incoming ssh connection allowing network
40 commands (ping/ssh) to be executed from within the
41 tenant-network-namespace
42 Tempest host performs key-based authentication to the ssh server via
43 floating IP address
44
45 connectivity test is done by pinging destination server via source server
46 ssh connection.
47 success - ping returns
48 failure - ping_timeout reached
49
Eran Kurisb5f69782016-04-10 11:48:28 +030050 multi-node:
51 Multi-Node mode is enabled when CONF.compute.min_compute_nodes > 1.
52 Tests connectivity between servers on different compute nodes.
53 When enabled, test will boot each new server to different
54 compute nodes.
55
Yair Fried4d7efa62013-11-17 17:12:29 +020056 setup:
Yair Friedbf2e2c42014-01-28 12:06:38 +020057 for primary tenant:
Yair Fried4d7efa62013-11-17 17:12:29 +020058 1. create a network&subnet
59 2. create a router (if public router isn't configured)
60 3. connect tenant network to public network via router
61 4. create an access point:
62 a. a security group open to incoming ssh connection
63 b. a VM with a floating ip
64 5. create a general empty security group (same as "default", but
65 without rules allowing in-tenant traffic)
Yair Fried4d7efa62013-11-17 17:12:29 +020066
67 tests:
68 1. _verify_network_details
69 2. _verify_mac_addr: for each access point verify that
70 (subnet, fix_ip, mac address) are as defined in the port list
71 3. _test_in_tenant_block: test that in-tenant traffic is disabled
72 without rules allowing it
73 4. _test_in_tenant_allow: test that in-tenant traffic is enabled
74 once an appropriate rule has been created
75 5. _test_cross_tenant_block: test that cross-tenant traffic is disabled
76 without a rule allowing it on destination tenant
77 6. _test_cross_tenant_allow:
78 * test that cross-tenant traffic is enabled once an appropriate
79 rule has been created on destination tenant.
80 * test that reverse traffic is still blocked
Fei Long Wang50131ee2015-02-02 16:58:24 +130081 * test than reverse traffic is enabled once an appropriate rule has
Yair Fried4d7efa62013-11-17 17:12:29 +020082 been created on source tenant
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -080083 7._test_port_update_new_security_group:
84 * test that traffic is blocked with default security group
85 * test that traffic is enabled after updating port with new security
86 group having appropriate rule
prdsilva8b733ad2014-12-09 02:54:49 -080087 8. _test_multiple_security_groups: test multiple security groups can be
88 associated with the vm
Yair Fried4d7efa62013-11-17 17:12:29 +020089
90 assumptions:
Yair Friedbf2e2c42014-01-28 12:06:38 +020091 1. alt_tenant/user existed and is different from primary_tenant/user
Yair Fried4d7efa62013-11-17 17:12:29 +020092 2. Public network is defined and reachable from the Tempest host
93 3. Public router can either be:
94 * defined, in which case all tenants networks can connect directly
95 to it, and cross tenant check will be done on the private IP of the
96 destination tenant
97 or
Fei Long Wang50131ee2015-02-02 16:58:24 +130098 * not defined (empty string), in which case each tenant will have
Yair Fried4d7efa62013-11-17 17:12:29 +020099 its own router connected to the public network
100 """
101
Andrea Frittolib21de6c2015-02-06 20:12:38 +0000102 credentials = ['primary', 'alt', 'admin']
103
Joe Gordon28788b42015-02-25 12:42:37 -0800104 class TenantProperties(object):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000105 """helper class to save tenant details
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000106
Yair Fried4d7efa62013-11-17 17:12:29 +0200107 id
108 credentials
109 network
110 subnet
111 security groups
112 servers
113 access point
Yair Friedbf2e2c42014-01-28 12:06:38 +0200114 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200115
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000116 def __init__(self, credentials):
Yair Frieddb6c9e92014-08-06 08:53:13 +0300117 self.manager = clients.Manager(credentials)
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000118 # Credentials from manager are filled with both names and IDs
119 self.creds = self.manager.credentials
Yair Fried4d7efa62013-11-17 17:12:29 +0200120 self.network = None
121 self.subnet = None
122 self.router = None
123 self.security_groups = {}
124 self.servers = list()
125
Yair Friedbf2e2c42014-01-28 12:06:38 +0200126 def set_network(self, network, subnet, router):
Yair Fried4d7efa62013-11-17 17:12:29 +0200127 self.network = network
128 self.subnet = subnet
129 self.router = router
130
Yair Fried4d7efa62013-11-17 17:12:29 +0200131 @classmethod
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000132 def skip_checks(cls):
133 super(TestSecurityGroupsBasicOps, cls).skip_checks()
Yair Frieddb6c9e92014-08-06 08:53:13 +0300134 if CONF.baremetal.driver_enabled:
135 msg = ('Not currently supported by baremetal.')
Yair Frieddb6c9e92014-08-06 08:53:13 +0300136 raise cls.skipException(msg)
Itzik Brown06952672015-03-29 12:38:58 +0300137 if CONF.network.port_vnic_type in ['direct', 'macvtap']:
138 msg = ('Not currently supported when using vnic_type'
139 ' direct or macvtap')
140 raise cls.skipException(msg)
Sean Dagueed6e5862016-04-04 10:49:13 -0400141 if not (CONF.network.project_networks_reachable or
Matthew Treinish6c072292014-01-29 19:15:52 +0000142 CONF.network.public_network_id):
Sean Dagueed6e5862016-04-04 10:49:13 -0400143 msg = ('Either project_networks_reachable must be "true", or '
Yair Fried4d7efa62013-11-17 17:12:29 +0200144 'public_network_id must be defined.')
Yair Fried4d7efa62013-11-17 17:12:29 +0200145 raise cls.skipException(msg)
Bence Romsics41f3f852016-01-11 13:48:23 +0100146 if not test.is_extension_enabled('security-group', 'network'):
147 msg = "security-group extension not enabled."
148 raise cls.skipException(msg)
Yair Fried4d7efa62013-11-17 17:12:29 +0200149
150 @classmethod
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000151 def setup_credentials(cls):
Yair Fried764610a2014-04-07 12:17:05 +0300152 # Create no network resources for these tests.
153 cls.set_network_resources()
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000154 super(TestSecurityGroupsBasicOps, cls).setup_credentials()
Yair Fried4d7efa62013-11-17 17:12:29 +0200155 # TODO(mnewby) Consider looking up entities as needed instead
156 # of storing them as collections on the class.
Yair Fried79b0a912014-10-20 11:15:37 +0300157
Yair Fried79b0a912014-10-20 11:15:37 +0300158 # Credentials from the manager are filled with both IDs and Names
159 cls.alt_creds = cls.alt_manager.credentials
160
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000161 @classmethod
162 def resource_setup(cls):
163 super(TestSecurityGroupsBasicOps, cls).resource_setup()
Yair Friedca5cfb52016-01-04 15:41:55 +0200164
165 cls.multi_node = CONF.compute.min_compute_nodes > 1 and \
Yair Fried95914122016-03-03 09:14:40 +0200166 test.is_scheduler_filter_enabled("DifferentHostFilter")
Yair Friedca5cfb52016-01-04 15:41:55 +0200167 if cls.multi_node:
168 LOG.info("Working in Multi Node mode")
169 else:
170 LOG.info("Working in Single Node mode")
171
Yair Fried4d7efa62013-11-17 17:12:29 +0200172 cls.floating_ips = {}
173 cls.tenants = {}
Andrea Frittolib21de6c2015-02-06 20:12:38 +0000174 creds = cls.manager.credentials
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000175 cls.primary_tenant = cls.TenantProperties(creds)
176 cls.alt_tenant = cls.TenantProperties(cls.alt_creds)
Yair Friedbf2e2c42014-01-28 12:06:38 +0200177 for tenant in [cls.primary_tenant, cls.alt_tenant]:
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000178 cls.tenants[tenant.creds.tenant_id] = tenant
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000179
Yair Friedbf2e2c42014-01-28 12:06:38 +0200180 cls.floating_ip_access = not CONF.network.public_router_id
Yair Fried4d7efa62013-11-17 17:12:29 +0200181
Yair Friedbf2e2c42014-01-28 12:06:38 +0200182 def setUp(self):
Yair Friedca5cfb52016-01-04 15:41:55 +0200183 """Set up a single tenant with an accessible server.
184
185 If multi-host is enabled, save created server uuids.
186 """
187 self.servers = []
188
Yair Friedbf2e2c42014-01-28 12:06:38 +0200189 super(TestSecurityGroupsBasicOps, self).setUp()
190 self._deploy_tenant(self.primary_tenant)
191 self._verify_network_details(self.primary_tenant)
192 self._verify_mac_addr(self.primary_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200193
Yair Frieddb6c9e92014-08-06 08:53:13 +0300194 def _create_tenant_keypairs(self, tenant):
195 keypair = self.create_keypair(tenant.manager.keypairs_client)
196 tenant.keypair = keypair
Yair Fried4d7efa62013-11-17 17:12:29 +0200197
198 def _create_tenant_security_groups(self, tenant):
Yair Fried4d7efa62013-11-17 17:12:29 +0200199 access_sg = self._create_empty_security_group(
200 namestart='secgroup_access-',
Yair Frieddb6c9e92014-08-06 08:53:13 +0300201 tenant_id=tenant.creds.tenant_id,
John Warrenf9606e92015-12-10 12:12:42 -0500202 client=tenant.manager.security_groups_client
Yair Fried4d7efa62013-11-17 17:12:29 +0200203 )
Yair Friedbf2e2c42014-01-28 12:06:38 +0200204
Sean Dagueed6e5862016-04-04 10:49:13 -0400205 # don't use default secgroup since it allows in-project traffic
Yair Fried4d7efa62013-11-17 17:12:29 +0200206 def_sg = self._create_empty_security_group(
207 namestart='secgroup_general-',
Yair Frieddb6c9e92014-08-06 08:53:13 +0300208 tenant_id=tenant.creds.tenant_id,
John Warrenf9606e92015-12-10 12:12:42 -0500209 client=tenant.manager.security_groups_client
Yair Fried4d7efa62013-11-17 17:12:29 +0200210 )
211 tenant.security_groups.update(access=access_sg, default=def_sg)
212 ssh_rule = dict(
213 protocol='tcp',
214 port_range_min=22,
215 port_range_max=22,
216 direction='ingress',
217 )
John Warren456d9ae2016-01-12 15:36:33 -0500218 sec_group_rules_client = tenant.manager.security_group_rules_client
219 self._create_security_group_rule(
220 secgroup=access_sg,
221 sec_group_rules_client=sec_group_rules_client,
222 **ssh_rule)
Yair Fried4d7efa62013-11-17 17:12:29 +0200223
224 def _verify_network_details(self, tenant):
225 # Checks that we see the newly created network/subnet/router via
226 # checking the result of list_[networks,routers,subnets]
227 # Check that (router, subnet) couple exist in port_list
228 seen_nets = self._list_networks()
229 seen_names = [n['name'] for n in seen_nets]
230 seen_ids = [n['id'] for n in seen_nets]
231
Steve Heyman33735f22016-05-24 09:28:08 -0500232 self.assertIn(tenant.network['name'], seen_names)
233 self.assertIn(tenant.network['id'], seen_ids)
Yair Fried4d7efa62013-11-17 17:12:29 +0200234
235 seen_subnets = [(n['id'], n['cidr'], n['network_id'])
236 for n in self._list_subnets()]
Steve Heyman33735f22016-05-24 09:28:08 -0500237 mysubnet = (tenant.subnet['id'], tenant.subnet['cidr'],
238 tenant.network['id'])
Yair Fried4d7efa62013-11-17 17:12:29 +0200239 self.assertIn(mysubnet, seen_subnets)
240
241 seen_routers = self._list_routers()
242 seen_router_ids = [n['id'] for n in seen_routers]
243 seen_router_names = [n['name'] for n in seen_routers]
244
Steve Heyman33735f22016-05-24 09:28:08 -0500245 self.assertIn(tenant.router['name'], seen_router_names)
246 self.assertIn(tenant.router['id'], seen_router_ids)
Yair Fried4d7efa62013-11-17 17:12:29 +0200247
Steve Heyman33735f22016-05-24 09:28:08 -0500248 myport = (tenant.router['id'], tenant.subnet['id'])
Yair Fried4d7efa62013-11-17 17:12:29 +0200249 router_ports = [(i['device_id'], i['fixed_ips'][0]['subnet_id']) for i
Yair Frieddb6c9e92014-08-06 08:53:13 +0300250 in self._list_ports()
Attila Fazekasd7e08a62016-10-07 13:05:05 +0200251 if net_info.is_router_interface_port(i)]
Yair Fried4d7efa62013-11-17 17:12:29 +0200252
253 self.assertIn(myport, router_ports)
254
Markus Zoeller156b5da2016-07-11 18:10:31 +0200255 def _create_server(self, name, tenant, security_groups, **kwargs):
Yair Friedca5cfb52016-01-04 15:41:55 +0200256 """Creates a server and assigns it to security group.
257
258 If multi-host is enabled, Ensures servers are created on different
259 compute nodes, by storing created servers' ids and uses different_host
260 as scheduler_hints on creation.
261 Validates servers are created as requested, using admin client.
262 """
Ken'ichi Ohmichi1b3461e2014-12-02 03:41:07 +0000263 security_groups_names = [{'name': s['name']} for s in security_groups]
Yair Friedca5cfb52016-01-04 15:41:55 +0200264 if self.multi_node:
265 kwargs["scheduler_hints"] = {'different_host': self.servers}
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +0000266 server = self.create_server(
267 name=name,
Steve Heyman33735f22016-05-24 09:28:08 -0500268 networks=[{'uuid': tenant.network["id"]}],
lanoux5fc14522015-09-21 08:17:35 +0000269 key_name=tenant.keypair['name'],
270 security_groups=security_groups_names,
271 wait_until='ACTIVE',
Yair Friedca5cfb52016-01-04 15:41:55 +0200272 clients=tenant.manager,
273 **kwargs)
Markus Zoeller156b5da2016-07-11 18:10:31 +0200274 if 'security_groups' in server:
275 self.assertEqual(
276 sorted([s['name'] for s in security_groups]),
277 sorted([s['name'] for s in server['security_groups']]))
Yair Friedca5cfb52016-01-04 15:41:55 +0200278
279 # Verify servers are on different compute nodes
280 if self.multi_node:
281 adm_get_server = self.admin_manager.servers_client.show_server
282 new_host = adm_get_server(server["id"])["server"][
283 "OS-EXT-SRV-ATTR:host"]
284 host_list = [adm_get_server(s)["server"]["OS-EXT-SRV-ATTR:host"]
285 for s in self.servers]
286 self.assertNotIn(new_host, host_list,
287 message="Failed to boot servers on different "
288 "Compute nodes.")
289
290 self.servers.append(server["id"])
291
Claudiu Belufaa98912014-09-01 16:50:28 +0300292 return server
Yair Fried4d7efa62013-11-17 17:12:29 +0200293
294 def _create_tenant_servers(self, tenant, num=1):
295 for i in range(num):
Ken'ichi Ohmichi6ded8df2015-03-23 02:00:19 +0000296 name = 'server-{tenant}-gen-{num}'.format(
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000297 tenant=tenant.creds.tenant_name,
Yair Fried4d7efa62013-11-17 17:12:29 +0200298 num=i
299 )
300 name = data_utils.rand_name(name)
Markus Zoeller156b5da2016-07-11 18:10:31 +0200301 server = self._create_server(name, tenant,
302 [tenant.security_groups['default']])
Yair Fried4d7efa62013-11-17 17:12:29 +0200303 tenant.servers.append(server)
304
305 def _set_access_point(self, tenant):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000306 # creates a server in a secgroup with rule allowing external ssh
Sean Dagueed6e5862016-04-04 10:49:13 -0400307 # in order to access project internal network
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000308 # workaround ip namespace
Yair Frieddb6c9e92014-08-06 08:53:13 +0300309 secgroups = tenant.security_groups.values()
Ken'ichi Ohmichi6ded8df2015-03-23 02:00:19 +0000310 name = 'server-{tenant}-access_point'.format(
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000311 tenant=tenant.creds.tenant_name)
Yair Fried4d7efa62013-11-17 17:12:29 +0200312 name = data_utils.rand_name(name)
313 server = self._create_server(name, tenant,
314 security_groups=secgroups)
Yair Fried4d7efa62013-11-17 17:12:29 +0200315 tenant.access_point = server
Yair Frieddb6c9e92014-08-06 08:53:13 +0300316 self._assign_floating_ips(tenant, server)
Yair Fried4d7efa62013-11-17 17:12:29 +0200317
Yair Frieddb6c9e92014-08-06 08:53:13 +0300318 def _assign_floating_ips(self, tenant, server):
Matthew Treinish6c072292014-01-29 19:15:52 +0000319 public_network_id = CONF.network.public_network_id
Yair Friedae0e73d2014-11-24 11:56:26 +0200320 floating_ip = self.create_floating_ip(
Yair Frieddb6c9e92014-08-06 08:53:13 +0300321 server, public_network_id,
John Warrenfbf2a892015-11-17 12:36:14 -0500322 client=tenant.manager.floating_ips_client)
Yair Frieddb6c9e92014-08-06 08:53:13 +0300323 self.floating_ips.setdefault(server['id'], floating_ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200324
Markus Zoeller156b5da2016-07-11 18:10:31 +0200325 def _create_tenant_network(self, tenant, port_security_enabled=True):
Yair Frieddb6c9e92014-08-06 08:53:13 +0300326 network, subnet, router = self.create_networks(
John Warren3961acd2015-10-02 14:38:53 -0400327 networks_client=tenant.manager.networks_client,
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000328 routers_client=tenant.manager.routers_client,
Markus Zoeller156b5da2016-07-11 18:10:31 +0200329 subnets_client=tenant.manager.subnets_client,
330 port_security_enabled=port_security_enabled)
Yair Friedbf2e2c42014-01-28 12:06:38 +0200331 tenant.set_network(network, subnet, router)
Yair Fried4d7efa62013-11-17 17:12:29 +0200332
Yair Fried4d7efa62013-11-17 17:12:29 +0200333 def _deploy_tenant(self, tenant_or_id):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000334 """creates:
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000335
Yair Fried4d7efa62013-11-17 17:12:29 +0200336 network
337 subnet
338 router (if public not defined)
339 access security group
340 access-point server
Yair Fried4d7efa62013-11-17 17:12:29 +0200341 """
342 if not isinstance(tenant_or_id, self.TenantProperties):
343 tenant = self.tenants[tenant_or_id]
Yair Fried4d7efa62013-11-17 17:12:29 +0200344 else:
345 tenant = tenant_or_id
Yair Frieddb6c9e92014-08-06 08:53:13 +0300346 self._create_tenant_keypairs(tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200347 self._create_tenant_network(tenant)
348 self._create_tenant_security_groups(tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200349 self._set_access_point(tenant)
350
351 def _get_server_ip(self, server, floating=False):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000352 """returns the ip (floating/internal) of a server"""
Yair Fried4d7efa62013-11-17 17:12:29 +0200353 if floating:
Steve Heyman33735f22016-05-24 09:28:08 -0500354 server_ip = self.floating_ips[server['id']]['floating_ip_address']
Yair Fried4d7efa62013-11-17 17:12:29 +0200355 else:
armando-migliacciod03f2642014-02-21 19:55:50 -0800356 server_ip = None
Steve Heyman33735f22016-05-24 09:28:08 -0500357 network_name = self.tenants[server['tenant_id']].network['name']
Yair Frieddb6c9e92014-08-06 08:53:13 +0300358 if network_name in server['addresses']:
359 server_ip = server['addresses'][network_name][0]['addr']
armando-migliacciod03f2642014-02-21 19:55:50 -0800360 return server_ip
Yair Fried4d7efa62013-11-17 17:12:29 +0200361
362 def _connect_to_access_point(self, tenant):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000363 """create ssh connection to tenant access point"""
Yair Fried4d7efa62013-11-17 17:12:29 +0200364 access_point_ssh = \
Steve Heyman33735f22016-05-24 09:28:08 -0500365 self.floating_ips[tenant.access_point['id']]['floating_ip_address']
Yair Frieddb6c9e92014-08-06 08:53:13 +0300366 private_key = tenant.keypair['private_key']
Jordan Pittierbbb17122016-01-26 17:10:55 +0100367 access_point_ssh = self.get_remote_client(
368 access_point_ssh, private_key=private_key)
Yair Fried4d7efa62013-11-17 17:12:29 +0200369 return access_point_ssh
370
Yair Fried4d7efa62013-11-17 17:12:29 +0200371 def _check_connectivity(self, access_point, ip, should_succeed=True):
372 if should_succeed:
373 msg = "Timed out waiting for %s to become reachable" % ip
374 else:
Yair Fried4d7efa62013-11-17 17:12:29 +0200375 msg = "%s is reachable" % ip
Matthew Treinish53483132014-12-09 18:50:06 -0500376 self.assertTrue(self._check_remote_connectivity(access_point, ip,
377 should_succeed), msg)
Yair Fried4d7efa62013-11-17 17:12:29 +0200378
379 def _test_in_tenant_block(self, tenant):
380 access_point_ssh = self._connect_to_access_point(tenant)
381 for server in tenant.servers:
382 self._check_connectivity(access_point=access_point_ssh,
383 ip=self._get_server_ip(server),
384 should_succeed=False)
385
386 def _test_in_tenant_allow(self, tenant):
387 ruleset = dict(
388 protocol='icmp',
Steve Heyman33735f22016-05-24 09:28:08 -0500389 remote_group_id=tenant.security_groups['default']['id'],
Yair Fried4d7efa62013-11-17 17:12:29 +0200390 direction='ingress'
391 )
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000392 self._create_security_group_rule(
Yair Fried4d7efa62013-11-17 17:12:29 +0200393 secgroup=tenant.security_groups['default'],
Yair Friedca5cfb52016-01-04 15:41:55 +0200394 security_groups_client=tenant.manager.security_groups_client,
Yair Fried4d7efa62013-11-17 17:12:29 +0200395 **ruleset
396 )
397 access_point_ssh = self._connect_to_access_point(tenant)
398 for server in tenant.servers:
399 self._check_connectivity(access_point=access_point_ssh,
400 ip=self._get_server_ip(server))
Yair Fried4d7efa62013-11-17 17:12:29 +0200401
402 def _test_cross_tenant_block(self, source_tenant, dest_tenant):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000403 # if public router isn't defined, then dest_tenant access is via
404 # floating-ip
Yair Fried4d7efa62013-11-17 17:12:29 +0200405 access_point_ssh = self._connect_to_access_point(source_tenant)
406 ip = self._get_server_ip(dest_tenant.access_point,
407 floating=self.floating_ip_access)
408 self._check_connectivity(access_point=access_point_ssh, ip=ip,
409 should_succeed=False)
410
411 def _test_cross_tenant_allow(self, source_tenant, dest_tenant):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000412 """check for each direction:
413
Yair Fried4d7efa62013-11-17 17:12:29 +0200414 creating rule for tenant incoming traffic enables only 1way traffic
Yair Friedbf2e2c42014-01-28 12:06:38 +0200415 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200416 ruleset = dict(
417 protocol='icmp',
418 direction='ingress'
419 )
John Warren456d9ae2016-01-12 15:36:33 -0500420 sec_group_rules_client = (
421 dest_tenant.manager.security_group_rules_client)
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000422 self._create_security_group_rule(
Yair Fried4d7efa62013-11-17 17:12:29 +0200423 secgroup=dest_tenant.security_groups['default'],
John Warren456d9ae2016-01-12 15:36:33 -0500424 sec_group_rules_client=sec_group_rules_client,
Yair Fried4d7efa62013-11-17 17:12:29 +0200425 **ruleset
426 )
Yair Friedbf2e2c42014-01-28 12:06:38 +0200427 access_point_ssh = self._connect_to_access_point(source_tenant)
428 ip = self._get_server_ip(dest_tenant.access_point,
429 floating=self.floating_ip_access)
430 self._check_connectivity(access_point_ssh, ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200431
Yair Friedbf2e2c42014-01-28 12:06:38 +0200432 # test that reverse traffic is still blocked
433 self._test_cross_tenant_block(dest_tenant, source_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200434
Yair Friedbf2e2c42014-01-28 12:06:38 +0200435 # allow reverse traffic and check
John Warren456d9ae2016-01-12 15:36:33 -0500436 sec_group_rules_client = (
437 source_tenant.manager.security_group_rules_client)
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000438 self._create_security_group_rule(
Yair Friedbf2e2c42014-01-28 12:06:38 +0200439 secgroup=source_tenant.security_groups['default'],
John Warren456d9ae2016-01-12 15:36:33 -0500440 sec_group_rules_client=sec_group_rules_client,
Yair Friedbf2e2c42014-01-28 12:06:38 +0200441 **ruleset
442 )
Yair Fried4d7efa62013-11-17 17:12:29 +0200443
Yair Friedbf2e2c42014-01-28 12:06:38 +0200444 access_point_ssh_2 = self._connect_to_access_point(dest_tenant)
445 ip = self._get_server_ip(source_tenant.access_point,
446 floating=self.floating_ip_access)
447 self._check_connectivity(access_point_ssh_2, ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200448
449 def _verify_mac_addr(self, tenant):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000450 """Verify that VM has the same ip, mac as listed in port"""
451
Yair Fried4d7efa62013-11-17 17:12:29 +0200452 access_point_ssh = self._connect_to_access_point(tenant)
453 mac_addr = access_point_ssh.get_mac_address()
454 mac_addr = mac_addr.strip().lower()
Henry Gessau78ab4b02014-03-31 15:10:13 -0400455 # Get the fixed_ips and mac_address fields of all ports. Select
456 # only those two columns to reduce the size of the response.
Yair Frieddb6c9e92014-08-06 08:53:13 +0300457 port_list = self._list_ports(fields=['fixed_ips', 'mac_address'])
Yair Fried4d7efa62013-11-17 17:12:29 +0200458 port_detail_list = [
459 (port['fixed_ips'][0]['subnet_id'],
460 port['fixed_ips'][0]['ip_address'],
Henry Gessau78ab4b02014-03-31 15:10:13 -0400461 port['mac_address'].lower())
462 for port in port_list if port['fixed_ips']
Yair Fried4d7efa62013-11-17 17:12:29 +0200463 ]
464 server_ip = self._get_server_ip(tenant.access_point)
Steve Heyman33735f22016-05-24 09:28:08 -0500465 subnet_id = tenant.subnet['id']
Yair Fried4d7efa62013-11-17 17:12:29 +0200466 self.assertIn((subnet_id, server_ip, mac_addr), port_detail_list)
467
Chris Hoge7579c1a2015-02-26 14:12:15 -0800468 @test.idempotent_id('e79f879e-debb-440c-a7e4-efeda05b6848')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900469 @test.services('compute', 'network')
Yair Fried4d7efa62013-11-17 17:12:29 +0200470 def test_cross_tenant_traffic(self):
Andrea Frittoli (andreaf)1f342412015-05-12 16:37:19 +0100471 if not self.credentials_provider.is_multi_tenant():
Yair Fried79b0a912014-10-20 11:15:37 +0300472 raise self.skipException("No secondary tenant defined")
Nachi Ueno26b4c972014-01-17 06:15:13 -0800473 try:
Sean Dagueed6e5862016-04-04 10:49:13 -0400474 # deploy new project
Yair Friedbf2e2c42014-01-28 12:06:38 +0200475 self._deploy_tenant(self.alt_tenant)
476 self._verify_network_details(self.alt_tenant)
477 self._verify_mac_addr(self.alt_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200478
Nachi Ueno26b4c972014-01-17 06:15:13 -0800479 # cross tenant check
Yair Friedbf2e2c42014-01-28 12:06:38 +0200480 source_tenant = self.primary_tenant
Nachi Ueno26b4c972014-01-17 06:15:13 -0800481 dest_tenant = self.alt_tenant
482 self._test_cross_tenant_block(source_tenant, dest_tenant)
483 self._test_cross_tenant_allow(source_tenant, dest_tenant)
484 except Exception:
Yair Friedbf2e2c42014-01-28 12:06:38 +0200485 for tenant in self.tenants.values():
486 self._log_console_output(servers=tenant.servers)
487 raise
488
Chris Hoge7579c1a2015-02-26 14:12:15 -0800489 @test.idempotent_id('63163892-bbf6-4249-aa12-d5ea1f8f421b')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900490 @test.services('compute', 'network')
Yair Friedbf2e2c42014-01-28 12:06:38 +0200491 def test_in_tenant_traffic(self):
492 try:
493 self._create_tenant_servers(self.primary_tenant, num=1)
494
495 # in-tenant check
496 self._test_in_tenant_block(self.primary_tenant)
497 self._test_in_tenant_allow(self.primary_tenant)
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800498 except Exception:
499 for tenant in self.tenants.values():
500 self._log_console_output(servers=tenant.servers)
501 raise
Yair Friedbf2e2c42014-01-28 12:06:38 +0200502
Chris Hoge7579c1a2015-02-26 14:12:15 -0800503 @test.idempotent_id('f4d556d7-1526-42ad-bafb-6bebf48568f6')
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800504 @test.services('compute', 'network')
505 def test_port_update_new_security_group(self):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000506 """Verifies the traffic after updating the vm port
507
508 With new security group having appropriate rule.
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800509 """
510 new_tenant = self.primary_tenant
511
512 # Create empty security group and add icmp rule in it
513 new_sg = self._create_empty_security_group(
514 namestart='secgroup_new-',
515 tenant_id=new_tenant.creds.tenant_id,
John Warrenf9606e92015-12-10 12:12:42 -0500516 client=new_tenant.manager.security_groups_client)
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800517 icmp_rule = dict(
518 protocol='icmp',
519 direction='ingress',
520 )
John Warren456d9ae2016-01-12 15:36:33 -0500521 sec_group_rules_client = new_tenant.manager.security_group_rules_client
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800522 self._create_security_group_rule(
523 secgroup=new_sg,
John Warren456d9ae2016-01-12 15:36:33 -0500524 sec_group_rules_client=sec_group_rules_client,
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800525 **icmp_rule)
526 new_tenant.security_groups.update(new_sg=new_sg)
527
528 # Create server with default security group
Ken'ichi Ohmichi6ded8df2015-03-23 02:00:19 +0000529 name = 'server-{tenant}-gen-1'.format(
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800530 tenant=new_tenant.creds.tenant_name
531 )
532 name = data_utils.rand_name(name)
Markus Zoeller156b5da2016-07-11 18:10:31 +0200533 server = self._create_server(name, new_tenant,
534 [new_tenant.security_groups['default']])
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800535
536 # Check connectivity failure with default security group
537 try:
538 access_point_ssh = self._connect_to_access_point(new_tenant)
539 self._check_connectivity(access_point=access_point_ssh,
540 ip=self._get_server_ip(server),
541 should_succeed=False)
542 server_id = server['id']
543 port_id = self._list_ports(device_id=server_id)[0]['id']
544
545 # update port with new security group and check connectivity
John Warren49c0fe52015-10-22 12:35:54 -0400546 self.ports_client.update_port(port_id, security_groups=[
Steve Heyman33735f22016-05-24 09:28:08 -0500547 new_tenant.security_groups['new_sg']['id']])
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800548 self._check_connectivity(
549 access_point=access_point_ssh,
550 ip=self._get_server_ip(server))
Yair Friedbf2e2c42014-01-28 12:06:38 +0200551 except Exception:
552 for tenant in self.tenants.values():
553 self._log_console_output(servers=tenant.servers)
Nachi Ueno26b4c972014-01-17 06:15:13 -0800554 raise
prdsilva8b733ad2014-12-09 02:54:49 -0800555
Chris Hoge7579c1a2015-02-26 14:12:15 -0800556 @test.idempotent_id('d2f77418-fcc4-439d-b935-72eca704e293')
prdsilva8b733ad2014-12-09 02:54:49 -0800557 @test.services('compute', 'network')
558 def test_multiple_security_groups(self):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000559 """Verify multiple security groups and checks that rules
560
prdsilva8b733ad2014-12-09 02:54:49 -0800561 provided in the both the groups is applied onto VM
562 """
563 tenant = self.primary_tenant
564 ip = self._get_server_ip(tenant.access_point,
565 floating=self.floating_ip_access)
lanoux283273b2015-12-04 03:01:54 -0800566 ssh_login = CONF.validation.image_ssh_user
prdsilva8b733ad2014-12-09 02:54:49 -0800567 private_key = tenant.keypair['private_key']
568 self.check_vm_connectivity(ip,
569 should_connect=False)
570 ruleset = dict(
571 protocol='icmp',
572 direction='ingress'
573 )
574 self._create_security_group_rule(
575 secgroup=tenant.security_groups['default'],
576 **ruleset
577 )
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000578 # NOTE: Vm now has 2 security groups one with ssh rule(
579 # already added in setUp() method),and other with icmp rule
580 # (added in the above step).The check_vm_connectivity tests
581 # -that vm ping test is successful
582 # -ssh to vm is successful
prdsilva8b733ad2014-12-09 02:54:49 -0800583 self.check_vm_connectivity(ip,
584 username=ssh_login,
585 private_key=private_key,
586 should_connect=True)
YAMAMOTO Takashi51e04082015-09-08 18:44:23 +0900587
588 @test.requires_ext(service='network', extension='port-security')
589 @test.idempotent_id('7c811dcc-263b-49a3-92d2-1b4d8405f50c')
590 @test.services('compute', 'network')
591 def test_port_security_disable_security_group(self):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000592 """Verify the default security group rules is disabled."""
YAMAMOTO Takashi51e04082015-09-08 18:44:23 +0900593 new_tenant = self.primary_tenant
594
595 # Create server
596 name = 'server-{tenant}-gen-1'.format(
597 tenant=new_tenant.creds.tenant_name
598 )
599 name = data_utils.rand_name(name)
Markus Zoeller156b5da2016-07-11 18:10:31 +0200600 server = self._create_server(name, new_tenant,
601 [new_tenant.security_groups['default']])
YAMAMOTO Takashi51e04082015-09-08 18:44:23 +0900602
603 access_point_ssh = self._connect_to_access_point(new_tenant)
604 server_id = server['id']
605 port_id = self._list_ports(device_id=server_id)[0]['id']
606
607 # Flip the port's port security and check connectivity
608 try:
John Warren49c0fe52015-10-22 12:35:54 -0400609 self.ports_client.update_port(port_id,
610 port_security_enabled=True,
611 security_groups=[])
YAMAMOTO Takashi51e04082015-09-08 18:44:23 +0900612 self._check_connectivity(access_point=access_point_ssh,
613 ip=self._get_server_ip(server),
614 should_succeed=False)
615
John Warren49c0fe52015-10-22 12:35:54 -0400616 self.ports_client.update_port(port_id,
617 port_security_enabled=False,
618 security_groups=[])
YAMAMOTO Takashi51e04082015-09-08 18:44:23 +0900619 self._check_connectivity(
620 access_point=access_point_ssh,
621 ip=self._get_server_ip(server))
622 except Exception:
623 for tenant in self.tenants.values():
624 self._log_console_output(servers=tenant.servers)
625 raise
Markus Zoeller156b5da2016-07-11 18:10:31 +0200626
627 @test.requires_ext(service='network', extension='port-security')
628 @test.idempotent_id('13ccf253-e5ad-424b-9c4a-97b88a026699')
629 @testtools.skipUnless(
630 CONF.compute_feature_enabled.allow_port_security_disabled,
631 'Port security must be enabled.')
Matt Riedemann039b2fe2016-09-15 16:12:24 -0400632 # TODO(mriedem): We shouldn't actually need to check this since neutron
633 # disables the port_security extension by default, but the problem is nova
634 # assumes port_security_enabled=True if it's not set on the network
635 # resource, which will mean nova may attempt to apply a security group on
636 # a port on that network which would fail. This is really a bug in nova.
637 @testtools.skipUnless(
638 CONF.network_feature_enabled.port_security,
639 'Port security must be enabled.')
Markus Zoeller156b5da2016-07-11 18:10:31 +0200640 @test.services('compute', 'network')
641 def test_boot_into_disabled_port_security_network_without_secgroup(self):
642 tenant = self.primary_tenant
643 self._create_tenant_network(tenant, port_security_enabled=False)
644 self.assertFalse(tenant.network['port_security_enabled'])
645 name = data_utils.rand_name('server-smoke')
646 sec_groups = []
647 server = self._create_server(name, tenant, sec_groups)
648 server_id = server['id']
649 ports = self._list_ports(device_id=server_id)
650 self.assertEqual(1, len(ports))
651 for port in ports:
652 self.assertEmpty(port['security_groups'],
653 "Neutron shouldn't even use it's default sec "
654 "group.")