From 0ed2eac7b8b64be99f95c3106052275bb3e8eec6 Mon Sep 17 00:00:00 2001 From: xin liang Date: Fri, 1 Nov 2024 10:11:40 +0800 Subject: [PATCH] Dev: ssh_key: Avoid hardcoding the ssh key type as RSA In KeyFileManager, use class variable to store the key type instead of hardcoding it as RSA. --- crmsh/ssh_key.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crmsh/ssh_key.py b/crmsh/ssh_key.py index 8271f59b9..3a2e87ea8 100644 --- a/crmsh/ssh_key.py +++ b/crmsh/ssh_key.py @@ -183,6 +183,7 @@ def list(self) -> typing.List[Key]: class KeyFileManager: + DEFAULT_KEY_TYPE = 'rsa' KNOWN_KEY_TYPES = ['rsa', 'ed25519', 'ecdsa'] # dsa is not listed here as it is not so secure KNOWN_PUBLIC_KEY_FILENAME_PATTERN = re.compile('/id_(?:{})\\.pub$'.format('|'.join(KNOWN_KEY_TYPES))) @@ -232,7 +233,7 @@ def ensure_key_pair_exists_for_user( * list_of_public_keys: all public keys of known types, including the newly generated one """ script = '''if [ ! \\( {condition} \\) ]; then - ssh-keygen -t rsa -f ~/.ssh/id_rsa -q -C "Cluster internal on $(hostname)" -N '' <> /dev/null + ssh-keygen -t {key_type} -f ~/.ssh/id_{key_type} -q -C "Cluster internal on $(hostname)" -N '' <> /dev/null echo 'GENERATED=1' fi for file in ~/.ssh/id_{{{pattern}}}; do @@ -245,6 +246,7 @@ def ensure_key_pair_exists_for_user( done '''.format( condition=' -o '.join([f'-f ~/.ssh/id_{t}' for t in self.KNOWN_KEY_TYPES]), + key_type=self.DEFAULT_KEY_TYPE, pattern=','.join(self.KNOWN_KEY_TYPES), ) result = self.cluster_shell.subprocess_run_without_input(