Skip to content

Commit f226dc4

Browse files
Dev: bootstrap: should pass SSH_AUTH_SOCK when swapping keys (#1633)
as we allow to mix agent-based and key file based authentication.
1 parent 339d1f6 commit f226dc4

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

crmsh/bootstrap.py

+29-11
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,9 @@ def _init_ssh_on_remote_nodes(
915915
elif not result.public_keys:
916916
pass
917917
elif isinstance(result.public_keys[0], ssh_key.KeyFile):
918-
public_key = ssh_key.InMemoryPublicKey(generate_ssh_key_pair_on_remote(local_user, node, user, user))
918+
public_key = ssh_key.InMemoryPublicKey(
919+
generate_ssh_key_pair_on_remote(local_shell, local_user, node, user, user),
920+
)
919921
public_key_list.append(public_key)
920922
authorized_key_manager.add(node, user, public_key)
921923
authorized_key_manager.add(None, local_user, public_key)
@@ -1075,12 +1077,12 @@ def ssh_copy_id(local_user, remote_user, remote_node):
10751077

10761078

10771079
def generate_ssh_key_pair_on_remote(
1080+
shell: sh.LocalShell,
10781081
local_sudoer: str,
10791082
remote_host: str, remote_sudoer: str,
1080-
remote_user: str
1083+
remote_user: str,
10811084
) -> str:
10821085
"""generate a key pair on remote and return the public key"""
1083-
shell = sh.LocalShell()
10841086
# pass cmd through stdin rather than as arguments. It seems sudo has its own argument parsing mechanics,
10851087
# which breaks shell expansion used in cmd
10861088
generate_key_script = f'''
@@ -1136,7 +1138,11 @@ def generate_ssh_key_pair_on_remote(
11361138
return result.stdout.decode('utf-8').strip()
11371139

11381140

1139-
def export_ssh_key_non_interactive(local_user_to_export, remote_user_to_swap, remote_node, local_sudoer, remote_sudoer):
1141+
def export_ssh_key_non_interactive(
1142+
shell: sh.LocalShell,
1143+
local_user_to_export, remote_user_to_swap,
1144+
remote_node, local_sudoer, remote_sudoer,
1145+
):
11401146
"""Copy ssh key from local to remote's authorized_keys. Require a configured non-interactive ssh authentication."""
11411147
# ssh-copy-id will prompt for the password of the destination user
11421148
# this is unwanted, so we write to the authorised_keys file ourselve
@@ -1146,7 +1152,7 @@ def export_ssh_key_non_interactive(local_user_to_export, remote_user_to_swap, re
11461152
{key}
11471153
EOF
11481154
'''.format(user=remote_user_to_swap, key=public_key)
1149-
result = sh.LocalShell().su_subprocess_run(
1155+
result = shell.su_subprocess_run(
11501156
local_sudoer,
11511157
'ssh {} {}@{} sudo /bin/sh'.format(constants.SSH_OPTION, remote_sudoer, remote_node),
11521158
input=cmd.encode('utf-8'),
@@ -1707,7 +1713,9 @@ def join_ssh_impl(local_user, seed_host, seed_user, ssh_public_keys: typing.List
17071713
if not result.public_keys:
17081714
pass
17091715
elif isinstance(result.public_keys[0], ssh_key.KeyFile):
1710-
public_key = ssh_key.InMemoryPublicKey(generate_ssh_key_pair_on_remote(local_user, seed_host, seed_user, seed_user))
1716+
public_key = ssh_key.InMemoryPublicKey(
1717+
generate_ssh_key_pair_on_remote(local_shell, local_user, seed_host, seed_user, seed_user),
1718+
)
17111719
authorized_key_manager.add( None, local_user, public_key)
17121720
logger.info('A public key is added to authorized_keys for user %s: %s', local_user, public_key.fingerprint())
17131721
elif isinstance(result.public_keys[0], ssh_key.InMemoryPublicKey):
@@ -1770,16 +1778,26 @@ def swap_public_ssh_key(
17701778
local_user_to_swap,
17711779
remote_user_to_swap,
17721780
local_sudoer,
1773-
remote_sudoer
1781+
remote_sudoer,
1782+
local_shell: sh.LocalShell = None, # FIXME: should not have default value
17741783
):
17751784
"""
17761785
Swap public ssh key between remote_node and local
17771786
"""
1787+
if local_shell is None:
1788+
local_shell = sh.LocalShell()
17781789
# Detect whether need password to login to remote_node
1779-
if utils.check_ssh_passwd_need(local_user_to_swap, remote_user_to_swap, remote_node):
1780-
export_ssh_key_non_interactive(local_user_to_swap, remote_user_to_swap, remote_node, local_sudoer, remote_sudoer)
1790+
if utils.check_ssh_passwd_need(local_user_to_swap, remote_user_to_swap, remote_node, local_shell):
1791+
export_ssh_key_non_interactive(
1792+
local_shell,
1793+
local_user_to_swap, remote_user_to_swap,
1794+
remote_node, local_sudoer, remote_sudoer,
1795+
)
17811796

1782-
public_key = generate_ssh_key_pair_on_remote(local_sudoer, remote_node, remote_sudoer, remote_user_to_swap)
1797+
public_key = generate_ssh_key_pair_on_remote(
1798+
local_shell,
1799+
local_sudoer, remote_node, remote_sudoer, remote_user_to_swap,
1800+
)
17831801
ssh_key.AuthorizedKeyManager(sh.SSHShell(sh.LocalShell(), local_user_to_swap)).add(
17841802
None, local_user_to_swap, ssh_key.InMemoryPublicKey(public_key),
17851803
)
@@ -1905,7 +1923,7 @@ def setup_passwordless_with_other_nodes(init_node, remote_user):
19051923
_merge_ssh_authorized_keys(cluster_node_list)
19061924
if local_user != 'hacluster':
19071925
change_user_shell('hacluster', node)
1908-
swap_public_ssh_key(node, 'hacluster', 'hacluster', local_user, remote_privileged_user)
1926+
swap_public_ssh_key(node, 'hacluster', 'hacluster', local_user, remote_privileged_user, local_shell)
19091927
if local_user != 'hacluster':
19101928
swap_key_for_hacluster(cluster_node_list)
19111929

0 commit comments

Comments
 (0)