| 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 |
|
| 17 |
import logging |
| 18 |
|
| 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.osc import utils as osc_utils |
| 26 |
from neutronclient.osc.v2.fwaas import constants as const |
| 27 |
|
| 28 |
|
| 29 |
LOG = logging.getLogger(__name__) |
| 30 |
|
| 31 |
_formatters = {} |
| 32 |
|
| 33 |
|
| 34 |
_attr_map_dict = { |
| 35 |
'id': 'ID', |
| 36 |
'name': 'Name', |
| 37 |
'description': 'Description', |
| 38 |
'firewall_rules': 'Firewall Rules', |
| 39 |
'audited': 'Audited', |
| 40 |
'shared': 'Shared', |
| 41 |
'tenant_id': 'Project', |
| 42 |
'project_id': 'Project', |
| 43 |
} |
| 44 |
|
| 45 |
_attr_map = ( |
| 46 |
('id', 'ID', column_util.LIST_BOTH), |
| 47 |
('name', 'Name', column_util.LIST_BOTH), |
| 48 |
('firewall_rules', 'Firewall Rules', column_util.LIST_BOTH), |
| 49 |
('description', 'Description', column_util.LIST_LONG_ONLY), |
| 50 |
('audited', 'Audited', column_util.LIST_LONG_ONLY), |
| 51 |
('shared', 'Shared', column_util.LIST_LONG_ONLY), |
| 52 |
('tenant_id', 'Project', column_util.LIST_LONG_ONLY), |
| 53 |
) |
| 54 |
|
| 55 |
|
| 56 |
def _get_common_attrs(client_manager, parsed_args, is_create=True): |
| 57 |
attrs = {} |
| 58 |
client = client_manager.network |
| 59 |
|
| 60 |
if is_create: |
| 61 |
if 'project' in parsed_args and parsed_args.project is not None: |
| 62 |
attrs['tenant_id'] = osc_utils.find_project( |
| 63 |
client_manager.identity, |
| 64 |
parsed_args.project, |
| 65 |
parsed_args.project_domain, |
| 66 |
).id |
| 67 |
if parsed_args.firewall_rule and parsed_args.no_firewall_rule: |
| 68 |
_firewall_rules = [] |
| 69 |
for f in parsed_args.firewall_rule: |
| 70 |
_firewall_rules.append(client.find_firewall_rule(f)['id']) |
| 71 |
attrs[const.FWRS] = _firewall_rules |
| 72 |
elif parsed_args.firewall_rule: |
| 73 |
rules = [] |
| 74 |
if not is_create: |
| 75 |
foobar = client.find_firewall_policy( |
| 76 |
parsed_args.firewall_policy) |
| 77 |
rules += foobar[const.FWRS] |
| 78 |
for f in parsed_args.firewall_rule: |
| 79 |
rules.append(client.find_firewall_rule(f)['id']) |
| 80 |
attrs[const.FWRS] = rules |
| 81 |
elif parsed_args.no_firewall_rule: |
| 82 |
attrs[const.FWRS] = [] |
| 83 |
if parsed_args.audited: |
| 84 |
attrs['audited'] = True |
| 85 |
if parsed_args.no_audited: |
| 86 |
attrs['audited'] = False |
| 87 |
if parsed_args.name: |
| 88 |
attrs['name'] = str(parsed_args.name) |
| 89 |
if parsed_args.description: |
| 90 |
attrs['description'] = str(parsed_args.description) |
| 91 |
if parsed_args.share: |
| 92 |
attrs['shared'] = True |
| 93 |
if parsed_args.no_share: |
| 94 |
attrs['shared'] = False |
| 95 |
return attrs |
| 96 |
|
| 97 |
|
| 98 |
def _get_common_parser(parser): |
| 99 |
parser.add_argument( |
| 100 |
'--description', |
| 101 |
help=_('Description of the firewall policy')) |
| 102 |
audited_group = parser.add_mutually_exclusive_group() |
| 103 |
audited_group.add_argument( |
| 104 |
'--audited', |
| 105 |
action='store_true', |
| 106 |
help=_('Enable auditing for the policy')) |
| 107 |
audited_group.add_argument( |
| 108 |
'--no-audited', |
| 109 |
action='store_true', |
| 110 |
help=_('Disable auditing for the policy')) |
| 111 |
shared_group = parser.add_mutually_exclusive_group() |
| 112 |
shared_group.add_argument( |
| 113 |
'--share', |
| 114 |
action='store_true', |
| 115 |
help=_('Share the firewall policy to be used in all projects ' |
| 116 |
'(by default, it is restricted to be used by the ' |
| 117 |
'current project).')) |
| 118 |
shared_group.add_argument( |
| 119 |
'--no-share', |
| 120 |
action='store_true', |
| 121 |
help=_('Restrict use of the firewall policy to the ' |
| 122 |
'current project')) |
| 123 |
return parser |
| 124 |
|
| 125 |
|
| 126 |
class CreateFirewallPolicy(command.ShowOne): |
| 127 |
_description = _("Create a new firewall policy") |
| 128 |
|
| 129 |
def get_parser(self, prog_name): |
| 130 |
parser = super(CreateFirewallPolicy, self).get_parser(prog_name) |
| 131 |
_get_common_parser(parser) |
| 132 |
osc_utils.add_project_owner_option_to_parser(parser) |
| 133 |
parser.add_argument( |
| 134 |
'name', |
| 135 |
metavar='<name>', |
| 136 |
help=_('Name for the firewall policy')) |
| 137 |
fwr_group = parser.add_mutually_exclusive_group() |
| 138 |
fwr_group.add_argument( |
| 139 |
'--firewall-rule', |
| 140 |
action='append', |
| 141 |
metavar='<firewall-rule>', |
| 142 |
help=_('Firewall rule(s) to apply (name or ID)')) |
| 143 |
fwr_group.add_argument( |
| 144 |
'--no-firewall-rule', |
| 145 |
action='store_true', |
| 146 |
help=_('Unset all firewall rules from firewall policy')) |
| 147 |
return parser |
| 148 |
|
| 149 |
def take_action(self, parsed_args): |
| 150 |
client = self.app.client_manager.network |
| 151 |
attrs = _get_common_attrs(self.app.client_manager, parsed_args) |
| 152 |
obj = client.create_firewall_policy(**attrs) |
| 153 |
display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 154 |
obj, _attr_map_dict, ['location', 'tenant_id']) |
| 155 |
data = utils.get_dict_properties(obj, columns, formatters=_formatters) |
| 156 |
return (display_columns, data) |
| 157 |
|
| 158 |
|
| 159 |
class DeleteFirewallPolicy(command.Command): |
| 160 |
_description = _("Delete firewall policy(s)") |
| 161 |
|
| 162 |
def get_parser(self, prog_name): |
| 163 |
parser = super(DeleteFirewallPolicy, self).get_parser(prog_name) |
| 164 |
parser.add_argument( |
| 165 |
const.FWP, |
| 166 |
metavar='<firewall-policy>', |
| 167 |
nargs='+', |
| 168 |
help=_('Firewall policy(s) to delete (name or ID)')) |
| 169 |
return parser |
| 170 |
|
| 171 |
def take_action(self, parsed_args): |
| 172 |
client = self.app.client_manager.network |
| 173 |
result = 0 |
| 174 |
for fwp in parsed_args.firewall_policy: |
| 175 |
try: |
| 176 |
fwp_id = client.find_firewall_policy(fwp)['id'] |
| 177 |
client.delete_firewall_policy(fwp_id) |
| 178 |
except Exception as e: |
| 179 |
result += 1 |
| 180 |
LOG.error(_("Failed to delete Firewall policy with " |
| 181 |
"name or ID '%(firewall_policy)s': %(e)s"), |
| 182 |
{const.FWP: fwp, 'e': e}) |
| 183 |
|
| 184 |
if result > 0: |
| 185 |
total = len(parsed_args.firewall_policy) |
| 186 |
msg = (_("%(result)s of %(total)s firewall policy(s) " |
| 187 |
"failed to delete.") % {'result': result, 'total': total}) |
| 188 |
raise exceptions.CommandError(msg) |
| 189 |
|
| 190 |
|
| 191 |
class FirewallPolicyInsertRule(command.Command): |
| 192 |
_description = _("Insert a rule into a given firewall policy") |
| 193 |
|
| 194 |
def get_parser(self, prog_name): |
| 195 |
parser = super(FirewallPolicyInsertRule, self).get_parser(prog_name) |
| 196 |
parser.add_argument( |
| 197 |
const.FWP, |
| 198 |
metavar='<firewall-policy>', |
| 199 |
help=_('Firewall policy to insert rule (name or ID)')) |
| 200 |
parser.add_argument( |
| 201 |
'--insert-before', |
| 202 |
metavar='<firewall-rule>', |
| 203 |
help=_('Insert the new rule before this existing rule ' |
| 204 |
'(name or ID)')) |
| 205 |
parser.add_argument( |
| 206 |
'--insert-after', |
| 207 |
metavar='<firewall-rule>', |
| 208 |
help=_('Insert the new rule after this existing rule ' |
| 209 |
'(name or ID)')) |
| 210 |
parser.add_argument( |
| 211 |
const.FWR, |
| 212 |
metavar='<firewall-rule>', |
| 213 |
help=_('Firewall rule to be inserted (name or ID)')) |
| 214 |
return parser |
| 215 |
|
| 216 |
def args2body(self, parsed_args): |
| 217 |
client = self.app.client_manager.network |
| 218 |
_rule_id = _get_required_firewall_rule(client, parsed_args) |
| 219 |
_insert_before = '' |
| 220 |
if 'insert_before' in parsed_args: |
| 221 |
if parsed_args.insert_before: |
| 222 |
_insert_before = client.find_firewall_rule( |
| 223 |
parsed_args.insert_before)['id'] |
| 224 |
_insert_after = '' |
| 225 |
if 'insert_after' in parsed_args: |
| 226 |
if parsed_args.insert_after: |
| 227 |
_insert_after = client.find_firewall_rule( |
| 228 |
parsed_args.insert_after)['id'] |
| 229 |
return {'firewall_rule_id': _rule_id, |
| 230 |
'insert_before': _insert_before, |
| 231 |
'insert_after': _insert_after} |
| 232 |
|
| 233 |
def take_action(self, parsed_args): |
| 234 |
client = self.app.client_manager.network |
| 235 |
policy_id = client.find_firewall_policy( |
| 236 |
parsed_args.firewall_policy)['id'] |
| 237 |
body = self.args2body(parsed_args) |
| 238 |
client.insert_rule_into_policy(policy_id, **body) |
| 239 |
rule_id = body['firewall_rule_id'] |
| 240 |
policy = parsed_args.firewall_policy |
| 241 |
print((_('Inserted firewall rule %(rule)s in firewall policy ' |
| 242 |
'%(policy)s') % {'rule': rule_id, 'policy': policy}), |
| 243 |
file=self.app.stdout) |
| 244 |
|
| 245 |
|
| 246 |
class FirewallPolicyRemoveRule(command.Command): |
| 247 |
_description = _("Remove a rule from a given firewall policy") |
| 248 |
|
| 249 |
def get_parser(self, prog_name): |
| 250 |
parser = super(FirewallPolicyRemoveRule, self).get_parser(prog_name) |
| 251 |
parser.add_argument( |
| 252 |
const.FWP, |
| 253 |
metavar='<firewall-policy>', |
| 254 |
help=_('Firewall policy to remove rule (name or ID)')) |
| 255 |
parser.add_argument( |
| 256 |
const.FWR, |
| 257 |
metavar='<firewall-rule>', |
| 258 |
help=_('Firewall rule to remove from policy (name or ID)')) |
| 259 |
return parser |
| 260 |
|
| 261 |
def take_action(self, parsed_args): |
| 262 |
client = self.app.client_manager.network |
| 263 |
policy_id = client.find_firewall_policy( |
| 264 |
parsed_args.firewall_policy)['id'] |
| 265 |
fwr_id = _get_required_firewall_rule(client, parsed_args) |
| 266 |
body = {'firewall_rule_id': fwr_id} |
| 267 |
client.remove_rule_from_policy(policy_id, **body) |
| 268 |
rule_id = body['firewall_rule_id'] |
| 269 |
policy = parsed_args.firewall_policy |
| 270 |
print((_('Removed firewall rule %(rule)s from firewall policy ' |
| 271 |
'%(policy)s') % {'rule': rule_id, 'policy': policy}), |
| 272 |
file=self.app.stdout) |
| 273 |
|
| 274 |
|
| 275 |
class ListFirewallPolicy(command.Lister): |
| 276 |
_description = _("List firewall policies") |
| 277 |
|
| 278 |
def get_parser(self, prog_name): |
| 279 |
parser = super(ListFirewallPolicy, self).get_parser(prog_name) |
| 280 |
parser.add_argument( |
| 281 |
'--long', |
| 282 |
action='store_true', |
| 283 |
default=False, |
| 284 |
help=_("List additional fields in output") |
| 285 |
) |
| 286 |
return parser |
| 287 |
|
| 288 |
def take_action(self, parsed_args): |
| 289 |
client = self.app.client_manager.network |
| 290 |
obj = client.firewall_policies() |
| 291 |
headers, columns = column_util.get_column_definitions( |
| 292 |
_attr_map, long_listing=parsed_args.long) |
| 293 |
return (headers, (utils.get_dict_properties( |
| 294 |
s, columns, formatters=_formatters) for s in obj)) |
| 295 |
|
| 296 |
|
| 297 |
class SetFirewallPolicy(command.Command): |
| 298 |
_description = _("Set firewall policy properties") |
| 299 |
|
| 300 |
def get_parser(self, prog_name): |
| 301 |
parser = super(SetFirewallPolicy, self).get_parser(prog_name) |
| 302 |
_get_common_parser(parser) |
| 303 |
parser.add_argument( |
| 304 |
const.FWP, |
| 305 |
metavar='<firewall-policy>', |
| 306 |
help=_('Firewall policy to update (name or ID)')) |
| 307 |
parser.add_argument( |
| 308 |
'--name', |
| 309 |
metavar='<name>', |
| 310 |
help=_('Name for the firewall policy')) |
| 311 |
parser.add_argument( |
| 312 |
'--firewall-rule', |
| 313 |
action='append', |
| 314 |
metavar='<firewall-rule>', |
| 315 |
help=_('Firewall rule(s) to apply (name or ID)')) |
| 316 |
parser.add_argument( |
| 317 |
'--no-firewall-rule', |
| 318 |
action='store_true', |
| 319 |
help=_('Remove all firewall rules from firewall policy')) |
| 320 |
return parser |
| 321 |
|
| 322 |
def take_action(self, parsed_args): |
| 323 |
client = self.app.client_manager.network |
| 324 |
fwp_id = client.find_firewall_policy( |
| 325 |
parsed_args.firewall_policy)['id'] |
| 326 |
attrs = _get_common_attrs(self.app.client_manager, |
| 327 |
parsed_args, is_create=False) |
| 328 |
try: |
| 329 |
client.update_firewall_policy(fwp_id, **attrs) |
| 330 |
except Exception as e: |
| 331 |
msg = (_("Failed to set firewall policy '%(policy)s': %(e)s") |
| 332 |
% {'policy': parsed_args.firewall_policy, 'e': e}) |
| 333 |
raise exceptions.CommandError(msg) |
| 334 |
|
| 335 |
|
| 336 |
class ShowFirewallPolicy(command.ShowOne): |
| 337 |
_description = _("Display firewall policy details") |
| 338 |
|
| 339 |
def get_parser(self, prog_name): |
| 340 |
parser = super(ShowFirewallPolicy, self).get_parser(prog_name) |
| 341 |
parser.add_argument( |
| 342 |
const.FWP, |
| 343 |
metavar='<firewall-policy>', |
| 344 |
help=_('Firewall policy to show (name or ID)')) |
| 345 |
return parser |
| 346 |
|
| 347 |
def take_action(self, parsed_args): |
| 348 |
client = self.app.client_manager.network |
| 349 |
fwp_id = client.find_firewall_policy( |
| 350 |
parsed_args.firewall_policy)['id'] |
| 351 |
obj = client.get_firewall_policy(fwp_id) |
| 352 |
display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 353 |
obj, _attr_map_dict, ['location', 'tenant_id']) |
| 354 |
data = utils.get_dict_properties(obj, columns, formatters=_formatters) |
| 355 |
return (display_columns, data) |
| 356 |
|
| 357 |
|
| 358 |
def _get_required_firewall_rule(client, parsed_args): |
| 359 |
if not parsed_args.firewall_rule: |
| 360 |
msg = (_("Firewall rule (name or ID) is required.")) |
| 361 |
raise exceptions.CommandError(msg) |
| 362 |
return client.find_firewall_rule(parsed_args.firewall_rule)['id'] |
| 363 |
|
| 364 |
|
| 365 |
class UnsetFirewallPolicy(command.Command): |
| 366 |
_description = _("Unset firewall policy properties") |
| 367 |
|
| 368 |
def get_parser(self, prog_name): |
| 369 |
parser = super(UnsetFirewallPolicy, self).get_parser(prog_name) |
| 370 |
parser.add_argument( |
| 371 |
const.FWP, |
| 372 |
metavar='<firewall-policy>', |
| 373 |
help=_('Firewall policy to unset (name or ID)')) |
| 374 |
firewall_rule_group = parser.add_mutually_exclusive_group() |
| 375 |
firewall_rule_group.add_argument( |
| 376 |
'--firewall-rule', |
| 377 |
action='append', |
| 378 |
metavar='<firewall-rule>', |
| 379 |
help=_('Remove firewall rule(s) from the firewall policy ' |
| 380 |
'(name or ID)')) |
| 381 |
firewall_rule_group.add_argument( |
| 382 |
'--all-firewall-rule', |
| 383 |
action='store_true', |
| 384 |
help=_('Remove all firewall rules from the firewall policy')) |
| 385 |
parser.add_argument( |
| 386 |
'--audited', |
| 387 |
action='store_true', |
| 388 |
help=_('Disable auditing for the policy')) |
| 389 |
parser.add_argument( |
| 390 |
'--share', |
| 391 |
action='store_true', |
| 392 |
help=_('Restrict use of the firewall policy to the ' |
| 393 |
'current project')) |
| 394 |
return parser |
| 395 |
|
| 396 |
def _get_attrs(self, client_manager, parsed_args): |
| 397 |
attrs = {} |
| 398 |
client = client_manager.network |
| 399 |
|
| 400 |
if parsed_args.firewall_rule: |
| 401 |
current = client.find_firewall_policy( |
| 402 |
parsed_args.firewall_policy)[const.FWRS] |
| 403 |
removed = [] |
| 404 |
for f in set(parsed_args.firewall_rule): |
| 405 |
removed.append(client.find_firewall_rule(f)['id']) |
| 406 |
attrs[const.FWRS] = [r for r in current if r not in removed] |
| 407 |
if parsed_args.all_firewall_rule: |
| 408 |
attrs[const.FWRS] = [] |
| 409 |
if parsed_args.audited: |
| 410 |
attrs['audited'] = False |
| 411 |
if parsed_args.share: |
| 412 |
attrs['shared'] = False |
| 413 |
return attrs |
| 414 |
|
| 415 |
def take_action(self, parsed_args): |
| 416 |
client = self.app.client_manager.network |
| 417 |
fwp_id = client.find_firewall_policy( |
| 418 |
parsed_args.firewall_policy)['id'] |
| 419 |
attrs = self._get_attrs(self.app.client_manager, parsed_args) |
| 420 |
try: |
| 421 |
client.update_firewall_policy(fwp_id, **attrs) |
| 422 |
except Exception as e: |
| 423 |
msg = (_("Failed to unset firewall policy '%(policy)s': %(e)s") |
| 424 |
% {'policy': parsed_args.firewall_policy, 'e': e}) |
| 425 |
raise exceptions.CommandError(msg) |