blob: f59354d04062b04938ef8a5af925ca4a28832dd5 [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
Yair Fried4d7efa62013-11-17 17:12:29 +020016
Andrea Frittolif9cde7e2014-02-18 09:57:04 +000017from tempest import clients
Fei Long Wangd39431f2015-05-14 11:30:48 +120018from tempest.common.utils import data_utils
Matthew Treinish6c072292014-01-29 19:15:52 +000019from tempest import config
Yair Fried4d7efa62013-11-17 17:12:29 +020020from tempest.scenario import manager
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090021from tempest import test
Yair Fried4d7efa62013-11-17 17:12:29 +020022
Matthew Treinish6c072292014-01-29 19:15:52 +000023CONF = config.CONF
Yair Friedca5cfb52016-01-04 15:41:55 +020024LOG = log.getLogger(__name__)
Matthew Treinish6c072292014-01-29 19:15:52 +000025
Yair Fried4d7efa62013-11-17 17:12:29 +020026
Andrea Frittoli4971fc82014-09-25 10:22:20 +010027class TestSecurityGroupsBasicOps(manager.NetworkScenarioTest):
Yair Fried4d7efa62013-11-17 17:12:29 +020028
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +000029 """The test suite for security groups
30
Yair Fried4d7efa62013-11-17 17:12:29 +020031 This test suite assumes that Nova has been configured to
32 boot VM's with Neutron-managed networking, and attempts to
33 verify cross tenant connectivity as follows
34
35 ssh:
36 in order to overcome "ip namespace", each tenant has an "access point"
37 VM with floating-ip open to incoming ssh connection allowing network
38 commands (ping/ssh) to be executed from within the
39 tenant-network-namespace
40 Tempest host performs key-based authentication to the ssh server via
41 floating IP address
42
43 connectivity test is done by pinging destination server via source server
44 ssh connection.
45 success - ping returns
46 failure - ping_timeout reached
47
48 setup:
Yair Friedbf2e2c42014-01-28 12:06:38 +020049 for primary tenant:
Yair Fried4d7efa62013-11-17 17:12:29 +020050 1. create a network&subnet
51 2. create a router (if public router isn't configured)
52 3. connect tenant network to public network via router
53 4. create an access point:
54 a. a security group open to incoming ssh connection
55 b. a VM with a floating ip
56 5. create a general empty security group (same as "default", but
57 without rules allowing in-tenant traffic)
Yair Fried4d7efa62013-11-17 17:12:29 +020058
59 tests:
60 1. _verify_network_details
61 2. _verify_mac_addr: for each access point verify that
62 (subnet, fix_ip, mac address) are as defined in the port list
63 3. _test_in_tenant_block: test that in-tenant traffic is disabled
64 without rules allowing it
65 4. _test_in_tenant_allow: test that in-tenant traffic is enabled
66 once an appropriate rule has been created
67 5. _test_cross_tenant_block: test that cross-tenant traffic is disabled
68 without a rule allowing it on destination tenant
69 6. _test_cross_tenant_allow:
70 * test that cross-tenant traffic is enabled once an appropriate
71 rule has been created on destination tenant.
72 * test that reverse traffic is still blocked
Fei Long Wang50131ee2015-02-02 16:58:24 +130073 * test than reverse traffic is enabled once an appropriate rule has
Yair Fried4d7efa62013-11-17 17:12:29 +020074 been created on source tenant
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -080075 7._test_port_update_new_security_group:
76 * test that traffic is blocked with default security group
77 * test that traffic is enabled after updating port with new security
78 group having appropriate rule
prdsilva8b733ad2014-12-09 02:54:49 -080079 8. _test_multiple_security_groups: test multiple security groups can be
80 associated with the vm
Yair Fried4d7efa62013-11-17 17:12:29 +020081
82 assumptions:
Yair Friedbf2e2c42014-01-28 12:06:38 +020083 1. alt_tenant/user existed and is different from primary_tenant/user
Yair Fried4d7efa62013-11-17 17:12:29 +020084 2. Public network is defined and reachable from the Tempest host
85 3. Public router can either be:
86 * defined, in which case all tenants networks can connect directly
87 to it, and cross tenant check will be done on the private IP of the
88 destination tenant
89 or
Fei Long Wang50131ee2015-02-02 16:58:24 +130090 * not defined (empty string), in which case each tenant will have
Yair Fried4d7efa62013-11-17 17:12:29 +020091 its own router connected to the public network
92 """
93
Andrea Frittolib21de6c2015-02-06 20:12:38 +000094 credentials = ['primary', 'alt', 'admin']
95
Joe Gordon28788b42015-02-25 12:42:37 -080096 class TenantProperties(object):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +000097 """helper class to save tenant details
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +000098
Yair Fried4d7efa62013-11-17 17:12:29 +020099 id
100 credentials
101 network
102 subnet
103 security groups
104 servers
105 access point
Yair Friedbf2e2c42014-01-28 12:06:38 +0200106 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200107
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000108 def __init__(self, credentials):
Yair Frieddb6c9e92014-08-06 08:53:13 +0300109 self.manager = clients.Manager(credentials)
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000110 # Credentials from manager are filled with both names and IDs
111 self.creds = self.manager.credentials
Yair Fried4d7efa62013-11-17 17:12:29 +0200112 self.network = None
113 self.subnet = None
114 self.router = None
115 self.security_groups = {}
116 self.servers = list()
117
Yair Friedbf2e2c42014-01-28 12:06:38 +0200118 def set_network(self, network, subnet, router):
Yair Fried4d7efa62013-11-17 17:12:29 +0200119 self.network = network
120 self.subnet = subnet
121 self.router = router
122
Yair Fried4d7efa62013-11-17 17:12:29 +0200123 @classmethod
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000124 def skip_checks(cls):
125 super(TestSecurityGroupsBasicOps, cls).skip_checks()
Yair Frieddb6c9e92014-08-06 08:53:13 +0300126 if CONF.baremetal.driver_enabled:
127 msg = ('Not currently supported by baremetal.')
Yair Frieddb6c9e92014-08-06 08:53:13 +0300128 raise cls.skipException(msg)
Itzik Brown06952672015-03-29 12:38:58 +0300129 if CONF.network.port_vnic_type in ['direct', 'macvtap']:
130 msg = ('Not currently supported when using vnic_type'
131 ' direct or macvtap')
132 raise cls.skipException(msg)
Matthew Treinish6c072292014-01-29 19:15:52 +0000133 if not (CONF.network.tenant_networks_reachable or
134 CONF.network.public_network_id):
Yair Fried4d7efa62013-11-17 17:12:29 +0200135 msg = ('Either tenant_networks_reachable must be "true", or '
136 'public_network_id must be defined.')
Yair Fried4d7efa62013-11-17 17:12:29 +0200137 raise cls.skipException(msg)
Bence Romsics41f3f852016-01-11 13:48:23 +0100138 if not test.is_extension_enabled('security-group', 'network'):
139 msg = "security-group extension not enabled."
140 raise cls.skipException(msg)
Yair Fried4d7efa62013-11-17 17:12:29 +0200141
142 @classmethod
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000143 def setup_credentials(cls):
Yair Fried764610a2014-04-07 12:17:05 +0300144 # Create no network resources for these tests.
145 cls.set_network_resources()
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000146 super(TestSecurityGroupsBasicOps, cls).setup_credentials()
Yair Fried4d7efa62013-11-17 17:12:29 +0200147 # TODO(mnewby) Consider looking up entities as needed instead
148 # of storing them as collections on the class.
Yair Fried79b0a912014-10-20 11:15:37 +0300149
Yair Fried79b0a912014-10-20 11:15:37 +0300150 # Credentials from the manager are filled with both IDs and Names
151 cls.alt_creds = cls.alt_manager.credentials
152
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000153 @classmethod
154 def resource_setup(cls):
155 super(TestSecurityGroupsBasicOps, cls).resource_setup()
Yair Friedca5cfb52016-01-04 15:41:55 +0200156
157 cls.multi_node = CONF.compute.min_compute_nodes > 1 and \
Yair Fried95914122016-03-03 09:14:40 +0200158 test.is_scheduler_filter_enabled("DifferentHostFilter")
Yair Friedca5cfb52016-01-04 15:41:55 +0200159 if cls.multi_node:
160 LOG.info("Working in Multi Node mode")
161 else:
162 LOG.info("Working in Single Node mode")
163
Yair Fried4d7efa62013-11-17 17:12:29 +0200164 cls.floating_ips = {}
165 cls.tenants = {}
Andrea Frittolib21de6c2015-02-06 20:12:38 +0000166 creds = cls.manager.credentials
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000167 cls.primary_tenant = cls.TenantProperties(creds)
168 cls.alt_tenant = cls.TenantProperties(cls.alt_creds)
Yair Friedbf2e2c42014-01-28 12:06:38 +0200169 for tenant in [cls.primary_tenant, cls.alt_tenant]:
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000170 cls.tenants[tenant.creds.tenant_id] = tenant
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000171
Yair Friedbf2e2c42014-01-28 12:06:38 +0200172 cls.floating_ip_access = not CONF.network.public_router_id
Yair Fried4d7efa62013-11-17 17:12:29 +0200173
Yair Friedbf2e2c42014-01-28 12:06:38 +0200174 def setUp(self):
Yair Friedca5cfb52016-01-04 15:41:55 +0200175 """Set up a single tenant with an accessible server.
176
177 If multi-host is enabled, save created server uuids.
178 """
179 self.servers = []
180
Yair Friedbf2e2c42014-01-28 12:06:38 +0200181 super(TestSecurityGroupsBasicOps, self).setUp()
182 self._deploy_tenant(self.primary_tenant)
183 self._verify_network_details(self.primary_tenant)
184 self._verify_mac_addr(self.primary_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200185
Yair Frieddb6c9e92014-08-06 08:53:13 +0300186 def _create_tenant_keypairs(self, tenant):
187 keypair = self.create_keypair(tenant.manager.keypairs_client)
188 tenant.keypair = keypair
Yair Fried4d7efa62013-11-17 17:12:29 +0200189
190 def _create_tenant_security_groups(self, tenant):
Yair Fried4d7efa62013-11-17 17:12:29 +0200191 access_sg = self._create_empty_security_group(
192 namestart='secgroup_access-',
Yair Frieddb6c9e92014-08-06 08:53:13 +0300193 tenant_id=tenant.creds.tenant_id,
John Warrenf9606e92015-12-10 12:12:42 -0500194 client=tenant.manager.security_groups_client
Yair Fried4d7efa62013-11-17 17:12:29 +0200195 )
Yair Friedbf2e2c42014-01-28 12:06:38 +0200196
Yair Fried4d7efa62013-11-17 17:12:29 +0200197 # don't use default secgroup since it allows in-tenant traffic
198 def_sg = self._create_empty_security_group(
199 namestart='secgroup_general-',
Yair Frieddb6c9e92014-08-06 08:53:13 +0300200 tenant_id=tenant.creds.tenant_id,
John Warrenf9606e92015-12-10 12:12:42 -0500201 client=tenant.manager.security_groups_client
Yair Fried4d7efa62013-11-17 17:12:29 +0200202 )
203 tenant.security_groups.update(access=access_sg, default=def_sg)
204 ssh_rule = dict(
205 protocol='tcp',
206 port_range_min=22,
207 port_range_max=22,
208 direction='ingress',
209 )
John Warren456d9ae2016-01-12 15:36:33 -0500210 sec_group_rules_client = tenant.manager.security_group_rules_client
211 self._create_security_group_rule(
212 secgroup=access_sg,
213 sec_group_rules_client=sec_group_rules_client,
214 **ssh_rule)
Yair Fried4d7efa62013-11-17 17:12:29 +0200215
216 def _verify_network_details(self, tenant):
217 # Checks that we see the newly created network/subnet/router via
218 # checking the result of list_[networks,routers,subnets]
219 # Check that (router, subnet) couple exist in port_list
220 seen_nets = self._list_networks()
221 seen_names = [n['name'] for n in seen_nets]
222 seen_ids = [n['id'] for n in seen_nets]
223
224 self.assertIn(tenant.network.name, seen_names)
225 self.assertIn(tenant.network.id, seen_ids)
226
227 seen_subnets = [(n['id'], n['cidr'], n['network_id'])
228 for n in self._list_subnets()]
229 mysubnet = (tenant.subnet.id, tenant.subnet.cidr, tenant.network.id)
230 self.assertIn(mysubnet, seen_subnets)
231
232 seen_routers = self._list_routers()
233 seen_router_ids = [n['id'] for n in seen_routers]
234 seen_router_names = [n['name'] for n in seen_routers]
235
236 self.assertIn(tenant.router.name, seen_router_names)
237 self.assertIn(tenant.router.id, seen_router_ids)
238
239 myport = (tenant.router.id, tenant.subnet.id)
240 router_ports = [(i['device_id'], i['fixed_ips'][0]['subnet_id']) for i
Yair Frieddb6c9e92014-08-06 08:53:13 +0300241 in self._list_ports()
armando-migliacciobcfbbeb2014-08-11 18:33:47 -0700242 if self._is_router_port(i)]
Yair Fried4d7efa62013-11-17 17:12:29 +0200243
244 self.assertIn(myport, router_ports)
245
armando-migliacciobcfbbeb2014-08-11 18:33:47 -0700246 def _is_router_port(self, port):
247 """Return True if port is a router interface."""
248 # NOTE(armando-migliaccio): match device owner for both centralized
249 # and distributed routers; 'device_owner' is "" by default.
250 return port['device_owner'].startswith('network:router_interface')
251
Yair Friedca5cfb52016-01-04 15:41:55 +0200252 def _create_server(self, name, tenant, security_groups=None, **kwargs):
253 """Creates a server and assigns it to security group.
254
255 If multi-host is enabled, Ensures servers are created on different
256 compute nodes, by storing created servers' ids and uses different_host
257 as scheduler_hints on creation.
258 Validates servers are created as requested, using admin client.
259 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200260 if security_groups is None:
Yair Frieddb6c9e92014-08-06 08:53:13 +0300261 security_groups = [tenant.security_groups['default']]
Ken'ichi Ohmichi1b3461e2014-12-02 03:41:07 +0000262 security_groups_names = [{'name': s['name']} for s in security_groups]
Yair Friedca5cfb52016-01-04 15:41:55 +0200263 if self.multi_node:
264 kwargs["scheduler_hints"] = {'different_host': self.servers}
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +0000265 server = self.create_server(
266 name=name,
lanoux5fc14522015-09-21 08:17:35 +0000267 networks=[{'uuid': tenant.network.id}],
268 key_name=tenant.keypair['name'],
269 security_groups=security_groups_names,
270 wait_until='ACTIVE',
Yair Friedca5cfb52016-01-04 15:41:55 +0200271 clients=tenant.manager,
272 **kwargs)
Claudiu Belufaa98912014-09-01 16:50:28 +0300273 self.assertEqual(
274 sorted([s['name'] for s in security_groups]),
275 sorted([s['name'] for s in server['security_groups']]))
Yair Friedca5cfb52016-01-04 15:41:55 +0200276
277 # Verify servers are on different compute nodes
278 if self.multi_node:
279 adm_get_server = self.admin_manager.servers_client.show_server
280 new_host = adm_get_server(server["id"])["server"][
281 "OS-EXT-SRV-ATTR:host"]
282 host_list = [adm_get_server(s)["server"]["OS-EXT-SRV-ATTR:host"]
283 for s in self.servers]
284 self.assertNotIn(new_host, host_list,
285 message="Failed to boot servers on different "
286 "Compute nodes.")
287
288 self.servers.append(server["id"])
289
Claudiu Belufaa98912014-09-01 16:50:28 +0300290 return server
Yair Fried4d7efa62013-11-17 17:12:29 +0200291
292 def _create_tenant_servers(self, tenant, num=1):
293 for i in range(num):
Ken'ichi Ohmichi6ded8df2015-03-23 02:00:19 +0000294 name = 'server-{tenant}-gen-{num}'.format(
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000295 tenant=tenant.creds.tenant_name,
Yair Fried4d7efa62013-11-17 17:12:29 +0200296 num=i
297 )
298 name = data_utils.rand_name(name)
299 server = self._create_server(name, tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200300 tenant.servers.append(server)
301
302 def _set_access_point(self, tenant):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000303 # creates a server in a secgroup with rule allowing external ssh
304 # in order to access tenant internal network
305 # workaround ip namespace
Yair Frieddb6c9e92014-08-06 08:53:13 +0300306 secgroups = tenant.security_groups.values()
Ken'ichi Ohmichi6ded8df2015-03-23 02:00:19 +0000307 name = 'server-{tenant}-access_point'.format(
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000308 tenant=tenant.creds.tenant_name)
Yair Fried4d7efa62013-11-17 17:12:29 +0200309 name = data_utils.rand_name(name)
310 server = self._create_server(name, tenant,
311 security_groups=secgroups)
Yair Fried4d7efa62013-11-17 17:12:29 +0200312 tenant.access_point = server
Yair Frieddb6c9e92014-08-06 08:53:13 +0300313 self._assign_floating_ips(tenant, server)
Yair Fried4d7efa62013-11-17 17:12:29 +0200314
Yair Frieddb6c9e92014-08-06 08:53:13 +0300315 def _assign_floating_ips(self, tenant, server):
Matthew Treinish6c072292014-01-29 19:15:52 +0000316 public_network_id = CONF.network.public_network_id
Yair Friedae0e73d2014-11-24 11:56:26 +0200317 floating_ip = self.create_floating_ip(
Yair Frieddb6c9e92014-08-06 08:53:13 +0300318 server, public_network_id,
John Warrenfbf2a892015-11-17 12:36:14 -0500319 client=tenant.manager.floating_ips_client)
Yair Frieddb6c9e92014-08-06 08:53:13 +0300320 self.floating_ips.setdefault(server['id'], floating_ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200321
322 def _create_tenant_network(self, tenant):
Yair Frieddb6c9e92014-08-06 08:53:13 +0300323 network, subnet, router = self.create_networks(
John Warren94d8faf2015-09-15 12:22:24 -0400324 client=tenant.manager.network_client,
John Warren3961acd2015-10-02 14:38:53 -0400325 networks_client=tenant.manager.networks_client,
326 subnets_client=tenant.manager.subnets_client)
Yair Friedbf2e2c42014-01-28 12:06:38 +0200327 tenant.set_network(network, subnet, router)
Yair Fried4d7efa62013-11-17 17:12:29 +0200328
Yair Fried4d7efa62013-11-17 17:12:29 +0200329 def _deploy_tenant(self, tenant_or_id):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000330 """creates:
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000331
Yair Fried4d7efa62013-11-17 17:12:29 +0200332 network
333 subnet
334 router (if public not defined)
335 access security group
336 access-point server
Yair Fried4d7efa62013-11-17 17:12:29 +0200337 """
338 if not isinstance(tenant_or_id, self.TenantProperties):
339 tenant = self.tenants[tenant_or_id]
Yair Fried4d7efa62013-11-17 17:12:29 +0200340 else:
341 tenant = tenant_or_id
Yair Frieddb6c9e92014-08-06 08:53:13 +0300342 self._create_tenant_keypairs(tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200343 self._create_tenant_network(tenant)
344 self._create_tenant_security_groups(tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200345 self._set_access_point(tenant)
346
347 def _get_server_ip(self, server, floating=False):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000348 """returns the ip (floating/internal) of a server"""
Yair Fried4d7efa62013-11-17 17:12:29 +0200349 if floating:
Yair Frieddb6c9e92014-08-06 08:53:13 +0300350 server_ip = self.floating_ips[server['id']].floating_ip_address
Yair Fried4d7efa62013-11-17 17:12:29 +0200351 else:
armando-migliacciod03f2642014-02-21 19:55:50 -0800352 server_ip = None
Yair Frieddb6c9e92014-08-06 08:53:13 +0300353 network_name = self.tenants[server['tenant_id']].network.name
354 if network_name in server['addresses']:
355 server_ip = server['addresses'][network_name][0]['addr']
armando-migliacciod03f2642014-02-21 19:55:50 -0800356 return server_ip
Yair Fried4d7efa62013-11-17 17:12:29 +0200357
358 def _connect_to_access_point(self, tenant):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000359 """create ssh connection to tenant access point"""
Yair Fried4d7efa62013-11-17 17:12:29 +0200360 access_point_ssh = \
Yair Frieddb6c9e92014-08-06 08:53:13 +0300361 self.floating_ips[tenant.access_point['id']].floating_ip_address
362 private_key = tenant.keypair['private_key']
Jordan Pittierbbb17122016-01-26 17:10:55 +0100363 access_point_ssh = self.get_remote_client(
364 access_point_ssh, private_key=private_key)
Yair Fried4d7efa62013-11-17 17:12:29 +0200365 return access_point_ssh
366
Yair Fried4d7efa62013-11-17 17:12:29 +0200367 def _check_connectivity(self, access_point, ip, should_succeed=True):
368 if should_succeed:
369 msg = "Timed out waiting for %s to become reachable" % ip
370 else:
Yair Fried4d7efa62013-11-17 17:12:29 +0200371 msg = "%s is reachable" % ip
Matthew Treinish53483132014-12-09 18:50:06 -0500372 self.assertTrue(self._check_remote_connectivity(access_point, ip,
373 should_succeed), msg)
Yair Fried4d7efa62013-11-17 17:12:29 +0200374
375 def _test_in_tenant_block(self, tenant):
376 access_point_ssh = self._connect_to_access_point(tenant)
377 for server in tenant.servers:
378 self._check_connectivity(access_point=access_point_ssh,
379 ip=self._get_server_ip(server),
380 should_succeed=False)
381
382 def _test_in_tenant_allow(self, tenant):
383 ruleset = dict(
384 protocol='icmp',
385 remote_group_id=tenant.security_groups['default'].id,
386 direction='ingress'
387 )
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000388 self._create_security_group_rule(
Yair Fried4d7efa62013-11-17 17:12:29 +0200389 secgroup=tenant.security_groups['default'],
Yair Friedca5cfb52016-01-04 15:41:55 +0200390 security_groups_client=tenant.manager.security_groups_client,
Yair Fried4d7efa62013-11-17 17:12:29 +0200391 **ruleset
392 )
393 access_point_ssh = self._connect_to_access_point(tenant)
394 for server in tenant.servers:
395 self._check_connectivity(access_point=access_point_ssh,
396 ip=self._get_server_ip(server))
Yair Fried4d7efa62013-11-17 17:12:29 +0200397
398 def _test_cross_tenant_block(self, source_tenant, dest_tenant):
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +0000399 # if public router isn't defined, then dest_tenant access is via
400 # floating-ip
Yair Fried4d7efa62013-11-17 17:12:29 +0200401 access_point_ssh = self._connect_to_access_point(source_tenant)
402 ip = self._get_server_ip(dest_tenant.access_point,
403 floating=self.floating_ip_access)
404 self._check_connectivity(access_point=access_point_ssh, ip=ip,
405 should_succeed=False)
406
407 def _test_cross_tenant_allow(self, source_tenant, dest_tenant):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000408 """check for each direction:
409
Yair Fried4d7efa62013-11-17 17:12:29 +0200410 creating rule for tenant incoming traffic enables only 1way traffic
Yair Friedbf2e2c42014-01-28 12:06:38 +0200411 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200412 ruleset = dict(
413 protocol='icmp',
414 direction='ingress'
415 )
John Warren456d9ae2016-01-12 15:36:33 -0500416 sec_group_rules_client = (
417 dest_tenant.manager.security_group_rules_client)
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000418 self._create_security_group_rule(
Yair Fried4d7efa62013-11-17 17:12:29 +0200419 secgroup=dest_tenant.security_groups['default'],
John Warren456d9ae2016-01-12 15:36:33 -0500420 sec_group_rules_client=sec_group_rules_client,
Yair Fried4d7efa62013-11-17 17:12:29 +0200421 **ruleset
422 )
Yair Friedbf2e2c42014-01-28 12:06:38 +0200423 access_point_ssh = self._connect_to_access_point(source_tenant)
424 ip = self._get_server_ip(dest_tenant.access_point,
425 floating=self.floating_ip_access)
426 self._check_connectivity(access_point_ssh, ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200427
Yair Friedbf2e2c42014-01-28 12:06:38 +0200428 # test that reverse traffic is still blocked
429 self._test_cross_tenant_block(dest_tenant, source_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200430
Yair Friedbf2e2c42014-01-28 12:06:38 +0200431 # allow reverse traffic and check
John Warren456d9ae2016-01-12 15:36:33 -0500432 sec_group_rules_client = (
433 source_tenant.manager.security_group_rules_client)
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000434 self._create_security_group_rule(
Yair Friedbf2e2c42014-01-28 12:06:38 +0200435 secgroup=source_tenant.security_groups['default'],
John Warren456d9ae2016-01-12 15:36:33 -0500436 sec_group_rules_client=sec_group_rules_client,
Yair Friedbf2e2c42014-01-28 12:06:38 +0200437 **ruleset
438 )
Yair Fried4d7efa62013-11-17 17:12:29 +0200439
Yair Friedbf2e2c42014-01-28 12:06:38 +0200440 access_point_ssh_2 = self._connect_to_access_point(dest_tenant)
441 ip = self._get_server_ip(source_tenant.access_point,
442 floating=self.floating_ip_access)
443 self._check_connectivity(access_point_ssh_2, ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200444
445 def _verify_mac_addr(self, tenant):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000446 """Verify that VM has the same ip, mac as listed in port"""
447
Yair Fried4d7efa62013-11-17 17:12:29 +0200448 access_point_ssh = self._connect_to_access_point(tenant)
449 mac_addr = access_point_ssh.get_mac_address()
450 mac_addr = mac_addr.strip().lower()
Henry Gessau78ab4b02014-03-31 15:10:13 -0400451 # Get the fixed_ips and mac_address fields of all ports. Select
452 # only those two columns to reduce the size of the response.
Yair Frieddb6c9e92014-08-06 08:53:13 +0300453 port_list = self._list_ports(fields=['fixed_ips', 'mac_address'])
Yair Fried4d7efa62013-11-17 17:12:29 +0200454 port_detail_list = [
455 (port['fixed_ips'][0]['subnet_id'],
456 port['fixed_ips'][0]['ip_address'],
Henry Gessau78ab4b02014-03-31 15:10:13 -0400457 port['mac_address'].lower())
458 for port in port_list if port['fixed_ips']
Yair Fried4d7efa62013-11-17 17:12:29 +0200459 ]
460 server_ip = self._get_server_ip(tenant.access_point)
461 subnet_id = tenant.subnet.id
462 self.assertIn((subnet_id, server_ip, mac_addr), port_detail_list)
463
Chris Hoge7579c1a2015-02-26 14:12:15 -0800464 @test.idempotent_id('e79f879e-debb-440c-a7e4-efeda05b6848')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900465 @test.services('compute', 'network')
Yair Fried4d7efa62013-11-17 17:12:29 +0200466 def test_cross_tenant_traffic(self):
Andrea Frittoli (andreaf)1f342412015-05-12 16:37:19 +0100467 if not self.credentials_provider.is_multi_tenant():
Yair Fried79b0a912014-10-20 11:15:37 +0300468 raise self.skipException("No secondary tenant defined")
Nachi Ueno26b4c972014-01-17 06:15:13 -0800469 try:
Yair Friedbf2e2c42014-01-28 12:06:38 +0200470 # deploy new tenant
471 self._deploy_tenant(self.alt_tenant)
472 self._verify_network_details(self.alt_tenant)
473 self._verify_mac_addr(self.alt_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200474
Nachi Ueno26b4c972014-01-17 06:15:13 -0800475 # cross tenant check
Yair Friedbf2e2c42014-01-28 12:06:38 +0200476 source_tenant = self.primary_tenant
Nachi Ueno26b4c972014-01-17 06:15:13 -0800477 dest_tenant = self.alt_tenant
478 self._test_cross_tenant_block(source_tenant, dest_tenant)
479 self._test_cross_tenant_allow(source_tenant, dest_tenant)
480 except Exception:
Yair Friedbf2e2c42014-01-28 12:06:38 +0200481 for tenant in self.tenants.values():
482 self._log_console_output(servers=tenant.servers)
483 raise
484
Chris Hoge7579c1a2015-02-26 14:12:15 -0800485 @test.idempotent_id('63163892-bbf6-4249-aa12-d5ea1f8f421b')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900486 @test.services('compute', 'network')
Yair Friedbf2e2c42014-01-28 12:06:38 +0200487 def test_in_tenant_traffic(self):
488 try:
489 self._create_tenant_servers(self.primary_tenant, num=1)
490
491 # in-tenant check
492 self._test_in_tenant_block(self.primary_tenant)
493 self._test_in_tenant_allow(self.primary_tenant)
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800494 except Exception:
495 for tenant in self.tenants.values():
496 self._log_console_output(servers=tenant.servers)
497 raise
Yair Friedbf2e2c42014-01-28 12:06:38 +0200498
Chris Hoge7579c1a2015-02-26 14:12:15 -0800499 @test.idempotent_id('f4d556d7-1526-42ad-bafb-6bebf48568f6')
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800500 @test.services('compute', 'network')
501 def test_port_update_new_security_group(self):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000502 """Verifies the traffic after updating the vm port
503
504 With new security group having appropriate rule.
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800505 """
506 new_tenant = self.primary_tenant
507
508 # Create empty security group and add icmp rule in it
509 new_sg = self._create_empty_security_group(
510 namestart='secgroup_new-',
511 tenant_id=new_tenant.creds.tenant_id,
John Warrenf9606e92015-12-10 12:12:42 -0500512 client=new_tenant.manager.security_groups_client)
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800513 icmp_rule = dict(
514 protocol='icmp',
515 direction='ingress',
516 )
John Warren456d9ae2016-01-12 15:36:33 -0500517 sec_group_rules_client = new_tenant.manager.security_group_rules_client
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800518 self._create_security_group_rule(
519 secgroup=new_sg,
John Warren456d9ae2016-01-12 15:36:33 -0500520 sec_group_rules_client=sec_group_rules_client,
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800521 **icmp_rule)
522 new_tenant.security_groups.update(new_sg=new_sg)
523
524 # Create server with default security group
Ken'ichi Ohmichi6ded8df2015-03-23 02:00:19 +0000525 name = 'server-{tenant}-gen-1'.format(
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800526 tenant=new_tenant.creds.tenant_name
527 )
528 name = data_utils.rand_name(name)
529 server = self._create_server(name, new_tenant)
530
531 # Check connectivity failure with default security group
532 try:
533 access_point_ssh = self._connect_to_access_point(new_tenant)
534 self._check_connectivity(access_point=access_point_ssh,
535 ip=self._get_server_ip(server),
536 should_succeed=False)
537 server_id = server['id']
538 port_id = self._list_ports(device_id=server_id)[0]['id']
539
540 # update port with new security group and check connectivity
John Warren49c0fe52015-10-22 12:35:54 -0400541 self.ports_client.update_port(port_id, security_groups=[
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800542 new_tenant.security_groups['new_sg'].id])
543 self._check_connectivity(
544 access_point=access_point_ssh,
545 ip=self._get_server_ip(server))
Yair Friedbf2e2c42014-01-28 12:06:38 +0200546 except Exception:
547 for tenant in self.tenants.values():
548 self._log_console_output(servers=tenant.servers)
Nachi Ueno26b4c972014-01-17 06:15:13 -0800549 raise
prdsilva8b733ad2014-12-09 02:54:49 -0800550
Chris Hoge7579c1a2015-02-26 14:12:15 -0800551 @test.idempotent_id('d2f77418-fcc4-439d-b935-72eca704e293')
prdsilva8b733ad2014-12-09 02:54:49 -0800552 @test.services('compute', 'network')
553 def test_multiple_security_groups(self):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000554 """Verify multiple security groups and checks that rules
555
prdsilva8b733ad2014-12-09 02:54:49 -0800556 provided in the both the groups is applied onto VM
557 """
558 tenant = self.primary_tenant
559 ip = self._get_server_ip(tenant.access_point,
560 floating=self.floating_ip_access)
lanoux283273b2015-12-04 03:01:54 -0800561 ssh_login = CONF.validation.image_ssh_user
prdsilva8b733ad2014-12-09 02:54:49 -0800562 private_key = tenant.keypair['private_key']
563 self.check_vm_connectivity(ip,
564 should_connect=False)
565 ruleset = dict(
566 protocol='icmp',
567 direction='ingress'
568 )
569 self._create_security_group_rule(
570 secgroup=tenant.security_groups['default'],
571 **ruleset
572 )
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000573 # NOTE: Vm now has 2 security groups one with ssh rule(
574 # already added in setUp() method),and other with icmp rule
575 # (added in the above step).The check_vm_connectivity tests
576 # -that vm ping test is successful
577 # -ssh to vm is successful
prdsilva8b733ad2014-12-09 02:54:49 -0800578 self.check_vm_connectivity(ip,
579 username=ssh_login,
580 private_key=private_key,
581 should_connect=True)
YAMAMOTO Takashi51e04082015-09-08 18:44:23 +0900582
583 @test.requires_ext(service='network', extension='port-security')
584 @test.idempotent_id('7c811dcc-263b-49a3-92d2-1b4d8405f50c')
585 @test.services('compute', 'network')
586 def test_port_security_disable_security_group(self):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +0000587 """Verify the default security group rules is disabled."""
YAMAMOTO Takashi51e04082015-09-08 18:44:23 +0900588 new_tenant = self.primary_tenant
589
590 # Create server
591 name = 'server-{tenant}-gen-1'.format(
592 tenant=new_tenant.creds.tenant_name
593 )
594 name = data_utils.rand_name(name)
595 server = self._create_server(name, new_tenant)
596
597 access_point_ssh = self._connect_to_access_point(new_tenant)
598 server_id = server['id']
599 port_id = self._list_ports(device_id=server_id)[0]['id']
600
601 # Flip the port's port security and check connectivity
602 try:
John Warren49c0fe52015-10-22 12:35:54 -0400603 self.ports_client.update_port(port_id,
604 port_security_enabled=True,
605 security_groups=[])
YAMAMOTO Takashi51e04082015-09-08 18:44:23 +0900606 self._check_connectivity(access_point=access_point_ssh,
607 ip=self._get_server_ip(server),
608 should_succeed=False)
609
John Warren49c0fe52015-10-22 12:35:54 -0400610 self.ports_client.update_port(port_id,
611 port_security_enabled=False,
612 security_groups=[])
YAMAMOTO Takashi51e04082015-09-08 18:44:23 +0900613 self._check_connectivity(
614 access_point=access_point_ssh,
615 ip=self._get_server_ip(server))
616 except Exception:
617 for tenant in self.tenants.values():
618 self._log_console_output(servers=tenant.servers)
619 raise