Skip to content

Sudo ipv4 ipv6 mask tests#31

Closed
shridhargadekar wants to merge 2 commits into
RedHat-SP-Security:masterfrom
shridhargadekar:sudo-ipv4-ipv6-mask-tests
Closed

Sudo ipv4 ipv6 mask tests#31
shridhargadekar wants to merge 2 commits into
RedHat-SP-Security:masterfrom
shridhargadekar:sudo-ipv4-ipv6-mask-tests

Conversation

@shridhargadekar
Copy link
Copy Markdown
Collaborator

@shridhargadekar shridhargadekar commented May 15, 2026

Summary by Sourcery

Extend sudo integration tests to cover IP-based sudoHost matching with IPv4/IPv6 addresses and CIDR masks, and broaden existing duplicate sudo user coverage across multiple users.

New Features:

  • Add tests verifying sudoHost rules allow sudo for matching IPv4 and IPv6 addresses, including CIDR-masked hosts.
  • Add tests verifying sudoHost rules deny sudo for non-matching IPv4 and IPv6 addresses, including CIDR-masked hosts.

Enhancements:

  • Broaden duplicate sudoUser test to cover multiple users and ensure sudo rule lists are not mangled across different identities.

Tests:

  • Introduce parametrized BareLDAP sudo tests for IPv4/IPv6 sudoHost allow/deny scenarios with and without CIDR masks.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 15, 2026

Reviewer's Guide

Extends sudo tests to cover IP-based sudoHost matching for IPv4/IPv6 (with and without CIDR masks) and strengthens the duplicate sudoUser test by using multiple users and iterating over them to verify rule resolution is not mangled.

File-Level Changes

Change Details Files
Broaden duplicate sudoUser test coverage to multiple users to ensure sudo rule resolution remains correct when multiple short and fully-qualified names are present.
  • Updated test docstring setup/steps to mention users user-1 through user-4 and running sudo/listing for multiple users.
  • Created additional users (user-2, user-3) in the test fixture setup.
  • Expanded the sudorule definition to include user-1, user-2, user-2@domain, and user-3 with host ALL and /bin/ls command.
  • Replaced single-user sudo list/run assertions with a loop asserting list and run succeed for each of user-1, user-2, and user-3.
pytest/tests/test_sudo.py
Add new tests validating sudoHost matching and denial for IPv4 and IPv6 addresses, including CIDR masks, using loopback IP configuration.
  • Introduced parametrized test test_sudo__host_ipv4_ipv6_with_mask_allowed to verify sudo access is granted when client IP matches sudoHost for ipv4_with_mask, ipv6, and ipv6_with_mask cases.
  • Configured client loopback interface inside the test by adding the appropriate IPv4 (/32) or IPv6 (/128) address based on parameterized ip_type and client_ip.
  • Created corresponding parametrized test test_sudo__host_ipv4_ipv6_with_mask_denied to assert sudo list/run fail when client IP does not match the configured sudoHost.
  • Ensured tests clean up by deleting the configured IP address from the loopback interface in a finally block for both allowed and denied scenarios.
pytest/tests/test_sudo.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • In test_sudo__duplicate_sudo_user the setup docstring mentions creating user-4, but the test never creates or uses it; either add the missing user usage or update the description to match the actual behavior.
  • In the new IP-based sudo tests you call client.host.ssh.run(..., raise_on_error=False) but never check the result; consider asserting or at least validating the command succeeded so the tests don't silently pass when IP configuration fails.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `test_sudo__duplicate_sudo_user` the setup docstring mentions creating `user-4`, but the test never creates or uses it; either add the missing user usage or update the description to match the actual behavior.
- In the new IP-based sudo tests you call `client.host.ssh.run(..., raise_on_error=False)` but never check the result; consider asserting or at least validating the command succeeded so the tests don't silently pass when IP configuration fails.

## Individual Comments

