# HG changeset patch # User Mads Kiilerich # Date 2019-12-29 01:47:29 # Node ID 1eca0ed41a6e3481ffe52119b268b1ac12267d66 # Parent 74b7aa45c1e15e239d1999b206f003f74033f750 ssh: let SshKeyModelException inherit from vcs RepositoryError - such exceptions are shown nicely in the UI diff --git a/kallithea/bin/kallithea_cli_ssh.py b/kallithea/bin/kallithea_cli_ssh.py --- a/kallithea/bin/kallithea_cli_ssh.py +++ b/kallithea/bin/kallithea_cli_ssh.py @@ -25,7 +25,7 @@ import kallithea.bin.kallithea_cli_base 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 @@ def ssh_update_authorized_keys(): 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) diff --git a/kallithea/model/ssh_key.py b/kallithea/model/ssh_key.py --- a/kallithea/model/ssh_key.py +++ b/kallithea/model/ssh_key.py @@ -30,6 +30,7 @@ from tg.i18n import ugettext as _ 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 @@ from kallithea.model.meta import Session log = logging.getLogger(__name__) -class SshKeyModelException(Exception): +class SshKeyModelException(RepositoryError): """Exception raised by SshKeyModel methods to report errors""" @@ -114,7 +115,7 @@ class SshKeyModel(object): # 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 @@ class SshKeyModel(object): 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: