From 9e1c26c755dd4ddf390d775a743105a8f6a2c460 Mon Sep 17 00:00:00 2001 From: Florence Blanc-Renaud Date: Tue, 2 Oct 2018 16:14:05 +0200 Subject: [PATCH] certdb: provide meaningful err msg for wrong PIN ipa-server-install or ipa-replica-install do not provide a meaningful error message in CA-less mode when the install fails because of a wrong PIN. Update the err msg so that it provides a hint to the user. Fixes https://pagure.io/freeipa/issue/5378 Reviewed-By: Fraser Tweedale --- ipapython/certdb.py | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/ipapython/certdb.py b/ipapython/certdb.py index 1b588037e..34c43a9c5 100644 --- a/ipapython/certdb.py +++ b/ipapython/certdb.py @@ -221,6 +221,24 @@ KEY_RE = re.compile( ) +class Pkcs12ImportIncorrectPasswordError(RuntimeError): + """ Raised when import_pkcs12 fails because of a wrong password. + """ + pass + + +class Pkcs12ImportOpenError(RuntimeError): + """ Raised when import_pkcs12 fails trying to open the file. + """ + pass + + +class Pkcs12ImportUnknownError(RuntimeError): + """ Raised when import_pkcs12 fails because of an unknown error. + """ + pass + + class NSSDatabase: """A general-purpose wrapper around a NSS cert database @@ -578,13 +596,15 @@ class NSSDatabase: try: self.run_pk12util(args) except ipautil.CalledProcessError as e: - if e.returncode == 17: - raise RuntimeError("incorrect password for pkcs#12 file %s" % - pkcs12_filename) + if e.returncode == 17 or e.returncode == 18: + raise Pkcs12ImportIncorrectPasswordError( + "incorrect password for pkcs#12 file %s" % pkcs12_filename) elif e.returncode == 10: - raise RuntimeError("Failed to open %s" % pkcs12_filename) + raise Pkcs12ImportOpenError( + "Failed to open %s" % pkcs12_filename) else: - raise RuntimeError("unknown error import pkcs#12 file %s" % + raise Pkcs12ImportUnknownError( + "unknown error import pkcs#12 file %s" % pkcs12_filename) finally: if pkcs12_password_file is not None: @@ -722,8 +742,13 @@ class NSSDatabase: if import_keys: try: self.import_pkcs12(filename, key_password) - except RuntimeError: + except Pkcs12ImportUnknownError: + # the file may not be a PKCS#12 file, + # go to the generic error about unrecognized format pass + except RuntimeError as e: + raise RuntimeError("Failed to load %s: %s" % + (filename, str(e))) else: if key_file: raise RuntimeError( @@ -749,7 +774,9 @@ class NSSDatabase: continue - raise RuntimeError("Failed to load %s" % filename) + # Supported formats were tried but none succeeded + raise RuntimeError("Failed to load %s: unrecognized format" % + filename) if import_keys and not key_file: raise RuntimeError(