@@ -915,7 +915,9 @@ def _init_ssh_on_remote_nodes(
915
915
elif not result .public_keys :
916
916
pass
917
917
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
+ )
919
921
public_key_list .append (public_key )
920
922
authorized_key_manager .add (node , user , public_key )
921
923
authorized_key_manager .add (None , local_user , public_key )
@@ -1075,12 +1077,12 @@ def ssh_copy_id(local_user, remote_user, remote_node):
1075
1077
1076
1078
1077
1079
def generate_ssh_key_pair_on_remote (
1080
+ shell : sh .LocalShell ,
1078
1081
local_sudoer : str ,
1079
1082
remote_host : str , remote_sudoer : str ,
1080
- remote_user : str
1083
+ remote_user : str ,
1081
1084
) -> str :
1082
1085
"""generate a key pair on remote and return the public key"""
1083
- shell = sh .LocalShell ()
1084
1086
# pass cmd through stdin rather than as arguments. It seems sudo has its own argument parsing mechanics,
1085
1087
# which breaks shell expansion used in cmd
1086
1088
generate_key_script = f'''
@@ -1136,7 +1138,11 @@ def generate_ssh_key_pair_on_remote(
1136
1138
return result .stdout .decode ('utf-8' ).strip ()
1137
1139
1138
1140
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
+ ):
1140
1146
"""Copy ssh key from local to remote's authorized_keys. Require a configured non-interactive ssh authentication."""
1141
1147
# ssh-copy-id will prompt for the password of the destination user
1142
1148
# 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
1146
1152
{key}
1147
1153
EOF
1148
1154
''' .format (user = remote_user_to_swap , key = public_key )
1149
- result = sh . LocalShell () .su_subprocess_run (
1155
+ result = shell .su_subprocess_run (
1150
1156
local_sudoer ,
1151
1157
'ssh {} {}@{} sudo /bin/sh' .format (constants .SSH_OPTION , remote_sudoer , remote_node ),
1152
1158
input = cmd .encode ('utf-8' ),
@@ -1707,7 +1713,9 @@ def join_ssh_impl(local_user, seed_host, seed_user, ssh_public_keys: typing.List
1707
1713
if not result .public_keys :
1708
1714
pass
1709
1715
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
+ )
1711
1719
authorized_key_manager .add ( None , local_user , public_key )
1712
1720
logger .info ('A public key is added to authorized_keys for user %s: %s' , local_user , public_key .fingerprint ())
1713
1721
elif isinstance (result .public_keys [0 ], ssh_key .InMemoryPublicKey ):
@@ -1770,16 +1778,26 @@ def swap_public_ssh_key(
1770
1778
local_user_to_swap ,
1771
1779
remote_user_to_swap ,
1772
1780
local_sudoer ,
1773
- remote_sudoer
1781
+ remote_sudoer ,
1782
+ local_shell : sh .LocalShell = None , # FIXME: should not have default value
1774
1783
):
1775
1784
"""
1776
1785
Swap public ssh key between remote_node and local
1777
1786
"""
1787
+ if local_shell is None :
1788
+ local_shell = sh .LocalShell ()
1778
1789
# 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
+ )
1781
1796
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
+ )
1783
1801
ssh_key .AuthorizedKeyManager (sh .SSHShell (sh .LocalShell (), local_user_to_swap )).add (
1784
1802
None , local_user_to_swap , ssh_key .InMemoryPublicKey (public_key ),
1785
1803
)
@@ -1905,7 +1923,7 @@ def setup_passwordless_with_other_nodes(init_node, remote_user):
1905
1923
_merge_ssh_authorized_keys (cluster_node_list )
1906
1924
if local_user != 'hacluster' :
1907
1925
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 )
1909
1927
if local_user != 'hacluster' :
1910
1928
swap_key_for_hacluster (cluster_node_list )
1911
1929
0 commit comments