Skip to content

Commit

Permalink
Fix bugs / improve get_cert_days behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Feb 2, 2021
1 parent a22e4d6 commit 73166fb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion plugins/module_utils/acme/backend_cryptography.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,9 @@ def get_cert_days(self, cert_filename=None, cert_content=None, now=None):
If now is not specified, datetime.datetime.now() is used.
'''
if cert_filename is not None:
cert_content = read_file(cert_filename)
cert_content = None
if os.path.exists(cert_filename):
cert_content = read_file(cert_filename)
else:
cert_content = to_bytes(cert_content)

Expand Down
10 changes: 6 additions & 4 deletions plugins/module_utils/acme/backend_openssl_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def get_csr_identifiers(self, csr_filename=None, csr_content=None):
data = csr_content.encode('utf-8')

openssl_csr_cmd = [self.openssl_binary, "req", "-in", filename, "-noout", "-text"]
dummy, out, dummy = self.module.run_command(openssl_csr_cmd, data=data, check_rc=True)
dummy, out, dummy = self.module.run_command(openssl_csr_cmd, data=data, check_rc=True, binary_data=True)

identifiers = set([])
common_name = re.search(r"Subject:.* CN\s?=\s?([^\s,;/]+)", to_text(out, errors='surrogate_or_strict'))
Expand Down Expand Up @@ -260,15 +260,17 @@ def get_cert_days(self, cert_filename=None, cert_content=None, now=None):
filename = '-'
data = cert_content.encode('utf-8')
cert_filename_suffix = ''
else:
elif cert_filename is not None:
if not os.path.exists(cert_filename):
return -1
cert_filename_suffix = ' in {0}'.format(cert_filename)
else:
return -1

openssl_cert_cmd = [self.openssl_binary, "x509", "-in", filename, "-noout", "-text"]
dummy, out, dummy = self.module.run_command(openssl_cert_cmd, data=data, check_rc=True, encoding=None)
dummy, out, dummy = self.module.run_command(openssl_cert_cmd, data=data, check_rc=True, binary_data=True)
try:
not_after_str = re.search(r"\s+Not After\s*:\s+(.*)", out.decode('utf8')).group(1)
not_after_str = re.search(r"\s+Not After\s*:\s+(.*)", to_text(out, errors='surrogate_or_strict')).group(1)
not_after = datetime.datetime.fromtimestamp(time.mktime(time.strptime(not_after_str, '%b %d %H:%M:%S %Y %Z')))
except AttributeError:
raise ModuleFailException("No 'Not after' date found{0}".format(cert_filename_suffix))
Expand Down

0 comments on commit 73166fb

Please sign in to comment.