Skip to content

Commit 9770256

Browse files
committed
Display a warning when using invalid command arguments.
For example if you pass `sudo_user=fizzadar` without `sudo=True`, this will now show a warning instead of doing nothing. This is, IMO, safter than implicitly applying `sudo=True`. One possible improvement is to error in such situations, but that might open up another can of worms, plus it's a breaking (v2) change.
1 parent 85760f9 commit 9770256

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

pyinfra/api/connectors/util.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,16 @@ def _show_use_su_login_warning():
203203
))
204204

205205

206+
# TODO (v2): possibly raise an error for invalid arguments
207+
def _warn_invalid_auth_args(args, requires_key, invalid_keys):
208+
for key in invalid_keys:
209+
value = args.get(key)
210+
if value:
211+
logger.warning((
212+
'Invalid auth argument: cannot use `{0}={1}` without `{2}`'
213+
).format(key, value, requires_key))
214+
215+
206216
def make_unix_command(
207217
command,
208218
env=None,
@@ -240,12 +250,11 @@ def make_unix_command(
240250
if chdir:
241251
command = StringCommand('cd', chdir, '&&', command)
242252

243-
# Quote the command as a string
253+
# Quote the command as a string before we prepend auth args
244254
command = QuoteString(command)
245255

246256
command_bits = []
247257

248-
# Use sudo (w/user?)
249258
if use_sudo_password:
250259
askpass_filename, sudo_password = use_sudo_password
251260
command_bits.extend([
@@ -270,8 +279,13 @@ def make_unix_command(
270279

271280
if sudo_user:
272281
command_bits.extend(('-u', sudo_user))
282+
else:
283+
_warn_invalid_auth_args(
284+
locals(),
285+
'sudo',
286+
('use_sudo_password', 'use_sudo_login', 'preserve_sudo_env', 'sudo_user'),
287+
)
273288

274-
# Switch user with su
275289
if su_user:
276290
command_bits.append('su')
277291

@@ -290,6 +304,12 @@ def make_unix_command(
290304
# Quote the whole shell -c 'command' as BSD `su` does not have a shell option
291305
command_bits.append(QuoteString(StringCommand(shell_executable, '-c', command)))
292306
else:
307+
_warn_invalid_auth_args(
308+
locals(),
309+
'su_user',
310+
('use_su_login', 'preserve_su_env', 'su_shell'),
311+
)
312+
293313
# Otherwise simply use thee shell directly
294314
command_bits.extend([shell_executable, '-c', command])
295315

0 commit comments

Comments
 (0)