From 587636b5250648a256922a6229103560a780ace5 Mon Sep 17 00:00:00 2001 From: steve-dave Date: Sat, 24 Sep 2016 16:37:57 +1000 Subject: [PATCH] ec2_win_password.py - handle missing or unparseable key file more intuitively (#2729) --- .../modules/extras/cloud/amazon/ec2_win_password.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/ansible/modules/extras/cloud/amazon/ec2_win_password.py b/lib/ansible/modules/extras/cloud/amazon/ec2_win_password.py index 30fbcc08a0..df5250ba81 100644 --- a/lib/ansible/modules/extras/cloud/amazon/ec2_win_password.py +++ b/lib/ansible/modules/extras/cloud/amazon/ec2_win_password.py @@ -146,9 +146,15 @@ def main(): try: f = open(key_file, 'r') - key = RSA.importKey(f.read(), key_passphrase) - finally: - f.close() + except IOError as e: + module.fail_json(msg = "I/O error (%d) opening key file: %s" % (e.errno, e.strerror)) + else: + try: + with f: + key = RSA.importKey(f.read(), key_passphrase) + except (ValueError, IndexError, TypeError) as e: + module.fail_json(msg = "unable to parse key file") + cipher = PKCS1_v1_5.new(key) sentinel = 'password decryption failed!!!'