Skip to content

Commit b2d98e2

Browse files
committed
Move sudo password retrieve into make host unix command util.
1 parent 9238b5e commit b2d98e2

File tree

5 files changed

+15
-37
lines changed

5 files changed

+15
-37
lines changed

pyinfra/api/connectors/chroot.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pyinfra.progress import progress_spinner
1313

1414
from .local import run_shell_command as run_local_shell_command
15-
from .util import get_sudo_password, make_unix_command_for_host
15+
from .util import make_unix_command_for_host
1616

1717

1818
@memoize
@@ -57,16 +57,8 @@ def run_shell_command(
5757
print_output=False,
5858
print_input=False,
5959
return_combined_output=False,
60-
use_sudo_password=False,
6160
**command_kwargs
6261
):
63-
64-
if use_sudo_password:
65-
command_kwargs['use_sudo_password'] = get_sudo_password(
66-
host,
67-
use_sudo_password,
68-
)
69-
7062
chroot_directory = host.host_data['chroot_directory']
7163

7264
command = make_unix_command_for_host(state, host, command, **command_kwargs)

pyinfra/api/connectors/local.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from .util import (
1616
execute_command_with_sudo_retry,
17-
get_sudo_password,
1817
make_unix_command_for_host,
1918
run_local_process,
2019
split_combined_output,
@@ -43,7 +42,6 @@ def run_shell_command(
4342
print_output=False,
4443
print_input=False,
4544
return_combined_output=False,
46-
use_sudo_password=False,
4745
**command_kwargs
4846
):
4947
'''
@@ -63,11 +61,6 @@ def run_shell_command(
6361
stdout and stderr are both lists of strings from each buffer.
6462
'''
6563

66-
if use_sudo_password:
67-
command_kwargs['use_sudo_password'] = get_sudo_password(
68-
host, use_sudo_password,
69-
)
70-
7164
def execute_command():
7265
unix_command = make_unix_command_for_host(state, host, command, **command_kwargs)
7366
actual_command = unix_command.get_raw_value()

pyinfra/api/connectors/ssh.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
from .sshuserclient import SSHClient
3434
from .util import (
3535
execute_command_with_sudo_retry,
36-
get_sudo_password,
3736
make_unix_command_for_host,
3837
read_buffers_into_queue,
3938
run_local_process,
@@ -261,7 +260,6 @@ def run_shell_command(
261260
print_output=False,
262261
print_input=False,
263262
return_combined_output=False,
264-
use_sudo_password=False,
265263
**command_kwargs
266264
):
267265
'''
@@ -282,11 +280,6 @@ def run_shell_command(
282280
stdout and stderr are both lists of strings from each buffer.
283281
'''
284282

285-
if use_sudo_password:
286-
command_kwargs['use_sudo_password'] = get_sudo_password(
287-
host, use_sudo_password,
288-
)
289-
290283
def execute_command():
291284
unix_command = make_unix_command_for_host(state, host, command, **command_kwargs)
292285
actual_command = unix_command.get_raw_value()

pyinfra/api/connectors/util.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ def execute_command_with_sudo_retry(host, command_kwargs, execute_command):
5757
if return_code != 0 and combined_output:
5858
last_line = combined_output[-1][1]
5959
if last_line == 'sudo: a password is required':
60-
command_kwargs['use_sudo_password'] = get_sudo_password(
61-
host,
62-
use_sudo_password=True, # ask for the password
63-
)
60+
command_kwargs['use_sudo_password'] = True # ask for the password
6461
return_code, combined_output = execute_command()
6562

6663
return return_code, combined_output
@@ -166,7 +163,7 @@ def write_stdin(stdin, buffer):
166163
buffer.close()
167164

168165

169-
def get_sudo_password(host, use_sudo_password):
166+
def _get_sudo_password(host, use_sudo_password):
170167
sudo_askpass_uploaded = host.connector_data.get('sudo_askpass_uploaded', False)
171168
if not sudo_askpass_uploaded:
172169
host.put_file(get_sudo_askpass_exe(), SUDO_ASKPASS_EXE_FILENAME)
@@ -182,7 +179,7 @@ def get_sudo_password(host, use_sudo_password):
182179
else:
183180
sudo_password = use_sudo_password
184181

185-
return (SUDO_ASKPASS_EXE_FILENAME, shlex_quote(sudo_password))
182+
return shlex_quote(sudo_password)
186183

187184

188185
def remove_any_sudo_askpass_file(host):
@@ -230,6 +227,10 @@ def make_unix_command_for_host(state, host, *command_args, **command_kwargs):
230227
('use_su_login', 'preserve_su_env', 'su_shell'),
231228
)
232229

230+
use_sudo_password = command_kwargs.pop('use_sudo_password', None)
231+
if use_sudo_password:
232+
command_kwargs['sudo_password'] = _get_sudo_password(host, use_sudo_password)
233+
233234
return make_unix_command(*command_args, **command_kwargs)
234235

235236

@@ -247,7 +248,7 @@ def make_unix_command(
247248
sudo=False,
248249
sudo_user=None,
249250
use_sudo_login=False,
250-
use_sudo_password=False,
251+
sudo_password=False,
251252
preserve_sudo_env=False,
252253
# Doas config
253254
doas=False,
@@ -284,18 +285,17 @@ def make_unix_command(
284285
if doas_user:
285286
command_bits.extend(['-u', doas_user])
286287

287-
if use_sudo_password:
288-
askpass_filename, sudo_password = use_sudo_password
288+
if sudo_password:
289289
command_bits.extend([
290290
'env',
291-
'SUDO_ASKPASS={0}'.format(askpass_filename),
291+
'SUDO_ASKPASS={0}'.format(SUDO_ASKPASS_EXE_FILENAME),
292292
MaskString('{0}={1}'.format(SUDO_ASKPASS_ENV_VAR, sudo_password)),
293293
])
294294

295295
if sudo:
296296
command_bits.extend(['sudo', '-H'])
297297

298-
if use_sudo_password:
298+
if sudo_password:
299299
command_bits.extend(['-A', '-k']) # use askpass, disable cache
300300
else:
301301
command_bits.append('-n') # disable prompt/interactivity

tests/test_connectors/test_ssh.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,13 +569,13 @@ def test_run_shell_command_sudo_password_automatic_prompt(
569569
#
570570

571571
@patch('pyinfra.api.connectors.ssh.SSHClient')
572-
@patch('pyinfra.api.connectors.util.get_sudo_password')
572+
@patch('pyinfra.api.connectors.util._get_sudo_password')
573573
def test_run_shell_command_retry_for_sudo_password(
574574
self,
575575
fake_get_sudo_password,
576576
fake_ssh_client,
577577
):
578-
fake_get_sudo_password.return_value = ['FILENAME', 'PASSWORD']
578+
fake_get_sudo_password.return_value = 'PASSWORD'
579579

580580
fake_ssh = MagicMock()
581581
fake_stdin = MagicMock()
@@ -599,7 +599,7 @@ def test_run_shell_command_retry_for_sudo_password(
599599
assert out[0] is True
600600
assert fake_get_sudo_password.called
601601
fake_ssh.exec_command.assert_called_with(
602-
"env SUDO_ASKPASS=FILENAME PYINFRA_SUDO_PASSWORD=PASSWORD sh -c 'echo hi'",
602+
"env SUDO_ASKPASS=pyinfra-sudo-askpass PYINFRA_SUDO_PASSWORD=PASSWORD sh -c 'echo hi'",
603603
get_pty=False,
604604
)
605605

0 commit comments

Comments
 (0)