Skip to content

Feature Alternatives to sudo for privilege escalation - #67897

Open
TheSemicolons wants to merge 1 commit into
saltstack:masterfrom
TheSemicolons:sudo_alternative
Open

TheSemicolons wants to merge 1 commit into
saltstack:masterfrom
TheSemicolons:sudo_alternative

Conversation

@TheSemicolons

@TheSemicolons TheSemicolons commented Mar 20, 2025 •

Copy link
Copy Markdown

What does this PR do?

This commit adds functionality for alternative privilege escalation
commands to be used via the sudo_cmd variable in the minion config
file. If the variable is absent the default of sudo is used.

What issues does this PR fix or reference?

Fixes #67255

Previous Behavior

Only sudo was available for privilege escalation.

New Behavior

sudo is the default privilege escalation. sudo_cmd has been added
and 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

@TheSemicolons
TheSemicolons requested a review from a team as a code owner March 20, 2025 05:01

@twangboy twangboy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 twangboy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs a changelog and a test

@twangboy twangboy added the test:full Run the full test suite label Jul 1, 2025
@twangboy twangboy added this to the Argon v3008.0 milestone Jul 1, 2025
@twangboy twangboy added needs-testcase PR needs test cases written, or the issue is about a bug/feature that needs test cases needs-changelog labels Jul 1, 2025
@TheSemicolons

Copy link
Copy Markdown
Author

Added as a new config option by adding it to salt/config/__init__.py and documented in conf/minion and doc/ref/configuration/minion.rst. Unsure how to write the test.

@twangboy twangboy left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now all we need is a test

Comment thread salt/executors/sudo.py Outdated

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"):

@bdrx312 bdrx312 Dec 19, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread salt/executors/sudo.py Outdated
"""
cmd = [
"sudo",
opts.get("sudo_cmd") if opts.get("sudo_cmd") else "sudo",

@bdrx312 bdrx312 Dec 19, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest opts.get("sudo_cmd") or "sudo", to avoid an extra opts lookup.

@twangboy twangboy modified the milestones: Argon v3008.0, Argon v3008.1 Jun 5, 2026
@dwoz dwoz modified the milestones: Argon v3008.1, Potassium v3009.0 Jun 19, 2026
@dwoz dwoz removed the needs-testcase PR needs test cases written, or the issue is about a bug/feature that needs test cases label Jun 19, 2026
…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
@dwoz
dwoz force-pushed the sudo_alternative branch from 64b9877 to b141fa9 Compare June 26, 2026 12:07
.. code-block:: yaml

sudo_cmd: doas

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra white space

Comment thread salt/executors/sudo.py
Comment on lines +15 to +17
if salt.utils.path.which(__opts__.get("sudo_cmd") or "sudo") and __opts__.get(
"sudo_user"
):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:full Run the full test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants