diff --git a/plugins/module_utils/ansible_freeipa_module.py b/plugins/module_utils/ansible_freeipa_module.py index 2f0ebff6ef9e4555dd10561b2ef1f43487d4b2d6..36cc456633b63f3589a2c7240da6e0077dc474d8 100644 --- a/plugins/module_utils/ansible_freeipa_module.py +++ b/plugins/module_utils/ansible_freeipa_module.py @@ -54,6 +54,7 @@ import tempfile import shutil import socket import base64 +import binascii import ast import time from datetime import datetime @@ -644,6 +645,7 @@ def encode_certificate(cert): Encode a certificate using base64. It also takes FreeIPA and Python versions into account. + This is used to convert the certificates returned by find and show. """ if isinstance(cert, (str, unicode, bytes)): encoded = base64.b64encode(cert) @@ -654,6 +656,33 @@ def encode_certificate(cert): return encoded +def convert_input_certificates(module, certs, state): + """ + Convert certificates. + + Remove all newlines and white spaces from the certificates. + This is used on input parameter certificates of modules. + """ + if certs is None: + return None + + _certs = [] + for cert in certs: + try: + _cert = base64.b64encode(base64.b64decode(cert)).decode("ascii") + except (TypeError, binascii.Error) as e: + # Idempotency: Do not fail for an invalid cert for state absent. + # The invalid certificate can not be set in FreeIPA. + if state == "absent": + continue + module.fail_json( + msg="Certificate %s: Base64 decoding failed: %s" % + (repr(cert), str(e))) + _certs.append(_cert) + + return _certs + + def load_cert_from_str(cert): cert = cert.strip() if not cert.startswith("-----BEGIN CERTIFICATE-----"):