Skip to content
Snippets Groups Projects
Commit 84b5d33c authored by Thomas Woerner's avatar Thomas Woerner
Browse files

ansible_freeipa_module: New function convert_input_certificates

Certificates given by ansible could have leading and trailing white
space, but also multi line input is possible that also could have
leading and training white space and newlines.

New function:
- convert_input_certificates(module, certs, state)
parent a1230cab
No related branches found
No related tags found
No related merge requests found
...@@ -54,6 +54,7 @@ import tempfile ...@@ -54,6 +54,7 @@ import tempfile
import shutil import shutil
import socket import socket
import base64 import base64
import binascii
import ast import ast
import time import time
from datetime import datetime from datetime import datetime
...@@ -644,6 +645,7 @@ def encode_certificate(cert): ...@@ -644,6 +645,7 @@ def encode_certificate(cert):
Encode a certificate using base64. Encode a certificate using base64.
It also takes FreeIPA and Python versions into account. 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)): if isinstance(cert, (str, unicode, bytes)):
encoded = base64.b64encode(cert) encoded = base64.b64encode(cert)
...@@ -654,6 +656,33 @@ def encode_certificate(cert): ...@@ -654,6 +656,33 @@ def encode_certificate(cert):
return encoded 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): def load_cert_from_str(cert):
cert = cert.strip() cert = cert.strip()
if not cert.startswith("-----BEGIN CERTIFICATE-----"): if not cert.startswith("-----BEGIN CERTIFICATE-----"):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment