Repository navigation
Feature Alternatives to sudo for privilege escalation - #67897
TheSemicolons wants to merge 1 commit into
Conversation
twangboy
left a comment
There was a problem hiding this comment.
We probably want to add this as a new config option by adding it to salt/config/__init__.py and document it in conf/minion and doc/ref/configuration/minion.rst
twangboy
left a comment
There was a problem hiding this comment.
This also needs a changelog and a test
|
Added as a new config option by adding it to |
|
|
||
| def __virtual__(): | ||
| if salt.utils.path.which("sudo") and __opts__.get("sudo_user"): | ||
| if (salt.utils.path.which(__opts__.get("sudo_cmd")) or salt.utils.path.which("sudo")) and __opts__.get("sudo_user"): |
There was a problem hiding this comment.
The cmd usage later uses sudo_cmd even if the which sudo_cmd here is False. These should be consistent so this should probably be
if salt.utils.path.which(__opts__.get("sudo_cmd") or "sudo") and __opts__.get("sudo_user"):
instead.
| """ | ||
| cmd = [ | ||
| "sudo", | ||
| opts.get("sudo_cmd") if opts.get("sudo_cmd") else "sudo", |
There was a problem hiding this comment.
I suggest opts.get("sudo_cmd") or "sudo", to avoid an extra opts lookup.
…escalation Adds `sudo_cmd` minion config option to allow alternatives to `sudo` (e.g. `doas`) for privilege escalation when `sudo_user` is configured. - Add `sudo_cmd` to VALID_OPTS and DEFAULT_MINION_OPTS in salt/config/__init__.py - Update `salt/executors/sudo.py` __virtual__ and execute() to use `sudo_cmd` - Document `sudo_cmd` in conf/minion and doc/ref/configuration/minion.rst - Add comprehensive unit tests covering default sudo, sudo_cmd alternative, --priv arg, and __virtual__ load/fail scenarios - Add changelog entry for issue saltstack#67255 Fixes saltstack#67255
| .. code-block:: yaml | ||
|
|
||
| sudo_cmd: doas | ||
|
|
| if salt.utils.path.which(__opts__.get("sudo_cmd") or "sudo") and __opts__.get( | ||
| "sudo_user" | ||
| ): |
There was a problem hiding this comment.
If a user explicitly sets sudo_cmd: /path/to/broken_or_missing_command in their minion config, salt.utils.path.which() will return None. Because the condition fails, virtual returns False, and the entire executor module fails to load.
def __virtual__():
# Fall back to "sudo" if sudo_cmd is empty/unset
cmd = __opts__.get("sudo_cmd") or "sudo"
# Ensure either the configured command OR the fallback "sudo" is available
if (salt.utils.path.which(cmd) or salt.utils.path.which("sudo")) and __opts__.get("sudo_user"):
return __virtualname__
return False| opts = {"sudo_user": "saltdev", "sudo_cmd": "doas"} | ||
| with _with_opts(opts), patch("salt.utils.path.which", return_value=None): | ||
| result = salt.executors.sudo.__virtual__() | ||
| assert result is False |
There was a problem hiding this comment.
Once you make the change to salt.executors.sudo, you'll want to add a test case in tests/pytests/unit/executors/test_sudo.py to ensure that virtual still returns "sudo" if sudo_cmd is broken but sudo is present on the minion:
def test_sudo_virtual_loads_with_fallback_when_sudo_cmd_invalid(tmp_path):
"""__virtual__ returns virtualname if explicit sudo_cmd is missing but sudo exists."""
fake_sudo = tmp_path / "sudo"
fake_sudo.touch()
fake_sudo.chmod(0o755)
opts = { "sudo_user": "saltdev", "sudo_cmd": "broken_command" }
with _with_opts(opts), patch(
"salt.utils.path.which",
side_effect=lambda x: str(fake_sudo) if x == "sudo" else None,
):
result = salt.executors.sudo.__virtual__()
assert result == "sudo"
What does this PR do?
This commit adds functionality for alternative privilege escalation
commands to be used via the
sudo_cmdvariable in the minion configfile. If the variable is absent the default of
sudois used.What issues does this PR fix or reference?
Fixes #67255
Previous Behavior
Only
sudowas available for privilege escalation.New Behavior
sudois the default privilege escalation.sudo_cmdhas been addedand allows alternatives to be specified in the minion config file via this
variable.
Merge requirements satisfied?
[NOTICE] Bug fixes or features added to Salt require tests.
Commits signed with GPG?
Yes