Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/68790.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make `salt-ssh` work without issues using `domain\user` notation for remote user with SSH.
5 changes: 4 additions & 1 deletion salt/client/ssh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,10 @@ def __init__(
self.python_env = kwargs.get("ssh_python_env")
else:
if user:
thin_dir = DEFAULT_THIN_DIR.replace("%%USER%%", user)
thin_dir = DEFAULT_THIN_DIR.replace(
"%%USER%%",
re.sub(r"[^a-zA-Z0-9\._\-@]", "_", user),
)
else:
thin_dir = DEFAULT_THIN_DIR.replace("%%USER%%", "root")
self.thin_dir = thin_dir.replace(
Expand Down
4 changes: 2 additions & 2 deletions salt/client/ssh/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def _key_opts(self):
if self.priv and self.priv != "agent-forwarding":
options.append(f"IdentityFile={self.priv}")
if self.user:
options.append(f"User={self.user}")
options.append(f"User={shlex.quote(self.user)}")
if self.identities_only:
options.append("IdentitiesOnly=yes")

Expand Down Expand Up @@ -202,7 +202,7 @@ def _passwd_opts(self):
if self.port:
options.append(f"Port={self.port}")
if self.user:
options.append(f"User={self.user}")
options.append(f"User={shlex.quote(self.user)}")
if self.identities_only:
options.append("IdentitiesOnly=yes")

Expand Down
15 changes: 15 additions & 0 deletions tests/pytests/unit/client/ssh/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,18 @@ def test_scp_command_execution_uses_custom_path():
args, _ = mock_run_cmd.call_args
assert "/custom/scp" in args[0]
assert "source_file.txt example.com:/path/dest_file.txt" in args[0]


def test_ssh_using_user_with_backslash():
_shell = shell.Shell(
opts={"_ssh_version": (4, 9)},
host="host.example.org",
user="exampledomain\\user",
passwd="password",
)
with patch.object(
_shell, "_run_cmd", return_value=(None, None, None)
) as mock_run_cmd:
cmd_string = _shell.exec_cmd("whoami")
args, _ = mock_run_cmd.call_args
assert " User='exampledomain\\user' " in args[0]
18 changes: 18 additions & 0 deletions tests/pytests/unit/client/ssh/test_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,3 +958,21 @@ def test_run_integration_with_no_pre_hook(opts, target):
with patch.object(single_instance, "cmd_block", mock_cmd_block):
stdout, stderr, retcode = single_instance.run()
assert retcode == 0


def test_check_thin_dir_with_backslash_user(opts):
"""
Test `thin_dir` path generation for the user with backslash in the name
"""
single = ssh.Single(
opts,
opts["argv"],
"host.example.org",
"host.example.org",
user="exampledomain\\user",
mods={},
fsclient=None,
mine=False,
)
assert single.thin_dir == single.opts["thin_dir"]
assert ".exampledomain_user_" in single.thin_dir
Loading