| 1 |
# Copyright 2016-2017 FUJITSU LIMITED |
| 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 |
# |
| 16 |
import logging |
| 17 |
|
| 18 |
from cliff import columns as cliff_columns |
| 19 |
from osc_lib.command import command |
| 20 |
from osc_lib import exceptions |
| 21 |
from osc_lib import utils |
| 22 |
from osc_lib.utils import columns as column_util |
| 23 |
|
| 24 |
from neutronclient._i18n import _ |
| 25 |
from neutronclient.common import utils as nc_utils |
| 26 |
from neutronclient.osc import utils as osc_utils |
| 27 |
from neutronclient.osc.v2.fwaas import constants as const |
| 28 |
|
| 29 |
|
| 30 |
LOG = logging.getLogger(__name__) |
| 31 |
|
| 32 |
|
| 33 |
_attr_map_dict = { |
| 34 |
'id': 'ID', |
| 35 |
'name': 'Name', |
| 36 |
'enabled': 'Enabled', |
| 37 |
'summary': 'Summary', |
| 38 |
'description': 'Description', |
| 39 |
'firewall_policy_id': 'Firewall Policy', |
| 40 |
'ip_version': 'IP Version', |
| 41 |
'action': 'Action', |
| 42 |
'protocol': 'Protocol', |
| 43 |
'source_ip_address': 'Source IP Address', |
| 44 |
'source_port': 'Source Port', |
| 45 |
'destination_ip_address': 'Destination IP Address', |
| 46 |
'destination_port': 'Destination Port', |
| 47 |
'shared': 'Shared', |
| 48 |
'source_firewall_group_id': 'Source Firewall Group ID', |
| 49 |
'destination_firewall_group_id': 'Destination Firewall Group ID', |
| 50 |
'tenant_id': 'Project', |
| 51 |
'project_id': 'Project', |
| 52 |
} |
| 53 |
|
| 54 |
_attr_map = ( |
| 55 |
('id', 'ID', column_util.LIST_BOTH), |
| 56 |
('name', 'Name', column_util.LIST_BOTH), |
| 57 |
('enabled', 'Enabled', column_util.LIST_BOTH), |
| 58 |
('summary', 'Summary', column_util.LIST_SHORT_ONLY), |
| 59 |
('description', 'Description', column_util.LIST_LONG_ONLY), |
| 60 |
('firewall_policy_id', 'Firewall Policy', column_util.LIST_BOTH), |
| 61 |
('ip_version', 'IP Version', column_util.LIST_LONG_ONLY), |
| 62 |
('action', 'Action', column_util.LIST_LONG_ONLY), |
| 63 |
('protocol', 'Protocol', column_util.LIST_LONG_ONLY), |
| 64 |
('source_ip_address', 'Source IP Address', column_util.LIST_LONG_ONLY), |
| 65 |
('source_port', 'Source Port', column_util.LIST_LONG_ONLY), |
| 66 |
('destination_ip_address', 'Destination IP Address', |
| 67 |
column_util.LIST_LONG_ONLY), |
| 68 |
('destination_port', 'Destination Port', column_util.LIST_LONG_ONLY), |
| 69 |
('shared', 'Shared', column_util.LIST_LONG_ONLY), |
| 70 |
('tenant_id', 'Project', column_util.LIST_LONG_ONLY), |
| 71 |
('source_firewall_group_id', 'Source Firewall Group ID', |
| 72 |
column_util.LIST_LONG_ONLY), |
| 73 |
('destination_firewall_group_id', 'Destination Firewall Group ID', |
| 74 |
column_util.LIST_LONG_ONLY), |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
def _get_common_parser(parser): |
| 79 |
parser.add_argument( |
| 80 |
'--name', |
| 81 |
metavar='<name>', |
| 82 |
help=_('Name of the firewall rule')) |
| 83 |
parser.add_argument( |
| 84 |
'--description', |
| 85 |
metavar='<description>', |
| 86 |
help=_('Description of the firewall rule')) |
| 87 |
parser.add_argument( |
| 88 |
'--protocol', |
| 89 |
type=nc_utils.convert_to_lowercase, |
| 90 |
help=_('IP protocol (ah, dccp, egp, esp, gre, icmp, igmp, ' |
| 91 |
'ipv6-encap, ipv6-frag, ipv6-icmp, ipv6-nonxt, ipv6-opts, ' |
| 92 |
'ipv6-route, ospf, pgm, rsvp, sctp, tcp, udp, udplite, ' |
| 93 |
'vrrp and integer representations [0-255] or any; ' |
| 94 |
'default: any (all protocols))')) |
| 95 |
parser.add_argument( |
| 96 |
'--action', |
| 97 |
choices=['allow', 'deny', 'reject'], |
| 98 |
type=nc_utils.convert_to_lowercase, |
| 99 |
help=_('Action for the firewall rule')) |
| 100 |
parser.add_argument( |
| 101 |
'--ip-version', |
| 102 |
metavar='<ip-version>', |
| 103 |
choices=['4', '6'], |
| 104 |
help=_('Set IP version 4 or 6 (default is 4)')) |
| 105 |
src_ip_group = parser.add_mutually_exclusive_group() |
| 106 |
src_ip_group.add_argument( |
| 107 |
'--source-ip-address', |
| 108 |
metavar='<source-ip-address>', |
| 109 |
help=_('Source IP address or subnet')) |
| 110 |
src_ip_group.add_argument( |
| 111 |
'--no-source-ip-address', |
| 112 |
action='store_true', |
| 113 |
help=_('Detach source IP address')) |
| 114 |
dst_ip_group = parser.add_mutually_exclusive_group() |
| 115 |
dst_ip_group.add_argument( |
| 116 |
'--destination-ip-address', |
| 117 |
metavar='<destination-ip-address>', |
| 118 |
help=_('Destination IP address or subnet')) |
| 119 |
dst_ip_group.add_argument( |
| 120 |
'--no-destination-ip-address', |
| 121 |
action='store_true', |
| 122 |
help=_('Detach destination IP address')) |
| 123 |
src_port_group = parser.add_mutually_exclusive_group() |
| 124 |
src_port_group.add_argument( |
| 125 |
'--source-port', |
| 126 |
metavar='<source-port>', |
| 127 |
help=_('Source port number or range' |
| 128 |
'(integer in [1, 65535] or range like 123:456)')) |
| 129 |
src_port_group.add_argument( |
| 130 |
'--no-source-port', |
| 131 |
action='store_true', |
| 132 |
help=_('Detach source port number or range')) |
| 133 |
dst_port_group = parser.add_mutually_exclusive_group() |
| 134 |
dst_port_group.add_argument( |
| 135 |
'--destination-port', |
| 136 |
metavar='<destination-port>', |
| 137 |
help=_('Destination port number or range' |
| 138 |
'(integer in [1, 65535] or range like 123:456)')) |
| 139 |
dst_port_group.add_argument( |
| 140 |
'--no-destination-port', |
| 141 |
action='store_true', |
| 142 |
help=_('Detach destination port number or range')) |
| 143 |
shared_group = parser.add_mutually_exclusive_group() |
| 144 |
shared_group.add_argument( |
| 145 |
'--share', |
| 146 |
action='store_true', |
| 147 |
help=_('Share the firewall rule to be used in all projects ' |
| 148 |
'(by default, it is restricted to be used by the ' |
| 149 |
'current project).')) |
| 150 |
shared_group.add_argument( |
| 151 |
'--no-share', |
| 152 |
action='store_true', |
| 153 |
help=_('Restrict use of the firewall rule to the current project')) |
| 154 |
enable_group = parser.add_mutually_exclusive_group() |
| 155 |
enable_group.add_argument( |
| 156 |
'--enable-rule', |
| 157 |
action='store_true', |
| 158 |
help=_('Enable this rule (default is enabled)')) |
| 159 |
enable_group.add_argument( |
| 160 |
'--disable-rule', |
| 161 |
action='store_true', |
| 162 |
help=_('Disable this rule')) |
| 163 |
src_fwg_group = parser.add_mutually_exclusive_group() |
| 164 |
src_fwg_group.add_argument( |
| 165 |
'--source-firewall-group', |
| 166 |
metavar='<source-firewall-group>', |
| 167 |
help=_('Source firewall group (name or ID)')) |
| 168 |
src_fwg_group.add_argument( |
| 169 |
'--no-source-firewall-group', |
| 170 |
action='store_true', |
| 171 |
help=_('No associated destination firewall group')) |
| 172 |
dst_fwg_group = parser.add_mutually_exclusive_group() |
| 173 |
dst_fwg_group.add_argument( |
| 174 |
'--destination-firewall-group', |
| 175 |
metavar='<destination-firewall-group>', |
| 176 |
help=_('Destination firewall group (name or ID)')) |
| 177 |
dst_fwg_group.add_argument( |
| 178 |
'--no-destination-firewall-group', |
| 179 |
action='store_true', |
| 180 |
help=_('No associated destination firewall group')) |
| 181 |
return parser |
| 182 |
|
| 183 |
|
| 184 |
def _get_common_attrs(client_manager, parsed_args, is_create=True): |
| 185 |
attrs = {} |
| 186 |
client = client_manager.network |
| 187 |
if is_create: |
| 188 |
if 'project' in parsed_args and parsed_args.project is not None: |
| 189 |
attrs['tenant_id'] = osc_utils.find_project( |
| 190 |
client_manager.identity, |
| 191 |
parsed_args.project, |
| 192 |
parsed_args.project_domain, |
| 193 |
).id |
| 194 |
if parsed_args.name: |
| 195 |
attrs['name'] = str(parsed_args.name) |
| 196 |
if parsed_args.description: |
| 197 |
attrs['description'] = str(parsed_args.description) |
| 198 |
if parsed_args.protocol: |
| 199 |
protocol = parsed_args.protocol |
| 200 |
attrs['protocol'] = None if protocol == 'any' else protocol |
| 201 |
if parsed_args.action: |
| 202 |
attrs['action'] = parsed_args.action |
| 203 |
if parsed_args.ip_version: |
| 204 |
attrs['ip_version'] = str(parsed_args.ip_version) |
| 205 |
if parsed_args.source_port: |
| 206 |
attrs['source_port'] = parsed_args.source_port |
| 207 |
if parsed_args.no_source_port: |
| 208 |
attrs['source_port'] = None |
| 209 |
if parsed_args.source_ip_address: |
| 210 |
attrs['source_ip_address'] = parsed_args.source_ip_address |
| 211 |
if parsed_args.no_source_ip_address: |
| 212 |
attrs['source_ip_address'] = None |
| 213 |
if parsed_args.destination_port: |
| 214 |
attrs['destination_port'] = str(parsed_args.destination_port) |
| 215 |
if parsed_args.no_destination_port: |
| 216 |
attrs['destination_port'] = None |
| 217 |
if parsed_args.destination_ip_address: |
| 218 |
attrs['destination_ip_address'] = str( |
| 219 |
parsed_args.destination_ip_address) |
| 220 |
if parsed_args.no_destination_ip_address: |
| 221 |
attrs['destination_ip_address'] = None |
| 222 |
if parsed_args.enable_rule: |
| 223 |
attrs['enabled'] = True |
| 224 |
if parsed_args.disable_rule: |
| 225 |
attrs['enabled'] = False |
| 226 |
if parsed_args.share: |
| 227 |
attrs['shared'] = True |
| 228 |
if parsed_args.no_share: |
| 229 |
attrs['shared'] = False |
| 230 |
if parsed_args.source_firewall_group: |
| 231 |
attrs['source_firewall_group_id'] = client.find_firewall_group( |
| 232 |
parsed_args.source_firewall_group)['id'] |
| 233 |
if parsed_args.no_source_firewall_group: |
| 234 |
attrs['source_firewall_group_id'] = None |
| 235 |
if parsed_args.destination_firewall_group: |
| 236 |
attrs['destination_firewall_group_id'] = client.find_firewall_group( |
| 237 |
parsed_args.destination_firewall_group)['id'] |
| 238 |
if parsed_args.no_destination_firewall_group: |
| 239 |
attrs['destination_firewall_group_id'] = None |
| 240 |
return attrs |
| 241 |
|
| 242 |
|
| 243 |
class ProtocolColumn(cliff_columns.FormattableColumn): |
| 244 |
def human_readable(self): |
| 245 |
return self._value if self._value else 'any' |
| 246 |
|
| 247 |
|
| 248 |
_formatters = {'protocol': ProtocolColumn} |
| 249 |
|
| 250 |
|
| 251 |
class CreateFirewallRule(command.ShowOne): |
| 252 |
_description = _("Create a new firewall rule") |
| 253 |
|
| 254 |
def get_parser(self, prog_name): |
| 255 |
parser = super(CreateFirewallRule, self).get_parser(prog_name) |
| 256 |
_get_common_parser(parser) |
| 257 |
osc_utils.add_project_owner_option_to_parser(parser) |
| 258 |
return parser |
| 259 |
|
| 260 |
def take_action(self, parsed_args): |
| 261 |
client = self.app.client_manager.network |
| 262 |
attrs = _get_common_attrs(self.app.client_manager, parsed_args) |
| 263 |
obj = client.create_firewall_rule(**attrs) |
| 264 |
display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 265 |
obj, _attr_map_dict, ['location', 'tenant_id']) |
| 266 |
data = utils.get_dict_properties(obj, columns, formatters=_formatters) |
| 267 |
return display_columns, data |
| 268 |
|
| 269 |
|
| 270 |
class DeleteFirewallRule(command.Command): |
| 271 |
_description = _("Delete firewall rule(s)") |
| 272 |
|
| 273 |
def get_parser(self, prog_name): |
| 274 |
parser = super(DeleteFirewallRule, self).get_parser(prog_name) |
| 275 |
parser.add_argument( |
| 276 |
const.FWR, |
| 277 |
metavar='<firewall-rule>', |
| 278 |
nargs='+', |
| 279 |
help=_('Firewall rule(s) to delete (name or ID)')) |
| 280 |
return parser |
| 281 |
|
| 282 |
def take_action(self, parsed_args): |
| 283 |
client = self.app.client_manager.network |
| 284 |
result = 0 |
| 285 |
for fwr in parsed_args.firewall_rule: |
| 286 |
try: |
| 287 |
fwr_id = client.find_firewall_rule(fwr)['id'] |
| 288 |
client.delete_firewall_rule(fwr_id) |
| 289 |
except Exception as e: |
| 290 |
result += 1 |
| 291 |
LOG.error(_("Failed to delete Firewall rule with " |
| 292 |
"name or ID '%(firewall_rule)s': %(e)s"), |
| 293 |
{const.FWR: fwr, 'e': e}) |
| 294 |
|
| 295 |
if result > 0: |
| 296 |
total = len(parsed_args.firewall_rule) |
| 297 |
msg = (_("%(result)s of %(total)s firewall rule(s) failed " |
| 298 |
"to delete.") % {'result': result, 'total': total}) |
| 299 |
raise exceptions.CommandError(msg) |
| 300 |
|
| 301 |
|
| 302 |
class ListFirewallRule(command.Lister): |
| 303 |
_description = _("List firewall rules that belong to a given tenant") |
| 304 |
|
| 305 |
def get_parser(self, prog_name): |
| 306 |
parser = super(ListFirewallRule, self).get_parser(prog_name) |
| 307 |
parser.add_argument( |
| 308 |
'--long', |
| 309 |
action='store_true', |
| 310 |
default=False, |
| 311 |
help=_("List additional fields in output") |
| 312 |
) |
| 313 |
return parser |
| 314 |
|
| 315 |
def extend_list(self, data, parsed_args): |
| 316 |
ext_data = [] |
| 317 |
for d in data: |
| 318 |
protocol = d['protocol'].upper() if d['protocol'] else 'ANY' |
| 319 |
src_ip = 'none specified' |
| 320 |
dst_ip = 'none specified' |
| 321 |
src_port = '(none specified)' |
| 322 |
dst_port = '(none specified)' |
| 323 |
if 'source_ip_address' in d and d['source_ip_address']: |
| 324 |
src_ip = str(d['source_ip_address']).lower() |
| 325 |
if 'source_port' in d and d['source_port']: |
| 326 |
src_port = '(' + str(d['source_port']).lower() + ')' |
| 327 |
if 'destination_ip_address' in d and d['destination_ip_address']: |
| 328 |
dst_ip = str(d['destination_ip_address']).lower() |
| 329 |
if 'destination_port' in d and d['destination_port']: |
| 330 |
dst_port = '(' + str(d['destination_port']).lower() + ')' |
| 331 |
action = d['action'] if d.get('action') else 'no-action' |
| 332 |
src = 'source(port): ' + src_ip + src_port |
| 333 |
dst = 'dest(port): ' + dst_ip + dst_port |
| 334 |
d['summary'] = ',\n '.join([protocol, src, dst, action]) |
| 335 |
ext_data.append(d) |
| 336 |
return ext_data |
| 337 |
|
| 338 |
def take_action(self, parsed_args): |
| 339 |
client = self.app.client_manager.network |
| 340 |
obj = client.firewall_rules() |
| 341 |
obj_extend = self.extend_list(obj, parsed_args) |
| 342 |
headers, columns = column_util.get_column_definitions( |
| 343 |
_attr_map, long_listing=parsed_args.long) |
| 344 |
return (headers, (utils.get_dict_properties( |
| 345 |
s, columns, formatters=_formatters) for s in obj_extend)) |
| 346 |
|
| 347 |
|
| 348 |
class SetFirewallRule(command.Command): |
| 349 |
_description = _("Set firewall rule properties") |
| 350 |
|
| 351 |
def get_parser(self, prog_name): |
| 352 |
parser = super(SetFirewallRule, self).get_parser(prog_name) |
| 353 |
_get_common_parser(parser) |
| 354 |
parser.add_argument( |
| 355 |
const.FWR, |
| 356 |
metavar='<firewall-rule>', |
| 357 |
help=_('Firewall rule to set (name or ID)')) |
| 358 |
return parser |
| 359 |
|
| 360 |
def take_action(self, parsed_args): |
| 361 |
client = self.app.client_manager.network |
| 362 |
attrs = _get_common_attrs(self.app.client_manager, |
| 363 |
parsed_args, is_create=False) |
| 364 |
fwr_id = client.find_firewall_rule(parsed_args.firewall_rule)['id'] |
| 365 |
try: |
| 366 |
client.update_firewall_rule(fwr_id, **attrs) |
| 367 |
except Exception as e: |
| 368 |
msg = (_("Failed to set firewall rule '%(rule)s': %(e)s") |
| 369 |
% {'rule': parsed_args.firewall_rule, 'e': e}) |
| 370 |
raise exceptions.CommandError(msg) |
| 371 |
|
| 372 |
|
| 373 |
class ShowFirewallRule(command.ShowOne): |
| 374 |
_description = _("Display firewall rule details") |
| 375 |
|
| 376 |
def get_parser(self, prog_name): |
| 377 |
parser = super(ShowFirewallRule, self).get_parser(prog_name) |
| 378 |
parser.add_argument( |
| 379 |
const.FWR, |
| 380 |
metavar='<firewall-rule>', |
| 381 |
help=_('Firewall rule to display (name or ID)')) |
| 382 |
return parser |
| 383 |
|
| 384 |
def take_action(self, parsed_args): |
| 385 |
client = self.app.client_manager.network |
| 386 |
fwr_id = client.find_firewall_rule(parsed_args.firewall_rule)['id'] |
| 387 |
obj = client.get_firewall_rule(fwr_id) |
| 388 |
display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 389 |
obj, _attr_map_dict, ['location', 'tenant_id']) |
| 390 |
data = utils.get_dict_properties(obj, columns, formatters=_formatters) |
| 391 |
return (display_columns, data) |
| 392 |
|
| 393 |
|
| 394 |
class UnsetFirewallRule(command.Command): |
| 395 |
_description = _("Unset firewall rule properties") |
| 396 |
|
| 397 |
def get_parser(self, prog_name): |
| 398 |
parser = super(UnsetFirewallRule, self).get_parser(prog_name) |
| 399 |
parser.add_argument( |
| 400 |
const.FWR, |
| 401 |
metavar='<firewall-rule>', |
| 402 |
help=_('Firewall rule to unset (name or ID)')) |
| 403 |
parser.add_argument( |
| 404 |
'--source-ip-address', |
| 405 |
action='store_true', |
| 406 |
help=_('Source IP address or subnet')) |
| 407 |
parser.add_argument( |
| 408 |
'--destination-ip-address', |
| 409 |
action='store_true', |
| 410 |
help=_('Destination IP address or subnet')) |
| 411 |
parser.add_argument( |
| 412 |
'--source-port', |
| 413 |
action='store_true', |
| 414 |
help=_('Source port number or range' |
| 415 |
'(integer in [1, 65535] or range like 123:456)')) |
| 416 |
parser.add_argument( |
| 417 |
'--destination-port', |
| 418 |
action='store_true', |
| 419 |
help=_('Destination port number or range' |
| 420 |
'(integer in [1, 65535] or range like 123:456)')) |
| 421 |
parser.add_argument( |
| 422 |
'--share', |
| 423 |
action='store_true', |
| 424 |
help=_('Restrict use of the firewall rule to the current project')) |
| 425 |
parser.add_argument( |
| 426 |
'--enable-rule', |
| 427 |
action='store_true', |
| 428 |
help=_('Disable this rule')) |
| 429 |
|
| 430 |
parser.add_argument( |
| 431 |
'--source-firewall-group', |
| 432 |
action='store_true', |
| 433 |
help=_('Source firewall group (name or ID)')) |
| 434 |
|
| 435 |
parser.add_argument( |
| 436 |
'--destination-firewall-group', |
| 437 |
action='store_true', |
| 438 |
help=_('Destination firewall group (name or ID)')) |
| 439 |
return parser |
| 440 |
|
| 441 |
def _get_attrs(self, client_manager, parsed_args): |
| 442 |
attrs = {} |
| 443 |
if parsed_args.source_ip_address: |
| 444 |
attrs['source_ip_address'] = None |
| 445 |
if parsed_args.source_port: |
| 446 |
attrs['source_port'] = None |
| 447 |
if parsed_args.destination_ip_address: |
| 448 |
attrs['destination_ip_address'] = None |
| 449 |
if parsed_args.destination_port: |
| 450 |
attrs['destination_port'] = None |
| 451 |
if parsed_args.share: |
| 452 |
attrs['shared'] = False |
| 453 |
if parsed_args.enable_rule: |
| 454 |
attrs['enabled'] = False |
| 455 |
if parsed_args.source_firewall_group: |
| 456 |
attrs['source_firewall_group_id'] = None |
| 457 |
if parsed_args.source_firewall_group: |
| 458 |
attrs['destination_firewall_group_id'] = None |
| 459 |
return attrs |
| 460 |
|
| 461 |
def take_action(self, parsed_args): |
| 462 |
client = self.app.client_manager.network |
| 463 |
attrs = self._get_attrs(self.app.client_manager, parsed_args) |
| 464 |
fwr_id = client.find_firewall_rule(parsed_args.firewall_rule)['id'] |
| 465 |
try: |
| 466 |
client.update_firewall_rule(fwr_id, **attrs) |
| 467 |
except Exception as e: |
| 468 |
msg = (_("Failed to unset firewall rule '%(rule)s': %(e)s") |
| 469 |
% {'rule': parsed_args.firewall_rule, 'e': e}) |
| 470 |
raise exceptions.CommandError(msg) |