Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 1 | # 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 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 16 | from oslo_log import log as logging |
Matthew Treinish | 01472ff | 2015-02-20 17:26:52 -0500 | [diff] [blame] | 17 | from tempest_lib.common.utils import data_utils |
| 18 | |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 19 | from tempest import clients |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 20 | from tempest import config |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 21 | from tempest.scenario import manager |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 22 | from tempest import test |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 23 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 24 | CONF = config.CONF |
| 25 | |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 26 | LOG = logging.getLogger(__name__) |
| 27 | |
| 28 | |
Andrea Frittoli | 4971fc8 | 2014-09-25 10:22:20 +0100 | [diff] [blame] | 29 | class TestSecurityGroupsBasicOps(manager.NetworkScenarioTest): |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 30 | |
| 31 | """ |
| 32 | This test suite assumes that Nova has been configured to |
| 33 | boot VM's with Neutron-managed networking, and attempts to |
| 34 | verify cross tenant connectivity as follows |
| 35 | |
| 36 | ssh: |
| 37 | in order to overcome "ip namespace", each tenant has an "access point" |
| 38 | VM with floating-ip open to incoming ssh connection allowing network |
| 39 | commands (ping/ssh) to be executed from within the |
| 40 | tenant-network-namespace |
| 41 | Tempest host performs key-based authentication to the ssh server via |
| 42 | floating IP address |
| 43 | |
| 44 | connectivity test is done by pinging destination server via source server |
| 45 | ssh connection. |
| 46 | success - ping returns |
| 47 | failure - ping_timeout reached |
| 48 | |
| 49 | setup: |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 50 | for primary tenant: |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 51 | 1. create a network&subnet |
| 52 | 2. create a router (if public router isn't configured) |
| 53 | 3. connect tenant network to public network via router |
| 54 | 4. create an access point: |
| 55 | a. a security group open to incoming ssh connection |
| 56 | b. a VM with a floating ip |
| 57 | 5. create a general empty security group (same as "default", but |
| 58 | without rules allowing in-tenant traffic) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 59 | |
| 60 | tests: |
| 61 | 1. _verify_network_details |
| 62 | 2. _verify_mac_addr: for each access point verify that |
| 63 | (subnet, fix_ip, mac address) are as defined in the port list |
| 64 | 3. _test_in_tenant_block: test that in-tenant traffic is disabled |
| 65 | without rules allowing it |
| 66 | 4. _test_in_tenant_allow: test that in-tenant traffic is enabled |
| 67 | once an appropriate rule has been created |
| 68 | 5. _test_cross_tenant_block: test that cross-tenant traffic is disabled |
| 69 | without a rule allowing it on destination tenant |
| 70 | 6. _test_cross_tenant_allow: |
| 71 | * test that cross-tenant traffic is enabled once an appropriate |
| 72 | rule has been created on destination tenant. |
| 73 | * test that reverse traffic is still blocked |
Fei Long Wang | 50131ee | 2015-02-02 16:58:24 +1300 | [diff] [blame] | 74 | * test than reverse traffic is enabled once an appropriate rule has |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 75 | been created on source tenant |
Rajkumar Thiyagarajan | d9e964a | 2014-12-17 01:55:52 -0800 | [diff] [blame] | 76 | 7._test_port_update_new_security_group: |
| 77 | * test that traffic is blocked with default security group |
| 78 | * test that traffic is enabled after updating port with new security |
| 79 | group having appropriate rule |
prdsilva | 8b733ad | 2014-12-09 02:54:49 -0800 | [diff] [blame] | 80 | 8. _test_multiple_security_groups: test multiple security groups can be |
| 81 | associated with the vm |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 82 | |
| 83 | assumptions: |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 84 | 1. alt_tenant/user existed and is different from primary_tenant/user |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 85 | 2. Public network is defined and reachable from the Tempest host |
| 86 | 3. Public router can either be: |
| 87 | * defined, in which case all tenants networks can connect directly |
| 88 | to it, and cross tenant check will be done on the private IP of the |
| 89 | destination tenant |
| 90 | or |
Fei Long Wang | 50131ee | 2015-02-02 16:58:24 +1300 | [diff] [blame] | 91 | * not defined (empty string), in which case each tenant will have |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 92 | its own router connected to the public network |
| 93 | """ |
| 94 | |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 95 | credentials = ['primary', 'alt', 'admin'] |
| 96 | |
Joe Gordon | 28788b4 | 2015-02-25 12:42:37 -0800 | [diff] [blame] | 97 | class TenantProperties(object): |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 98 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 99 | helper class to save tenant details |
| 100 | id |
| 101 | credentials |
| 102 | network |
| 103 | subnet |
| 104 | security groups |
| 105 | servers |
| 106 | access point |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 107 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 108 | |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 109 | def __init__(self, credentials): |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 110 | self.manager = clients.Manager(credentials) |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 111 | # Credentials from manager are filled with both names and IDs |
| 112 | self.creds = self.manager.credentials |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 113 | self.network = None |
| 114 | self.subnet = None |
| 115 | self.router = None |
| 116 | self.security_groups = {} |
| 117 | self.servers = list() |
| 118 | |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 119 | def set_network(self, network, subnet, router): |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 120 | self.network = network |
| 121 | self.subnet = subnet |
| 122 | self.router = router |
| 123 | |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 124 | @classmethod |
Emily Hugenbruch | 5e2d2a2 | 2015-02-25 21:35:45 +0000 | [diff] [blame] | 125 | def skip_checks(cls): |
| 126 | super(TestSecurityGroupsBasicOps, cls).skip_checks() |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 127 | if CONF.baremetal.driver_enabled: |
| 128 | msg = ('Not currently supported by baremetal.') |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 129 | raise cls.skipException(msg) |
Itzik Brown | 0695267 | 2015-03-29 12:38:58 +0300 | [diff] [blame] | 130 | if CONF.network.port_vnic_type in ['direct', 'macvtap']: |
| 131 | msg = ('Not currently supported when using vnic_type' |
| 132 | ' direct or macvtap') |
| 133 | raise cls.skipException(msg) |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 134 | if not (CONF.network.tenant_networks_reachable or |
| 135 | CONF.network.public_network_id): |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 136 | msg = ('Either tenant_networks_reachable must be "true", or ' |
| 137 | 'public_network_id must be defined.') |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 138 | raise cls.skipException(msg) |
| 139 | |
| 140 | @classmethod |
Emily Hugenbruch | 5e2d2a2 | 2015-02-25 21:35:45 +0000 | [diff] [blame] | 141 | def setup_credentials(cls): |
Yair Fried | 764610a | 2014-04-07 12:17:05 +0300 | [diff] [blame] | 142 | # Create no network resources for these tests. |
| 143 | cls.set_network_resources() |
Emily Hugenbruch | 5e2d2a2 | 2015-02-25 21:35:45 +0000 | [diff] [blame] | 144 | super(TestSecurityGroupsBasicOps, cls).setup_credentials() |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 145 | # TODO(mnewby) Consider looking up entities as needed instead |
| 146 | # of storing them as collections on the class. |
Yair Fried | 79b0a91 | 2014-10-20 11:15:37 +0300 | [diff] [blame] | 147 | |
Yair Fried | 79b0a91 | 2014-10-20 11:15:37 +0300 | [diff] [blame] | 148 | # Credentials from the manager are filled with both IDs and Names |
| 149 | cls.alt_creds = cls.alt_manager.credentials |
| 150 | |
Emily Hugenbruch | 5e2d2a2 | 2015-02-25 21:35:45 +0000 | [diff] [blame] | 151 | @classmethod |
| 152 | def resource_setup(cls): |
| 153 | super(TestSecurityGroupsBasicOps, cls).resource_setup() |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 154 | cls.floating_ips = {} |
| 155 | cls.tenants = {} |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 156 | creds = cls.manager.credentials |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 157 | cls.primary_tenant = cls.TenantProperties(creds) |
| 158 | cls.alt_tenant = cls.TenantProperties(cls.alt_creds) |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 159 | for tenant in [cls.primary_tenant, cls.alt_tenant]: |
Andrea Frittoli | 86ad28d | 2014-03-20 10:09:12 +0000 | [diff] [blame] | 160 | cls.tenants[tenant.creds.tenant_id] = tenant |
Emily Hugenbruch | 5e2d2a2 | 2015-02-25 21:35:45 +0000 | [diff] [blame] | 161 | |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 162 | cls.floating_ip_access = not CONF.network.public_router_id |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 163 | |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 164 | def cleanup_wrapper(self, resource): |
| 165 | self.cleanup_resource(resource, self.__class__.__name__) |
| 166 | |
| 167 | def setUp(self): |
| 168 | super(TestSecurityGroupsBasicOps, self).setUp() |
| 169 | self._deploy_tenant(self.primary_tenant) |
| 170 | self._verify_network_details(self.primary_tenant) |
| 171 | self._verify_mac_addr(self.primary_tenant) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 172 | |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 173 | def _create_tenant_keypairs(self, tenant): |
| 174 | keypair = self.create_keypair(tenant.manager.keypairs_client) |
| 175 | tenant.keypair = keypair |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 176 | |
| 177 | def _create_tenant_security_groups(self, tenant): |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 178 | access_sg = self._create_empty_security_group( |
| 179 | namestart='secgroup_access-', |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 180 | tenant_id=tenant.creds.tenant_id, |
| 181 | client=tenant.manager.network_client |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 182 | ) |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 183 | |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 184 | # don't use default secgroup since it allows in-tenant traffic |
| 185 | def_sg = self._create_empty_security_group( |
| 186 | namestart='secgroup_general-', |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 187 | tenant_id=tenant.creds.tenant_id, |
| 188 | client=tenant.manager.network_client |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 189 | ) |
| 190 | tenant.security_groups.update(access=access_sg, default=def_sg) |
| 191 | ssh_rule = dict( |
| 192 | protocol='tcp', |
| 193 | port_range_min=22, |
| 194 | port_range_max=22, |
| 195 | direction='ingress', |
| 196 | ) |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 197 | self._create_security_group_rule(secgroup=access_sg, |
| 198 | client=tenant.manager.network_client, |
| 199 | **ssh_rule) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 200 | |
| 201 | def _verify_network_details(self, tenant): |
| 202 | # Checks that we see the newly created network/subnet/router via |
| 203 | # checking the result of list_[networks,routers,subnets] |
| 204 | # Check that (router, subnet) couple exist in port_list |
| 205 | seen_nets = self._list_networks() |
| 206 | seen_names = [n['name'] for n in seen_nets] |
| 207 | seen_ids = [n['id'] for n in seen_nets] |
| 208 | |
| 209 | self.assertIn(tenant.network.name, seen_names) |
| 210 | self.assertIn(tenant.network.id, seen_ids) |
| 211 | |
| 212 | seen_subnets = [(n['id'], n['cidr'], n['network_id']) |
| 213 | for n in self._list_subnets()] |
| 214 | mysubnet = (tenant.subnet.id, tenant.subnet.cidr, tenant.network.id) |
| 215 | self.assertIn(mysubnet, seen_subnets) |
| 216 | |
| 217 | seen_routers = self._list_routers() |
| 218 | seen_router_ids = [n['id'] for n in seen_routers] |
| 219 | seen_router_names = [n['name'] for n in seen_routers] |
| 220 | |
| 221 | self.assertIn(tenant.router.name, seen_router_names) |
| 222 | self.assertIn(tenant.router.id, seen_router_ids) |
| 223 | |
| 224 | myport = (tenant.router.id, tenant.subnet.id) |
| 225 | router_ports = [(i['device_id'], i['fixed_ips'][0]['subnet_id']) for i |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 226 | in self._list_ports() |
armando-migliaccio | bcfbbeb | 2014-08-11 18:33:47 -0700 | [diff] [blame] | 227 | if self._is_router_port(i)] |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 228 | |
| 229 | self.assertIn(myport, router_ports) |
| 230 | |
armando-migliaccio | bcfbbeb | 2014-08-11 18:33:47 -0700 | [diff] [blame] | 231 | def _is_router_port(self, port): |
| 232 | """Return True if port is a router interface.""" |
| 233 | # NOTE(armando-migliaccio): match device owner for both centralized |
| 234 | # and distributed routers; 'device_owner' is "" by default. |
| 235 | return port['device_owner'].startswith('network:router_interface') |
| 236 | |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 237 | def _create_server(self, name, tenant, security_groups=None): |
| 238 | """ |
| 239 | creates a server and assigns to security group |
| 240 | """ |
| 241 | self._set_compute_context(tenant) |
| 242 | if security_groups is None: |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 243 | security_groups = [tenant.security_groups['default']] |
Ken'ichi Ohmichi | 1b3461e | 2014-12-02 03:41:07 +0000 | [diff] [blame] | 244 | security_groups_names = [{'name': s['name']} for s in security_groups] |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 245 | create_kwargs = { |
Dirk Mueller | 8cf7972 | 2014-09-12 17:37:15 +0200 | [diff] [blame] | 246 | 'networks': [ |
| 247 | {'uuid': tenant.network.id}, |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 248 | ], |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 249 | 'key_name': tenant.keypair['name'], |
Ken'ichi Ohmichi | 1b3461e | 2014-12-02 03:41:07 +0000 | [diff] [blame] | 250 | 'security_groups': security_groups_names, |
Itzik Brown | 2ca01cd | 2014-12-08 12:58:20 +0200 | [diff] [blame] | 251 | 'tenant_id': tenant.creds.tenant_id, |
| 252 | 'network_client': tenant.manager.network_client |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 253 | } |
Claudiu Belu | faa9891 | 2014-09-01 16:50:28 +0300 | [diff] [blame] | 254 | server = self.create_server(name=name, create_kwargs=create_kwargs) |
| 255 | self.assertEqual( |
| 256 | sorted([s['name'] for s in security_groups]), |
| 257 | sorted([s['name'] for s in server['security_groups']])) |
| 258 | return server |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 259 | |
| 260 | def _create_tenant_servers(self, tenant, num=1): |
| 261 | for i in range(num): |
Ken'ichi Ohmichi | 6ded8df | 2015-03-23 02:00:19 +0000 | [diff] [blame] | 262 | name = 'server-{tenant}-gen-{num}'.format( |
Andrea Frittoli | 86ad28d | 2014-03-20 10:09:12 +0000 | [diff] [blame] | 263 | tenant=tenant.creds.tenant_name, |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 264 | num=i |
| 265 | ) |
| 266 | name = data_utils.rand_name(name) |
| 267 | server = self._create_server(name, tenant) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 268 | tenant.servers.append(server) |
| 269 | |
| 270 | def _set_access_point(self, tenant): |
| 271 | """ |
| 272 | creates a server in a secgroup with rule allowing external ssh |
| 273 | in order to access tenant internal network |
| 274 | workaround ip namespace |
| 275 | """ |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 276 | secgroups = tenant.security_groups.values() |
Ken'ichi Ohmichi | 6ded8df | 2015-03-23 02:00:19 +0000 | [diff] [blame] | 277 | name = 'server-{tenant}-access_point'.format( |
Andrea Frittoli | 86ad28d | 2014-03-20 10:09:12 +0000 | [diff] [blame] | 278 | tenant=tenant.creds.tenant_name) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 279 | name = data_utils.rand_name(name) |
| 280 | server = self._create_server(name, tenant, |
| 281 | security_groups=secgroups) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 282 | tenant.access_point = server |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 283 | self._assign_floating_ips(tenant, server) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 284 | |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 285 | def _assign_floating_ips(self, tenant, server): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 286 | public_network_id = CONF.network.public_network_id |
Yair Fried | ae0e73d | 2014-11-24 11:56:26 +0200 | [diff] [blame] | 287 | floating_ip = self.create_floating_ip( |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 288 | server, public_network_id, |
| 289 | client=tenant.manager.network_client) |
| 290 | self.floating_ips.setdefault(server['id'], floating_ip) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 291 | |
| 292 | def _create_tenant_network(self, tenant): |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 293 | network, subnet, router = self.create_networks( |
| 294 | client=tenant.manager.network_client) |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 295 | tenant.set_network(network, subnet, router) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 296 | |
| 297 | def _set_compute_context(self, tenant): |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 298 | self.servers_client = tenant.manager.servers_client |
| 299 | return self.servers_client |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 300 | |
| 301 | def _deploy_tenant(self, tenant_or_id): |
| 302 | """ |
| 303 | creates: |
| 304 | network |
| 305 | subnet |
| 306 | router (if public not defined) |
| 307 | access security group |
| 308 | access-point server |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 309 | """ |
| 310 | if not isinstance(tenant_or_id, self.TenantProperties): |
| 311 | tenant = self.tenants[tenant_or_id] |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 312 | else: |
| 313 | tenant = tenant_or_id |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 314 | self._set_compute_context(tenant) |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 315 | self._create_tenant_keypairs(tenant) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 316 | self._create_tenant_network(tenant) |
| 317 | self._create_tenant_security_groups(tenant) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 318 | self._set_access_point(tenant) |
| 319 | |
| 320 | def _get_server_ip(self, server, floating=False): |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 321 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 322 | returns the ip (floating/internal) of a server |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 323 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 324 | if floating: |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 325 | server_ip = self.floating_ips[server['id']].floating_ip_address |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 326 | else: |
armando-migliaccio | d03f264 | 2014-02-21 19:55:50 -0800 | [diff] [blame] | 327 | server_ip = None |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 328 | network_name = self.tenants[server['tenant_id']].network.name |
| 329 | if network_name in server['addresses']: |
| 330 | server_ip = server['addresses'][network_name][0]['addr'] |
armando-migliaccio | d03f264 | 2014-02-21 19:55:50 -0800 | [diff] [blame] | 331 | return server_ip |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 332 | |
| 333 | def _connect_to_access_point(self, tenant): |
| 334 | """ |
| 335 | create ssh connection to tenant access point |
| 336 | """ |
| 337 | access_point_ssh = \ |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 338 | self.floating_ips[tenant.access_point['id']].floating_ip_address |
| 339 | private_key = tenant.keypair['private_key'] |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 340 | access_point_ssh = self._ssh_to_server(access_point_ssh, |
| 341 | private_key=private_key) |
| 342 | return access_point_ssh |
| 343 | |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 344 | def _check_connectivity(self, access_point, ip, should_succeed=True): |
| 345 | if should_succeed: |
| 346 | msg = "Timed out waiting for %s to become reachable" % ip |
| 347 | else: |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 348 | msg = "%s is reachable" % ip |
Matthew Treinish | 5348313 | 2014-12-09 18:50:06 -0500 | [diff] [blame] | 349 | self.assertTrue(self._check_remote_connectivity(access_point, ip, |
| 350 | should_succeed), msg) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 351 | |
| 352 | def _test_in_tenant_block(self, tenant): |
| 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), |
| 357 | should_succeed=False) |
| 358 | |
| 359 | def _test_in_tenant_allow(self, tenant): |
| 360 | ruleset = dict( |
| 361 | protocol='icmp', |
| 362 | remote_group_id=tenant.security_groups['default'].id, |
| 363 | direction='ingress' |
| 364 | ) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 365 | self._create_security_group_rule( |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 366 | secgroup=tenant.security_groups['default'], |
| 367 | **ruleset |
| 368 | ) |
| 369 | access_point_ssh = self._connect_to_access_point(tenant) |
| 370 | for server in tenant.servers: |
| 371 | self._check_connectivity(access_point=access_point_ssh, |
| 372 | ip=self._get_server_ip(server)) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 373 | |
| 374 | def _test_cross_tenant_block(self, source_tenant, dest_tenant): |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 375 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 376 | if public router isn't defined, then dest_tenant access is via |
| 377 | floating-ip |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 378 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 379 | access_point_ssh = self._connect_to_access_point(source_tenant) |
| 380 | ip = self._get_server_ip(dest_tenant.access_point, |
| 381 | floating=self.floating_ip_access) |
| 382 | self._check_connectivity(access_point=access_point_ssh, ip=ip, |
| 383 | should_succeed=False) |
| 384 | |
| 385 | def _test_cross_tenant_allow(self, source_tenant, dest_tenant): |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 386 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 387 | check for each direction: |
| 388 | creating rule for tenant incoming traffic enables only 1way traffic |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 389 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 390 | ruleset = dict( |
| 391 | protocol='icmp', |
| 392 | direction='ingress' |
| 393 | ) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 394 | self._create_security_group_rule( |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 395 | secgroup=dest_tenant.security_groups['default'], |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 396 | client=dest_tenant.manager.network_client, |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 397 | **ruleset |
| 398 | ) |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 399 | access_point_ssh = self._connect_to_access_point(source_tenant) |
| 400 | ip = self._get_server_ip(dest_tenant.access_point, |
| 401 | floating=self.floating_ip_access) |
| 402 | self._check_connectivity(access_point_ssh, ip) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 403 | |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 404 | # test that reverse traffic is still blocked |
| 405 | self._test_cross_tenant_block(dest_tenant, source_tenant) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 406 | |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 407 | # allow reverse traffic and check |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 408 | self._create_security_group_rule( |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 409 | secgroup=source_tenant.security_groups['default'], |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 410 | client=source_tenant.manager.network_client, |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 411 | **ruleset |
| 412 | ) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 413 | |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 414 | access_point_ssh_2 = self._connect_to_access_point(dest_tenant) |
| 415 | ip = self._get_server_ip(source_tenant.access_point, |
| 416 | floating=self.floating_ip_access) |
| 417 | self._check_connectivity(access_point_ssh_2, ip) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 418 | |
| 419 | def _verify_mac_addr(self, tenant): |
| 420 | """ |
| 421 | verify that VM (tenant's access point) has the same ip,mac as listed in |
| 422 | port list |
| 423 | """ |
| 424 | access_point_ssh = self._connect_to_access_point(tenant) |
| 425 | mac_addr = access_point_ssh.get_mac_address() |
| 426 | mac_addr = mac_addr.strip().lower() |
Henry Gessau | 78ab4b0 | 2014-03-31 15:10:13 -0400 | [diff] [blame] | 427 | # Get the fixed_ips and mac_address fields of all ports. Select |
| 428 | # only those two columns to reduce the size of the response. |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 429 | port_list = self._list_ports(fields=['fixed_ips', 'mac_address']) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 430 | port_detail_list = [ |
| 431 | (port['fixed_ips'][0]['subnet_id'], |
| 432 | port['fixed_ips'][0]['ip_address'], |
Henry Gessau | 78ab4b0 | 2014-03-31 15:10:13 -0400 | [diff] [blame] | 433 | port['mac_address'].lower()) |
| 434 | for port in port_list if port['fixed_ips'] |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 435 | ] |
| 436 | server_ip = self._get_server_ip(tenant.access_point) |
| 437 | subnet_id = tenant.subnet.id |
| 438 | self.assertIn((subnet_id, server_ip, mac_addr), port_detail_list) |
| 439 | |
Chris Hoge | 7579c1a | 2015-02-26 14:12:15 -0800 | [diff] [blame] | 440 | @test.idempotent_id('e79f879e-debb-440c-a7e4-efeda05b6848') |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 441 | @test.services('compute', 'network') |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 442 | def test_cross_tenant_traffic(self): |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 443 | if not self.credentials_provider.is_multi_tenant(): |
Yair Fried | 79b0a91 | 2014-10-20 11:15:37 +0300 | [diff] [blame] | 444 | raise self.skipException("No secondary tenant defined") |
Nachi Ueno | 26b4c97 | 2014-01-17 06:15:13 -0800 | [diff] [blame] | 445 | try: |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 446 | # deploy new tenant |
| 447 | self._deploy_tenant(self.alt_tenant) |
| 448 | self._verify_network_details(self.alt_tenant) |
| 449 | self._verify_mac_addr(self.alt_tenant) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 450 | |
Nachi Ueno | 26b4c97 | 2014-01-17 06:15:13 -0800 | [diff] [blame] | 451 | # cross tenant check |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 452 | source_tenant = self.primary_tenant |
Nachi Ueno | 26b4c97 | 2014-01-17 06:15:13 -0800 | [diff] [blame] | 453 | dest_tenant = self.alt_tenant |
| 454 | self._test_cross_tenant_block(source_tenant, dest_tenant) |
| 455 | self._test_cross_tenant_allow(source_tenant, dest_tenant) |
| 456 | except Exception: |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 457 | for tenant in self.tenants.values(): |
| 458 | self._log_console_output(servers=tenant.servers) |
| 459 | raise |
| 460 | |
Chris Hoge | 7579c1a | 2015-02-26 14:12:15 -0800 | [diff] [blame] | 461 | @test.idempotent_id('63163892-bbf6-4249-aa12-d5ea1f8f421b') |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 462 | @test.services('compute', 'network') |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 463 | def test_in_tenant_traffic(self): |
| 464 | try: |
| 465 | self._create_tenant_servers(self.primary_tenant, num=1) |
| 466 | |
| 467 | # in-tenant check |
| 468 | self._test_in_tenant_block(self.primary_tenant) |
| 469 | self._test_in_tenant_allow(self.primary_tenant) |
Rajkumar Thiyagarajan | d9e964a | 2014-12-17 01:55:52 -0800 | [diff] [blame] | 470 | except Exception: |
| 471 | for tenant in self.tenants.values(): |
| 472 | self._log_console_output(servers=tenant.servers) |
| 473 | raise |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 474 | |
Chris Hoge | 7579c1a | 2015-02-26 14:12:15 -0800 | [diff] [blame] | 475 | @test.idempotent_id('f4d556d7-1526-42ad-bafb-6bebf48568f6') |
Rajkumar Thiyagarajan | d9e964a | 2014-12-17 01:55:52 -0800 | [diff] [blame] | 476 | @test.services('compute', 'network') |
| 477 | def test_port_update_new_security_group(self): |
| 478 | """ |
| 479 | This test verifies the traffic after updating the vm port with new |
Fei Long Wang | 50131ee | 2015-02-02 16:58:24 +1300 | [diff] [blame] | 480 | security group having appropriate rule. |
Rajkumar Thiyagarajan | d9e964a | 2014-12-17 01:55:52 -0800 | [diff] [blame] | 481 | """ |
| 482 | new_tenant = self.primary_tenant |
| 483 | |
| 484 | # Create empty security group and add icmp rule in it |
| 485 | new_sg = self._create_empty_security_group( |
| 486 | namestart='secgroup_new-', |
| 487 | tenant_id=new_tenant.creds.tenant_id, |
| 488 | client=new_tenant.manager.network_client) |
| 489 | icmp_rule = dict( |
| 490 | protocol='icmp', |
| 491 | direction='ingress', |
| 492 | ) |
| 493 | self._create_security_group_rule( |
| 494 | secgroup=new_sg, |
| 495 | client=new_tenant.manager.network_client, |
| 496 | **icmp_rule) |
| 497 | new_tenant.security_groups.update(new_sg=new_sg) |
| 498 | |
| 499 | # Create server with default security group |
Ken'ichi Ohmichi | 6ded8df | 2015-03-23 02:00:19 +0000 | [diff] [blame] | 500 | name = 'server-{tenant}-gen-1'.format( |
Rajkumar Thiyagarajan | d9e964a | 2014-12-17 01:55:52 -0800 | [diff] [blame] | 501 | tenant=new_tenant.creds.tenant_name |
| 502 | ) |
| 503 | name = data_utils.rand_name(name) |
| 504 | server = self._create_server(name, new_tenant) |
| 505 | |
| 506 | # Check connectivity failure with default security group |
| 507 | try: |
| 508 | access_point_ssh = self._connect_to_access_point(new_tenant) |
| 509 | self._check_connectivity(access_point=access_point_ssh, |
| 510 | ip=self._get_server_ip(server), |
| 511 | should_succeed=False) |
| 512 | server_id = server['id'] |
| 513 | port_id = self._list_ports(device_id=server_id)[0]['id'] |
| 514 | |
| 515 | # update port with new security group and check connectivity |
| 516 | self.network_client.update_port(port_id, security_groups=[ |
| 517 | new_tenant.security_groups['new_sg'].id]) |
| 518 | self._check_connectivity( |
| 519 | access_point=access_point_ssh, |
| 520 | ip=self._get_server_ip(server)) |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 521 | except Exception: |
| 522 | for tenant in self.tenants.values(): |
| 523 | self._log_console_output(servers=tenant.servers) |
Nachi Ueno | 26b4c97 | 2014-01-17 06:15:13 -0800 | [diff] [blame] | 524 | raise |
prdsilva | 8b733ad | 2014-12-09 02:54:49 -0800 | [diff] [blame] | 525 | |
Chris Hoge | 7579c1a | 2015-02-26 14:12:15 -0800 | [diff] [blame] | 526 | @test.idempotent_id('d2f77418-fcc4-439d-b935-72eca704e293') |
prdsilva | 8b733ad | 2014-12-09 02:54:49 -0800 | [diff] [blame] | 527 | @test.services('compute', 'network') |
| 528 | def test_multiple_security_groups(self): |
| 529 | """ |
| 530 | This test verifies multiple security groups and checks that rules |
| 531 | provided in the both the groups is applied onto VM |
| 532 | """ |
| 533 | tenant = self.primary_tenant |
| 534 | ip = self._get_server_ip(tenant.access_point, |
| 535 | floating=self.floating_ip_access) |
| 536 | ssh_login = CONF.compute.image_ssh_user |
| 537 | private_key = tenant.keypair['private_key'] |
| 538 | self.check_vm_connectivity(ip, |
| 539 | should_connect=False) |
| 540 | ruleset = dict( |
| 541 | protocol='icmp', |
| 542 | direction='ingress' |
| 543 | ) |
| 544 | self._create_security_group_rule( |
| 545 | secgroup=tenant.security_groups['default'], |
| 546 | **ruleset |
| 547 | ) |
| 548 | """ |
| 549 | Vm now has 2 security groups one with ssh rule( |
| 550 | already added in setUp() method),and other with icmp rule |
| 551 | (added in the above step).The check_vm_connectivity tests |
| 552 | -that vm ping test is successful |
| 553 | -ssh to vm is successful |
| 554 | """ |
| 555 | self.check_vm_connectivity(ip, |
| 556 | username=ssh_login, |
| 557 | private_key=private_key, |
| 558 | should_connect=True) |