From 551a020e785fa2253e47eefe6312781e7dbaf91d Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Tue, 29 Aug 2017 09:14:48 -0500 Subject: [PATCH] Fix incorrect use of datetime in letsencrypt (#27672) Also correctly sets the locale for run_command executions using the module provided method for setting the locale instead of the locale module in python. --- lib/ansible/modules/web_infrastructure/letsencrypt.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/ansible/modules/web_infrastructure/letsencrypt.py b/lib/ansible/modules/web_infrastructure/letsencrypt.py index 1c728fb8934..517c8382d4d 100644 --- a/lib/ansible/modules/web_infrastructure/letsencrypt.py +++ b/lib/ansible/modules/web_infrastructure/letsencrypt.py @@ -159,7 +159,6 @@ import binascii import copy import hashlib import json -import locale import os import re import shutil @@ -215,12 +214,12 @@ def get_cert_days(module, cert_file): _, out, _ = module.run_command(openssl_cert_cmd, check_rc=True) try: not_after_str = re.search(r"\s+Not After\s*:\s+(.*)", out.decode('utf8')).group(1) - not_after = datetime.datetime.fromtimestamp(time.mktime(time.strptime(not_after_str, '%b %d %H:%M:%S %Y %Z'))) + not_after = datetime.fromtimestamp(time.mktime(time.strptime(not_after_str, '%b %d %H:%M:%S %Y %Z'))) except AttributeError: module.fail_json(msg="No 'Not after' date found in {0}".format(cert_file)) except ValueError: module.fail_json(msg="Failed to parse 'Not after' date of {0}".format(cert_file)) - now = datetime.datetime.utcnow() + now = datetime.utcnow() return (not_after - now).days @@ -789,7 +788,7 @@ def main(): ) # AnsibleModule() changes the locale, so change it back to C because we rely on time.strptime() when parsing certificate dates. - locale.setlocale(locale.LC_ALL, "C") + module.run_command_environ_update = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C', LC_CTYPE='C') cert_days = get_cert_days(module, module.params['dest']) if cert_days < module.params['remaining_days']: