| 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_ssl_key_cert |
| 13 |
short_description: Import/Delete SSL keys and certs from BIG-IP |
| 14 |
description: |
| 15 |
- This module imports/deletes SSL keys and certificates on a BIG-IP. |
| 16 |
Keys can be imported from key files on the local disk, in PEM format. |
| 17 |
Certificates can be imported from certificate and key files on the local |
| 18 |
disk, in PEM format. |
| 19 |
version_added: "1.6.0" |
| 20 |
options: |
| 21 |
key_content: |
| 22 |
description: |
| 23 |
- Sets the contents of a key directly to the specified value. This is |
| 24 |
used with lookup plugins, or for anything with formatting or templating. |
| 25 |
This must be provided when C(state) is C(present). |
| 26 |
type: str |
| 27 |
state: |
| 28 |
description: |
| 29 |
- When C(present), ensures the key and/or cert is uploaded to the |
| 30 |
device. When C(absent), ensures the key and/or cert is removed |
| 31 |
from the device. If the key and/or cert is currently in use, the module |
| 32 |
will not be able to remove the key. |
| 33 |
type: str |
| 34 |
choices: |
| 35 |
- present |
| 36 |
- absent |
| 37 |
default: present |
| 38 |
key_name: |
| 39 |
description: |
| 40 |
- The name of the key. |
| 41 |
type: str |
| 42 |
passphrase: |
| 43 |
description: |
| 44 |
- Passphrase on key. |
| 45 |
type: str |
| 46 |
cert_content: |
| 47 |
description: |
| 48 |
- Sets the contents of a certificate directly to the specified value. |
| 49 |
This is used with lookup plugins or for anything with formatting or |
| 50 |
- C(content) must be provided when C(state) is C(present). |
| 51 |
type: str |
| 52 |
cert_name: |
| 53 |
description: |
| 54 |
- SSL certificate name. This is the cert name used when importing a certificate |
| 55 |
into the BIG-IP. It also determines the filenames of the objects on the LTM. |
| 56 |
type: str |
| 57 |
issuer_cert: |
| 58 |
description: |
| 59 |
- Issuer certificate used for OCSP monitoring. |
| 60 |
- This parameter is only valid on versions of BIG-IP 13.0.0 or above. |
| 61 |
type: str |
| 62 |
true_names: |
| 63 |
description: |
| 64 |
- When C(true), the module does not append C(.crt) and C(.key) extensions to the given certificate and key names. |
| 65 |
- When C(false), the module appends C(.crt) and C(.key) extensions to the given certificate and key names. |
| 66 |
type: bool |
| 67 |
default: false |
| 68 |
version_added: "1.24.0" |
| 69 |
partition: |
| 70 |
description: |
| 71 |
- Device partition to manage resources on. |
| 72 |
type: str |
| 73 |
default: Common |
| 74 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 75 |
author: |
| 76 |
- Nitin Khanna (@nitinthewiz) |
| 77 |
''' |
| 78 |
|
| 79 |
EXAMPLES = r''' |
| 80 |
- name: Import both key and cert |
| 81 |
bigip_ssl_key_cert: |
| 82 |
key_content: "{{ lookup('file', 'key.pem') }}" |
| 83 |
key_name: cert1 |
| 84 |
cert_content: "{{ lookup('file', 'cert.pem') }}" |
| 85 |
cert_name: cert1 |
| 86 |
state: present |
| 87 |
provider: |
| 88 |
password: secret |
| 89 |
server: lb.mydomain.com |
| 90 |
user: admin |
| 91 |
delegate_to: localhost |
| 92 |
''' |
| 93 |
|
| 94 |
RETURN = r''' |
| 95 |
# only common fields returned |
| 96 |
''' |
| 97 |
|
| 98 |
import hashlib |
| 99 |
import os |
| 100 |
import re |
| 101 |
from datetime import datetime |
| 102 |
|
| 103 |
from ansible.module_utils.basic import ( |
| 104 |
AnsibleModule, env_fallback |
| 105 |
) |
| 106 |
|
| 107 |
from ..module_utils.bigip import F5RestClient |
| 108 |
from ..module_utils.common import ( |
| 109 |
F5ModuleError, AnsibleF5Parameters, transform_name, flatten_boolean, |
| 110 |
f5_argument_spec, fq_name, merge_two_dicts |
| 111 |
) |
| 112 |
from ..module_utils.icontrol import ( |
| 113 |
TransactionContextManager, upload_file, tmos_version |
| 114 |
) |
| 115 |
from ..module_utils.teem import send_teem |
| 116 |
|
| 117 |
try: |
| 118 |
from StringIO import StringIO |
| 119 |
except ImportError: |
| 120 |
from io import StringIO |
| 121 |
|
| 122 |
|
| 123 |
class Parameters(AnsibleF5Parameters): |
| 124 |
download_path = '/var/config/rest/downloads' |
| 125 |
|
| 126 |
api_map = { |
| 127 |
'sourcePath': 'source_path', |
| 128 |
'issuerCert': 'issuer_cert', |
| 129 |
} |
| 130 |
|
| 131 |
api_attributes = [ |
| 132 |
'passphrase', |
| 133 |
'sourcePath', |
| 134 |
'issuerCert', |
| 135 |
] |
| 136 |
|
| 137 |
returnables = [ |
| 138 |
'checksum', |
| 139 |
'source_path', |
| 140 |
'issuer_cert', |
| 141 |
] |
| 142 |
|
| 143 |
updatables = [ |
| 144 |
'key_checksum', |
| 145 |
'cert_checksum', |
| 146 |
'content', |
| 147 |
'issuer_cert', |
| 148 |
'source_path', |
| 149 |
] |
| 150 |
|
| 151 |
|
| 152 |
class ApiParameters(Parameters): |
| 153 |
@property |
| 154 |
def key_filename(self): |
| 155 |
if self._values['name'] is None: |
| 156 |
return None |
| 157 |
return self._values['name'] |
| 158 |
|
| 159 |
@property |
| 160 |
def key_source_path(self): |
| 161 |
if self.key_filename is None: |
| 162 |
return None |
| 163 |
if self._values['key_source_path'] is None: |
| 164 |
return None |
| 165 |
else: |
| 166 |
return self._values['key_source_path'] |
| 167 |
|
| 168 |
@property |
| 169 |
def cert_filename(self): |
| 170 |
if self._values['name'] is None: |
| 171 |
return None |
| 172 |
return self._values['name'] |
| 173 |
|
| 174 |
@property |
| 175 |
def cert_source_path(self): |
| 176 |
if self.cert_filename is None: |
| 177 |
return None |
| 178 |
if self._values['cert_source_path'] is None: |
| 179 |
return None |
| 180 |
else: |
| 181 |
return self._values['cert_source_path'] |
| 182 |
|
| 183 |
@property |
| 184 |
def key_checksum(self): |
| 185 |
if self._values['key_checksum'] is None: |
| 186 |
return None |
| 187 |
pattern = r'SHA1:\d+:(?P<value>[\w+]{40})' |
| 188 |
matches = re.match(pattern, self._values['key_checksum']) |
| 189 |
if matches: |
| 190 |
return matches.group('value') |
| 191 |
|
| 192 |
@property |
| 193 |
def cert_checksum(self): |
| 194 |
if self._values['cert_checksum'] is None: |
| 195 |
return None |
| 196 |
pattern = r'SHA1:\d+:(?P<value>[\w+]{40})' |
| 197 |
matches = re.match(pattern, self._values['cert_checksum']) |
| 198 |
if matches: |
| 199 |
return matches.group('value') |
| 200 |
|
| 201 |
|
| 202 |
class ModuleParameters(Parameters): |
| 203 |
def _get_hash(self, content): |
| 204 |
k = hashlib.sha1() |
| 205 |
s = StringIO(content) |
| 206 |
while True: |
| 207 |
data = s.read(1024) |
| 208 |
if not data: |
| 209 |
break |
| 210 |
k.update(data.encode('utf-8')) |
| 211 |
return k.hexdigest() |
| 212 |
|
| 213 |
@property |
| 214 |
def issuer_cert(self): |
| 215 |
if self._values['issuer_cert'] is None: |
| 216 |
return None |
| 217 |
name = fq_name(self.partition, self._values['issuer_cert']) |
| 218 |
true_name = flatten_boolean(self.true_names) |
| 219 |
if true_name == 'yes': |
| 220 |
return name |
| 221 |
else: |
| 222 |
if name.endswith('.crt'): |
| 223 |
return name |
| 224 |
else: |
| 225 |
return name + '.crt' |
| 226 |
|
| 227 |
@property |
| 228 |
def key_filename(self): |
| 229 |
if self.key_name is None: |
| 230 |
return None |
| 231 |
true_name = flatten_boolean(self.true_names) |
| 232 |
if true_name == 'yes': |
| 233 |
return self.key_name |
| 234 |
else: |
| 235 |
if self.key_name.endswith('.key'): |
| 236 |
return self.key_name |
| 237 |
else: |
| 238 |
return self.key_name + '.key' |
| 239 |
|
| 240 |
@property |
| 241 |
def cert_filename(self): |
| 242 |
if self.cert_name is None: |
| 243 |
return None |
| 244 |
true_name = flatten_boolean(self.true_names) |
| 245 |
if true_name == 'yes': |
| 246 |
return self.cert_name |
| 247 |
else: |
| 248 |
if self.cert_name.endswith('.crt'): |
| 249 |
return self.cert_name |
| 250 |
else: |
| 251 |
return self.cert_name + '.crt' |
| 252 |
|
| 253 |
@property |
| 254 |
def key_checksum(self): |
| 255 |
if self.key_content is None: |
| 256 |
return None |
| 257 |
return self._get_hash(self.key_content) |
| 258 |
|
| 259 |
@property |
| 260 |
def cert_checksum(self): |
| 261 |
if self.cert_content is None: |
| 262 |
return None |
| 263 |
return self._get_hash(self.cert_content) |
| 264 |
|
| 265 |
@property |
| 266 |
def key_source_path(self): |
| 267 |
result = 'file://' + os.path.join( |
| 268 |
self.download_path, |
| 269 |
self.key_filename |
| 270 |
) |
| 271 |
return result |
| 272 |
|
| 273 |
@property |
| 274 |
def cert_source_path(self): |
| 275 |
result = 'file://' + os.path.join( |
| 276 |
self.download_path, |
| 277 |
self.cert_filename |
| 278 |
) |
| 279 |
return result |
| 280 |
|
| 281 |
|
| 282 |
class Changes(Parameters): |
| 283 |
def to_return(self): |
| 284 |
result = {} |
| 285 |
try: |
| 286 |
for returnable in self.returnables: |
| 287 |
result[returnable] = getattr(self, returnable) |
| 288 |
result = self._filter_params(result) |
| 289 |
except Exception: |
| 290 |
raise |
| 291 |
return result |
| 292 |
|
| 293 |
|
| 294 |
class UsableChanges(Changes): |
| 295 |
pass |
| 296 |
|
| 297 |
|
| 298 |
class ReportableChanges(Changes): |
| 299 |
pass |
| 300 |
|
| 301 |
|
| 302 |
class Difference(object): |
| 303 |
def __init__(self, want, have=None): |
| 304 |
self.want = want |
| 305 |
self.have = have |
| 306 |
|
| 307 |
def compare(self, param): |
| 308 |
try: |
| 309 |
result = getattr(self, param) |
| 310 |
return result |
| 311 |
except AttributeError: |
| 312 |
return self.__default(param) |
| 313 |
|
| 314 |
def __default(self, param): |
| 315 |
attr1 = getattr(self.want, param) |
| 316 |
try: |
| 317 |
attr2 = getattr(self.have, param) |
| 318 |
if attr1 != attr2: |
| 319 |
return attr1 |
| 320 |
except AttributeError: |
| 321 |
return attr1 |
| 322 |
|
| 323 |
@property |
| 324 |
def key_checksum(self): |
| 325 |
if self.want.key_checksum is None: |
| 326 |
return None |
| 327 |
if self.want.key_checksum != self.have.key_checksum: |
| 328 |
return self.want.key_checksum |
| 329 |
|
| 330 |
@property |
| 331 |
def key_source_path(self): |
| 332 |
if self.want.key_source_path is None: |
| 333 |
return None |
| 334 |
if self.want.key_source_path == self.have.key_source_path: |
| 335 |
if self.key_checksum: |
| 336 |
return self.want.key_source_path |
| 337 |
if self.want.key_source_path != self.have.key_source_path: |
| 338 |
return self.want.key_source_path |
| 339 |
|
| 340 |
@property |
| 341 |
def cert_source_path(self): |
| 342 |
if self.want.source_path is None: |
| 343 |
return None |
| 344 |
if self.want.source_path == self.have.source_path: |
| 345 |
if self.cert_content: |
| 346 |
return self.want.source_path |
| 347 |
if self.want.source_path != self.have.source_path: |
| 348 |
return self.want.source_path |
| 349 |
|
| 350 |
@property |
| 351 |
def cert_content(self): |
| 352 |
if self.want.cert_checksum != self.have.checksum: |
| 353 |
result = dict( |
| 354 |
checksum=self.want.cert_checksum, |
| 355 |
content=self.want.cert_content |
| 356 |
) |
| 357 |
return result |
| 358 |
|
| 359 |
|
| 360 |
class ModuleManager(object): |
| 361 |
def __init__(self, *args, **kwargs): |
| 362 |
self.module = kwargs.get('module', None) |
| 363 |
self.client = F5RestClient(**self.module.params) |
| 364 |
self.want = ModuleParameters(params=self.module.params) |
| 365 |
self.have = ApiParameters() |
| 366 |
self.changes = UsableChanges() |
| 367 |
|
| 368 |
def _set_changed_options(self): |
| 369 |
changed = {} |
| 370 |
for key in Parameters.returnables: |
| 371 |
if getattr(self.want, key) is not None: |
| 372 |
changed[key] = getattr(self.want, key) |
| 373 |
if changed: |
| 374 |
self.changes = UsableChanges(params=changed) |
| 375 |
|
| 376 |
def _update_changed_options(self): |
| 377 |
diff = Difference(self.want, self.have) |
| 378 |
updatables = Parameters.updatables |
| 379 |
changed = dict() |
| 380 |
for k in updatables: |
| 381 |
change = diff.compare(k) |
| 382 |
if change is None: |
| 383 |
continue |
| 384 |
else: |
| 385 |
if isinstance(change, dict): |
| 386 |
changed.update(change) |
| 387 |
else: |
| 388 |
changed[k] = change |
| 389 |
if changed: |
| 390 |
self.changes = UsableChanges(params=changed) |
| 391 |
return True |
| 392 |
return False |
| 393 |
|
| 394 |
def _announce_deprecations(self, result): |
| 395 |
warnings = result.pop('__warnings', []) |
| 396 |
for warning in warnings: |
| 397 |
self.client.module.deprecate( |
| 398 |
msg=warning['msg'], |
| 399 |
version=warning['version'] |
| 400 |
) |
| 401 |
|
| 402 |
def exec_module(self): |
| 403 |
start = datetime.now().isoformat() |
| 404 |
version = tmos_version(self.client) |
| 405 |
changed = False |
| 406 |
result = dict() |
| 407 |
state = self.want.state |
| 408 |
|
| 409 |
if state == "present": |
| 410 |
changed = self.present() |
| 411 |
elif state == "absent": |
| 412 |
changed = self.absent() |
| 413 |
|
| 414 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 415 |
changes = reportable.to_return() |
| 416 |
result.update(**changes) |
| 417 |
result.update(dict(changed=changed)) |
| 418 |
self._announce_deprecations(result) |
| 419 |
send_teem(start, self.client, self.module, version) |
| 420 |
return result |
| 421 |
|
| 422 |
def present(self): |
| 423 |
if self.exists(): |
| 424 |
return self.update() |
| 425 |
else: |
| 426 |
return self.create() |
| 427 |
|
| 428 |
def absent(self): |
| 429 |
if self.exists(): |
| 430 |
return self.remove() |
| 431 |
return False |
| 432 |
|
| 433 |
def should_update(self): |
| 434 |
result = self._update_changed_options() |
| 435 |
if result: |
| 436 |
return True |
| 437 |
return False |
| 438 |
|
| 439 |
def update(self): |
| 440 |
self.have = self.read_current_from_device() |
| 441 |
if not self.should_update(): |
| 442 |
return False |
| 443 |
if self.module.check_mode: |
| 444 |
return True |
| 445 |
self.update_on_device() |
| 446 |
return True |
| 447 |
|
| 448 |
def remove(self): |
| 449 |
if self.module.check_mode: |
| 450 |
return True |
| 451 |
self.remove_from_device() |
| 452 |
if self.exists(): |
| 453 |
raise F5ModuleError("Failed to delete the resource.") |
| 454 |
return True |
| 455 |
|
| 456 |
def create(self): |
| 457 |
self._set_changed_options() |
| 458 |
if self.module.check_mode: |
| 459 |
return True |
| 460 |
self.create_on_device() |
| 461 |
if self.want.key_filename: |
| 462 |
self.remove_uploaded_file_from_device(self.want.key_filename + "_key") |
| 463 |
if self.want.cert_filename: |
| 464 |
self.remove_uploaded_file_from_device(self.want.cert_filename + "_cert") |
| 465 |
return True |
| 466 |
|
| 467 |
def remove_uploaded_file_from_device(self, name): |
| 468 |
filepath = '/var/config/rest/downloads/{0}'.format(name) |
| 469 |
params = { |
| 470 |
"command": "run", |
| 471 |
"utilCmdArgs": filepath |
| 472 |
} |
| 473 |
uri = "https://{0}:{1}/mgmt/tm/util/unix-rm".format( |
| 474 |
self.client.provider['server'], |
| 475 |
self.client.provider['server_port'] |
| 476 |
) |
| 477 |
resp = self.client.api.post(uri, json=params) |
| 478 |
try: |
| 479 |
response = resp.json() |
| 480 |
except ValueError as ex: |
| 481 |
raise F5ModuleError(str(ex)) |
| 482 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 483 |
return True |
| 484 |
raise F5ModuleError(resp.content) |
| 485 |
|
| 486 |
def exists(self): |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
if self.want.key_name: |
| 491 |
uri = "https://{0}:{1}/mgmt/tm/sys/file/ssl-key/{2}".format( |
| 492 |
self.client.provider['server'], |
| 493 |
self.client.provider['server_port'], |
| 494 |
transform_name(self.want.partition, self.want.key_filename) |
| 495 |
) |
| 496 |
resp = self.client.api.get(uri) |
| 497 |
try: |
| 498 |
response = resp.json() |
| 499 |
except ValueError as ex: |
| 500 |
raise F5ModuleError(str(ex)) |
| 501 |
|
| 502 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 503 |
return False |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 508 |
|
| 509 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 510 |
if 'message' in response: |
| 511 |
raise F5ModuleError(response['message']) |
| 512 |
else: |
| 513 |
raise F5ModuleError(resp.content) |
| 514 |
|
| 515 |
if self.want.cert_name: |
| 516 |
uri = "https://{0}:{1}/mgmt/tm/sys/file/ssl-cert/{2}".format( |
| 517 |
self.client.provider['server'], |
| 518 |
self.client.provider['server_port'], |
| 519 |
transform_name(self.want.partition, self.want.cert_filename) |
| 520 |
) |
| 521 |
resp = self.client.api.get(uri) |
| 522 |
try: |
| 523 |
response = resp.json() |
| 524 |
except ValueError as ex: |
| 525 |
raise F5ModuleError(str(ex)) |
| 526 |
|
| 527 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 528 |
return False |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 533 |
|
| 534 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 535 |
if 'message' in response: |
| 536 |
raise F5ModuleError(response['message']) |
| 537 |
else: |
| 538 |
raise F5ModuleError(resp.content) |
| 539 |
|
| 540 |
return True |
| 541 |
|
| 542 |
def upload_file_to_device(self, content, name): |
| 543 |
url = 'https://{0}:{1}/mgmt/shared/file-transfer/uploads'.format( |
| 544 |
self.client.provider['server'], |
| 545 |
self.client.provider['server_port'] |
| 546 |
) |
| 547 |
try: |
| 548 |
upload_file(self.client, url, content, name) |
| 549 |
except F5ModuleError: |
| 550 |
raise F5ModuleError( |
| 551 |
"Failed to upload the file." |
| 552 |
) |
| 553 |
|
| 554 |
def _prepare_links(self): |
| 555 |
|
| 556 |
links = list() |
| 557 |
|
| 558 |
if self.want.key_name: |
| 559 |
key_link = "https://{0}:{1}/mgmt/tm/sys/file/ssl-key/{2}".format( |
| 560 |
self.client.provider['server'], |
| 561 |
self.client.provider['server_port'], |
| 562 |
transform_name(self.want.partition, self.want.key_filename) |
| 563 |
) |
| 564 |
links.append(key_link) |
| 565 |
|
| 566 |
if self.want.cert_name: |
| 567 |
cert_link = "https://{0}:{1}/mgmt/tm/sys/file/ssl-cert/{2}".format( |
| 568 |
self.client.provider['server'], |
| 569 |
self.client.provider['server_port'], |
| 570 |
transform_name(self.want.partition, self.want.cert_filename) |
| 571 |
) |
| 572 |
links.append(cert_link) |
| 573 |
|
| 574 |
return links |
| 575 |
|
| 576 |
def _prepare_links_for_update(self, params_dict): |
| 577 |
|
| 578 |
links_and_params = list() |
| 579 |
|
| 580 |
if self.want.key_name: |
| 581 |
key_link = "https://{0}:{1}/mgmt/tm/sys/file/ssl-key/{2}".format( |
| 582 |
self.client.provider['server'], |
| 583 |
self.client.provider['server_port'], |
| 584 |
transform_name(self.want.partition, self.want.key_filename) |
| 585 |
) |
| 586 |
key_params_dict = params_dict.copy() |
| 587 |
key_params_dict['sourcePath'] = self.want.key_source_path |
| 588 |
if self.want.passphrase: |
| 589 |
key_params_dict['passphrase'] = self.want.passphrase |
| 590 |
links_and_params.append({'link': key_link, 'params': key_params_dict}) |
| 591 |
|
| 592 |
if self.want.cert_name: |
| 593 |
cert_link = "https://{0}:{1}/mgmt/tm/sys/file/ssl-cert/{2}".format( |
| 594 |
self.client.provider['server'], |
| 595 |
self.client.provider['server_port'], |
| 596 |
transform_name(self.want.partition, self.want.cert_filename) |
| 597 |
) |
| 598 |
cert_params_dict = params_dict.copy() |
| 599 |
cert_params_dict['sourcePath'] = self.want.cert_source_path |
| 600 |
|
| 601 |
links_and_params.append({'link': cert_link, 'params': cert_params_dict}) |
| 602 |
|
| 603 |
return links_and_params |
| 604 |
|
| 605 |
def _prepare_links_for_create(self, params_dict): |
| 606 |
|
| 607 |
links_and_params = list() |
| 608 |
|
| 609 |
if self.want.key_name: |
| 610 |
key_link = "https://{0}:{1}/mgmt/tm/sys/file/ssl-key/".format( |
| 611 |
self.client.provider['server'], |
| 612 |
self.client.provider['server_port'] |
| 613 |
) |
| 614 |
key_params_dict = params_dict.copy() |
| 615 |
key_params_dict['name'] = self.want.key_filename |
| 616 |
key_params_dict['sourcePath'] = self.want.key_source_path + "_key" |
| 617 |
if self.want.passphrase: |
| 618 |
key_params_dict['passphrase'] = self.want.passphrase |
| 619 |
links_and_params.append({'link': key_link, 'params': key_params_dict}) |
| 620 |
|
| 621 |
if self.want.cert_name: |
| 622 |
cert_link = "https://{0}:{1}/mgmt/tm/sys/file/ssl-cert/".format( |
| 623 |
self.client.provider['server'], |
| 624 |
self.client.provider['server_port'] |
| 625 |
) |
| 626 |
cert_params_dict = params_dict.copy() |
| 627 |
cert_params_dict['name'] = self.want.cert_filename |
| 628 |
cert_params_dict['sourcePath'] = self.want.cert_source_path + "_cert" |
| 629 |
|
| 630 |
links_and_params.append({'link': cert_link, 'params': cert_params_dict}) |
| 631 |
|
| 632 |
return links_and_params |
| 633 |
|
| 634 |
def create_on_device(self): |
| 635 |
params = self.changes.api_params() |
| 636 |
params['partition'] = self.want.partition |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
links_and_params = self._prepare_links_for_create(params) |
| 641 |
|
| 642 |
if self.want.key_name: |
| 643 |
key_content = StringIO(self.want.key_content) |
| 644 |
self.upload_file_to_device(key_content, self.want.key_filename + "_key") |
| 645 |
|
| 646 |
if self.want.cert_name: |
| 647 |
cert_content = StringIO(self.want.cert_content) |
| 648 |
self.upload_file_to_device(cert_content, self.want.cert_filename + "_cert") |
| 649 |
|
| 650 |
with TransactionContextManager(self.client) as transact: |
| 651 |
for link in links_and_params: |
| 652 |
resp = transact.api.post(link['link'], json=link['params']) |
| 653 |
try: |
| 654 |
response = resp.json() |
| 655 |
except ValueError as ex: |
| 656 |
raise F5ModuleError(str(ex)) |
| 657 |
|
| 658 |
if not (resp.status in [200, 201] or 'code' in response and |
| 659 |
response['code'] in [200, 201]): |
| 660 |
raise F5ModuleError(resp.content) |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
if self.want.cert_name: |
| 668 |
params = self.want.api_params() |
| 669 |
if params: |
| 670 |
uri = "https://{0}:{1}/mgmt/tm/sys/file/ssl-cert/{2}".format( |
| 671 |
self.client.provider['server'], |
| 672 |
self.client.provider['server_port'], |
| 673 |
transform_name(self.want.partition, self.want.cert_filename) |
| 674 |
) |
| 675 |
resp = self.client.api.put(uri, json=params) |
| 676 |
try: |
| 677 |
response = resp.json() |
| 678 |
except ValueError as ex: |
| 679 |
raise F5ModuleError(str(ex)) |
| 680 |
|
| 681 |
if not (resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]): |
| 682 |
raise F5ModuleError(resp.content) |
| 683 |
|
| 684 |
return True |
| 685 |
|
| 686 |
def update_on_device(self): |
| 687 |
params = self.changes.api_params() |
| 688 |
|
| 689 |
if self.want.key_name: |
| 690 |
key_content = StringIO(self.want.key_content) |
| 691 |
self.upload_file_to_device(key_content, self.want.key_filename) |
| 692 |
|
| 693 |
if self.want.cert_name: |
| 694 |
cert_content = StringIO(self.want.cert_content) |
| 695 |
self.upload_file_to_device(cert_content, self.want.cert_filename) |
| 696 |
|
| 697 |
links_and_params = self._prepare_links_for_update(params) |
| 698 |
with TransactionContextManager(self.client) as transact: |
| 699 |
for link in links_and_params: |
| 700 |
resp = transact.api.patch(link['link'], json=link['params']) |
| 701 |
try: |
| 702 |
response = resp.json() |
| 703 |
except ValueError as ex: |
| 704 |
raise F5ModuleError(str(ex)) |
| 705 |
|
| 706 |
if not (resp.status in [200, 201] or 'code' in response and |
| 707 |
response['code'] in [200, 201]): |
| 708 |
raise F5ModuleError(resp.content) |
| 709 |
return True |
| 710 |
|
| 711 |
def remove_from_device(self): |
| 712 |
links = self._prepare_links() |
| 713 |
with TransactionContextManager(self.client) as transact: |
| 714 |
for link in links: |
| 715 |
resp = transact.api.delete(link) |
| 716 |
try: |
| 717 |
response = resp.json() |
| 718 |
except ValueError as ex: |
| 719 |
raise F5ModuleError(str(ex)) |
| 720 |
|
| 721 |
if not (resp.status in [200, 201]): |
| 722 |
raise F5ModuleError(resp.content) |
| 723 |
return True |
| 724 |
|
| 725 |
def read_current_from_device(self): |
| 726 |
final_response = {} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
if self.want.key_name: |
| 731 |
uri = "https://{0}:{1}/mgmt/tm/sys/file/ssl-key/{2}".format( |
| 732 |
self.client.provider['server'], |
| 733 |
self.client.provider['server_port'], |
| 734 |
transform_name(self.want.partition, self.want.key_filename) |
| 735 |
) |
| 736 |
resp = self.client.api.get(uri) |
| 737 |
try: |
| 738 |
response = resp.json() |
| 739 |
except ValueError as ex: |
| 740 |
raise F5ModuleError(str(ex)) |
| 741 |
|
| 742 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 743 |
response['key_checksum'] = response['checksum'] |
| 744 |
response['key_source_path'] = response['sourcePath'] |
| 745 |
final_response = merge_two_dicts(final_response, response) |
| 746 |
else: |
| 747 |
raise F5ModuleError(resp.content) |
| 748 |
|
| 749 |
if self.want.cert_name: |
| 750 |
uri = "https://{0}:{1}/mgmt/tm/sys/file/ssl-cert/{2}".format( |
| 751 |
self.client.provider['server'], |
| 752 |
self.client.provider['server_port'], |
| 753 |
transform_name(self.want.partition, self.want.cert_filename) |
| 754 |
) |
| 755 |
|
| 756 |
query = '?expandSubcollections=true' |
| 757 |
resp = self.client.api.get(uri + query) |
| 758 |
|
| 759 |
try: |
| 760 |
response = resp.json() |
| 761 |
except ValueError as ex: |
| 762 |
raise F5ModuleError(str(ex)) |
| 763 |
|
| 764 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 765 |
response['cert_checksum'] = response['checksum'] |
| 766 |
response['cert_source_path'] = response['sourcePath'] |
| 767 |
final_response = merge_two_dicts(final_response, response) |
| 768 |
else: |
| 769 |
raise F5ModuleError(resp.content) |
| 770 |
|
| 771 |
return ApiParameters(params=final_response) |
| 772 |
|
| 773 |
|
| 774 |
class ArgumentSpec(object): |
| 775 |
def __init__(self): |
| 776 |
self.supports_check_mode = True |
| 777 |
argument_spec = dict( |
| 778 |
key_name=dict(), |
| 779 |
key_content=dict( |
| 780 |
no_log=True |
| 781 |
), |
| 782 |
passphrase=dict( |
| 783 |
no_log=True |
| 784 |
), |
| 785 |
cert_name=dict(), |
| 786 |
cert_content=dict( |
| 787 |
no_log=True |
| 788 |
), |
| 789 |
issuer_cert=dict(), |
| 790 |
true_names=dict( |
| 791 |
type='bool', |
| 792 |
default='no' |
| 793 |
), |
| 794 |
state=dict( |
| 795 |
required=False, |
| 796 |
default='present', |
| 797 |
choices=['absent', 'present'] |
| 798 |
), |
| 799 |
partition=dict( |
| 800 |
default='Common', |
| 801 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 802 |
) |
| 803 |
) |
| 804 |
self.argument_spec = {} |
| 805 |
self.argument_spec.update(f5_argument_spec) |
| 806 |
self.argument_spec.update(argument_spec) |
| 807 |
|
| 808 |
|
| 809 |
def main(): |
| 810 |
spec = ArgumentSpec() |
| 811 |
|
| 812 |
module = AnsibleModule( |
| 813 |
argument_spec=spec.argument_spec, |
| 814 |
supports_check_mode=spec.supports_check_mode |
| 815 |
) |
| 816 |
|
| 817 |
try: |
| 818 |
mm = ModuleManager(module=module) |
| 819 |
results = mm.exec_module() |
| 820 |
module.exit_json(**results) |
| 821 |
except F5ModuleError as ex: |
| 822 |
module.fail_json(msg=str(ex)) |
| 823 |
|
| 824 |
|
| 825 |
if __name__ == '__main__': |
| 826 |
main() |