| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
from neutron.objects.logapi import logging_resource as log_object |
| 17 |
from neutron.objects import ports as port_objects |
| 18 |
from neutron_lib import constants as nl_const |
| 19 |
from neutron_lib.plugins import directory |
| 20 |
|
| 21 |
from neutron_fwaas.common import fwaas_constants |
| 22 |
from neutron_fwaas.services.logapi import constants |
| 23 |
|
| 24 |
fw_plugin_db = None |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
def _get_ports_being_logged(context, log_obj): |
| 30 |
"""Return a list of ports being logged with a given log_obj""" |
| 31 |
|
| 32 |
target_id = log_obj['target_id'] |
| 33 |
resource_id = log_obj['resource_id'] |
| 34 |
|
| 35 |
|
| 36 |
if target_id: |
| 37 |
fwg_id = fw_plugin_db.get_fwg_attached_to_port(context, target_id) |
| 38 |
if fwg_id: |
| 39 |
port_ids = [target_id] |
| 40 |
else: |
| 41 |
port_ids = [] |
| 42 |
|
| 43 |
elif resource_id: |
| 44 |
port_ids = \ |
| 45 |
fw_plugin_db.get_ports_in_firewall_group(context, resource_id) |
| 46 |
|
| 47 |
else: |
| 48 |
tenant_id = log_obj['project_id'] |
| 49 |
port_ids = fw_plugin_db.get_fwg_ports_in_tenant(context, tenant_id) |
| 50 |
|
| 51 |
filtered_port_ids = [] |
| 52 |
for port_id in port_ids: |
| 53 |
port = port_objects.Port.get_object(context, id=port_id) |
| 54 |
device_owner = port.get('device_owner', '') |
| 55 |
|
| 56 |
|
| 57 |
if device_owner in nl_const.ROUTER_INTERFACE_OWNERS: |
| 58 |
|
| 59 |
if port.get('status') == nl_const.PORT_STATUS_ACTIVE: |
| 60 |
|
| 61 |
fwg = fw_plugin_db.get_fwg_attached_to_port(context, port_id) |
| 62 |
if fwg: |
| 63 |
filtered_port_ids.append(port_id) |
| 64 |
return filtered_port_ids |
| 65 |
|
| 66 |
|
| 67 |
def _make_log_info_dict(log_obj, port_ids): |
| 68 |
log_info = { |
| 69 |
'id': log_obj['id'], |
| 70 |
'ports_log': port_ids, |
| 71 |
'event': log_obj['event'], |
| 72 |
'project_id': log_obj['project_id'] |
| 73 |
} |
| 74 |
return log_info |
| 75 |
|
| 76 |
|
| 77 |
def get_logs_for_port(context, port_id): |
| 78 |
"""Return a list of log_resources bound to a given port_id""" |
| 79 |
|
| 80 |
global fw_plugin_db |
| 81 |
if not fw_plugin_db: |
| 82 |
fw_plugin = directory.get_plugin(fwaas_constants.FIREWALL_V2) |
| 83 |
|
| 84 |
|
| 85 |
if not fw_plugin: |
| 86 |
return [] |
| 87 |
fw_plugin_db = fw_plugin.driver.firewall_db |
| 88 |
|
| 89 |
logs_bounded = [] |
| 90 |
port = port_objects.Port.get_object(context, id=port_id) |
| 91 |
|
| 92 |
if not port: |
| 93 |
return logs_bounded |
| 94 |
|
| 95 |
device_owner = port.get('device_owner', '') |
| 96 |
if device_owner not in nl_const.ROUTER_INTERFACE_OWNERS: |
| 97 |
return logs_bounded |
| 98 |
|
| 99 |
|
| 100 |
fwg_id = fw_plugin_db.get_fwg_attached_to_port(context, port_id) |
| 101 |
if not fwg_id: |
| 102 |
return logs_bounded |
| 103 |
|
| 104 |
project_id = port['project_id'] |
| 105 |
log_objs = log_object.Log.get_objects( |
| 106 |
context, project_id=project_id, |
| 107 |
resource_type=constants.FIREWALL_GROUP, enabled=True) |
| 108 |
|
| 109 |
for log_obj in log_objs: |
| 110 |
if log_obj.resource_id == fwg_id: |
| 111 |
logs_bounded.append(log_obj) |
| 112 |
elif log_obj.target_id == port['id']: |
| 113 |
logs_bounded.append(log_obj) |
| 114 |
elif not log_obj.target_id and not log_obj.resource_id: |
| 115 |
logs_bounded.append(log_obj) |
| 116 |
return logs_bounded |
| 117 |
|
| 118 |
|
| 119 |
def get_logs_for_fwg(context, fwg_id, ports_delta): |
| 120 |
"""Return a list of log_resources bound to a firewall group""" |
| 121 |
|
| 122 |
global fw_plugin_db |
| 123 |
if not fw_plugin_db: |
| 124 |
fw_plugin = directory.get_plugin(fwaas_constants.FIREWALL_V2) |
| 125 |
|
| 126 |
|
| 127 |
if not fw_plugin: |
| 128 |
return [] |
| 129 |
fw_plugin_db = fw_plugin.driver.firewall_db |
| 130 |
|
| 131 |
project_id = context.tenant_id |
| 132 |
log_objs = log_object.Log.get_objects( |
| 133 |
context, project_id=project_id, |
| 134 |
resource_type=constants.FIREWALL_GROUP, enabled=True) |
| 135 |
|
| 136 |
log_resources = [] |
| 137 |
for log_obj in log_objs: |
| 138 |
if log_obj.resource_id == fwg_id: |
| 139 |
log_resources.append(log_obj) |
| 140 |
elif log_obj.target_id in ports_delta: |
| 141 |
log_resources.append(log_obj) |
| 142 |
elif not log_obj.resource_id and not log_obj.target_id: |
| 143 |
log_resources.append(log_obj) |
| 144 |
return log_resources |
| 145 |
|
| 146 |
|
| 147 |
def get_fwg_log_info_for_port(context, port_ids): |
| 148 |
"""Return a list of firewall group log info for a given port |
| 149 |
The list has format as below: |
| 150 |
|
| 151 |
[ |
| 152 |
{ |
| 153 |
'event': 'ALL', |
| 154 |
'id': '733e0499-e69e-4106-a84a-635fbc5fbbc0', |
| 155 |
'project_id': '46f70361-ba71-4bd0-9769-3573fd227c4b', |
| 156 |
'ports_log': |
| 157 |
[ |
| 158 |
port1_id, |
| 159 |
port2_id, |
| 160 |
] |
| 161 |
}, |
| 162 |
] |
| 163 |
:param context: current running context information |
| 164 |
:param port_ids: list of ports which needed to get firewall group log info |
| 165 |
|
| 166 |
""" |
| 167 |
|
| 168 |
global fw_plugin_db |
| 169 |
if not fw_plugin_db: |
| 170 |
fw_plugin = directory.get_plugin(fwaas_constants.FIREWALL_V2) |
| 171 |
|
| 172 |
|
| 173 |
if not fw_plugin: |
| 174 |
return [] |
| 175 |
fw_plugin_db = fw_plugin.driver.firewall_db |
| 176 |
|
| 177 |
logs_info = [] |
| 178 |
log_bounds = set() |
| 179 |
for port_id in port_ids: |
| 180 |
log_objs = get_logs_for_port(context, port_id) |
| 181 |
if log_objs: |
| 182 |
log_bounds |= set(log_objs) |
| 183 |
if log_bounds: |
| 184 |
for log_resource in log_bounds: |
| 185 |
port_ids = _get_ports_being_logged(context, log_resource) |
| 186 |
log_info = _make_log_info_dict(log_resource, port_ids) |
| 187 |
logs_info.append(log_info) |
| 188 |
return logs_info |
| 189 |
|
| 190 |
|
| 191 |
def get_fwg_log_info_for_log_resources(context, log_resources): |
| 192 |
"""Return a list of firewall group log info for list of log_resources |
| 193 |
The list has format as below: |
| 194 |
|
| 195 |
[ |
| 196 |
{ |
| 197 |
'event': 'ALL', |
| 198 |
'id': '733e0499-e69e-4106-a84a-635fbc5fbbc0', |
| 199 |
'project_id': '46f70361-ba71-4bd0-9769-3573fd227c4b', |
| 200 |
'ports_log': |
| 201 |
[ |
| 202 |
port1_id, |
| 203 |
port2_id, |
| 204 |
] |
| 205 |
}, |
| 206 |
] |
| 207 |
:param context: current running context information |
| 208 |
:param log_resources: list of log_resources, which needed to get firewall |
| 209 |
groups log info |
| 210 |
|
| 211 |
""" |
| 212 |
|
| 213 |
global fw_plugin_db |
| 214 |
if not fw_plugin_db: |
| 215 |
fw_plugin = directory.get_plugin(fwaas_constants.FIREWALL_V2) |
| 216 |
|
| 217 |
|
| 218 |
if not fw_plugin: |
| 219 |
return [] |
| 220 |
fw_plugin_db = fw_plugin.driver.firewall_db |
| 221 |
|
| 222 |
logs_info = [] |
| 223 |
for log_resource in log_resources: |
| 224 |
ports_id = _get_ports_being_logged(context, log_resource) |
| 225 |
log_info = _make_log_info_dict(log_resource, ports_id) |
| 226 |
logs_info.append(log_info) |
| 227 |
|
| 228 |
return logs_info |