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