### Comment 1
<location path="pytest/tests/test_sudo.py" line_range="662-618" />
<code_context>
+        client.sssd.common.sudo()
+        client.sssd.start()
+
+        assert client.auth.sudo.list("user-1", "Secret123"), f"Sudo list failed for sudoHost={host_value}!"
+        assert client.auth.sudo.run("user-1", "Secret123", command="/bin/ls /root"), "Sudo command failed!"
+
+    finally:
</code_context>
<issue_to_address>
**suggestion (testing):** Strengthen assertions by checking expected sudo rules, not just success/failure

Here you only assert that `sudo.list(...)` is truthy, while other tests in this file use the `expected=` parameter to validate the returned rules. Where possible, assert the specific expected rule(s) (e.g. that `ALL` or `/bin/ls` appears) so the test verifies not just that access is granted but that the resolved rule content is correct for IPv4/IPv6 and CIDR sudoHost values.

Suggested implementation:

```python
        assert client.auth.sudo.list(
            "user-1",
            "Secret123",
            expected=[host_value, "ALL"],
        ), f"Sudo list failed for sudoHost={host_value}!"

```

The `expected` parameter usage here assumes that:
1. `client.auth.sudo.list(...)` accepts an `expected` keyword argument.
2. The list output contains both the `sudoHost` value (`host_value`) and the command (`ALL`) as strings that can be matched directly.

You should align the `expected=[host_value, "ALL"]` structure with the patterns used elsewhere in `pytest/tests/test_sudo.py`. For example, if other tests pass `expected` as a single string, a dict, or a more specific pattern (e.g. `expected=[f"sudoHost: {host_value}", "sudoCommand: ALL"]`), adjust this call to match that exact convention so the assertion validates the resolved rule content in the same way.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread pytest/tests/test_sudo.py
@@ -611,3 +616,105 @@ def test_sudo__defaults_set_no_auth_and_sudo_rule_has_mandatory_auth(client: Cli
assert client.auth.sudo.list("user-1", expected=["(root) PASSWD: ALL"]), "Sudo list failed!"
assert not client.auth.sudo.run("user-1", command="/bin/ls /root"), "Sudo command successful!"
assert client.auth.sudo.run("user-1", "Secret123", command="/bin/ls /root"), "Sudo command failed!"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (testing): Strengthen assertions by checking expected sudo rules, not just success/failure

Here you only assert that sudo.list(...) is truthy, while other tests in this file use the expected= parameter to validate the returned rules. Where possible, assert the specific expected rule(s) (e.g. that ALL or /bin/ls appears) so the test verifies not just that access is granted but that the resolved rule content is correct for IPv4/IPv6 and CIDR sudoHost values.

Suggested implementation:

        assert client.auth.sudo.list(
            "user-1",
            "Secret123",
            expected=[host_value, "ALL"],
        ), f"Sudo list failed for sudoHost={host_value}!"

The expected parameter usage here assumes that:

  1. client.auth.sudo.list(...) accepts an expected keyword argument.
  2. The list output contains both the sudoHost value (host_value) and the command (ALL) as strings that can be matched directly.

You should align the expected=[host_value, "ALL"] structure with the patterns used elsewhere in pytest/tests/test_sudo.py. For example, if other tests pass expected as a single string, a dict, or a more specific pattern (e.g. expected=[f"sudoHost: {host_value}", "sudoCommand: ALL"]), adjust this call to match that exact convention so the assertion validates the resolved rule content in the same way.

jakub-vavra-cz and others added 2 commits May 15, 2026 18:10
- test_sudo__host_ipv4_ipv6_with_mask_allowed:
   allowed access when client IP matches sudoHost
   (IPv4 with mask,
   IPv6, IPv6 with mask)
- test_sudo__host_ipv4_ipv6_with_mask_denied:
   Tests denied access when client IP doesn't match sudoHost

Each test is parameterized to cover 3 scenarios (6 total test runs).
Tests verify that sudoHost works correctly with IPv4/IPv6 addresses and CIDR notation.
@shridhargadekar shridhargadekar force-pushed the sudo-ipv4-ipv6-mask-tests branch from 685931e to 349862d Compare May 15, 2026 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants