From 3302c65e0563ca4c651bf79460e13120755c6b49 Mon Sep 17 00:00:00 2001 From: Taylor Daugherty Date: Thu, 21 Jan 2016 09:30:49 -0500 Subject: [PATCH] Add the get_config() method Will first check if the env variable exists, and if it doesn't, reads the JSON from a config file. Shorted the filename to config.json. Required to be in the script directory if no ENV is set. json parsing error handling --- letsencrypt-aws.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/letsencrypt-aws.py b/letsencrypt-aws.py index 59cb998..713607b 100644 --- a/letsencrypt-aws.py +++ b/letsencrypt-aws.py @@ -27,6 +27,8 @@ # One day PERSISTENT_SLEEP_INTERVAL = 60 * 60 * 24 DNS_TTL = 30 +ENV_VAR = "LETSENCRYPT_AWS_CONFIG" +CONFIG_FILE = "config.json" class Logger(object): @@ -387,6 +389,32 @@ def acme_client_for_private_key(acme_directory_url, private_key): ) +def get_config(): + logger = Logger() + aws_json = os.getenv(ENV_VAR) + if aws_json is not None: + try: + config = json.loads(aws_json) + return config + except ValueError: + logger.emit("Error parsing JSON") + else: + try: + script_dir = os.path.dirname(os.path.abspath(__file__)) + except NameError: + script_dir = os.path.dirname(os.path.abspath(sys.argv[0])) + try: + abs_file_path = os.path.join(script_dir, CONFIG_FILE) + try: + config = json.loads(open(abs_file_path).read()) + return config + except ValueError: + logger.emit("Error parsing JSON") + except IOError: + logger.emit("AWS config cannot be found") + quit() + + @click.group() def cli(): pass @@ -422,7 +450,7 @@ def update_certificates(persistent=False, force_issue=False): # "acme_account_key": "s3://bucket/object", # "acme_directory_url": "(optional)" # } - config = json.loads(os.environ["LETSENCRYPT_AWS_CONFIG"]) + config = get_config() domains = config["domains"] acme_directory_url = config.get( "acme_directory_url", DEFAULT_ACME_DIRECTORY_URL @@ -460,7 +488,7 @@ def update_certificates(persistent=False, force_issue=False): ) def register(email, out): logger = Logger() - config = json.loads(os.environ.get("LETSENCRYPT_AWS_CONFIG", "{}")) + config = get_config() acme_directory_url = config.get( "acme_directory_url", DEFAULT_ACME_DIRECTORY_URL )