[PATCH 1/3] Nicer crash for when PyNaCl isn't available

Konstantin Ryabitsev <[email protected]> Fri, 7 May 2021 14:13:20 -0400
Newsgroups org.kernel.lore.signatures
Message-ID <[email protected]>
Don't backtrace all over the screen -- give a nice error message.

Signed-off-by: Konstantin Ryabitsev <[email protected]>
---
 patatt/__init__.py | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/patatt/__init__.py b/patatt/__init__.py
index a3870b8..e54bb10 100644
--- a/patatt/__init__.py
+++ b/patatt/__init__.py
@@ -222,8 +222,11 @@ class DevsigHeader:
     @staticmethod
     def _sign_ed25519(payload: bytes, privkey: bytes) -> Tuple[bytes, bytes]:
         global KEYCACHE
-        from nacl.signing import SigningKey
-        from nacl.encoding import Base64Encoder
+        try:
+            from nacl.signing import SigningKey
+            from nacl.encoding import Base64Encoder
+        except ModuleNotFoundError:
+            raise RuntimeError('This operation requires PyNaCl libraries')
 
         if privkey not in KEYCACHE:
             sk = SigningKey(privkey, encoder=Base64Encoder)
@@ -238,9 +241,12 @@ class DevsigHeader:
 
     @staticmethod
     def _validate_ed25519(sigdata: bytes, pubkey: bytes) -> bytes:
-        from nacl.signing import VerifyKey
-        from nacl.encoding import Base64Encoder
-        from nacl.exceptions import BadSignatureError
+        try:
+            from nacl.signing import VerifyKey
+            from nacl.encoding import Base64Encoder
+            from nacl.exceptions import BadSignatureError
+        except ModuleNotFoundError:
+            raise RuntimeError('This operation requires PyNaCl libraries')
 
         vk = VerifyKey(pubkey, encoder=Base64Encoder)
         try:
-- 
2.31.1