From 69f2b3d72791cb10897cf579d2f6941fcffb04d2 Mon Sep 17 00:00:00 2001 From: Michael Scherer Date: Wed, 20 Apr 2016 00:03:14 +0200 Subject: [PATCH] Fix error reporting for fetch_key (#2662) Since fetch_url already take care of the exception, the try/except clause is no longer working, so replace it with proper status checking, thus permitting to remove urlib2 from the import list. --- lib/ansible/modules/packaging/os/rpm_key.py | 25 ++++++++++----------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/ansible/modules/packaging/os/rpm_key.py b/lib/ansible/modules/packaging/os/rpm_key.py index 5517394c82b..cc2e783cb58 100644 --- a/lib/ansible/modules/packaging/os/rpm_key.py +++ b/lib/ansible/modules/packaging/os/rpm_key.py @@ -62,7 +62,6 @@ EXAMPLES = ''' ''' import re import os.path -import urllib2 import tempfile def is_pubkey(string): @@ -115,18 +114,18 @@ class RpmKey: def fetch_key(self, url): """Downloads a key from url, returns a valid path to a gpg key""" - try: - rsp, info = fetch_url(self.module, url) - key = rsp.read() - if not is_pubkey(key): - self.module.fail_json(msg="Not a public key: %s" % url) - tmpfd, tmpname = tempfile.mkstemp() - tmpfile = os.fdopen(tmpfd, "w+b") - tmpfile.write(key) - tmpfile.close() - return tmpname - except urllib2.URLError, e: - self.module.fail_json(msg=str(e)) + rsp, info = fetch_url(self.module, url) + if info['status'] != 200: + self.module.fail_json(msg="failed to fetch key at %s , error was: %s" % (url, info['msg'])) + + key = rsp.read() + if not is_pubkey(key): + self.module.fail_json(msg="Not a public key: %s" % url) + tmpfd, tmpname = tempfile.mkstemp() + tmpfile = os.fdopen(tmpfd, "w+b") + tmpfile.write(key) + tmpfile.close() + return tmpname def normalize_keyid(self, keyid): """Ensure a keyid doesn't have a leading 0x, has leading or trailing whitespace, and make sure is lowercase"""