| 1 |
# Copyright 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 |
from osc_lib.command import command |
| 18 |
from osc_lib import exceptions |
| 19 |
from osc_lib import utils |
| 20 |
from osc_lib.utils import columns as column_util |
| 21 |
from oslo_log import log as logging |
| 22 |
|
| 23 |
from neutronclient._i18n import _ |
| 24 |
from neutronclient.common import utils as nc_utils |
| 25 |
from neutronclient.osc import utils as osc_utils |
| 26 |
from neutronclient.osc.v2.vpnaas import utils as vpn_utils |
| 27 |
|
| 28 |
|
| 29 |
LOG = logging.getLogger(__name__) |
| 30 |
|
| 31 |
_attr_map = ( |
| 32 |
('id', 'ID', column_util.LIST_BOTH), |
| 33 |
('name', 'Name', column_util.LIST_BOTH), |
| 34 |
('auth_algorithm', 'Authentication Algorithm', column_util.LIST_BOTH), |
| 35 |
('encapsulation_mode', 'Encapsulation Mode', column_util.LIST_BOTH), |
| 36 |
('transform_protocol', 'Transform Protocol', column_util.LIST_BOTH), |
| 37 |
('encryption_algorithm', 'Encryption Algorithm', column_util.LIST_BOTH), |
| 38 |
('pfs', 'Perfect Forward Secrecy (PFS)', column_util.LIST_LONG_ONLY), |
| 39 |
('description', 'Description', column_util.LIST_LONG_ONLY), |
| 40 |
('project_id', 'Project', column_util.LIST_LONG_ONLY), |
| 41 |
('lifetime', 'Lifetime', column_util.LIST_LONG_ONLY), |
| 42 |
) |
| 43 |
|
| 44 |
_attr_map_dict = { |
| 45 |
'id': 'ID', |
| 46 |
'name': 'Name', |
| 47 |
'auth_algorithm': 'Authentication Algorithm', |
| 48 |
'encapsulation_mode': 'Encapsulation Mode', |
| 49 |
'transform_protocol': 'Transform Protocol', |
| 50 |
'encryption_algorithm': 'Encryption Algorithm', |
| 51 |
'pfs': 'Perfect Forward Secrecy (PFS)', |
| 52 |
'lifetime': 'Lifetime', |
| 53 |
'description': 'Description', |
| 54 |
'project_id': 'Project', |
| 55 |
} |
| 56 |
|
| 57 |
_auth_algorithms = [ |
| 58 |
'sha1', |
| 59 |
'sha256', |
| 60 |
'sha384', |
| 61 |
'sha512', |
| 62 |
'aes-xcbc', |
| 63 |
'aes-cmac', |
| 64 |
] |
| 65 |
|
| 66 |
_encryption_algorithms = [ |
| 67 |
'3des', |
| 68 |
'aes-128', |
| 69 |
'aes-192', |
| 70 |
'aes-256', |
| 71 |
'aes-128-ccm-8', |
| 72 |
'aes-192-ccm-8', |
| 73 |
'aes-256-ccm-8', |
| 74 |
'aes-128-ccm-12', |
| 75 |
'aes-192-ccm-12', |
| 76 |
'aes-256-ccm-12', |
| 77 |
'aes-128-ccm-16', |
| 78 |
'aes-192-ccm-16', |
| 79 |
'aes-256-ccm-16', |
| 80 |
'aes-128-gcm-8', |
| 81 |
'aes-192-gcm-8', |
| 82 |
'aes-256-gcm-8', |
| 83 |
'aes-128-gcm-12', |
| 84 |
'aes-192-gcm-12', |
| 85 |
'aes-256-gcm-12', |
| 86 |
'aes-128-gcm-16', |
| 87 |
'aes-192-gcm-16', |
| 88 |
'aes-256-gcm-16', |
| 89 |
'aes-128-ctr', |
| 90 |
'aes-192-ctr', |
| 91 |
'aes-256-ctr', |
| 92 |
] |
| 93 |
|
| 94 |
_pfs_groups = [ |
| 95 |
'group2', |
| 96 |
'group5', |
| 97 |
'group14', |
| 98 |
'group15', |
| 99 |
'group16', |
| 100 |
'group17', |
| 101 |
'group18', |
| 102 |
'group19', |
| 103 |
'group20', |
| 104 |
'group21', |
| 105 |
'group22', |
| 106 |
'group23', |
| 107 |
'group24', |
| 108 |
'group25', |
| 109 |
'group26', |
| 110 |
'group27', |
| 111 |
'group28', |
| 112 |
'group29', |
| 113 |
'group30', |
| 114 |
'group31', |
| 115 |
] |
| 116 |
|
| 117 |
|
| 118 |
def _convert_to_lowercase(string): |
| 119 |
return string.lower() |
| 120 |
|
| 121 |
|
| 122 |
def _get_common_parser(parser): |
| 123 |
parser.add_argument( |
| 124 |
'--description', |
| 125 |
metavar='<description>', |
| 126 |
help=_('Description of the IPsec policy')) |
| 127 |
parser.add_argument( |
| 128 |
'--auth-algorithm', |
| 129 |
choices=_auth_algorithms, |
| 130 |
type=_convert_to_lowercase, |
| 131 |
help=_('Authentication algorithm for IPsec policy')) |
| 132 |
parser.add_argument( |
| 133 |
'--encapsulation-mode', |
| 134 |
choices=['tunnel', 'transport'], |
| 135 |
type=_convert_to_lowercase, |
| 136 |
help=_('Encapsulation mode for IPsec policy')) |
| 137 |
parser.add_argument( |
| 138 |
'--encryption-algorithm', |
| 139 |
choices=_encryption_algorithms, |
| 140 |
type=_convert_to_lowercase, |
| 141 |
help=_('Encryption algorithm for IPsec policy')) |
| 142 |
parser.add_argument( |
| 143 |
'--lifetime', |
| 144 |
metavar="units=UNITS,value=VALUE", |
| 145 |
type=nc_utils.str2dict_type(optional_keys=['units', 'value']), |
| 146 |
help=vpn_utils.lifetime_help("IPsec")) |
| 147 |
parser.add_argument( |
| 148 |
'--pfs', |
| 149 |
choices=_pfs_groups, |
| 150 |
type=_convert_to_lowercase, |
| 151 |
help=_('Perfect Forward Secrecy for IPsec policy')) |
| 152 |
parser.add_argument( |
| 153 |
'--transform-protocol', |
| 154 |
type=_convert_to_lowercase, |
| 155 |
choices=['esp', 'ah', 'ah-esp'], |
| 156 |
help=_('Transform protocol for IPsec policy')) |
| 157 |
|
| 158 |
|
| 159 |
def _get_common_attrs(client_manager, parsed_args, is_create=True): |
| 160 |
attrs = {} |
| 161 |
if is_create: |
| 162 |
if 'project' in parsed_args and parsed_args.project is not None: |
| 163 |
attrs['project_id'] = osc_utils.find_project( |
| 164 |
client_manager.identity, |
| 165 |
parsed_args.project, |
| 166 |
parsed_args.project_domain, |
| 167 |
).id |
| 168 |
if parsed_args.description: |
| 169 |
attrs['description'] = str(parsed_args.description) |
| 170 |
if parsed_args.auth_algorithm: |
| 171 |
attrs['auth_algorithm'] = parsed_args.auth_algorithm |
| 172 |
if parsed_args.encapsulation_mode: |
| 173 |
attrs['encapsulation_mode'] = parsed_args.encapsulation_mode |
| 174 |
if parsed_args.transform_protocol: |
| 175 |
attrs['transform_protocol'] = parsed_args.transform_protocol |
| 176 |
if parsed_args.encryption_algorithm: |
| 177 |
attrs['encryption_algorithm'] = parsed_args.encryption_algorithm |
| 178 |
if parsed_args.pfs: |
| 179 |
attrs['pfs'] = parsed_args.pfs |
| 180 |
if parsed_args.lifetime: |
| 181 |
vpn_utils.validate_lifetime_dict(parsed_args.lifetime) |
| 182 |
attrs['lifetime'] = parsed_args.lifetime |
| 183 |
return attrs |
| 184 |
|
| 185 |
|
| 186 |
class CreateIPsecPolicy(command.ShowOne): |
| 187 |
_description = _("Create an IPsec policy") |
| 188 |
|
| 189 |
def get_parser(self, prog_name): |
| 190 |
parser = super(CreateIPsecPolicy, self).get_parser(prog_name) |
| 191 |
_get_common_parser(parser) |
| 192 |
parser.add_argument( |
| 193 |
'name', |
| 194 |
metavar='<name>', |
| 195 |
help=_('Name of the IPsec policy')) |
| 196 |
osc_utils.add_project_owner_option_to_parser(parser) |
| 197 |
return parser |
| 198 |
|
| 199 |
def take_action(self, parsed_args): |
| 200 |
client = self.app.client_manager.network |
| 201 |
attrs = _get_common_attrs(self.app.client_manager, parsed_args) |
| 202 |
if parsed_args.name: |
| 203 |
attrs['name'] = str(parsed_args.name) |
| 204 |
obj = client.create_vpn_ipsec_policy(**attrs) |
| 205 |
display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 206 |
obj, _attr_map_dict, ['location', 'tenant_id', |
| 207 |
'phase1_negotiation_mode', 'units', 'value']) |
| 208 |
data = utils.get_dict_properties(obj, columns) |
| 209 |
return display_columns, data |
| 210 |
|
| 211 |
|
| 212 |
class DeleteIPsecPolicy(command.Command): |
| 213 |
_description = _("Delete IPsec policy(policies)") |
| 214 |
|
| 215 |
def get_parser(self, prog_name): |
| 216 |
parser = super(DeleteIPsecPolicy, self).get_parser(prog_name) |
| 217 |
parser.add_argument( |
| 218 |
'ipsecpolicy', |
| 219 |
metavar='<ipsec-policy>', |
| 220 |
nargs='+', |
| 221 |
help=_('ipsec policy to delete (name or ID)')) |
| 222 |
return parser |
| 223 |
|
| 224 |
def take_action(self, parsed_args): |
| 225 |
client = self.app.client_manager.network |
| 226 |
result = 0 |
| 227 |
for ipsec in parsed_args.ipsecpolicy: |
| 228 |
try: |
| 229 |
ipsec_id = client.find_vpn_ipsec_policy( |
| 230 |
ipsec, ignore_missing=False)['id'] |
| 231 |
client.delete_vpn_ipsec_policy(ipsec_id) |
| 232 |
except Exception as e: |
| 233 |
result += 1 |
| 234 |
LOG.error(_("Failed to delete IPsec policy with " |
| 235 |
"name or ID '%(ipsecpolicy)s': %(e)s"), |
| 236 |
{'ipsecpolicy': ipsec, 'e': e}) |
| 237 |
|
| 238 |
if result > 0: |
| 239 |
total = len(parsed_args.ipsecpolicy) |
| 240 |
msg = (_("%(result)s of %(total)s IPsec policy failed " |
| 241 |
"to delete.") % {'result': result, 'total': total}) |
| 242 |
raise exceptions.CommandError(msg) |
| 243 |
|
| 244 |
|
| 245 |
class ListIPsecPolicy(command.Lister): |
| 246 |
_description = _("List IPsec policies that belong to a given project") |
| 247 |
|
| 248 |
def get_parser(self, prog_name): |
| 249 |
parser = super(ListIPsecPolicy, self).get_parser(prog_name) |
| 250 |
parser.add_argument( |
| 251 |
'--long', |
| 252 |
action='store_true', |
| 253 |
default=False, |
| 254 |
help=_("List additional fields in output") |
| 255 |
) |
| 256 |
return parser |
| 257 |
|
| 258 |
def take_action(self, parsed_args): |
| 259 |
client = self.app.client_manager.network |
| 260 |
obj = client.vpn_ipsec_policies() |
| 261 |
headers, columns = column_util.get_column_definitions( |
| 262 |
_attr_map, long_listing=parsed_args.long) |
| 263 |
return (headers, (utils.get_dict_properties(s, columns) for s in obj)) |
| 264 |
|
| 265 |
|
| 266 |
class SetIPsecPolicy(command.Command): |
| 267 |
_description = _("Set IPsec policy properties") |
| 268 |
|
| 269 |
def get_parser(self, prog_name): |
| 270 |
parser = super(SetIPsecPolicy, self).get_parser(prog_name) |
| 271 |
_get_common_parser(parser) |
| 272 |
parser.add_argument( |
| 273 |
'--name', |
| 274 |
metavar='<name>', |
| 275 |
help=_('Name of the IPsec policy')) |
| 276 |
parser.add_argument( |
| 277 |
'ipsecpolicy', |
| 278 |
metavar='<ipsec-policy>', |
| 279 |
help=_('IPsec policy to set (name or ID)')) |
| 280 |
return parser |
| 281 |
|
| 282 |
def take_action(self, parsed_args): |
| 283 |
client = self.app.client_manager.network |
| 284 |
attrs = _get_common_attrs(self.app.client_manager, |
| 285 |
parsed_args, is_create=False) |
| 286 |
if parsed_args.name: |
| 287 |
attrs['name'] = str(parsed_args.name) |
| 288 |
ipsec_id = client.find_vpn_ipsec_policy( |
| 289 |
parsed_args.ipsecpolicy, ignore_missing=False)['id'] |
| 290 |
try: |
| 291 |
client.update_vpn_ipsec_policy(ipsec_id, **attrs) |
| 292 |
except Exception as e: |
| 293 |
msg = (_("Failed to set IPsec policy '%(ipsec)s': %(e)s") |
| 294 |
% {'ipsec': parsed_args.ipsecpolicy, 'e': e}) |
| 295 |
raise exceptions.CommandError(msg) |
| 296 |
|
| 297 |
|
| 298 |
class ShowIPsecPolicy(command.ShowOne): |
| 299 |
_description = _("Display IPsec policy details") |
| 300 |
|
| 301 |
def get_parser(self, prog_name): |
| 302 |
parser = super(ShowIPsecPolicy, self).get_parser(prog_name) |
| 303 |
parser.add_argument( |
| 304 |
'ipsecpolicy', |
| 305 |
metavar='<ipsec-policy>', |
| 306 |
help=_('IPsec policy to display (name or ID)')) |
| 307 |
return parser |
| 308 |
|
| 309 |
def take_action(self, parsed_args): |
| 310 |
client = self.app.client_manager.network |
| 311 |
ipsec_id = client.find_vpn_ipsec_policy( |
| 312 |
parsed_args.ipsecpolicy, ignore_missing=False)['id'] |
| 313 |
obj = client.get_vpn_ipsec_policy(ipsec_id) |
| 314 |
display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 315 |
obj, _attr_map_dict, ['location', 'tenant_id', |
| 316 |
'phase1_negotiation_mode', 'units', 'value']) |
| 317 |
data = utils.get_dict_properties(obj, columns) |
| 318 |
return (display_columns, data) |