1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Remove get_exception from crypto namespace

Fix removes get_exception in favor of native Python exception
handling. Also, added to_native to manage exception message.
This commit is contained in:
Yanis Guenane 2017-07-03 12:07:42 +02:00 committed by Toshio Kuratomi
parent db5c0b3b2b
commit ed15a0e9a7
3 changed files with 33 additions and 42 deletions

View file

@ -167,7 +167,7 @@ else:
pyopenssl_found = True pyopenssl_found = True
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception from ansible.module_utils._text import to_native
class CertificateSigningRequestError(Exception): class CertificateSigningRequestError(Exception):
@ -230,9 +230,8 @@ class CertificateSigningRequest(object):
csr_file = open(self.path, 'w') csr_file = open(self.path, 'w')
csr_file.write(crypto.dump_certificate_request(crypto.FILETYPE_PEM, self.request)) csr_file.write(crypto.dump_certificate_request(crypto.FILETYPE_PEM, self.request))
csr_file.close() csr_file.close()
except (IOError, OSError): except (IOError, OSError) as exc:
e = get_exception() raise CertificateSigningRequestError(exc)
raise CertificateSigningRequestError(e)
else: else:
self.changed = False self.changed = False
@ -245,10 +244,9 @@ class CertificateSigningRequest(object):
try: try:
os.remove(self.path) os.remove(self.path)
except OSError: except OSError as exc:
e = get_exception() if exc.errno != errno.ENOENT:
if e.errno != errno.ENOENT: raise CertificateSigningRequestError(exc)
raise CertificateSigningRequestError(e)
else: else:
self.changed = False self.changed = False
@ -305,9 +303,8 @@ def main():
try: try:
csr.generate(module) csr.generate(module)
except CertificateSigningRequestError: except CertificateSigningRequestError as exc:
e = get_exception() module.fail_json(msg=to_native(exc))
module.fail_json(msg=str(e))
else: else:
@ -318,9 +315,8 @@ def main():
try: try:
csr.remove() csr.remove()
except CertificateSigningRequestError: except CertificateSigningRequestError as exc:
e = get_exception() module.fail_json(msg=to_native(exc))
module.fail_json(msg=str(e))
result = csr.dump() result = csr.dump()

View file

@ -114,6 +114,7 @@ else:
import os import os
from ansible.module_utils._text import to_native
class PrivateKeyError(Exception): class PrivateKeyError(Exception):
@ -147,8 +148,8 @@ class PrivateKey(object):
try: try:
self.privatekey.generate_key(crypto_type, self.size) self.privatekey.generate_key(crypto_type, self.size)
except (TypeError, ValueError): except (TypeError, ValueError) as exc:
raise PrivateKeyError(get_exception()) raise PrivateKeyError(exc)
try: try:
privatekey_file = os.open(self.path, privatekey_file = os.open(self.path,
@ -157,9 +158,9 @@ class PrivateKey(object):
os.write(privatekey_file, crypto.dump_privatekey(crypto.FILETYPE_PEM, self.privatekey)) os.write(privatekey_file, crypto.dump_privatekey(crypto.FILETYPE_PEM, self.privatekey))
os.close(privatekey_file) os.close(privatekey_file)
except IOError: except IOError as exc:
self.remove() self.remove()
raise PrivateKeyError(get_exception()) raise PrivateKeyError(exc)
else: else:
self.changed = False self.changed = False
@ -173,10 +174,9 @@ class PrivateKey(object):
try: try:
os.remove(self.path) os.remove(self.path)
except OSError: except OSError as exc:
e = get_exception() if exc.errno != errno.ENOENT:
if e.errno != errno.ENOENT: raise PrivateKeyError(exc)
raise PrivateKeyError(e)
else: else:
self.changed = False self.changed = False
@ -230,9 +230,8 @@ def main():
try: try:
private_key.generate(module) private_key.generate(module)
except PrivateKeyError: except PrivateKeyError as exc:
e = get_exception() module.fail_json(msg=to_native(exc))
module.fail_json(msg=str(e))
else: else:
if module.check_mode: if module.check_mode:
@ -242,9 +241,8 @@ def main():
try: try:
private_key.remove() private_key.remove()
except PrivateKeyError: except PrivateKeyError as exc:
e = get_exception() module.fail_json(msg=to_native(exc))
module.fail_json(msg=str(e))
result = private_key.dump() result = private_key.dump()

View file

@ -99,6 +99,7 @@ else:
import os import os
from ansible.module_utils._text import to_native
class PublicKeyError(Exception): class PublicKeyError(Exception):
@ -127,10 +128,9 @@ class PublicKey(object):
publickey_file = open(self.path, 'w') publickey_file = open(self.path, 'w')
publickey_file.write(crypto.dump_publickey(crypto.FILETYPE_PEM, privatekey)) publickey_file.write(crypto.dump_publickey(crypto.FILETYPE_PEM, privatekey))
publickey_file.close() publickey_file.close()
except (IOError, OSError): except (IOError, OSError) as exc:
e = get_exception() raise PublicKeyError(exc)
raise PublicKeyError(e) except AttributeError as exc:
except AttributeError:
self.remove() self.remove()
raise PublicKeyError('You need to have PyOpenSSL>=16.0.0 to generate public keys') raise PublicKeyError('You need to have PyOpenSSL>=16.0.0 to generate public keys')
else: else:
@ -145,10 +145,9 @@ class PublicKey(object):
try: try:
os.remove(self.path) os.remove(self.path)
except OSError: except OSError as exc:
e = get_exception() if exc.errno != errno.ENOENT:
if e.errno != errno.ENOENT: raise PublicKeyError(exc)
raise PublicKeyError(e)
else: else:
self.changed = False self.changed = False
@ -205,9 +204,8 @@ def main():
try: try:
public_key.generate(module) public_key.generate(module)
except PublicKeyError: except PublicKeyError as exc:
e = get_exception() module.fail_json(msg=to_native(exc))
module.fail_json(msg=str(e))
else: else:
if module.check_mode: if module.check_mode:
@ -217,9 +215,8 @@ def main():
try: try:
public_key.remove() public_key.remove()
except PublicKeyError: except PublicKeyError as exc:
e = get_exception() module.fail_json(msg=to_native(exc))
module.fail_json(msg=str(e))
result = public_key.dump() result = public_key.dump()