Skip to content

Commit

Permalink
Merge pull request #996 from liangxin1300/20220728_ssh_key
Browse files Browse the repository at this point in the history
Dev: bootstrap: Generate public key from private key if not exist
  • Loading branch information
liangxin1300 authored Aug 10, 2022
2 parents 4f90c4e + 8f4d999 commit 387d071
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crmsh/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,10 +828,14 @@ def configure_ssh_key(user="root", remote=None):
"""
change_user_shell(user)

cmd = ""
private_key, public_key, authorized_file = key_files(user).values()
if not utils.detect_file(private_key, remote=remote):
logger.info("SSH key for {} does not exist, hence generate it now".format(user))
cmd = "ssh-keygen -q -f {} -C 'Cluster Internal on {}' -N ''".format(private_key, remote if remote else utils.this_node())
elif not utils.detect_file(public_key, remote=remote):
cmd = "ssh-keygen -y -f {} > {}".format(private_key, public_key)
if cmd:
cmd = utils.add_su(cmd, user)
utils.get_stdout_or_raise_error(cmd, remote=remote)

Expand Down
3 changes: 2 additions & 1 deletion test/unittests/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,15 @@ def test_configure_ssh_key_remote(self, mock_change_shell, mock_key_files, mock_
@mock.patch('crmsh.bootstrap.change_user_shell')
def test_configure_ssh_key(self, mock_change_shell, mock_key_files, mock_detect, mock_run, mock_append_unique):
mock_key_files.return_value = {"private": "/test/.ssh/id_rsa", "public": "/test/.ssh/id_rsa.pub", "authorized": "/test/.ssh/authorized_keys"}
mock_detect.side_effect = [True, False]
mock_detect.side_effect = [True, True, False]

bootstrap.configure_ssh_key("test")

mock_change_shell.assert_called_once_with("test")
mock_key_files.assert_called_once_with("test")
mock_detect.assert_has_calls([
mock.call("/test/.ssh/id_rsa", remote=None),
mock.call("/test/.ssh/id_rsa.pub", remote=None),
mock.call("/test/.ssh/authorized_keys", remote=None)
])
mock_append_unique.assert_called_once_with("/test/.ssh/id_rsa.pub", "/test/.ssh/authorized_keys", remote=None)
Expand Down

0 comments on commit 387d071

Please sign in to comment.