Skip to content

Commit 387d071

Browse files
authored
Merge pull request #996 from liangxin1300/20220728_ssh_key
Dev: bootstrap: Generate public key from private key if not exist
2 parents 4f90c4e + 8f4d999 commit 387d071

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

crmsh/bootstrap.py

+4
Original file line numberDiff line numberDiff line change
@@ -828,10 +828,14 @@ def configure_ssh_key(user="root", remote=None):
828828
"""
829829
change_user_shell(user)
830830

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

test/unittests/test_bootstrap.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,15 @@ def test_configure_ssh_key_remote(self, mock_change_shell, mock_key_files, mock_
362362
@mock.patch('crmsh.bootstrap.change_user_shell')
363363
def test_configure_ssh_key(self, mock_change_shell, mock_key_files, mock_detect, mock_run, mock_append_unique):
364364
mock_key_files.return_value = {"private": "/test/.ssh/id_rsa", "public": "/test/.ssh/id_rsa.pub", "authorized": "/test/.ssh/authorized_keys"}
365-
mock_detect.side_effect = [True, False]
365+
mock_detect.side_effect = [True, True, False]
366366

367367
bootstrap.configure_ssh_key("test")
368368

369369
mock_change_shell.assert_called_once_with("test")
370370
mock_key_files.assert_called_once_with("test")
371371
mock_detect.assert_has_calls([
372372
mock.call("/test/.ssh/id_rsa", remote=None),
373+
mock.call("/test/.ssh/id_rsa.pub", remote=None),
373374
mock.call("/test/.ssh/authorized_keys", remote=None)
374375
])
375376
mock_append_unique.assert_called_once_with("/test/.ssh/id_rsa.pub", "/test/.ssh/authorized_keys", remote=None)

0 commit comments

Comments
 (0)