Skip to content

Commit

Permalink
Dev: ssh_key: Avoid hardcoding the ssh key type as RSA
Browse files Browse the repository at this point in the history
In KeyFileManager, use class variable to store the key type instead of
hardcoding it as RSA.
  • Loading branch information
liangxin1300 committed Nov 1, 2024
1 parent 6ef4944 commit 0ed2eac
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion crmsh/ssh_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 0ed2eac

Please sign in to comment.