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 | |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 16 | from tempest import clients |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 17 | from tempest.common import debug |
| 18 | from tempest.common.utils import data_utils |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 19 | from tempest import config |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 20 | from tempest.openstack.common import log as logging |
| 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 |
| 74 | * test than revesre traffic is enabled once an appropriate rule has |
| 75 | been created on source tenant |
| 76 | |
| 77 | assumptions: |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 78 | 1. alt_tenant/user existed and is different from primary_tenant/user |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 79 | 2. Public network is defined and reachable from the Tempest host |
| 80 | 3. Public router can either be: |
| 81 | * defined, in which case all tenants networks can connect directly |
| 82 | to it, and cross tenant check will be done on the private IP of the |
| 83 | destination tenant |
| 84 | or |
| 85 | * not defined (empty string), in which case each tanant will have |
| 86 | its own router connected to the public network |
| 87 | """ |
| 88 | |
| 89 | class TenantProperties(): |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 90 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 91 | helper class to save tenant details |
| 92 | id |
| 93 | credentials |
| 94 | network |
| 95 | subnet |
| 96 | security groups |
| 97 | servers |
| 98 | access point |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 99 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 100 | |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 101 | def __init__(self, credentials): |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 102 | self.manager = clients.Manager(credentials) |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 103 | # Credentials from manager are filled with both names and IDs |
| 104 | self.creds = self.manager.credentials |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 105 | self.network = None |
| 106 | self.subnet = None |
| 107 | self.router = None |
| 108 | self.security_groups = {} |
| 109 | self.servers = list() |
| 110 | |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 111 | def set_network(self, network, subnet, router): |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 112 | self.network = network |
| 113 | self.subnet = subnet |
| 114 | self.router = router |
| 115 | |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 116 | @classmethod |
| 117 | def check_preconditions(cls): |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 118 | if CONF.baremetal.driver_enabled: |
| 119 | msg = ('Not currently supported by baremetal.') |
| 120 | cls.enabled = False |
| 121 | raise cls.skipException(msg) |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 122 | super(TestSecurityGroupsBasicOps, cls).check_preconditions() |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 123 | if not (CONF.network.tenant_networks_reachable or |
| 124 | CONF.network.public_network_id): |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 125 | msg = ('Either tenant_networks_reachable must be "true", or ' |
| 126 | 'public_network_id must be defined.') |
| 127 | cls.enabled = False |
| 128 | raise cls.skipException(msg) |
| 129 | |
| 130 | @classmethod |
Andrea Frittoli | ac20b5e | 2014-09-15 13:31:14 +0100 | [diff] [blame] | 131 | def resource_setup(cls): |
Yair Fried | 764610a | 2014-04-07 12:17:05 +0300 | [diff] [blame] | 132 | # Create no network resources for these tests. |
| 133 | cls.set_network_resources() |
Andrea Frittoli | ac20b5e | 2014-09-15 13:31:14 +0100 | [diff] [blame] | 134 | super(TestSecurityGroupsBasicOps, cls).resource_setup() |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 135 | # TODO(mnewby) Consider looking up entities as needed instead |
| 136 | # of storing them as collections on the class. |
Yair Fried | 79b0a91 | 2014-10-20 11:15:37 +0300 | [diff] [blame^] | 137 | |
| 138 | # get credentials for secondary tenant |
| 139 | cls.alt_creds = cls.isolated_creds.get_alt_creds() |
| 140 | cls.alt_manager = clients.Manager(cls.alt_creds) |
| 141 | # Credentials from the manager are filled with both IDs and Names |
| 142 | cls.alt_creds = cls.alt_manager.credentials |
| 143 | |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 144 | cls.floating_ips = {} |
| 145 | cls.tenants = {} |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 146 | creds = cls.credentials() |
| 147 | cls.primary_tenant = cls.TenantProperties(creds) |
| 148 | cls.alt_tenant = cls.TenantProperties(cls.alt_creds) |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 149 | for tenant in [cls.primary_tenant, cls.alt_tenant]: |
Andrea Frittoli | 86ad28d | 2014-03-20 10:09:12 +0000 | [diff] [blame] | 150 | cls.tenants[tenant.creds.tenant_id] = tenant |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 151 | cls.floating_ip_access = not CONF.network.public_router_id |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 152 | |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 153 | def cleanup_wrapper(self, resource): |
| 154 | self.cleanup_resource(resource, self.__class__.__name__) |
| 155 | |
| 156 | def setUp(self): |
| 157 | super(TestSecurityGroupsBasicOps, self).setUp() |
| 158 | self._deploy_tenant(self.primary_tenant) |
| 159 | self._verify_network_details(self.primary_tenant) |
| 160 | self._verify_mac_addr(self.primary_tenant) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 161 | |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 162 | def _create_tenant_keypairs(self, tenant): |
| 163 | keypair = self.create_keypair(tenant.manager.keypairs_client) |
| 164 | tenant.keypair = keypair |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 165 | |
| 166 | def _create_tenant_security_groups(self, tenant): |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 167 | access_sg = self._create_empty_security_group( |
| 168 | namestart='secgroup_access-', |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 169 | tenant_id=tenant.creds.tenant_id, |
| 170 | client=tenant.manager.network_client |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 171 | ) |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 172 | |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 173 | # don't use default secgroup since it allows in-tenant traffic |
| 174 | def_sg = self._create_empty_security_group( |
| 175 | namestart='secgroup_general-', |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 176 | tenant_id=tenant.creds.tenant_id, |
| 177 | client=tenant.manager.network_client |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 178 | ) |
| 179 | tenant.security_groups.update(access=access_sg, default=def_sg) |
| 180 | ssh_rule = dict( |
| 181 | protocol='tcp', |
| 182 | port_range_min=22, |
| 183 | port_range_max=22, |
| 184 | direction='ingress', |
| 185 | ) |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 186 | self._create_security_group_rule(secgroup=access_sg, |
| 187 | client=tenant.manager.network_client, |
| 188 | **ssh_rule) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 189 | |
| 190 | def _verify_network_details(self, tenant): |
| 191 | # Checks that we see the newly created network/subnet/router via |
| 192 | # checking the result of list_[networks,routers,subnets] |
| 193 | # Check that (router, subnet) couple exist in port_list |
| 194 | seen_nets = self._list_networks() |
| 195 | seen_names = [n['name'] for n in seen_nets] |
| 196 | seen_ids = [n['id'] for n in seen_nets] |
| 197 | |
| 198 | self.assertIn(tenant.network.name, seen_names) |
| 199 | self.assertIn(tenant.network.id, seen_ids) |
| 200 | |
| 201 | seen_subnets = [(n['id'], n['cidr'], n['network_id']) |
| 202 | for n in self._list_subnets()] |
| 203 | mysubnet = (tenant.subnet.id, tenant.subnet.cidr, tenant.network.id) |
| 204 | self.assertIn(mysubnet, seen_subnets) |
| 205 | |
| 206 | seen_routers = self._list_routers() |
| 207 | seen_router_ids = [n['id'] for n in seen_routers] |
| 208 | seen_router_names = [n['name'] for n in seen_routers] |
| 209 | |
| 210 | self.assertIn(tenant.router.name, seen_router_names) |
| 211 | self.assertIn(tenant.router.id, seen_router_ids) |
| 212 | |
| 213 | myport = (tenant.router.id, tenant.subnet.id) |
| 214 | 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] | 215 | in self._list_ports() |
armando-migliaccio | bcfbbeb | 2014-08-11 18:33:47 -0700 | [diff] [blame] | 216 | if self._is_router_port(i)] |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 217 | |
| 218 | self.assertIn(myport, router_ports) |
| 219 | |
armando-migliaccio | bcfbbeb | 2014-08-11 18:33:47 -0700 | [diff] [blame] | 220 | def _is_router_port(self, port): |
| 221 | """Return True if port is a router interface.""" |
| 222 | # NOTE(armando-migliaccio): match device owner for both centralized |
| 223 | # and distributed routers; 'device_owner' is "" by default. |
| 224 | return port['device_owner'].startswith('network:router_interface') |
| 225 | |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 226 | def _create_server(self, name, tenant, security_groups=None): |
| 227 | """ |
| 228 | creates a server and assigns to security group |
| 229 | """ |
| 230 | self._set_compute_context(tenant) |
| 231 | if security_groups is None: |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 232 | security_groups = [tenant.security_groups['default']] |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 233 | create_kwargs = { |
Dirk Mueller | 8cf7972 | 2014-09-12 17:37:15 +0200 | [diff] [blame] | 234 | 'networks': [ |
| 235 | {'uuid': tenant.network.id}, |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 236 | ], |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 237 | 'key_name': tenant.keypair['name'], |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 238 | 'security_groups': security_groups, |
Andrea Frittoli | 86ad28d | 2014-03-20 10:09:12 +0000 | [diff] [blame] | 239 | 'tenant_id': tenant.creds.tenant_id |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 240 | } |
Claudiu Belu | faa9891 | 2014-09-01 16:50:28 +0300 | [diff] [blame] | 241 | server = self.create_server(name=name, create_kwargs=create_kwargs) |
| 242 | self.assertEqual( |
| 243 | sorted([s['name'] for s in security_groups]), |
| 244 | sorted([s['name'] for s in server['security_groups']])) |
| 245 | return server |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 246 | |
| 247 | def _create_tenant_servers(self, tenant, num=1): |
| 248 | for i in range(num): |
| 249 | name = 'server-{tenant}-gen-{num}-'.format( |
Andrea Frittoli | 86ad28d | 2014-03-20 10:09:12 +0000 | [diff] [blame] | 250 | tenant=tenant.creds.tenant_name, |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 251 | num=i |
| 252 | ) |
| 253 | name = data_utils.rand_name(name) |
| 254 | server = self._create_server(name, tenant) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 255 | tenant.servers.append(server) |
| 256 | |
| 257 | def _set_access_point(self, tenant): |
| 258 | """ |
| 259 | creates a server in a secgroup with rule allowing external ssh |
| 260 | in order to access tenant internal network |
| 261 | workaround ip namespace |
| 262 | """ |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 263 | secgroups = tenant.security_groups.values() |
Andrea Frittoli | 86ad28d | 2014-03-20 10:09:12 +0000 | [diff] [blame] | 264 | name = 'server-{tenant}-access_point-'.format( |
| 265 | tenant=tenant.creds.tenant_name) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 266 | name = data_utils.rand_name(name) |
| 267 | server = self._create_server(name, tenant, |
| 268 | security_groups=secgroups) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 269 | tenant.access_point = server |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 270 | self._assign_floating_ips(tenant, server) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 271 | |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 272 | def _assign_floating_ips(self, tenant, server): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 273 | public_network_id = CONF.network.public_network_id |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 274 | floating_ip = self._create_floating_ip( |
| 275 | server, public_network_id, |
| 276 | client=tenant.manager.network_client) |
| 277 | self.floating_ips.setdefault(server['id'], floating_ip) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 278 | |
| 279 | def _create_tenant_network(self, tenant): |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 280 | network, subnet, router = self.create_networks( |
| 281 | client=tenant.manager.network_client) |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 282 | tenant.set_network(network, subnet, router) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 283 | |
| 284 | def _set_compute_context(self, tenant): |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 285 | self.servers_client = tenant.manager.servers_client |
| 286 | return self.servers_client |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 287 | |
| 288 | def _deploy_tenant(self, tenant_or_id): |
| 289 | """ |
| 290 | creates: |
| 291 | network |
| 292 | subnet |
| 293 | router (if public not defined) |
| 294 | access security group |
| 295 | access-point server |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 296 | """ |
| 297 | if not isinstance(tenant_or_id, self.TenantProperties): |
| 298 | tenant = self.tenants[tenant_or_id] |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 299 | else: |
| 300 | tenant = tenant_or_id |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 301 | self._set_compute_context(tenant) |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 302 | self._create_tenant_keypairs(tenant) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 303 | self._create_tenant_network(tenant) |
| 304 | self._create_tenant_security_groups(tenant) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 305 | self._set_access_point(tenant) |
| 306 | |
| 307 | def _get_server_ip(self, server, floating=False): |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 308 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 309 | returns the ip (floating/internal) of a server |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 310 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 311 | if floating: |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 312 | server_ip = self.floating_ips[server['id']].floating_ip_address |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 313 | else: |
armando-migliaccio | d03f264 | 2014-02-21 19:55:50 -0800 | [diff] [blame] | 314 | server_ip = None |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 315 | network_name = self.tenants[server['tenant_id']].network.name |
| 316 | if network_name in server['addresses']: |
| 317 | server_ip = server['addresses'][network_name][0]['addr'] |
armando-migliaccio | d03f264 | 2014-02-21 19:55:50 -0800 | [diff] [blame] | 318 | return server_ip |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 319 | |
| 320 | def _connect_to_access_point(self, tenant): |
| 321 | """ |
| 322 | create ssh connection to tenant access point |
| 323 | """ |
| 324 | access_point_ssh = \ |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 325 | self.floating_ips[tenant.access_point['id']].floating_ip_address |
| 326 | private_key = tenant.keypair['private_key'] |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 327 | access_point_ssh = self._ssh_to_server(access_point_ssh, |
| 328 | private_key=private_key) |
| 329 | return access_point_ssh |
| 330 | |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 331 | def _check_connectivity(self, access_point, ip, should_succeed=True): |
| 332 | if should_succeed: |
| 333 | msg = "Timed out waiting for %s to become reachable" % ip |
| 334 | else: |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 335 | msg = "%s is reachable" % ip |
| 336 | try: |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 337 | self.assertTrue(self._check_remote_connectivity(access_point, ip, |
| 338 | should_succeed), |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 339 | msg) |
Yair Fried | 3960c4d | 2014-05-07 15:20:30 +0300 | [diff] [blame] | 340 | except test.exceptions.SSHTimeout: |
| 341 | raise |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 342 | except Exception: |
Attila Fazekas | 6bfd649 | 2014-02-26 21:25:53 +0100 | [diff] [blame] | 343 | debug.log_net_debug() |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 344 | raise |
| 345 | |
| 346 | def _test_in_tenant_block(self, tenant): |
| 347 | access_point_ssh = self._connect_to_access_point(tenant) |
| 348 | for server in tenant.servers: |
| 349 | self._check_connectivity(access_point=access_point_ssh, |
| 350 | ip=self._get_server_ip(server), |
| 351 | should_succeed=False) |
| 352 | |
| 353 | def _test_in_tenant_allow(self, tenant): |
| 354 | ruleset = dict( |
| 355 | protocol='icmp', |
| 356 | remote_group_id=tenant.security_groups['default'].id, |
| 357 | direction='ingress' |
| 358 | ) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 359 | self._create_security_group_rule( |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 360 | secgroup=tenant.security_groups['default'], |
| 361 | **ruleset |
| 362 | ) |
| 363 | access_point_ssh = self._connect_to_access_point(tenant) |
| 364 | for server in tenant.servers: |
| 365 | self._check_connectivity(access_point=access_point_ssh, |
| 366 | ip=self._get_server_ip(server)) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 367 | |
| 368 | def _test_cross_tenant_block(self, source_tenant, dest_tenant): |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 369 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 370 | if public router isn't defined, then dest_tenant access is via |
| 371 | floating-ip |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 372 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 373 | access_point_ssh = self._connect_to_access_point(source_tenant) |
| 374 | ip = self._get_server_ip(dest_tenant.access_point, |
| 375 | floating=self.floating_ip_access) |
| 376 | self._check_connectivity(access_point=access_point_ssh, ip=ip, |
| 377 | should_succeed=False) |
| 378 | |
| 379 | def _test_cross_tenant_allow(self, source_tenant, dest_tenant): |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 380 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 381 | check for each direction: |
| 382 | creating rule for tenant incoming traffic enables only 1way traffic |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 383 | """ |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 384 | ruleset = dict( |
| 385 | protocol='icmp', |
| 386 | direction='ingress' |
| 387 | ) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 388 | self._create_security_group_rule( |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 389 | secgroup=dest_tenant.security_groups['default'], |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 390 | client=dest_tenant.manager.network_client, |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 391 | **ruleset |
| 392 | ) |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 393 | access_point_ssh = self._connect_to_access_point(source_tenant) |
| 394 | ip = self._get_server_ip(dest_tenant.access_point, |
| 395 | floating=self.floating_ip_access) |
| 396 | self._check_connectivity(access_point_ssh, ip) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 397 | |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 398 | # test that reverse traffic is still blocked |
| 399 | self._test_cross_tenant_block(dest_tenant, source_tenant) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 400 | |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 401 | # allow reverse traffic and check |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 402 | self._create_security_group_rule( |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 403 | secgroup=source_tenant.security_groups['default'], |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 404 | client=source_tenant.manager.network_client, |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 405 | **ruleset |
| 406 | ) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 407 | |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 408 | access_point_ssh_2 = self._connect_to_access_point(dest_tenant) |
| 409 | ip = self._get_server_ip(source_tenant.access_point, |
| 410 | floating=self.floating_ip_access) |
| 411 | self._check_connectivity(access_point_ssh_2, ip) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 412 | |
| 413 | def _verify_mac_addr(self, tenant): |
| 414 | """ |
| 415 | verify that VM (tenant's access point) has the same ip,mac as listed in |
| 416 | port list |
| 417 | """ |
| 418 | access_point_ssh = self._connect_to_access_point(tenant) |
| 419 | mac_addr = access_point_ssh.get_mac_address() |
| 420 | mac_addr = mac_addr.strip().lower() |
Henry Gessau | 78ab4b0 | 2014-03-31 15:10:13 -0400 | [diff] [blame] | 421 | # Get the fixed_ips and mac_address fields of all ports. Select |
| 422 | # only those two columns to reduce the size of the response. |
Yair Fried | db6c9e9 | 2014-08-06 08:53:13 +0300 | [diff] [blame] | 423 | port_list = self._list_ports(fields=['fixed_ips', 'mac_address']) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 424 | port_detail_list = [ |
| 425 | (port['fixed_ips'][0]['subnet_id'], |
| 426 | port['fixed_ips'][0]['ip_address'], |
Henry Gessau | 78ab4b0 | 2014-03-31 15:10:13 -0400 | [diff] [blame] | 427 | port['mac_address'].lower()) |
| 428 | for port in port_list if port['fixed_ips'] |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 429 | ] |
| 430 | server_ip = self._get_server_ip(tenant.access_point) |
| 431 | subnet_id = tenant.subnet.id |
| 432 | self.assertIn((subnet_id, server_ip, mac_addr), port_detail_list) |
| 433 | |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 434 | @test.attr(type='smoke') |
| 435 | @test.services('compute', 'network') |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 436 | def test_cross_tenant_traffic(self): |
Yair Fried | 79b0a91 | 2014-10-20 11:15:37 +0300 | [diff] [blame^] | 437 | if not self.isolated_creds.is_multi_tenant(): |
| 438 | raise self.skipException("No secondary tenant defined") |
Nachi Ueno | 26b4c97 | 2014-01-17 06:15:13 -0800 | [diff] [blame] | 439 | try: |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 440 | # deploy new tenant |
| 441 | self._deploy_tenant(self.alt_tenant) |
| 442 | self._verify_network_details(self.alt_tenant) |
| 443 | self._verify_mac_addr(self.alt_tenant) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 444 | |
Nachi Ueno | 26b4c97 | 2014-01-17 06:15:13 -0800 | [diff] [blame] | 445 | # cross tenant check |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 446 | source_tenant = self.primary_tenant |
Nachi Ueno | 26b4c97 | 2014-01-17 06:15:13 -0800 | [diff] [blame] | 447 | dest_tenant = self.alt_tenant |
| 448 | self._test_cross_tenant_block(source_tenant, dest_tenant) |
| 449 | self._test_cross_tenant_allow(source_tenant, dest_tenant) |
| 450 | except Exception: |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 451 | for tenant in self.tenants.values(): |
| 452 | self._log_console_output(servers=tenant.servers) |
| 453 | raise |
| 454 | |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 455 | @test.attr(type='smoke') |
| 456 | @test.services('compute', 'network') |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 457 | def test_in_tenant_traffic(self): |
| 458 | try: |
| 459 | self._create_tenant_servers(self.primary_tenant, num=1) |
| 460 | |
| 461 | # in-tenant check |
| 462 | self._test_in_tenant_block(self.primary_tenant) |
| 463 | self._test_in_tenant_allow(self.primary_tenant) |
| 464 | |
| 465 | except Exception: |
| 466 | for tenant in self.tenants.values(): |
| 467 | self._log_console_output(servers=tenant.servers) |
Nachi Ueno | 26b4c97 | 2014-01-17 06:15:13 -0800 | [diff] [blame] | 468 | raise |