| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: bigip_ike_peer |
| 13 |
short_description: Manage IPSec IKE Peer configuration on BIG-IP |
| 14 |
description: |
| 15 |
- Manage IPSec IKE Peer configuration on a BIG-IP device. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Specifies the name of the IKE peer. |
| 21 |
type: str |
| 22 |
required: True |
| 23 |
description: |
| 24 |
description: |
| 25 |
- Description of the IKE peer. |
| 26 |
type: str |
| 27 |
version: |
| 28 |
description: |
| 29 |
- Specifies which version of IKE (Internet Key Exchange) is used. |
| 30 |
- If the system you are configuring is the IPsec initiator, and you select |
| 31 |
both versions, the system tries using IKEv2 for negotiation. If the remote |
| 32 |
peer does not support IKEv2, the IPsec tunnel fails. To use IKEv1 in this |
| 33 |
case, you must deselect Version 2 and try again. |
| 34 |
- If the system you are configuring is the IPsec responder, and you select |
| 35 |
both versions, the IPsec initiator system determines which IKE version to use. |
| 36 |
- When creating a new IKE peer, this value is required. |
| 37 |
type: list |
| 38 |
elements: str |
| 39 |
choices: |
| 40 |
- v1 |
| 41 |
- v2 |
| 42 |
presented_id_type: |
| 43 |
description: |
| 44 |
- Specifies the identifier type the local system uses to identify |
| 45 |
itself to the peer during IKE Phase 1 negotiations. |
| 46 |
type: str |
| 47 |
choices: |
| 48 |
- address |
| 49 |
- asn1dn |
| 50 |
- fqdn |
| 51 |
- keyid-tag |
| 52 |
- user-fqdn |
| 53 |
- override |
| 54 |
presented_id_value: |
| 55 |
description: |
| 56 |
- Specifies a value for the identity when using a C(presented_id_type) of |
| 57 |
C(override). |
| 58 |
- This is a required value when C(version) includes (Cv2). |
| 59 |
type: str |
| 60 |
verified_id_type: |
| 61 |
description: |
| 62 |
- Specifies the identifier type the local system uses to identify |
| 63 |
the peer during IKE Phase 1 negotiation. |
| 64 |
- This is a required value when C(version) includes (Cv2). |
| 65 |
- When C(user-fqdn), value of C(verified_id_value) must be in the form of |
| 66 |
User @ DNS domain string. |
| 67 |
type: str |
| 68 |
choices: |
| 69 |
- address |
| 70 |
- asn1dn |
| 71 |
- fqdn |
| 72 |
- keyid-tag |
| 73 |
- user-fqdn |
| 74 |
- override |
| 75 |
verified_id_value: |
| 76 |
description: |
| 77 |
- Specifies a value for the identity when using a C(verified_id_type) of |
| 78 |
C(override). |
| 79 |
- This is a required value when C(version) includes (Cv2). |
| 80 |
type: str |
| 81 |
phase1_auth_method: |
| 82 |
description: |
| 83 |
- Specifies the authentication method for phase 1 negotiation. |
| 84 |
- When creating a new IKE peer, if this value is not specified, the default is |
| 85 |
C(rsa-signature). |
| 86 |
type: str |
| 87 |
choices: |
| 88 |
- pre-shared-key |
| 89 |
- rsa-signature |
| 90 |
phase1_lifetime: |
| 91 |
description: |
| 92 |
- Defines the lifetime in minutes of an IKE SA which will be proposed in the phase 1 negotiations. |
| 93 |
- The accepted value range is C(1 - 4294967295) minutes. |
| 94 |
- When creating a new IKE peer, if this value is not specified, the default value set by the system is |
| 95 |
C(1440) minutes. |
| 96 |
type: int |
| 97 |
version_added: "1.1.0" |
| 98 |
phase1_cert: |
| 99 |
description: |
| 100 |
- Specifies the digital certificate to use for the RSA signature. |
| 101 |
- When creating a new IKE peer, if this value is not specified, and |
| 102 |
C(phase1_auth_method) is C(rsa-signature), the default is C(default.crt). |
| 103 |
- This parameter is invalid when C(phase1_auth_method) is C(pre-shared-key). |
| 104 |
type: str |
| 105 |
phase1_key: |
| 106 |
description: |
| 107 |
- Specifies the public key the digital certificate contains. |
| 108 |
- When creating a new IKE peer, if this value is not specified, and |
| 109 |
C(phase1_auth_method) is C(rsa-signature), the default is C(default.key). |
| 110 |
- This parameter is invalid when C(phase1_auth_method) is C(pre-shared-key). |
| 111 |
type: str |
| 112 |
phase1_verify_peer_cert: |
| 113 |
description: |
| 114 |
- In IKEv2, specifies whether the certificate sent by the IKE peer is verified |
| 115 |
using the Trusted Certificate Authorities, a CRL, and/or a peer certificate. |
| 116 |
- In IKEv1, specifies whether the identifier sent by the peer is verified with |
| 117 |
the credentials in the certificate, in the following manner - ASN1DN; specifies |
| 118 |
that the entire certificate subject name is compared with the identifier. |
| 119 |
Address, FQDN, or User FQDN; specifies that the certificate's subjectAltName is |
| 120 |
compared with the identifier. If the two do not match, the negotiation fails. |
| 121 |
- When creating a new IKE peer, if this value is not specified, and |
| 122 |
C(phase1_auth_method) is C(rsa-signature), the default is C(false). |
| 123 |
- This parameter is invalid when C(phase1_auth_method) is C(pre-shared-key). |
| 124 |
type: bool |
| 125 |
preshared_key: |
| 126 |
description: |
| 127 |
- Specifies a string the IKE peers share for authenticating each other. |
| 128 |
- This parameter is only relevant when C(phase1_auth_method) is C(pre-shared-key). |
| 129 |
- This parameter is invalid when C(phase1_auth_method) is C(rsa-signature). |
| 130 |
type: str |
| 131 |
remote_address: |
| 132 |
description: |
| 133 |
- Displays the IP address of the BIG-IP system that is remote to the system |
| 134 |
you are configuring. |
| 135 |
type: str |
| 136 |
phase1_encryption_algorithm: |
| 137 |
description: |
| 138 |
- Specifies the algorithm to use for IKE encryption. |
| 139 |
- IKE C(version) C(v2) does not support C(blowfish), C(camellia), or C(cast128). |
| 140 |
type: str |
| 141 |
choices: |
| 142 |
- 3des |
| 143 |
- des |
| 144 |
- blowfish |
| 145 |
- cast128 |
| 146 |
- aes128 |
| 147 |
- aes192 |
| 148 |
- aes256 |
| 149 |
- camellia |
| 150 |
phase1_hash_algorithm: |
| 151 |
description: |
| 152 |
- Specifies the algorithm to use for IKE authentication. |
| 153 |
type: str |
| 154 |
choices: |
| 155 |
- sha1 |
| 156 |
- md5 |
| 157 |
- sha256 |
| 158 |
- sha384 |
| 159 |
- sha512 |
| 160 |
phase1_perfect_forward_secrecy: |
| 161 |
description: |
| 162 |
- Specifies the Diffie-Hellman group to use for IKE Phase 1 and Phase 2 negotiations. |
| 163 |
type: str |
| 164 |
choices: |
| 165 |
- ecp256 |
| 166 |
- ecp384 |
| 167 |
- ecp521 |
| 168 |
- modp768 |
| 169 |
- modp1024 |
| 170 |
- modp1536 |
| 171 |
- modp2048 |
| 172 |
- modp3072 |
| 173 |
- modp4096 |
| 174 |
- modp6144 |
| 175 |
- modp8192 |
| 176 |
update_password: |
| 177 |
description: |
| 178 |
- C(always) allows updating passwords if the user chooses to do so. |
| 179 |
C(on_create) only sets the password for newly created IKE peers. |
| 180 |
type: str |
| 181 |
choices: |
| 182 |
- always |
| 183 |
- on_create |
| 184 |
default: always |
| 185 |
partition: |
| 186 |
description: |
| 187 |
- Device partition to manage resources on. |
| 188 |
type: str |
| 189 |
default: Common |
| 190 |
state: |
| 191 |
description: |
| 192 |
- When C(present), ensures the resource exists. |
| 193 |
- When C(absent), ensures the resource is removed. |
| 194 |
type: str |
| 195 |
choices: |
| 196 |
- present |
| 197 |
- absent |
| 198 |
default: present |
| 199 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 200 |
author: |
| 201 |
- Tim Rupp (@caphrim007) |
| 202 |
- Wojciech Wypior (@wojtek0806) |
| 203 |
''' |
| 204 |
|
| 205 |
EXAMPLES = r''' |
| 206 |
- name: Create new IKE peer |
| 207 |
bigip_ike_peer: |
| 208 |
name: ike1 |
| 209 |
remote_address: 1.2.3.4 |
| 210 |
version: |
| 211 |
- v1 |
| 212 |
provider: |
| 213 |
password: secret |
| 214 |
server: lb.mydomain.com |
| 215 |
user: admin |
| 216 |
delegate_to: localhost |
| 217 |
|
| 218 |
- name: Change presented id type - keyid-tag |
| 219 |
bigip_ike_peer: |
| 220 |
name: ike1 |
| 221 |
presented_id_type: keyid-tag |
| 222 |
presented_id_value: key1 |
| 223 |
provider: |
| 224 |
password: secret |
| 225 |
server: lb.mydomain.com |
| 226 |
user: admin |
| 227 |
delegate_to: localhost |
| 228 |
|
| 229 |
- name: Remove IKE peer |
| 230 |
bigip_ike_peer: |
| 231 |
name: ike1 |
| 232 |
state: absent |
| 233 |
provider: |
| 234 |
password: secret |
| 235 |
server: lb.mydomain.com |
| 236 |
user: admin |
| 237 |
delegate_to: localhost |
| 238 |
''' |
| 239 |
|
| 240 |
RETURN = r''' |
| 241 |
presented_id_type: |
| 242 |
description: The new Presented ID Type value of the resource. |
| 243 |
returned: changed |
| 244 |
type: str |
| 245 |
sample: address |
| 246 |
verified_id_type: |
| 247 |
description: The new Verified ID Type value of the resource. |
| 248 |
returned: changed |
| 249 |
type: str |
| 250 |
sample: address |
| 251 |
phase1_auth_method: |
| 252 |
description: The new IKE Phase 1 Credentials Authentication Method value of the resource. |
| 253 |
returned: changed |
| 254 |
type: str |
| 255 |
sample: rsa-signature |
| 256 |
remote_address: |
| 257 |
description: The new Remote Address value of the resource. |
| 258 |
returned: changed |
| 259 |
type: str |
| 260 |
sample: 1.2.2.1 |
| 261 |
version: |
| 262 |
description: The new list of IKE versions. |
| 263 |
returned: changed |
| 264 |
type: list |
| 265 |
sample: ['v1', 'v2'] |
| 266 |
phase1_encryption_algorithm: |
| 267 |
description: The new IKE Phase 1 Encryption Algorithm. |
| 268 |
returned: changed |
| 269 |
type: str |
| 270 |
sample: 3des |
| 271 |
phase1_hash_algorithm: |
| 272 |
description: The new IKE Phase 1 Authentication Algorithm. |
| 273 |
returned: changed |
| 274 |
type: str |
| 275 |
sample: sha256 |
| 276 |
phase1_perfect_forward_secrecy: |
| 277 |
description: The new IKE Phase 1 Perfect Forward Secrecy. |
| 278 |
returned: changed |
| 279 |
type: str |
| 280 |
sample: modp1024 |
| 281 |
phase1_cert: |
| 282 |
description: The new IKE Phase 1 Certificate Credentials. |
| 283 |
returned: changed |
| 284 |
type: str |
| 285 |
sample: /Common/cert1.crt |
| 286 |
phase1_key: |
| 287 |
description: The new IKE Phase 1 Key Credentials. |
| 288 |
returned: changed |
| 289 |
type: str |
| 290 |
sample: /Common/cert1.key |
| 291 |
phase1_verify_peer_cert: |
| 292 |
description: The new IKE Phase 1 Key Verify Peer Certificate setting. |
| 293 |
returned: changed |
| 294 |
type: bool |
| 295 |
sample: true |
| 296 |
verified_id_value: |
| 297 |
description: The new Verified ID Value setting for the Verified ID Type. |
| 298 |
returned: changed |
| 299 |
type: str |
| 300 |
sample: 1.2.3.1 |
| 301 |
presented_id_value: |
| 302 |
description: The new Presented ID Value setting for the Presented ID Type. |
| 303 |
returned: changed |
| 304 |
type: str |
| 305 |
sample: 1.2.3.1 |
| 306 |
''' |
| 307 |
from datetime import datetime |
| 308 |
|
| 309 |
from ansible.module_utils.basic import ( |
| 310 |
AnsibleModule, env_fallback |
| 311 |
) |
| 312 |
|
| 313 |
from ..module_utils.bigip import F5RestClient |
| 314 |
from ..module_utils.common import ( |
| 315 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean, fq_name |
| 316 |
) |
| 317 |
from ..module_utils.compare import cmp_str_with_none |
| 318 |
from ..module_utils.icontrol import tmos_version |
| 319 |
from ..module_utils.teem import send_teem |
| 320 |
|
| 321 |
|
| 322 |
class Parameters(AnsibleF5Parameters): |
| 323 |
api_map = { |
| 324 |
'myIdType': 'presented_id_type', |
| 325 |
'peersIdType': 'verified_id_type', |
| 326 |
'phase1AuthMethod': 'phase1_auth_method', |
| 327 |
'presharedKeyEncrypted': 'preshared_key', |
| 328 |
'remoteAddress': 'remote_address', |
| 329 |
'version': 'version', |
| 330 |
'phase1EncryptAlgorithm': 'phase1_encryption_algorithm', |
| 331 |
'phase1HashAlgorithm': 'phase1_hash_algorithm', |
| 332 |
'phase1PerfectForwardSecrecy': 'phase1_perfect_forward_secrecy', |
| 333 |
'myCertFile': 'phase1_cert', |
| 334 |
'myCertKeyFile': 'phase1_key', |
| 335 |
'verifyCert': 'phase1_verify_peer_cert', |
| 336 |
'peersIdValue': 'verified_id_value', |
| 337 |
'myIdValue': 'presented_id_value', |
| 338 |
'lifetime': 'phase1_lifetime', |
| 339 |
} |
| 340 |
|
| 341 |
api_attributes = [ |
| 342 |
'myIdType', |
| 343 |
'peersIdType', |
| 344 |
'phase1AuthMethod', |
| 345 |
'presharedKeyEncrypted', |
| 346 |
'remoteAddress', |
| 347 |
'version', |
| 348 |
'phase1EncryptAlgorithm', |
| 349 |
'phase1HashAlgorithm', |
| 350 |
'phase1PerfectForwardSecrecy', |
| 351 |
'myCertFile', |
| 352 |
'myCertKeyFile', |
| 353 |
'verifyCert', |
| 354 |
'peersIdValue', |
| 355 |
'myIdValue', |
| 356 |
'description', |
| 357 |
'lifetime', |
| 358 |
] |
| 359 |
|
| 360 |
returnables = [ |
| 361 |
'presented_id_type', |
| 362 |
'verified_id_type', |
| 363 |
'phase1_auth_method', |
| 364 |
'preshared_key', |
| 365 |
'remote_address', |
| 366 |
'version', |
| 367 |
'phase1_encryption_algorithm', |
| 368 |
'phase1_hash_algorithm', |
| 369 |
'phase1_perfect_forward_secrecy', |
| 370 |
'phase1_cert', |
| 371 |
'phase1_key', |
| 372 |
'phase1_verify_peer_cert', |
| 373 |
'verified_id_value', |
| 374 |
'presented_id_value', |
| 375 |
'description', |
| 376 |
'phase1_lifetime', |
| 377 |
] |
| 378 |
|
| 379 |
updatables = [ |
| 380 |
'presented_id_type', |
| 381 |
'verified_id_type', |
| 382 |
'phase1_auth_method', |
| 383 |
'preshared_key', |
| 384 |
'remote_address', |
| 385 |
'version', |
| 386 |
'phase1_encryption_algorithm', |
| 387 |
'phase1_hash_algorithm', |
| 388 |
'phase1_perfect_forward_secrecy', |
| 389 |
'phase1_cert', |
| 390 |
'phase1_key', |
| 391 |
'phase1_verify_peer_cert', |
| 392 |
'verified_id_value', |
| 393 |
'presented_id_value', |
| 394 |
'description', |
| 395 |
'phase1_lifetime', |
| 396 |
] |
| 397 |
|
| 398 |
@property |
| 399 |
def phase1_verify_peer_cert(self): |
| 400 |
return flatten_boolean(self._values['phase1_verify_peer_cert']) |
| 401 |
|
| 402 |
|
| 403 |
class ApiParameters(Parameters): |
| 404 |
@property |
| 405 |
def description(self): |
| 406 |
if self._values['description'] in [None, 'none']: |
| 407 |
return None |
| 408 |
return self._values['description'] |
| 409 |
|
| 410 |
|
| 411 |
class ModuleParameters(Parameters): |
| 412 |
@property |
| 413 |
def phase1_cert(self): |
| 414 |
if self._values['phase1_cert'] is None: |
| 415 |
return None |
| 416 |
if self._values['phase1_cert'] in ['', 'none']: |
| 417 |
return '' |
| 418 |
return fq_name(self.partition, self._values['phase1_cert']) |
| 419 |
|
| 420 |
@property |
| 421 |
def phase1_key(self): |
| 422 |
if self._values['phase1_key'] is None: |
| 423 |
return None |
| 424 |
if self._values['phase1_key'] in ['', 'none']: |
| 425 |
return '' |
| 426 |
return fq_name(self.partition, self._values['phase1_key']) |
| 427 |
|
| 428 |
@property |
| 429 |
def phase1_lifetime(self): |
| 430 |
if self._values['phase1_lifetime'] is None: |
| 431 |
return None |
| 432 |
if 1 <= int(self._values['phase1_lifetime']) <= 4294967295: |
| 433 |
return int(self._values['phase1_lifetime']) |
| 434 |
raise F5ModuleError( |
| 435 |
"Valid 'phase1_lifetime' must be in range 1 - 4294967295." |
| 436 |
) |
| 437 |
|
| 438 |
@property |
| 439 |
def description(self): |
| 440 |
if self._values['description'] is None: |
| 441 |
return None |
| 442 |
elif self._values['description'] in ['none', '']: |
| 443 |
return '' |
| 444 |
return self._values['description'] |
| 445 |
|
| 446 |
|
| 447 |
class Changes(Parameters): |
| 448 |
def to_return(self): |
| 449 |
result = {} |
| 450 |
try: |
| 451 |
for returnable in self.returnables: |
| 452 |
result[returnable] = getattr(self, returnable) |
| 453 |
result = self._filter_params(result) |
| 454 |
except Exception: |
| 455 |
raise |
| 456 |
return result |
| 457 |
|
| 458 |
|
| 459 |
class UsableChanges(Changes): |
| 460 |
@property |
| 461 |
def phase1_verify_peer_cert(self): |
| 462 |
if self._values['phase1_verify_peer_cert'] is None: |
| 463 |
return None |
| 464 |
elif self._values['phase1_verify_peer_cert'] == 'yes': |
| 465 |
return 'true' |
| 466 |
else: |
| 467 |
return 'false' |
| 468 |
|
| 469 |
|
| 470 |
class ReportableChanges(Changes): |
| 471 |
@property |
| 472 |
def phase1_verify_peer_cert(self): |
| 473 |
return flatten_boolean(self._values['phase1_verify_peer_cert']) |
| 474 |
|
| 475 |
@property |
| 476 |
def preshared_key(self): |
| 477 |
return None |
| 478 |
|
| 479 |
|
| 480 |
class Difference(object): |
| 481 |
def __init__(self, want, have=None): |
| 482 |
self.want = want |
| 483 |
self.have = have |
| 484 |
|
| 485 |
def compare(self, param): |
| 486 |
try: |
| 487 |
result = getattr(self, param) |
| 488 |
return result |
| 489 |
except AttributeError: |
| 490 |
return self.__default(param) |
| 491 |
|
| 492 |
def __default(self, param): |
| 493 |
attr1 = getattr(self.want, param) |
| 494 |
try: |
| 495 |
attr2 = getattr(self.have, param) |
| 496 |
if attr1 != attr2: |
| 497 |
return attr1 |
| 498 |
except AttributeError: |
| 499 |
return attr1 |
| 500 |
|
| 501 |
@property |
| 502 |
def description(self): |
| 503 |
return cmp_str_with_none(self.want.description, self.have.description) |
| 504 |
|
| 505 |
|
| 506 |
class ModuleManager(object): |
| 507 |
def __init__(self, *args, **kwargs): |
| 508 |
self.module = kwargs.get('module', None) |
| 509 |
self.client = F5RestClient(**self.module.params) |
| 510 |
self.want = ModuleParameters(params=self.module.params) |
| 511 |
self.have = ApiParameters() |
| 512 |
self.changes = UsableChanges() |
| 513 |
|
| 514 |
def _set_changed_options(self): |
| 515 |
changed = {} |
| 516 |
for key in Parameters.returnables: |
| 517 |
if getattr(self.want, key) is not None: |
| 518 |
changed[key] = getattr(self.want, key) |
| 519 |
if changed: |
| 520 |
self.changes = UsableChanges(params=changed) |
| 521 |
|
| 522 |
def _update_changed_options(self): |
| 523 |
diff = Difference(self.want, self.have) |
| 524 |
updatables = Parameters.updatables |
| 525 |
changed = dict() |
| 526 |
for k in updatables: |
| 527 |
change = diff.compare(k) |
| 528 |
if change is None: |
| 529 |
continue |
| 530 |
else: |
| 531 |
if isinstance(change, dict): |
| 532 |
changed.update(change) |
| 533 |
else: |
| 534 |
changed[k] = change |
| 535 |
if changed: |
| 536 |
self.changes = UsableChanges(params=changed) |
| 537 |
return True |
| 538 |
return False |
| 539 |
|
| 540 |
def should_update(self): |
| 541 |
result = self._update_changed_options() |
| 542 |
if result: |
| 543 |
return True |
| 544 |
return False |
| 545 |
|
| 546 |
def exec_module(self): |
| 547 |
start = datetime.now().isoformat() |
| 548 |
version = tmos_version(self.client) |
| 549 |
changed = False |
| 550 |
result = dict() |
| 551 |
state = self.want.state |
| 552 |
|
| 553 |
if state == "present": |
| 554 |
changed = self.present() |
| 555 |
elif state == "absent": |
| 556 |
changed = self.absent() |
| 557 |
|
| 558 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 559 |
changes = reportable.to_return() |
| 560 |
result.update(**changes) |
| 561 |
result.update(dict(changed=changed)) |
| 562 |
self._announce_deprecations(result) |
| 563 |
send_teem(start, self.client, self.module, version) |
| 564 |
return result |
| 565 |
|
| 566 |
def _announce_deprecations(self, result): |
| 567 |
warnings = result.pop('__warnings', []) |
| 568 |
for warning in warnings: |
| 569 |
self.client.module.deprecate( |
| 570 |
msg=warning['msg'], |
| 571 |
version=warning['version'] |
| 572 |
) |
| 573 |
|
| 574 |
def present(self): |
| 575 |
if self.exists(): |
| 576 |
return self.update() |
| 577 |
else: |
| 578 |
return self.create() |
| 579 |
|
| 580 |
def exists(self): |
| 581 |
uri = "https://{0}:{1}/mgmt/tm/net/ipsec/ike-peer/{2}".format( |
| 582 |
self.client.provider['server'], |
| 583 |
self.client.provider['server_port'], |
| 584 |
transform_name(self.want.partition, self.want.name) |
| 585 |
) |
| 586 |
resp = self.client.api.get(uri) |
| 587 |
try: |
| 588 |
response = resp.json() |
| 589 |
except ValueError as ex: |
| 590 |
raise F5ModuleError(str(ex)) |
| 591 |
|
| 592 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 593 |
return False |
| 594 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 595 |
return True |
| 596 |
|
| 597 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 598 |
|
| 599 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 600 |
if 'message' in response: |
| 601 |
raise F5ModuleError(response['message']) |
| 602 |
else: |
| 603 |
raise F5ModuleError(resp.content) |
| 604 |
|
| 605 |
def update(self): |
| 606 |
self.have = self.read_current_from_device() |
| 607 |
|
| 608 |
if self.changes.version is not None and len(self.changes.version) == 0: |
| 609 |
raise F5ModuleError( |
| 610 |
"At least one version value must be specified." |
| 611 |
) |
| 612 |
|
| 613 |
if self.changes.phase1_auth_method == 'pre-shared-key': |
| 614 |
if self.changes.preshared_key is None and self.have.preshared_key is None: |
| 615 |
raise F5ModuleError( |
| 616 |
"A 'preshared_key' must be specified when changing 'phase1_auth_method' " |
| 617 |
"to 'pre-shared-key'." |
| 618 |
) |
| 619 |
|
| 620 |
if self.want.update_password == 'always': |
| 621 |
self.want.update({'preshared_key': self.want.preshared_key}) |
| 622 |
else: |
| 623 |
if self.want.preshared_key: |
| 624 |
del self.want._values['preshared_key'] |
| 625 |
|
| 626 |
if not self.should_update(): |
| 627 |
return False |
| 628 |
if self.module.check_mode: |
| 629 |
return True |
| 630 |
self.update_on_device() |
| 631 |
return True |
| 632 |
|
| 633 |
def remove(self): |
| 634 |
if self.module.check_mode: |
| 635 |
return True |
| 636 |
self.remove_from_device() |
| 637 |
if self.exists(): |
| 638 |
raise F5ModuleError("Failed to delete the resource.") |
| 639 |
return True |
| 640 |
|
| 641 |
def create(self): |
| 642 |
self._set_changed_options() |
| 643 |
if self.changes.version is None: |
| 644 |
raise F5ModuleError( |
| 645 |
"The 'version' parameter is required when creating a new IKE peer." |
| 646 |
) |
| 647 |
if self.changes.phase1_auth_method is None: |
| 648 |
self.changes.update({'phase1_auth_method': 'rsa-signature'}) |
| 649 |
if self.changes.phase1_cert is None: |
| 650 |
self.changes.update({'phase1_cert': 'default.crt'}) |
| 651 |
if self.changes.phase1_key is None: |
| 652 |
self.changes.update({'phase1_key': 'default.key'}) |
| 653 |
|
| 654 |
if self.module.check_mode: |
| 655 |
return True |
| 656 |
self.create_on_device() |
| 657 |
return True |
| 658 |
|
| 659 |
def create_on_device(self): |
| 660 |
params = self.changes.api_params() |
| 661 |
params['name'] = self.want.name |
| 662 |
params['partition'] = self.want.partition |
| 663 |
uri = "https://{0}:{1}/mgmt/tm/net/ipsec/ike-peer/".format( |
| 664 |
self.client.provider['server'], |
| 665 |
self.client.provider['server_port'] |
| 666 |
) |
| 667 |
resp = self.client.api.post(uri, json=params) |
| 668 |
try: |
| 669 |
response = resp.json() |
| 670 |
except ValueError as ex: |
| 671 |
raise F5ModuleError(str(ex)) |
| 672 |
|
| 673 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 674 |
return True |
| 675 |
raise F5ModuleError(resp.content) |
| 676 |
|
| 677 |
def update_on_device(self): |
| 678 |
params = self.changes.api_params() |
| 679 |
uri = "https://{0}:{1}/mgmt/tm/net/ipsec/ike-peer/{2}".format( |
| 680 |
self.client.provider['server'], |
| 681 |
self.client.provider['server_port'], |
| 682 |
transform_name(self.want.partition, self.want.name) |
| 683 |
) |
| 684 |
resp = self.client.api.patch(uri, json=params) |
| 685 |
try: |
| 686 |
response = resp.json() |
| 687 |
except ValueError as ex: |
| 688 |
raise F5ModuleError(str(ex)) |
| 689 |
|
| 690 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 691 |
return True |
| 692 |
raise F5ModuleError(resp.content) |
| 693 |
|
| 694 |
def absent(self): |
| 695 |
if self.exists(): |
| 696 |
return self.remove() |
| 697 |
return False |
| 698 |
|
| 699 |
def remove_from_device(self): |
| 700 |
uri = "https://{0}:{1}/mgmt/tm/net/ipsec/ike-peer/{2}".format( |
| 701 |
self.client.provider['server'], |
| 702 |
self.client.provider['server_port'], |
| 703 |
transform_name(self.want.partition, self.want.name) |
| 704 |
) |
| 705 |
response = self.client.api.delete(uri) |
| 706 |
|
| 707 |
if response.status in [200, 201]: |
| 708 |
return True |
| 709 |
raise F5ModuleError(response.content) |
| 710 |
|
| 711 |
def read_current_from_device(self): |
| 712 |
uri = "https://{0}:{1}/mgmt/tm/net/ipsec/ike-peer/{2}".format( |
| 713 |
self.client.provider['server'], |
| 714 |
self.client.provider['server_port'], |
| 715 |
transform_name(self.want.partition, self.want.name) |
| 716 |
) |
| 717 |
resp = self.client.api.get(uri) |
| 718 |
try: |
| 719 |
response = resp.json() |
| 720 |
except ValueError as ex: |
| 721 |
raise F5ModuleError(str(ex)) |
| 722 |
|
| 723 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 724 |
return ApiParameters(params=response) |
| 725 |
raise F5ModuleError(resp.content) |
| 726 |
|
| 727 |
|
| 728 |
class ArgumentSpec(object): |
| 729 |
def __init__(self): |
| 730 |
self.supports_check_mode = True |
| 731 |
argument_spec = dict( |
| 732 |
name=dict(required=True), |
| 733 |
presented_id_type=dict( |
| 734 |
choices=['address', 'asn1dn', 'fqdn', 'keyid-tag', 'user-fqdn', 'override'] |
| 735 |
), |
| 736 |
presented_id_value=dict(), |
| 737 |
verified_id_type=dict( |
| 738 |
choices=['address', 'asn1dn', 'fqdn', 'keyid-tag', 'user-fqdn', 'override'] |
| 739 |
), |
| 740 |
verified_id_value=dict(), |
| 741 |
phase1_auth_method=dict( |
| 742 |
choices=[ |
| 743 |
'pre-shared-key', 'rsa-signature' |
| 744 |
] |
| 745 |
), |
| 746 |
preshared_key=dict(no_log=True), |
| 747 |
remote_address=dict(), |
| 748 |
version=dict( |
| 749 |
type='list', |
| 750 |
elements='str', |
| 751 |
choices=['v1', 'v2'] |
| 752 |
), |
| 753 |
phase1_lifetime=dict(type='int'), |
| 754 |
phase1_encryption_algorithm=dict( |
| 755 |
choices=[ |
| 756 |
'3des', 'des', 'blowfish', 'cast128', 'aes128', 'aes192', |
| 757 |
'aes256', 'camellia' |
| 758 |
] |
| 759 |
), |
| 760 |
phase1_hash_algorithm=dict( |
| 761 |
choices=[ |
| 762 |
'sha1', 'md5', 'sha256', 'sha384', 'sha512' |
| 763 |
] |
| 764 |
), |
| 765 |
phase1_perfect_forward_secrecy=dict( |
| 766 |
choices=[ |
| 767 |
'ecp256', 'ecp384', 'ecp521', 'modp768', 'modp1024', 'modp1536', |
| 768 |
'modp2048', 'modp3072', 'modp4096', 'modp6144', 'modp8192' |
| 769 |
] |
| 770 |
), |
| 771 |
phase1_cert=dict(), |
| 772 |
phase1_key=dict(no_log=True), |
| 773 |
phase1_verify_peer_cert=dict(type='bool'), |
| 774 |
update_password=dict( |
| 775 |
default='always', |
| 776 |
choices=['always', 'on_create'], |
| 777 |
no_log=False |
| 778 |
), |
| 779 |
description=dict(), |
| 780 |
state=dict(default='present', choices=['absent', 'present']), |
| 781 |
partition=dict( |
| 782 |
default='Common', |
| 783 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 784 |
) |
| 785 |
) |
| 786 |
self.argument_spec = {} |
| 787 |
self.argument_spec.update(f5_argument_spec) |
| 788 |
self.argument_spec.update(argument_spec) |
| 789 |
self.required_if = [ |
| 790 |
['presented_id_type', 'fqdn', ['presented_id_value']], |
| 791 |
['presented_id_type', 'keyid-tag', ['presented_id_value']], |
| 792 |
['presented_id_type', 'user-fqdn', ['presented_id_value']], |
| 793 |
['presented_id_type', 'override', ['presented_id_value']], |
| 794 |
|
| 795 |
['verified_id_type', 'fqdn', ['verified_id_value']], |
| 796 |
['verified_id_type', 'keyid-tag', ['verified_id_value']], |
| 797 |
['verified_id_type', 'user-fqdn', ['verified_id_value']], |
| 798 |
['verified_id_type', 'override', ['verified_id_value']], |
| 799 |
] |
| 800 |
self.required_together = [ |
| 801 |
['phase1_cert', 'phase1_key'] |
| 802 |
] |
| 803 |
|
| 804 |
|
| 805 |
def main(): |
| 806 |
spec = ArgumentSpec() |
| 807 |
|
| 808 |
module = AnsibleModule( |
| 809 |
argument_spec=spec.argument_spec, |
| 810 |
supports_check_mode=spec.supports_check_mode, |
| 811 |
required_if=spec.required_if, |
| 812 |
required_together=spec.required_together, |
| 813 |
) |
| 814 |
|
| 815 |
try: |
| 816 |
mm = ModuleManager(module=module) |
| 817 |
results = mm.exec_module() |
| 818 |
module.exit_json(**results) |
| 819 |
except F5ModuleError as ex: |
| 820 |
module.fail_json(msg=str(ex)) |
| 821 |
|
| 822 |
|
| 823 |
if __name__ == '__main__': |
| 824 |
main() |