From 41a2542f0013c50dcbbea1ced34582d125d699e4 Mon Sep 17 00:00:00 2001
From: Joel Thompson <joel@jthompson.io>
Date: Sun, 27 Dec 2015 16:35:33 -0500
Subject: [PATCH] Ensure ec2_win_password doesn't leak file handle

Currently the module doesn't explicitly close the file handle. This
wraps the reading of the private key in a try/finally block to ensure
the file is properly closed.
---
 cloud/amazon/ec2_win_password.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/cloud/amazon/ec2_win_password.py b/cloud/amazon/ec2_win_password.py
index e3a012291e3..4ddf4f8f4cc 100644
--- a/cloud/amazon/ec2_win_password.py
+++ b/cloud/amazon/ec2_win_password.py
@@ -140,8 +140,11 @@ def main():
     if wait and datetime.datetime.now() >= end:
         module.fail_json(msg = "wait for password timeout after %d seconds" % wait_timeout)
 
-    f = open(key_file, 'r')
-    key = RSA.importKey(f.read(), key_passphrase)
+    try:
+        f = open(key_file, 'r')
+        key = RSA.importKey(f.read(), key_passphrase)
+    finally:
+        f.close()
     cipher = PKCS1_v1_5.new(key)
     sentinel = 'password decryption failed!!!'