| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import copy |
| 17 |
|
| 18 |
import netaddr |
| 19 |
|
| 20 |
from neutron_lib.api.definitions import constants as api_const |
| 21 |
from neutron_lib import constants as nl_constants |
| 22 |
from neutron_lib import context as lib_context |
| 23 |
from neutron_lib.db import api as db_api |
| 24 |
from neutron_lib.db import constants as db_constants |
| 25 |
from neutron_lib.db import model_base |
| 26 |
from neutron_lib.db import model_query |
| 27 |
from neutron_lib.db import resource_extend |
| 28 |
from neutron_lib.db import standard_attr |
| 29 |
from neutron_lib.db import utils as db_utils |
| 30 |
from neutron_lib import exceptions |
| 31 |
from neutron_lib.exceptions import firewall_v2 as f_exc |
| 32 |
from oslo_config import cfg |
| 33 |
from oslo_db import exception as db_exc |
| 34 |
from oslo_log import log as logging |
| 35 |
from oslo_utils import uuidutils |
| 36 |
import sqlalchemy as sa |
| 37 |
from sqlalchemy.ext.orderinglist import ordering_list |
| 38 |
from sqlalchemy import or_ |
| 39 |
from sqlalchemy import orm |
| 40 |
from sqlalchemy.orm import exc |
| 41 |
|
| 42 |
from neutron_fwaas.common import fwaas_constants as const |
| 43 |
|
| 44 |
|
| 45 |
LOG = logging.getLogger(__name__) |
| 46 |
|
| 47 |
|
| 48 |
class FirewallDefaultParameterExists(exceptions.InUse): |
| 49 |
"""Default Firewall Parameter conflict exception |
| 50 |
|
| 51 |
Occurs when user creates/updates any existing firewall resource with |
| 52 |
reserved parameter names. |
| 53 |
""" |
| 54 |
message = ("Operation cannot be performed since '%(name)s' " |
| 55 |
"is a reserved name for %(resource_type)s.") |
| 56 |
|
| 57 |
|
| 58 |
class FirewallDefaultObjectUpdateRestricted(FirewallDefaultParameterExists): |
| 59 |
message = ("Operation cannot be performed on default object " |
| 60 |
"'%(resource_id)s' of type %(resource_type)s.") |
| 61 |
|
| 62 |
|
| 63 |
class HasName: |
| 64 |
name = sa.Column(sa.String(db_constants.NAME_FIELD_SIZE)) |
| 65 |
|
| 66 |
|
| 67 |
class HasDescription: |
| 68 |
description = sa.Column( |
| 69 |
sa.String(db_constants.LONG_DESCRIPTION_FIELD_SIZE)) |
| 70 |
|
| 71 |
|
| 72 |
class FirewallRuleV2(standard_attr.HasStandardAttributes, model_base.BASEV2, |
| 73 |
model_base.HasId, HasName, model_base.HasProject): |
| 74 |
__tablename__ = "firewall_rules_v2" |
| 75 |
shared = sa.Column(sa.Boolean) |
| 76 |
protocol = sa.Column(sa.String(40)) |
| 77 |
ip_version = sa.Column(sa.Integer) |
| 78 |
source_ip_address = sa.Column(sa.String(46)) |
| 79 |
destination_ip_address = sa.Column(sa.String(46)) |
| 80 |
source_port_range_min = sa.Column(sa.Integer) |
| 81 |
source_port_range_max = sa.Column(sa.Integer) |
| 82 |
destination_port_range_min = sa.Column(sa.Integer) |
| 83 |
destination_port_range_max = sa.Column(sa.Integer) |
| 84 |
action = sa.Column(sa.Enum(*api_const.FW_VALID_ACTION_VALUES, |
| 85 |
name='firewallrules_action')) |
| 86 |
enabled = sa.Column(sa.Boolean) |
| 87 |
api_collections = ['firewall_rules'] |
| 88 |
collection_resource_map = {"firewall_rules": "firewall_rule"} |
| 89 |
tag_support = True |
| 90 |
|
| 91 |
|
| 92 |
class FirewallGroup(standard_attr.HasStandardAttributes, model_base.BASEV2, |
| 93 |
model_base.HasId, HasName, model_base.HasProject): |
| 94 |
__tablename__ = 'firewall_groups_v2' |
| 95 |
port_associations = orm.relationship( |
| 96 |
'FirewallGroupPortAssociation', |
| 97 |
backref=orm.backref('firewall_group_port_associations_v2', |
| 98 |
cascade='all, delete')) |
| 99 |
name = sa.Column(sa.String(db_constants.NAME_FIELD_SIZE)) |
| 100 |
ingress_firewall_policy_id = sa.Column( |
| 101 |
sa.String(db_constants.UUID_FIELD_SIZE), |
| 102 |
sa.ForeignKey('firewall_policies_v2.id')) |
| 103 |
egress_firewall_policy_id = sa.Column( |
| 104 |
sa.String(db_constants.UUID_FIELD_SIZE), |
| 105 |
sa.ForeignKey('firewall_policies_v2.id')) |
| 106 |
admin_state_up = sa.Column(sa.Boolean) |
| 107 |
status = sa.Column(sa.String(db_constants.STATUS_FIELD_SIZE)) |
| 108 |
shared = sa.Column(sa.Boolean) |
| 109 |
api_collections = ['firewall_groups'] |
| 110 |
collection_resource_map = {"firewall_groups": "firewall_group"} |
| 111 |
tag_support = True |
| 112 |
|
| 113 |
|
| 114 |
class DefaultFirewallGroup(model_base.BASEV2, model_base.HasProjectPrimaryKey): |
| 115 |
__tablename__ = "default_firewall_groups" |
| 116 |
firewall_group_id = sa.Column(sa.String(db_constants.UUID_FIELD_SIZE), |
| 117 |
sa.ForeignKey('firewall_groups_v2.id', |
| 118 |
ondelete="CASCADE"), |
| 119 |
nullable=False) |
| 120 |
firewall_group = orm.relationship( |
| 121 |
FirewallGroup, lazy='joined', |
| 122 |
backref=orm.backref('default_firewall_group', cascade='all,delete'), |
| 123 |
primaryjoin="FirewallGroup.id==DefaultFirewallGroup.firewall_group_id", |
| 124 |
) |
| 125 |
|
| 126 |
|
| 127 |
class FirewallGroupPortAssociation(model_base.BASEV2): |
| 128 |
__tablename__ = 'firewall_group_port_associations_v2' |
| 129 |
firewall_group_id = sa.Column(sa.String(db_constants.UUID_FIELD_SIZE), |
| 130 |
sa.ForeignKey('firewall_groups_v2.id', |
| 131 |
ondelete="CASCADE"), |
| 132 |
primary_key=True) |
| 133 |
port_id = sa.Column(sa.String(db_constants.UUID_FIELD_SIZE), |
| 134 |
sa.ForeignKey('ports.id', ondelete="CASCADE"), |
| 135 |
unique=True, |
| 136 |
primary_key=True) |
| 137 |
|
| 138 |
|
| 139 |
class FirewallPolicyRuleAssociation(model_base.BASEV2): |
| 140 |
|
| 141 |
"""Tracks FW Policy and Rule(s) Association""" |
| 142 |
|
| 143 |
__tablename__ = 'firewall_policy_rule_associations_v2' |
| 144 |
|
| 145 |
firewall_policy_id = sa.Column(sa.String(db_constants.UUID_FIELD_SIZE), |
| 146 |
sa.ForeignKey('firewall_policies_v2.id', |
| 147 |
ondelete="CASCADE"), |
| 148 |
primary_key=True) |
| 149 |
firewall_rule_id = sa.Column(sa.String(db_constants.UUID_FIELD_SIZE), |
| 150 |
sa.ForeignKey('firewall_rules_v2.id', |
| 151 |
ondelete="CASCADE"), |
| 152 |
primary_key=True) |
| 153 |
position = sa.Column(sa.Integer) |
| 154 |
|
| 155 |
|
| 156 |
class FirewallPolicy(standard_attr.HasStandardAttributes, model_base.BASEV2, |
| 157 |
model_base.HasId, HasName, model_base.HasProject): |
| 158 |
__tablename__ = 'firewall_policies_v2' |
| 159 |
name = sa.Column(sa.String(db_constants.NAME_FIELD_SIZE)) |
| 160 |
rule_count = sa.Column(sa.Integer) |
| 161 |
audited = sa.Column(sa.Boolean) |
| 162 |
rule_associations = orm.relationship( |
| 163 |
FirewallPolicyRuleAssociation, |
| 164 |
backref=orm.backref('firewall_policies_v2', cascade='all, delete'), |
| 165 |
order_by='FirewallPolicyRuleAssociation.position', |
| 166 |
collection_class=ordering_list('position', count_from=1)) |
| 167 |
shared = sa.Column(sa.Boolean) |
| 168 |
api_collections = ['firewall_policies'] |
| 169 |
collection_resource_map = {"firewall_policies": "firewall_policy"} |
| 170 |
tag_support = True |
| 171 |
|
| 172 |
|
| 173 |
def _list_firewall_groups_result_filter_hook(query, filters): |
| 174 |
values = filters and filters.get('ports', []) |
| 175 |
if values: |
| 176 |
query = query.join(FirewallGroupPortAssociation) |
| 177 |
query = query.filter(FirewallGroupPortAssociation.port_id.in_(values)) |
| 178 |
|
| 179 |
return query |
| 180 |
|
| 181 |
|
| 182 |
def _list_firewall_policies_result_filter_hook(query, filters): |
| 183 |
values = filters and filters.get('firewall_rules', []) |
| 184 |
if values: |
| 185 |
query = query.join(FirewallPolicyRuleAssociation) |
| 186 |
query = query.filter( |
| 187 |
FirewallPolicyRuleAssociation.firewall_rule_id.in_(values)) |
| 188 |
|
| 189 |
return query |
| 190 |
|
| 191 |
|
| 192 |
class FirewallPluginDb: |
| 193 |
|
| 194 |
def __new__(cls, *args, **kwargs): |
| 195 |
model_query.register_hook( |
| 196 |
FirewallGroup, |
| 197 |
"firewall_group_v2_filter_by_port_association", |
| 198 |
query_hook=None, |
| 199 |
filter_hook=None, |
| 200 |
result_filters=_list_firewall_groups_result_filter_hook) |
| 201 |
|
| 202 |
model_query.register_hook( |
| 203 |
FirewallPolicy, |
| 204 |
"firewall_policy_v2_filter_by_firewall_rule_association", |
| 205 |
query_hook=None, |
| 206 |
filter_hook=None, |
| 207 |
result_filters=_list_firewall_policies_result_filter_hook) |
| 208 |
return super().__new__(cls, *args, **kwargs) |
| 209 |
|
| 210 |
@db_api.CONTEXT_READER |
| 211 |
def _get_firewall_group(self, context, id): |
| 212 |
try: |
| 213 |
return model_query.get_by_id(context, FirewallGroup, id) |
| 214 |
except exc.NoResultFound: |
| 215 |
raise f_exc.FirewallGroupNotFound(firewall_id=id) |
| 216 |
|
| 217 |
@db_api.CONTEXT_READER |
| 218 |
def _get_firewall_policy(self, context, id): |
| 219 |
try: |
| 220 |
return model_query.get_by_id(context, FirewallPolicy, id) |
| 221 |
except exc.NoResultFound: |
| 222 |
raise f_exc.FirewallPolicyNotFound(firewall_policy_id=id) |
| 223 |
|
| 224 |
@db_api.CONTEXT_READER |
| 225 |
def _get_firewall_rule(self, context, id): |
| 226 |
try: |
| 227 |
return model_query.get_by_id(context, FirewallRuleV2, id) |
| 228 |
except exc.NoResultFound: |
| 229 |
raise f_exc.FirewallRuleNotFound(firewall_rule_id=id) |
| 230 |
|
| 231 |
def _validate_fwr_protocol_parameters(self, fwr): |
| 232 |
protocol = fwr['protocol'] |
| 233 |
source_port = fwr['source_port'] |
| 234 |
dest_port = fwr['destination_port'] |
| 235 |
|
| 236 |
if protocol and protocol not in (nl_constants.PROTO_NAME_TCP, |
| 237 |
nl_constants.PROTO_NAME_UDP): |
| 238 |
if source_port or dest_port: |
| 239 |
raise f_exc.FirewallRuleInvalidICMPParameter( |
| 240 |
param="Source, destination port") |
| 241 |
|
| 242 |
if not protocol and (source_port or dest_port): |
| 243 |
raise f_exc.FirewallRuleWithPortWithoutProtocolInvalid() |
| 244 |
|
| 245 |
def _validate_fwr_src_dst_ip_version(self, fwr, fwr_db=None): |
| 246 |
src_version = dst_version = None |
| 247 |
if fwr.get('source_ip_address', None): |
| 248 |
src_version = netaddr.IPNetwork(fwr['source_ip_address']).version |
| 249 |
if fwr.get('destination_ip_address', None): |
| 250 |
dst_version = netaddr.IPNetwork( |
| 251 |
fwr['destination_ip_address']).version |
| 252 |
rule_ip_version = fwr.get('ip_version', None) |
| 253 |
if not rule_ip_version and fwr_db: |
| 254 |
rule_ip_version = fwr_db.ip_version |
| 255 |
if ((src_version and src_version != rule_ip_version) or |
| 256 |
(dst_version and dst_version != rule_ip_version)): |
| 257 |
raise f_exc.FirewallIpAddressConflict() |
| 258 |
|
| 259 |
def _validate_fwr_port_range(self, min_port, max_port): |
| 260 |
if int(min_port) > int(max_port): |
| 261 |
port_range = '{}:{}'.format(min_port, max_port) |
| 262 |
raise f_exc.FirewallRuleInvalidPortValue(port=port_range) |
| 263 |
|
| 264 |
def _get_min_max_ports_from_range(self, port_range): |
| 265 |
if not port_range: |
| 266 |
return [None, None] |
| 267 |
min_port, sep, max_port = port_range.partition(":") |
| 268 |
if not max_port: |
| 269 |
max_port = min_port |
| 270 |
self._validate_fwr_port_range(min_port, max_port) |
| 271 |
return [int(min_port), int(max_port)] |
| 272 |
|
| 273 |
def _get_port_range_from_min_max_ports(self, min_port, max_port): |
| 274 |
if not min_port: |
| 275 |
return None |
| 276 |
if min_port == max_port: |
| 277 |
return str(min_port) |
| 278 |
self._validate_fwr_port_range(min_port, max_port) |
| 279 |
return '{}:{}'.format(min_port, max_port) |
| 280 |
|
| 281 |
def _make_firewall_rule_dict(self, firewall_rule, fields=None, |
| 282 |
policies=None): |
| 283 |
src_port_range = self._get_port_range_from_min_max_ports( |
| 284 |
firewall_rule['source_port_range_min'], |
| 285 |
firewall_rule['source_port_range_max']) |
| 286 |
dst_port_range = self._get_port_range_from_min_max_ports( |
| 287 |
firewall_rule['destination_port_range_min'], |
| 288 |
firewall_rule['destination_port_range_max']) |
| 289 |
res = {'id': firewall_rule['id'], |
| 290 |
'tenant_id': firewall_rule['tenant_id'], |
| 291 |
'name': firewall_rule['name'], |
| 292 |
'description': firewall_rule['description'], |
| 293 |
'protocol': firewall_rule['protocol'], |
| 294 |
'firewall_policy_id': policies, |
| 295 |
'ip_version': firewall_rule['ip_version'], |
| 296 |
'source_ip_address': firewall_rule['source_ip_address'], |
| 297 |
'destination_ip_address': |
| 298 |
firewall_rule['destination_ip_address'], |
| 299 |
'source_port': src_port_range, |
| 300 |
'destination_port': dst_port_range, |
| 301 |
'action': firewall_rule['action'], |
| 302 |
'enabled': firewall_rule['enabled'], |
| 303 |
'shared': firewall_rule['shared']} |
| 304 |
if hasattr(firewall_rule.standard_attr, 'id'): |
| 305 |
res['standard_attr_id'] = firewall_rule.standard_attr.id |
| 306 |
resource_extend.apply_funcs('firewall_rules', res, |
| 307 |
firewall_rule) |
| 308 |
return db_utils.resource_fields(res, fields) |
| 309 |
|
| 310 |
def _make_firewall_policy_dict(self, firewall_policy, fields=None): |
| 311 |
fw_rules = [ |
| 312 |
rule_association.firewall_rule_id |
| 313 |
for rule_association in firewall_policy['rule_associations']] |
| 314 |
res = {'id': firewall_policy['id'], |
| 315 |
'tenant_id': firewall_policy['tenant_id'], |
| 316 |
'name': firewall_policy['name'], |
| 317 |
'description': firewall_policy['description'], |
| 318 |
'audited': firewall_policy['audited'], |
| 319 |
'firewall_rules': fw_rules, |
| 320 |
'shared': firewall_policy['shared']} |
| 321 |
if hasattr(firewall_policy.standard_attr, 'id'): |
| 322 |
res['standard_attr_id'] = firewall_policy.standard_attr.id |
| 323 |
resource_extend.apply_funcs('firewall_policies', res, |
| 324 |
firewall_policy) |
| 325 |
return db_utils.resource_fields(res, fields) |
| 326 |
|
| 327 |
def _make_firewall_group_dict(self, firewall_group_db, fields=None): |
| 328 |
fwg_ports = [port_assoc.port_id for port_assoc in |
| 329 |
firewall_group_db.port_associations] |
| 330 |
res = {'id': firewall_group_db['id'], |
| 331 |
'tenant_id': firewall_group_db['tenant_id'], |
| 332 |
'name': firewall_group_db['name'], |
| 333 |
'description': firewall_group_db['description'], |
| 334 |
'ingress_firewall_policy_id': |
| 335 |
firewall_group_db['ingress_firewall_policy_id'], |
| 336 |
'egress_firewall_policy_id': |
| 337 |
firewall_group_db['egress_firewall_policy_id'], |
| 338 |
'admin_state_up': firewall_group_db['admin_state_up'], |
| 339 |
'ports': fwg_ports, |
| 340 |
'status': firewall_group_db['status'], |
| 341 |
'shared': firewall_group_db['shared']} |
| 342 |
if hasattr(firewall_group_db.standard_attr, 'id'): |
| 343 |
res['standard_attr_id'] = firewall_group_db.standard_attr.id |
| 344 |
resource_extend.apply_funcs('firewall_groups', res, |
| 345 |
firewall_group_db) |
| 346 |
return db_utils.resource_fields(res, fields) |
| 347 |
|
| 348 |
def _get_policy_ordered_rules(self, context, policy_id): |
| 349 |
query = (context.session.query(FirewallRuleV2) |
| 350 |
.join(FirewallPolicyRuleAssociation) |
| 351 |
.filter_by(firewall_policy_id=policy_id) |
| 352 |
.order_by(FirewallPolicyRuleAssociation.position)) |
| 353 |
return [self._make_firewall_rule_dict(rule) for rule in query] |
| 354 |
|
| 355 |
def make_firewall_group_dict_with_rules(self, context, firewall_group_id): |
| 356 |
firewall_group = self.get_firewall_group(context, firewall_group_id) |
| 357 |
ingress_policy_id = firewall_group['ingress_firewall_policy_id'] |
| 358 |
if ingress_policy_id: |
| 359 |
firewall_group['ingress_rule_list'] = ( |
| 360 |
self._get_policy_ordered_rules(context, ingress_policy_id)) |
| 361 |
else: |
| 362 |
firewall_group['ingress_rule_list'] = [] |
| 363 |
|
| 364 |
egress_policy_id = firewall_group['egress_firewall_policy_id'] |
| 365 |
if egress_policy_id: |
| 366 |
firewall_group['egress_rule_list'] = ( |
| 367 |
self._get_policy_ordered_rules(context, egress_policy_id)) |
| 368 |
else: |
| 369 |
firewall_group['egress_rule_list'] = [] |
| 370 |
return firewall_group |
| 371 |
|
| 372 |
def _check_firewall_rule_conflict(self, fwr_db, fwp_db): |
| 373 |
if not fwr_db['shared']: |
| 374 |
if fwr_db['tenant_id'] != fwp_db['tenant_id']: |
| 375 |
raise f_exc.FirewallRuleConflict( |
| 376 |
firewall_rule_id=fwr_db['id'], |
| 377 |
project_id=fwr_db['tenant_id']) |
| 378 |
|
| 379 |
def _process_rule_for_policy(self, context, firewall_policy_id, |
| 380 |
firewall_rule_id, position, association_db): |
| 381 |
with db_api.CONTEXT_READER.using(context): |
| 382 |
fwp_query = context.session.query( |
| 383 |
FirewallPolicy).with_for_update() |
| 384 |
fwp_db = fwp_query.filter_by(id=firewall_policy_id).one() |
| 385 |
if position: |
| 386 |
|
| 387 |
|
| 388 |
fwp_db.rule_associations.insert( |
| 389 |
position - 1, |
| 390 |
FirewallPolicyRuleAssociation( |
| 391 |
firewall_rule_id=firewall_rule_id)) |
| 392 |
else: |
| 393 |
fwp_db.rule_associations.remove(association_db) |
| 394 |
context.session.delete(association_db) |
| 395 |
fwp_db.rule_associations.reorder() |
| 396 |
fwp_db.audited = False |
| 397 |
return self._make_firewall_policy_dict(fwp_db) |
| 398 |
|
| 399 |
def _get_policy_rule_association_query(self, context, firewall_policy_id, |
| 400 |
firewall_rule_id): |
| 401 |
fwpra_query = context.session.query(FirewallPolicyRuleAssociation) |
| 402 |
return fwpra_query.filter_by(firewall_policy_id=firewall_policy_id, |
| 403 |
firewall_rule_id=firewall_rule_id) |
| 404 |
|
| 405 |
def _ensure_rule_not_already_associated(self, context, firewall_policy_id, |
| 406 |
firewall_rule_id): |
| 407 |
"""Checks that a rule is not already associated with a particular |
| 408 |
policy. If it is the function will throw an exception. |
| 409 |
""" |
| 410 |
try: |
| 411 |
self._get_policy_rule_association_query( |
| 412 |
context, firewall_policy_id, firewall_rule_id).one() |
| 413 |
raise f_exc.FirewallRuleAlreadyAssociated( |
| 414 |
firewall_rule_id=firewall_rule_id, |
| 415 |
firewall_policy_id=firewall_policy_id) |
| 416 |
except exc.NoResultFound: |
| 417 |
return |
| 418 |
|
| 419 |
def _get_policy_rule_association(self, context, firewall_policy_id, |
| 420 |
firewall_rule_id): |
| 421 |
"""Returns the association between a firewall rule and a firewall |
| 422 |
policy. Throws an exception if the assocition does not exist. |
| 423 |
""" |
| 424 |
try: |
| 425 |
return self._get_policy_rule_association_query( |
| 426 |
context, firewall_policy_id, firewall_rule_id).one() |
| 427 |
except exc.NoResultFound: |
| 428 |
raise f_exc.FirewallRuleNotAssociatedWithPolicy( |
| 429 |
firewall_rule_id=firewall_rule_id, |
| 430 |
firewall_policy_id=firewall_policy_id) |
| 431 |
|
| 432 |
def _create_default_firewall_rules(self, context, tenant_id): |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
in_fwr_v4 = { |
| 438 |
'description': 'default ingress rule for IPv4', |
| 439 |
'name': 'default ingress ipv4', |
| 440 |
'shared': cfg.CONF.default_fwg_rules.shared, |
| 441 |
'protocol': cfg.CONF.default_fwg_rules.protocol, |
| 442 |
'tenant_id': tenant_id, |
| 443 |
'ip_version': nl_constants.IP_VERSION_4, |
| 444 |
'action': cfg.CONF.default_fwg_rules.ingress_action, |
| 445 |
'enabled': cfg.CONF.default_fwg_rules.enabled, |
| 446 |
'source_port': cfg.CONF.default_fwg_rules.ingress_source_port, |
| 447 |
'source_ip_address': |
| 448 |
cfg.CONF.default_fwg_rules.ingress_source_ipv4_address, |
| 449 |
'destination_port': |
| 450 |
cfg.CONF.default_fwg_rules.ingress_destination_port, |
| 451 |
'destination_ip_address': |
| 452 |
cfg.CONF.default_fwg_rules. |
| 453 |
ingress_destination_ipv4_address, |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
in_fwr_v6 = copy.deepcopy(in_fwr_v4) |
| 458 |
in_fwr_v6['description'] = 'default ingress rule for IPv6' |
| 459 |
in_fwr_v6['name'] = 'default ingress ipv6' |
| 460 |
in_fwr_v6['ip_version'] = nl_constants.IP_VERSION_6 |
| 461 |
in_fwr_v6['source_ip_address'] = \ |
| 462 |
cfg.CONF.default_fwg_rules.ingress_source_ipv6_address |
| 463 |
in_fwr_v6['destination_ip_address'] = \ |
| 464 |
cfg.CONF.default_fwg_rules.ingress_destination_ipv6_address |
| 465 |
|
| 466 |
|
| 467 |
eg_fwr_v4 = copy.deepcopy(in_fwr_v4) |
| 468 |
eg_fwr_v4['description'] = 'default egress rule for IPv4' |
| 469 |
eg_fwr_v4['name'] = 'default egress ipv4' |
| 470 |
eg_fwr_v4['action'] = cfg.CONF.default_fwg_rules.egress_action |
| 471 |
eg_fwr_v4['source_port'] = \ |
| 472 |
cfg.CONF.default_fwg_rules.egress_source_port |
| 473 |
eg_fwr_v4['source_ip_address'] = \ |
| 474 |
cfg.CONF.default_fwg_rules.egress_source_ipv4_address |
| 475 |
eg_fwr_v4['destination_port'] = \ |
| 476 |
cfg.CONF.default_fwg_rules.egress_destination_port |
| 477 |
eg_fwr_v4['destination_ip_address'] = \ |
| 478 |
cfg.CONF.default_fwg_rules.egress_destination_ipv4_address |
| 479 |
|
| 480 |
|
| 481 |
eg_fwr_v6 = copy.deepcopy(in_fwr_v6) |
| 482 |
eg_fwr_v6['description'] = 'default egress rule for IPv6' |
| 483 |
eg_fwr_v6['name'] = 'default egress ipv6' |
| 484 |
eg_fwr_v6['action'] = cfg.CONF.default_fwg_rules.egress_action |
| 485 |
eg_fwr_v6['source_port'] = \ |
| 486 |
cfg.CONF.default_fwg_rules.egress_source_port |
| 487 |
eg_fwr_v6['source_ip_address'] = \ |
| 488 |
cfg.CONF.default_fwg_rules.egress_source_ipv6_address |
| 489 |
eg_fwr_v6['destination_port'] = \ |
| 490 |
cfg.CONF.default_fwg_rules.egress_destination_port |
| 491 |
eg_fwr_v6['destination_ip_address'] = \ |
| 492 |
cfg.CONF.default_fwg_rules.egress_destination_ipv6_address |
| 493 |
|
| 494 |
return { |
| 495 |
'in_ipv4': self.create_firewall_rule(context, in_fwr_v4)['id'], |
| 496 |
'in_ipv6': self.create_firewall_rule(context, in_fwr_v6)['id'], |
| 497 |
'eg_ipv4': self.create_firewall_rule(context, eg_fwr_v4)['id'], |
| 498 |
'eg_ipv6': self.create_firewall_rule(context, eg_fwr_v6)['id'], |
| 499 |
} |
| 500 |
|
| 501 |
def create_firewall_rule(self, context, firewall_rule): |
| 502 |
fwr = firewall_rule |
| 503 |
self._validate_fwr_protocol_parameters(fwr) |
| 504 |
self._validate_fwr_src_dst_ip_version(fwr) |
| 505 |
|
| 506 |
src_port_min, src_port_max = self._get_min_max_ports_from_range( |
| 507 |
fwr['source_port']) |
| 508 |
dst_port_min, dst_port_max = self._get_min_max_ports_from_range( |
| 509 |
fwr['destination_port']) |
| 510 |
with db_api.CONTEXT_WRITER.using(context): |
| 511 |
fwr_db = FirewallRuleV2( |
| 512 |
id=uuidutils.generate_uuid(), |
| 513 |
tenant_id=fwr['tenant_id'], |
| 514 |
name=fwr['name'], |
| 515 |
description=fwr['description'], |
| 516 |
protocol=fwr['protocol'], |
| 517 |
ip_version=fwr['ip_version'], |
| 518 |
source_ip_address=fwr['source_ip_address'], |
| 519 |
destination_ip_address=fwr['destination_ip_address'], |
| 520 |
source_port_range_min=src_port_min, |
| 521 |
source_port_range_max=src_port_max, |
| 522 |
destination_port_range_min=dst_port_min, |
| 523 |
destination_port_range_max=dst_port_max, |
| 524 |
action=fwr['action'], |
| 525 |
enabled=fwr['enabled'], |
| 526 |
shared=fwr['shared']) |
| 527 |
context.session.add(fwr_db) |
| 528 |
return self._make_firewall_rule_dict(fwr_db) |
| 529 |
|
| 530 |
def update_firewall_rule(self, context, id, firewall_rule): |
| 531 |
fwr = firewall_rule |
| 532 |
fwr_db = self._get_firewall_rule(context, id) |
| 533 |
fwr_db_updated = self._make_firewall_rule_dict(fwr_db) |
| 534 |
fwr_db_updated.update(fwr) |
| 535 |
|
| 536 |
self._validate_fwr_protocol_parameters(fwr_db_updated) |
| 537 |
self._validate_fwr_src_dst_ip_version(fwr_db_updated) |
| 538 |
if 'source_port' in fwr: |
| 539 |
src_port_min, src_port_max = self._get_min_max_ports_from_range( |
| 540 |
fwr['source_port']) |
| 541 |
fwr['source_port_range_min'] = src_port_min |
| 542 |
fwr['source_port_range_max'] = src_port_max |
| 543 |
del fwr['source_port'] |
| 544 |
if 'destination_port' in fwr: |
| 545 |
dst_port_min, dst_port_max = self._get_min_max_ports_from_range( |
| 546 |
fwr['destination_port']) |
| 547 |
fwr['destination_port_range_min'] = dst_port_min |
| 548 |
fwr['destination_port_range_max'] = dst_port_max |
| 549 |
del fwr['destination_port'] |
| 550 |
with db_api.CONTEXT_WRITER.using(context): |
| 551 |
fwr_db.update(fwr) |
| 552 |
|
| 553 |
fwp_ids = self.get_policies_with_rule(context, id) |
| 554 |
for fwp_id in fwp_ids: |
| 555 |
fwp_db = self._get_firewall_policy(context, fwp_id) |
| 556 |
fwp_db['audited'] = False |
| 557 |
return self._make_firewall_rule_dict(fwr_db) |
| 558 |
|
| 559 |
def delete_firewall_rule(self, context, id): |
| 560 |
with db_api.CONTEXT_WRITER.using(context): |
| 561 |
fwr = self._get_firewall_rule(context, id) |
| 562 |
|
| 563 |
if self.get_policies_with_rule(context, id): |
| 564 |
raise f_exc.FirewallRuleInUse(firewall_rule_id=id) |
| 565 |
context.session.delete(fwr) |
| 566 |
|
| 567 |
def insert_rule(self, context, id, rule_info): |
| 568 |
firewall_rule_id = rule_info['firewall_rule_id'] |
| 569 |
|
| 570 |
self._ensure_rule_not_already_associated(context, id, firewall_rule_id) |
| 571 |
insert_before = True |
| 572 |
ref_firewall_rule_id = None |
| 573 |
if 'insert_before' in rule_info: |
| 574 |
ref_firewall_rule_id = rule_info['insert_before'] |
| 575 |
if not ref_firewall_rule_id and 'insert_after' in rule_info: |
| 576 |
|
| 577 |
ref_firewall_rule_id = rule_info['insert_after'] |
| 578 |
insert_before = False |
| 579 |
with db_api.CONTEXT_WRITER.using(context): |
| 580 |
fwr_db = self._get_firewall_rule(context, firewall_rule_id) |
| 581 |
fwp_db = self._get_firewall_policy(context, id) |
| 582 |
self._check_firewall_rule_conflict(fwr_db, fwp_db) |
| 583 |
if ref_firewall_rule_id: |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
fwpra_db = self._get_policy_rule_association( |
| 590 |
context, id, ref_firewall_rule_id) |
| 591 |
if insert_before: |
| 592 |
position = fwpra_db.position |
| 593 |
else: |
| 594 |
position = fwpra_db.position + 1 |
| 595 |
else: |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
position = 1 |
| 602 |
return self._process_rule_for_policy(context, id, firewall_rule_id, |
| 603 |
position, None) |
| 604 |
|
| 605 |
def remove_rule(self, context, id, rule_info): |
| 606 |
firewall_rule_id = rule_info['firewall_rule_id'] |
| 607 |
with db_api.CONTEXT_WRITER.using(context): |
| 608 |
self._get_firewall_rule(context, firewall_rule_id) |
| 609 |
fwpra_db = self._get_policy_rule_association(context, id, |
| 610 |
firewall_rule_id) |
| 611 |
return self._process_rule_for_policy(context, id, firewall_rule_id, |
| 612 |
None, fwpra_db) |
| 613 |
|
| 614 |
def get_firewall_rule(self, context, id, fields=None): |
| 615 |
fwr = self._get_firewall_rule(context, id) |
| 616 |
policies = self.get_policies_with_rule(context, id) or None |
| 617 |
return self._make_firewall_rule_dict(fwr, fields, policies=policies) |
| 618 |
|
| 619 |
@db_api.CONTEXT_READER |
| 620 |
def get_firewall_rules(self, context, filters=None, fields=None): |
| 621 |
return model_query.get_collection( |
| 622 |
context, FirewallRuleV2, self._make_firewall_rule_dict, |
| 623 |
filters=filters, fields=fields) |
| 624 |
|
| 625 |
def _get_rules_in_policy(self, context, fwpid): |
| 626 |
"""Gets rules in a firewall policy""" |
| 627 |
with db_api.CONTEXT_READER.using(context): |
| 628 |
fw_pol_rule_qry = context.session.query( |
| 629 |
FirewallPolicyRuleAssociation).filter_by( |
| 630 |
firewall_policy_id=fwpid) |
| 631 |
fwp_rules = [entry.firewall_rule_id for entry in fw_pol_rule_qry] |
| 632 |
return fwp_rules |
| 633 |
|
| 634 |
def get_policies_with_rule(self, context, fwrid): |
| 635 |
"""Gets rules in a firewall policy""" |
| 636 |
with db_api.CONTEXT_READER.using(context): |
| 637 |
fw_pol_rule_qry = context.session.query( |
| 638 |
FirewallPolicyRuleAssociation).filter_by( |
| 639 |
firewall_rule_id=fwrid) |
| 640 |
fwps = [entry.firewall_policy_id for entry in fw_pol_rule_qry] |
| 641 |
return fwps |
| 642 |
|
| 643 |
def _set_rules_in_policy_rule_assoc(self, context, fwp_db, fwp): |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
rule_id_list = fwp['firewall_rules'] |
| 648 |
if not rule_id_list: |
| 649 |
return |
| 650 |
position = 0 |
| 651 |
with db_api.CONTEXT_WRITER.using(context): |
| 652 |
for rule_id in rule_id_list: |
| 653 |
fw_pol_rul_db = FirewallPolicyRuleAssociation( |
| 654 |
firewall_policy_id=fwp_db['id'], |
| 655 |
firewall_rule_id=rule_id, |
| 656 |
position=position) |
| 657 |
context.session.add(fw_pol_rul_db) |
| 658 |
position += 1 |
| 659 |
|
| 660 |
def _check_rules_for_policy_is_valid(self, context, fwp, fwp_db, |
| 661 |
rule_id_list, filters): |
| 662 |
with db_api.CONTEXT_READER.using(context): |
| 663 |
rules_in_fwr_db = model_query.get_collection_query( |
| 664 |
context, FirewallRuleV2, filters=filters) |
| 665 |
rules_dict = {fwr_db['id']: fwr_db for fwr_db in rules_in_fwr_db} |
| 666 |
for fwrule_id in rule_id_list: |
| 667 |
if fwrule_id not in rules_dict: |
| 668 |
|
| 669 |
raise f_exc.FirewallRuleNotFound( |
| 670 |
firewall_rule_id=fwrule_id) |
| 671 |
if 'shared' in fwp: |
| 672 |
if fwp['shared'] and not rules_dict[fwrule_id]['shared']: |
| 673 |
raise f_exc.FirewallRuleSharingConflict( |
| 674 |
firewall_rule_id=fwrule_id, |
| 675 |
firewall_policy_id=fwp_db['id']) |
| 676 |
elif fwp_db['shared'] and not rules_dict[fwrule_id]['shared']: |
| 677 |
raise f_exc.FirewallRuleSharingConflict( |
| 678 |
firewall_rule_id=fwrule_id, |
| 679 |
firewall_policy_id=fwp_db['id']) |
| 680 |
else: |
| 681 |
|
| 682 |
|
| 683 |
if not rules_dict[fwrule_id]['shared']: |
| 684 |
if (rules_dict[fwrule_id]['tenant_id'] != fwp_db[ |
| 685 |
'tenant_id']): |
| 686 |
raise f_exc.FirewallRuleConflict( |
| 687 |
firewall_rule_id=fwrule_id, |
| 688 |
project_id=rules_dict[fwrule_id]['tenant_id']) |
| 689 |
|
| 690 |
def _check_if_rules_shared_for_policy_shared(self, context, fwp_db, fwp): |
| 691 |
if fwp['shared']: |
| 692 |
rules_in_db = fwp_db.rule_associations |
| 693 |
for entry in rules_in_db: |
| 694 |
fwr_db = self._get_firewall_rule(context, |
| 695 |
entry.firewall_rule_id) |
| 696 |
if not fwr_db['shared']: |
| 697 |
raise f_exc.FirewallPolicySharingConflict( |
| 698 |
firewall_rule_id=fwr_db['id'], |
| 699 |
firewall_policy_id=fwp_db['id']) |
| 700 |
|
| 701 |
def get_fwgs_with_policy(self, context, fwp_id): |
| 702 |
with db_api.CONTEXT_READER.using(context): |
| 703 |
fwg_ing_pol_qry = context.session.query( |
| 704 |
FirewallGroup).filter_by( |
| 705 |
ingress_firewall_policy_id=fwp_id) |
| 706 |
ing_fwg_ids = [entry.id for entry in fwg_ing_pol_qry] |
| 707 |
fwg_eg_pol_qry = context.session.query( |
| 708 |
FirewallGroup).filter_by( |
| 709 |
egress_firewall_policy_id=fwp_id) |
| 710 |
eg_fwg_ids = [entry.id for entry in fwg_eg_pol_qry] |
| 711 |
return ing_fwg_ids, eg_fwg_ids |
| 712 |
|
| 713 |
def _check_fwgs_associated_with_policy_in_same_project(self, context, |
| 714 |
fwp_id, |
| 715 |
fwp_tenant_id): |
| 716 |
with db_api.CONTEXT_READER.using(context): |
| 717 |
fwg_with_fwp_id_db = context.session.query(FirewallGroup).filter( |
| 718 |
or_(FirewallGroup.ingress_firewall_policy_id == fwp_id, |
| 719 |
FirewallGroup.egress_firewall_policy_id == fwp_id)) |
| 720 |
for entry in fwg_with_fwp_id_db: |
| 721 |
if entry.tenant_id != fwp_tenant_id: |
| 722 |
raise f_exc.FirewallPolicyInUse( |
| 723 |
firewall_policy_id=fwp_id) |
| 724 |
|
| 725 |
def _delete_all_rules_from_policy(self, context, fwp_db): |
| 726 |
"""Deletes all FirewallPolicyRuleAssociation objects |
| 727 |
|
| 728 |
fwp_db is an DB dict representing firewall policy. |
| 729 |
Returns a dictionary with updated rule_associations. |
| 730 |
""" |
| 731 |
for rule_id in [rule_assoc.firewall_rule_id |
| 732 |
for rule_assoc in fwp_db['rule_associations']]: |
| 733 |
fwpra_db = self._get_policy_rule_association( |
| 734 |
context, fwp_db['id'], rule_id) |
| 735 |
fwp_db.rule_associations.remove(fwpra_db) |
| 736 |
context.session.delete(fwpra_db) |
| 737 |
fwp_db.rule_associations = [] |
| 738 |
return fwp_db |
| 739 |
|
| 740 |
def _set_rules_for_policy(self, context, firewall_policy_db, fwp): |
| 741 |
rule_id_list = fwp['firewall_rules'] |
| 742 |
fwp_db = firewall_policy_db |
| 743 |
with db_api.CONTEXT_WRITER.using(context): |
| 744 |
if not rule_id_list: |
| 745 |
self._delete_all_rules_from_policy(context, fwp_db) |
| 746 |
return |
| 747 |
|
| 748 |
filters = {'firewall_rule_id': [r_id for r_id in rule_id_list]} |
| 749 |
|
| 750 |
self._check_rules_for_policy_is_valid(context, fwp, fwp_db, |
| 751 |
rule_id_list, filters) |
| 752 |
|
| 753 |
self._delete_all_rules_from_policy(context, fwp_db) |
| 754 |
|
| 755 |
self._set_rules_in_policy_rule_assoc(context, fwp_db, fwp) |
| 756 |
|
| 757 |
|
| 758 |
filters['firewall_policy_id'] = [fwp_db['id']] |
| 759 |
rules_in_fpol_rul_db = model_query.get_collection_query( |
| 760 |
context, |
| 761 |
FirewallPolicyRuleAssociation, |
| 762 |
filters=filters) |
| 763 |
rules_dict = {fpol_rul_db['firewall_rule_id']: fpol_rul_db |
| 764 |
for fpol_rul_db in rules_in_fpol_rul_db} |
| 765 |
fwp_db.rule_associations = [] |
| 766 |
for fwrule_id in rule_id_list: |
| 767 |
fwp_db.rule_associations.append(rules_dict[fwrule_id]) |
| 768 |
fwp_db.rule_associations.reorder() |
| 769 |
|
| 770 |
def _create_default_firewall_policy(self, context, tenant_id, policy_type, |
| 771 |
**kwargs): |
| 772 |
fwrs = kwargs.get('firewall_rules', []) |
| 773 |
description = kwargs.get('description', '') |
| 774 |
name = (const.DEFAULT_FWP_INGRESS |
| 775 |
if policy_type == 'ingress' else const.DEFAULT_FWP_EGRESS) |
| 776 |
firewall_policy = { |
| 777 |
'name': name, |
| 778 |
'description': description, |
| 779 |
'audited': False, |
| 780 |
'shared': False, |
| 781 |
'firewall_rules': fwrs, |
| 782 |
'tenant_id': tenant_id, |
| 783 |
} |
| 784 |
return self._do_create_firewall_policy(context, firewall_policy) |
| 785 |
|
| 786 |
def _do_create_firewall_policy(self, context, firewall_policy): |
| 787 |
fwp = firewall_policy |
| 788 |
with db_api.CONTEXT_WRITER.using(context): |
| 789 |
fwp_db = FirewallPolicy( |
| 790 |
id=uuidutils.generate_uuid(), |
| 791 |
tenant_id=fwp['tenant_id'], |
| 792 |
name=fwp['name'], |
| 793 |
description=fwp['description'], |
| 794 |
audited=fwp['audited'], |
| 795 |
shared=fwp['shared']) |
| 796 |
context.session.add(fwp_db) |
| 797 |
self._set_rules_for_policy(context, fwp_db, fwp) |
| 798 |
return self._make_firewall_policy_dict(fwp_db) |
| 799 |
|
| 800 |
def create_firewall_policy(self, context, firewall_policy): |
| 801 |
self._ensure_not_default_resource(firewall_policy, 'firewall_policy') |
| 802 |
return self._do_create_firewall_policy(context, firewall_policy) |
| 803 |
|
| 804 |
def update_firewall_policy(self, context, id, firewall_policy): |
| 805 |
fwp = firewall_policy |
| 806 |
with db_api.CONTEXT_WRITER.using(context): |
| 807 |
fwp_db = self._get_firewall_policy(context, id) |
| 808 |
self._ensure_not_default_resource(fwp_db, 'firewall_policy', |
| 809 |
action="update") |
| 810 |
if not fwp.get('shared', True): |
| 811 |
|
| 812 |
|
| 813 |
self._check_fwgs_associated_with_policy_in_same_project( |
| 814 |
context, id, fwp_db['tenant_id']) |
| 815 |
if 'shared' in fwp and 'firewall_rules' not in fwp: |
| 816 |
self._check_if_rules_shared_for_policy_shared( |
| 817 |
context, fwp_db, fwp) |
| 818 |
if 'firewall_rules' in fwp: |
| 819 |
self._set_rules_for_policy(context, fwp_db, fwp) |
| 820 |
del fwp['firewall_rules'] |
| 821 |
if 'audited' not in fwp: |
| 822 |
fwp['audited'] = False |
| 823 |
fwp_db.update(fwp) |
| 824 |
return self._make_firewall_policy_dict(fwp_db) |
| 825 |
|
| 826 |
def delete_firewall_policy(self, context, id): |
| 827 |
with db_api.CONTEXT_WRITER.using(context): |
| 828 |
fwp_db = self._get_firewall_policy(context, id) |
| 829 |
|
| 830 |
qry = context.session.query(FirewallGroup) |
| 831 |
if qry.filter_by(ingress_firewall_policy_id=id).first(): |
| 832 |
raise f_exc.FirewallPolicyInUse(firewall_policy_id=id) |
| 833 |
elif qry.filter_by(egress_firewall_policy_id=id).first(): |
| 834 |
raise f_exc.FirewallPolicyInUse(firewall_policy_id=id) |
| 835 |
else: |
| 836 |
fwp_db = self._delete_all_rules_from_policy(context, fwp_db) |
| 837 |
context.session.delete(fwp_db) |
| 838 |
|
| 839 |
def get_firewall_policy(self, context, id, fields=None): |
| 840 |
fwp = self._get_firewall_policy(context, id) |
| 841 |
return self._make_firewall_policy_dict(fwp, fields) |
| 842 |
|
| 843 |
@db_api.CONTEXT_READER |
| 844 |
def get_firewall_policies(self, context, filters=None, fields=None): |
| 845 |
return model_query.get_collection( |
| 846 |
context, FirewallPolicy, self._make_firewall_policy_dict, |
| 847 |
filters=filters, fields=fields) |
| 848 |
|
| 849 |
def _set_ports_for_firewall_group(self, context, fwg_db, fwg): |
| 850 |
port_id_list = fwg['ports'] |
| 851 |
if not port_id_list: |
| 852 |
return |
| 853 |
|
| 854 |
exc_ports = [] |
| 855 |
for port_id in port_id_list: |
| 856 |
try: |
| 857 |
fwg_port_db = FirewallGroupPortAssociation( |
| 858 |
firewall_group_id=fwg_db['id'], |
| 859 |
port_id=port_id) |
| 860 |
context.session.add(fwg_port_db) |
| 861 |
except db_exc.DBDuplicateEntry: |
| 862 |
exc_ports.append(port_id) |
| 863 |
if exc_ports: |
| 864 |
raise f_exc.FirewallGroupPortInUse(port_ids=exc_ports) |
| 865 |
|
| 866 |
def get_ports_in_firewall_group(self, context, firewall_group_id): |
| 867 |
"""Get the Ports associated with the firewall group.""" |
| 868 |
with db_api.CONTEXT_READER.using(context): |
| 869 |
fw_group_port_qry = context.session.query( |
| 870 |
FirewallGroupPortAssociation) |
| 871 |
fw_group_port_rows = fw_group_port_qry.filter_by( |
| 872 |
firewall_group_id=firewall_group_id) |
| 873 |
fw_ports = [entry.port_id for entry in fw_group_port_rows] |
| 874 |
return fw_ports |
| 875 |
|
| 876 |
def _delete_ports_in_firewall_group(self, context, firewall_group_id): |
| 877 |
"""Delete the Ports associated with the firewall group.""" |
| 878 |
with db_api.CONTEXT_WRITER.using(context): |
| 879 |
fw_group_port_qry = context.session.query( |
| 880 |
FirewallGroupPortAssociation) |
| 881 |
fw_group_port_qry.filter_by( |
| 882 |
firewall_group_id=firewall_group_id).delete() |
| 883 |
return |
| 884 |
|
| 885 |
@db_api.CONTEXT_READER |
| 886 |
def _get_default_fwg_id(self, context, tenant_id): |
| 887 |
"""Returns an id of default firewall group for given tenant or None""" |
| 888 |
default_fwg = model_query.query_with_hooks( |
| 889 |
context.elevated(), FirewallGroup).filter_by( |
| 890 |
project_id=tenant_id, name=const.DEFAULT_FWG).first() |
| 891 |
if default_fwg: |
| 892 |
return default_fwg.id |
| 893 |
return None |
| 894 |
|
| 895 |
def get_fwg_attached_to_port(self, context, port_id): |
| 896 |
"""Return a firewall group ID that is attached to a given port""" |
| 897 |
with db_api.CONTEXT_READER.using(context): |
| 898 |
fwg_port = model_query.query_with_hooks( |
| 899 |
context, FirewallGroupPortAssociation).\ |
| 900 |
filter_by(port_id=port_id).first() |
| 901 |
if fwg_port: |
| 902 |
return fwg_port.firewall_group_id |
| 903 |
return None |
| 904 |
|
| 905 |
def get_fwg_ports_in_tenant(self, context, tenant_id): |
| 906 |
"""Return a list of ports under a given tenant""" |
| 907 |
try: |
| 908 |
fwg_id = FirewallGroupPortAssociation.firewall_group_id |
| 909 |
with db_api.CONTEXT_READER.using(context): |
| 910 |
port_qry = context.session.query( |
| 911 |
FirewallGroupPortAssociation.port_id).join( |
| 912 |
FirewallGroup, FirewallGroup.id == fwg_id).filter( |
| 913 |
FirewallGroup.tenant_id == tenant_id).all() |
| 914 |
return list({port for (port,) in port_qry}) |
| 915 |
except exc.NoResultFound: |
| 916 |
return [] |
| 917 |
|
| 918 |
def _ensure_default_firewall_group(self, context, tenant_id): |
| 919 |
"""Create a default firewall group if one doesn't exist for a tenant |
| 920 |
|
| 921 |
Returns the default firewall group id for a given tenant. |
| 922 |
""" |
| 923 |
exists = self._get_default_fwg_id(context, tenant_id) |
| 924 |
if exists: |
| 925 |
return exists |
| 926 |
|
| 927 |
try: |
| 928 |
|
| 929 |
ctx = lib_context.get_admin_context() |
| 930 |
with db_api.CONTEXT_WRITER.using(ctx): |
| 931 |
|
| 932 |
fwr_ids = self._create_default_firewall_rules( |
| 933 |
ctx, tenant_id) |
| 934 |
ingress_fwp = { |
| 935 |
'description': 'Ingress firewall policy', |
| 936 |
'firewall_rules': [fwr_ids['in_ipv4'], |
| 937 |
fwr_ids['in_ipv6']], |
| 938 |
} |
| 939 |
egress_fwp = { |
| 940 |
'description': 'Egress firewall policy', |
| 941 |
'firewall_rules': [fwr_ids['eg_ipv4'], |
| 942 |
fwr_ids['eg_ipv6']], |
| 943 |
} |
| 944 |
ingress_fwp_db = self._create_default_firewall_policy( |
| 945 |
ctx, tenant_id, 'ingress', **ingress_fwp) |
| 946 |
egress_fwp_db = self._create_default_firewall_policy( |
| 947 |
ctx, tenant_id, 'egress', **egress_fwp) |
| 948 |
|
| 949 |
fwg = { |
| 950 |
'name': const.DEFAULT_FWG, |
| 951 |
'tenant_id': tenant_id, |
| 952 |
'ingress_firewall_policy_id': ingress_fwp_db['id'], |
| 953 |
'egress_firewall_policy_id': egress_fwp_db['id'], |
| 954 |
'ports': [], |
| 955 |
'shared': False, |
| 956 |
'status': nl_constants.INACTIVE, |
| 957 |
'admin_state_up': True, |
| 958 |
'description': 'Default firewall group', |
| 959 |
} |
| 960 |
fwg_db = self._create_firewall_group( |
| 961 |
ctx, fwg, default_fwg=True) |
| 962 |
ctx.session.add(DefaultFirewallGroup( |
| 963 |
firewall_group_id=fwg_db['id'], |
| 964 |
project_id=tenant_id)) |
| 965 |
return fwg_db['id'] |
| 966 |
|
| 967 |
except db_exc.DBDuplicateEntry: |
| 968 |
|
| 969 |
LOG.debug("Default FWG was concurrently created") |
| 970 |
return self._get_default_fwg_id(context, tenant_id) |
| 971 |
|
| 972 |
def _create_firewall_group(self, context, firewall_group, |
| 973 |
default_fwg=False): |
| 974 |
"""Create a firewall group |
| 975 |
|
| 976 |
If default_fwg is True then a default firewall group is being created |
| 977 |
for a given tenant. |
| 978 |
""" |
| 979 |
fwg = firewall_group |
| 980 |
tenant_id = fwg['tenant_id'] |
| 981 |
if firewall_group.get('status') is None: |
| 982 |
fwg['status'] = nl_constants.CREATED |
| 983 |
|
| 984 |
if default_fwg: |
| 985 |
|
| 986 |
default_fwg_id = self._get_default_fwg_id(context, tenant_id) |
| 987 |
if default_fwg_id is not None: |
| 988 |
|
| 989 |
return self.get_firewall_group(context, default_fwg_id) |
| 990 |
else: |
| 991 |
|
| 992 |
|
| 993 |
self._ensure_default_firewall_group(context, tenant_id) |
| 994 |
|
| 995 |
with db_api.CONTEXT_WRITER.using(context): |
| 996 |
fwg_db = FirewallGroup( |
| 997 |
id=uuidutils.generate_uuid(), |
| 998 |
tenant_id=tenant_id, |
| 999 |
name=fwg['name'], |
| 1000 |
description=fwg['description'], |
| 1001 |
status=fwg['status'], |
| 1002 |
ingress_firewall_policy_id=fwg['ingress_firewall_policy_id'], |
| 1003 |
egress_firewall_policy_id=fwg['egress_firewall_policy_id'], |
| 1004 |
admin_state_up=fwg['admin_state_up'], |
| 1005 |
shared=fwg['shared']) |
| 1006 |
context.session.add(fwg_db) |
| 1007 |
self._set_ports_for_firewall_group(context, fwg_db, fwg) |
| 1008 |
return self._make_firewall_group_dict(fwg_db) |
| 1009 |
|
| 1010 |
def create_firewall_group(self, context, firewall_group): |
| 1011 |
self._ensure_not_default_resource(firewall_group, 'firewall_group') |
| 1012 |
return self._create_firewall_group(context, firewall_group) |
| 1013 |
|
| 1014 |
def update_firewall_group(self, context, id, firewall_group): |
| 1015 |
fwg = firewall_group |
| 1016 |
|
| 1017 |
self._ensure_not_default_resource(fwg, 'firewall_group') |
| 1018 |
with db_api.CONTEXT_WRITER.using(context): |
| 1019 |
fwg_db = self._get_firewall_group(context, id) |
| 1020 |
if _is_default(fwg_db): |
| 1021 |
attrs = [ |
| 1022 |
'name', 'description', 'admin_state_up', |
| 1023 |
'ingress_firewall_policy_id', 'egress_firewall_policy_id' |
| 1024 |
] |
| 1025 |
if context.is_admin: |
| 1026 |
attrs = ['name'] |
| 1027 |
for attr in attrs: |
| 1028 |
if attr in fwg: |
| 1029 |
raise FirewallDefaultObjectUpdateRestricted( |
| 1030 |
resource_type='Firewall Group', |
| 1031 |
resource_id=fwg_db['id']) |
| 1032 |
if 'ports' in fwg: |
| 1033 |
LOG.debug("Ports are updated in Firewall Group") |
| 1034 |
self._delete_ports_in_firewall_group(context, id) |
| 1035 |
self._set_ports_for_firewall_group(context, fwg_db, fwg) |
| 1036 |
del fwg['ports'] |
| 1037 |
|
| 1038 |
if fwg: |
| 1039 |
fwg_db.update( |
| 1040 |
db_utils.filter_non_model_columns(fwg, FirewallGroup)) |
| 1041 |
return self.get_firewall_group(context, id) |
| 1042 |
|
| 1043 |
def update_firewall_group_status(self, context, id, status, not_in=None): |
| 1044 |
"""Conditionally update firewall_group status. |
| 1045 |
Status transition is performed only if firewall is not in the specified |
| 1046 |
states as defined by 'not_in' list. |
| 1047 |
""" |
| 1048 |
|
| 1049 |
not_in = not_in or [] |
| 1050 |
with db_api.CONTEXT_WRITER.using(context): |
| 1051 |
return (context.session.query(FirewallGroup). |
| 1052 |
filter(FirewallGroup.id == id). |
| 1053 |
filter(~FirewallGroup.status.in_(not_in)). |
| 1054 |
update({'status': status}, synchronize_session=False)) |
| 1055 |
|
| 1056 |
def delete_firewall_group(self, context, id): |
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
with db_api.CONTEXT_WRITER.using(context): |
| 1061 |
|
| 1062 |
|
| 1063 |
try: |
| 1064 |
fwg_db = self._get_firewall_group(context, id) |
| 1065 |
except f_exc.FirewallGroupNotFound: |
| 1066 |
return |
| 1067 |
|
| 1068 |
if _is_default(fwg_db): |
| 1069 |
if context.is_admin: |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
context.session.query( |
| 1074 |
FirewallGroup).filter_by(id=id).delete() |
| 1075 |
fwp = [fwg_db['ingress_firewall_policy_id'], |
| 1076 |
fwg_db['egress_firewall_policy_id']] |
| 1077 |
for fwp_id in fwp: |
| 1078 |
self.delete_firewall_policy(context, fwp_id) |
| 1079 |
else: |
| 1080 |
|
| 1081 |
raise f_exc.FirewallGroupCannotRemoveDefault() |
| 1082 |
else: |
| 1083 |
context.session.query( |
| 1084 |
FirewallGroup).filter_by(id=id).delete() |
| 1085 |
|
| 1086 |
def _ensure_not_default_resource(self, resource_dict, r_type, action=None): |
| 1087 |
"""Checks that a resource is not default by checking its name |
| 1088 |
|
| 1089 |
A resource_dict can be either a dictionary in form {r_type : {}} or a |
| 1090 |
serialized object from db. |
| 1091 |
|
| 1092 |
Action is used to determine type of exception to be raised. |
| 1093 |
""" |
| 1094 |
resource = resource_dict.get(r_type) or resource_dict |
| 1095 |
if r_type == 'firewall_group': |
| 1096 |
if resource.get('name', '') == const.DEFAULT_FWG: |
| 1097 |
if action == "update": |
| 1098 |
raise FirewallDefaultObjectUpdateRestricted( |
| 1099 |
resource_type='Firewall Group', |
| 1100 |
resource_id=resource['id']) |
| 1101 |
raise FirewallDefaultParameterExists( |
| 1102 |
resource_type='Firewall Group', name=resource['name']) |
| 1103 |
elif r_type == 'firewall_policy': |
| 1104 |
if resource.get('name', '') in [const.DEFAULT_FWP_INGRESS, |
| 1105 |
const.DEFAULT_FWP_EGRESS]: |
| 1106 |
if action == "update": |
| 1107 |
raise FirewallDefaultObjectUpdateRestricted( |
| 1108 |
resource_type='Firewall Group', |
| 1109 |
resource_id=resource['id']) |
| 1110 |
raise FirewallDefaultParameterExists( |
| 1111 |
resource_type='Firewall Policy', name=resource['name']) |
| 1112 |
|
| 1113 |
def get_firewall_group(self, context, id, fields=None): |
| 1114 |
fw = self._get_firewall_group(context, id) |
| 1115 |
return self._make_firewall_group_dict(fw, fields) |
| 1116 |
|
| 1117 |
def get_firewall_groups(self, context, filters=None, fields=None): |
| 1118 |
if context.tenant_id: |
| 1119 |
tenant_id = filters.get('tenant_id') if filters else None |
| 1120 |
tenant_id = tenant_id[0] if tenant_id else context.tenant_id |
| 1121 |
self._ensure_default_firewall_group(context, tenant_id) |
| 1122 |
with db_api.CONTEXT_READER.using(context): |
| 1123 |
return model_query.get_collection( |
| 1124 |
context, FirewallGroup, self._make_firewall_group_dict, |
| 1125 |
filters=filters, fields=fields) |
| 1126 |
|
| 1127 |
|
| 1128 |
def _is_default(fwg_db): |
| 1129 |
return fwg_db['name'] == const.DEFAULT_FWG |