| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
from neutron.objects import ports |
| 17 |
from neutron.services.logapi.common import exceptions as log_exc |
| 18 |
from neutron.services.logapi.common import validators |
| 19 |
from neutron_lib import constants as nl_const |
| 20 |
from neutron_lib.plugins import directory |
| 21 |
from sqlalchemy.orm import exc as orm_exc |
| 22 |
|
| 23 |
from neutron_fwaas.common import fwaas_constants |
| 24 |
from neutron_fwaas.services.logapi import constants as log_const |
| 25 |
from neutron_fwaas.services.logapi import exceptions as fwg_log_exc |
| 26 |
|
| 27 |
fwg_plugin = None |
| 28 |
|
| 29 |
|
| 30 |
def _check_fwg(context, fwg_id): |
| 31 |
try: |
| 32 |
fwg = fwg_plugin.get_firewall_group(context, id=fwg_id) |
| 33 |
except orm_exc.NoResultFound: |
| 34 |
raise log_exc.ResourceNotFound(resource_id=fwg_id) |
| 35 |
|
| 36 |
if fwg['status'] != nl_const.ACTIVE: |
| 37 |
raise fwg_log_exc.FWGIsNotReadyForLogging( |
| 38 |
fwg_id=fwg_id, fwg_status=fwg['status']) |
| 39 |
|
| 40 |
|
| 41 |
def _check_fwg_port(context, port_id): |
| 42 |
|
| 43 |
|
| 44 |
port = ports.Port.get_object(context, id=port_id) |
| 45 |
if not port: |
| 46 |
raise log_exc.TargetResourceNotFound(target_id=port_id) |
| 47 |
|
| 48 |
device_owner = port.get('device_owner', '') |
| 49 |
|
| 50 |
if device_owner.startswith(nl_const.DEVICE_OWNER_COMPUTE_PREFIX): |
| 51 |
if not validators.validate_log_type_for_port( |
| 52 |
log_const.FIREWALL_GROUP, port): |
| 53 |
raise log_exc.LoggingTypeNotSupported( |
| 54 |
log_type=log_const.FIREWALL_GROUP, |
| 55 |
port_id=port_id) |
| 56 |
|
| 57 |
|
| 58 |
elif device_owner not in nl_const.ROUTER_INTERFACE_OWNERS: |
| 59 |
raise log_exc.LoggingTypeNotSupported( |
| 60 |
log_type=log_const.FIREWALL_GROUP, port_id=port_id) |
| 61 |
|
| 62 |
|
| 63 |
port_status = port.get('status') |
| 64 |
if port_status != nl_const.PORT_STATUS_ACTIVE: |
| 65 |
raise fwg_log_exc.PortIsNotReadyForLogging(target_id=port_id, |
| 66 |
port_status=port_status) |
| 67 |
|
| 68 |
|
| 69 |
fwg_id = fwg_plugin.driver.firewall_db.get_fwg_attached_to_port( |
| 70 |
context, port_id=port_id) |
| 71 |
|
| 72 |
if not fwg_id: |
| 73 |
raise fwg_log_exc.TargetResourceNotAssociated(target_id=port_id) |
| 74 |
|
| 75 |
fwg = fwg_plugin.get_firewall_group(context, id=fwg_id) |
| 76 |
|
| 77 |
if fwg['status'] != nl_const.ACTIVE: |
| 78 |
raise fwg_log_exc.FWGIsNotReadyForLogging(fwg_id=fwg_id, |
| 79 |
fwg_status=fwg['status']) |
| 80 |
|
| 81 |
|
| 82 |
def _check_target_resource_bound_fwg(context, fwg_id, target_id): |
| 83 |
ports = fwg_plugin.driver.firewall_db.get_ports_in_firewall_group( |
| 84 |
context=context, firewall_group_id=fwg_id) |
| 85 |
if target_id not in ports: |
| 86 |
raise log_exc.InvalidResourceConstraint( |
| 87 |
resource=log_const.FIREWALL_GROUP, |
| 88 |
resource_id=fwg_id, |
| 89 |
target_resource=log_const.TARGET_RESOURCE, |
| 90 |
target_id=target_id) |
| 91 |
|
| 92 |
|
| 93 |
@validators.ResourceValidateRequest.register(log_const.FIREWALL_GROUP) |
| 94 |
def validate_firewall_group_request(context, log_data): |
| 95 |
"""Validate a log request |
| 96 |
|
| 97 |
This method validates log request is satisfied or not. |
| 98 |
|
| 99 |
A ResourceNotFound will be raised if resource_id in log_data not exists or |
| 100 |
a TargetResourceNotFound will be raised if target_id in log_data not |
| 101 |
exists. Beside, FWGIsNotReadyForLogging will be raised in the case of |
| 102 |
queried firewall group is not in ACTIVE state. PortIsNotReadyForLogging |
| 103 |
exception will be raised if port is not in ACTIVE status. Besides, |
| 104 |
TargetResourceNotAssociated exception will be raised if a given port does |
| 105 |
not have any firewall group attach to. This method will also raise a |
| 106 |
LoggingTypeNotSupported, if there is no log_driver supporting for |
| 107 |
resource_type in log_data. |
| 108 |
|
| 109 |
In addition, if log_data specify both resource_id and target_id. A |
| 110 |
InvalidResourceConstraint will be raised if there is no constraint between |
| 111 |
resource_id and target_id. |
| 112 |
|
| 113 |
""" |
| 114 |
|
| 115 |
global fwg_plugin |
| 116 |
if not fwg_plugin: |
| 117 |
fwg_plugin = directory.get_plugin(fwaas_constants.FIREWALL_V2) |
| 118 |
resource_id = log_data.get('resource_id') |
| 119 |
target_id = log_data.get('target_id') |
| 120 |
if resource_id and target_id: |
| 121 |
_check_target_resource_bound_fwg(context, resource_id, target_id) |
| 122 |
if resource_id: |
| 123 |
_check_fwg(context, resource_id) |
| 124 |
if target_id: |
| 125 |
_check_fwg_port(context, target_id) |