changeset 8089:1eca0ed41a6e stable

ssh: let SshKeyModelException inherit from vcs RepositoryError - such exceptions are shown nicely in the UI
author Mads Kiilerich <mads@kiilerich.com>
date Sun, 29 Dec 2019 01:47:29 +0100
parents 74b7aa45c1e1
children c8af6594ded9
files kallithea/bin/kallithea_cli_ssh.py kallithea/model/ssh_key.py
diffstat 2 files changed, 10 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/bin/kallithea_cli_ssh.py	Sun Dec 29 01:43:18 2019 +0100
+++ b/kallithea/bin/kallithea_cli_ssh.py	Sun Dec 29 01:47:29 2019 +0100
@@ -25,7 +25,7 @@
 from kallithea.lib.utils2 import str2bool
 from kallithea.lib.vcs.backends.git.ssh import GitSshHandler
 from kallithea.lib.vcs.backends.hg.ssh import MercurialSshHandler
-from kallithea.model.ssh_key import SshKeyModel
+from kallithea.model.ssh_key import SshKeyModel, SshKeyModelException
 
 
 log = logging.getLogger(__name__)
@@ -83,5 +83,8 @@
 
     The file is usually maintained automatically, but this command will also re-write it.
     """
-
-    SshKeyModel().write_authorized_keys()
+    try:
+        SshKeyModel().write_authorized_keys()
+    except SshKeyModelException as e:
+        sys.stderr.write("%s\n" % e)
+        sys.exit(1)
--- a/kallithea/model/ssh_key.py	Sun Dec 29 01:43:18 2019 +0100
+++ b/kallithea/model/ssh_key.py	Sun Dec 29 01:47:29 2019 +0100
@@ -30,6 +30,7 @@
 
 from kallithea.lib import ssh
 from kallithea.lib.utils2 import safe_str, str2bool
+from kallithea.lib.vcs.exceptions import RepositoryError
 from kallithea.model.db import User, UserSshKeys
 from kallithea.model.meta import Session
 
@@ -37,7 +38,7 @@
 log = logging.getLogger(__name__)
 
 
-class SshKeyModelException(Exception):
+class SshKeyModelException(RepositoryError):
     """Exception raised by SshKeyModel methods to report errors"""
 
 
@@ -114,7 +115,7 @@
         # Now, test that the directory is or was created in a readable way by previous.
         if not (os.path.isdir(authorized_keys_dir) and
                 os.access(authorized_keys_dir, os.W_OK)):
-            raise Exception("Directory of authorized_keys cannot be written to so authorized_keys file %s cannot be written" % (authorized_keys))
+            raise SshKeyModelException("Directory of authorized_keys cannot be written to so authorized_keys file %s cannot be written" % (authorized_keys))
 
         # Make sure we don't overwrite a key file with important content
         if os.path.exists(authorized_keys):
@@ -125,7 +126,7 @@
                     elif ssh.SSH_OPTIONS in l and ' ssh-serve ' in l:
                         pass # Kallithea entries are ok to overwrite
                     else:
-                        raise Exception("Safety check failed, found %r in %s - please review and remove it" % (l.strip(), authorized_keys))
+                        raise SshKeyModelException("Safety check failed, found %r in %s - please review and remove it" % (l.strip(), authorized_keys))
 
         fh, tmp_authorized_keys = tempfile.mkstemp('.authorized_keys', dir=os.path.dirname(authorized_keys))
         with os.fdopen(fh, 'w') as f: