|
| 1 | +From 0e5312535388d603a80c2c54dc9900f01e9a37d8 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Toyam Cox < [email protected]> |
| 3 | +Date: Wed, 5 Mar 2025 22:42:29 -0500 |
| 4 | +Subject: [PATCH 1/2] Remove uses of spwd |
| 5 | + |
| 6 | +Module deprecated and no longer shipped with Python 3.13. |
| 7 | + |
| 8 | +Co-authored-by: Toyam Cox < [email protected]> |
| 9 | +Co-authored-by: Georg Pfuetzenreuter < [email protected]> |
| 10 | +Signed-off-by: Georg Pfuetzenreuter < [email protected]> |
| 11 | +--- |
| 12 | + changelog/67119.fixed.md | 1 + |
| 13 | + salt/modules/linux_shadow.py | 30 ++++++++++++++++++++---------- |
| 14 | + 2 files changed, 21 insertions(+), 10 deletions(-) |
| 15 | + create mode 100644 changelog/67119.fixed.md |
| 16 | + |
| 17 | +diff --git a/changelog/67119.fixed.md b/changelog/67119.fixed.md |
| 18 | +new file mode 100644 |
| 19 | +index 000000000000..34eca2d3b2a7 |
| 20 | +--- /dev/null |
| 21 | ++++ b/changelog/67119.fixed.md |
| 22 | +@@ -0,0 +1 @@ |
| 23 | ++Remove usage of spwd |
| 24 | +diff --git a/salt/modules/linux_shadow.py b/salt/modules/linux_shadow.py |
| 25 | +index 09fe73fdb548..09cba9f3b296 100644 |
| 26 | +--- a/salt/modules/linux_shadow.py |
| 27 | ++++ b/salt/modules/linux_shadow.py |
| 28 | +@@ -8,6 +8,7 @@ |
| 29 | + <module-provider-override>`. |
| 30 | + """ |
| 31 | + |
| 32 | ++import collections |
| 33 | + import datetime |
| 34 | + import functools |
| 35 | + import logging |
| 36 | +@@ -17,12 +18,6 @@ |
| 37 | + import salt.utils.files |
| 38 | + from salt.exceptions import CommandExecutionError |
| 39 | + |
| 40 | +-try: |
| 41 | +- import spwd |
| 42 | +-except ImportError: |
| 43 | +- pass |
| 44 | +- |
| 45 | +- |
| 46 | + try: |
| 47 | + import salt.utils.pycrypto |
| 48 | + |
| 49 | +@@ -34,6 +29,21 @@ |
| 50 | + |
| 51 | + log = logging.getLogger(__name__) |
| 52 | + |
| 53 | ++struct_spwd = collections.namedtuple( |
| 54 | ++ "struct_spwd", |
| 55 | ++ [ |
| 56 | ++ "sp_namp", |
| 57 | ++ "sp_pwdp", |
| 58 | ++ "sp_lstchg", |
| 59 | ++ "sp_min", |
| 60 | ++ "sp_max", |
| 61 | ++ "sp_warn", |
| 62 | ++ "sp_inact", |
| 63 | ++ "sp_expire", |
| 64 | ++ "sp_flag", |
| 65 | ++ ], |
| 66 | ++) |
| 67 | ++ |
| 68 | + |
| 69 | + def __virtual__(): |
| 70 | + return __virtualname__ if __grains__.get("kernel", "") == "Linux" else False |
| 71 | +@@ -71,7 +81,7 @@ def info(name, root=None): |
| 72 | + if root is not None: |
| 73 | + getspnam = functools.partial(_getspnam, root=root) |
| 74 | + else: |
| 75 | +- getspnam = functools.partial(spwd.getspnam) |
| 76 | ++ getspnam = functools.partial(_getspnam, root="/") |
| 77 | + |
| 78 | + try: |
| 79 | + data = getspnam(name) |
| 80 | +@@ -509,7 +519,7 @@ def list_users(root=None): |
| 81 | + if root is not None: |
| 82 | + getspall = functools.partial(_getspall, root=root) |
| 83 | + else: |
| 84 | +- getspall = functools.partial(spwd.getspall) |
| 85 | ++ getspall = functools.partial(_getspall, root="/") |
| 86 | + |
| 87 | + return sorted( |
| 88 | + user.sp_namp if hasattr(user, "sp_namp") else user.sp_nam for user in getspall() |
| 89 | +@@ -529,7 +539,7 @@ def _getspnam(name, root=None): |
| 90 | + # Generate a getspnam compatible output |
| 91 | + for i in range(2, 9): |
| 92 | + comps[i] = int(comps[i]) if comps[i] else -1 |
| 93 | +- return spwd.struct_spwd(comps) |
| 94 | ++ return struct_spwd(*comps) |
| 95 | + raise KeyError |
| 96 | + |
| 97 | + |
| 98 | +@@ -545,4 +555,4 @@ def _getspall(root=None): |
| 99 | + # Generate a getspall compatible output |
| 100 | + for i in range(2, 9): |
| 101 | + comps[i] = int(comps[i]) if comps[i] else -1 |
| 102 | +- yield spwd.struct_spwd(comps) |
| 103 | ++ yield struct_spwd(*comps) |
| 104 | + |
| 105 | +From 783ac120a16ee7165aaae30bd0c7765a1d8f6845 Mon Sep 17 00:00:00 2001 |
| 106 | +From: Georg Pfuetzenreuter < [email protected]> |
| 107 | +Date: Thu, 16 Oct 2025 20:38:06 +0200 |
| 108 | +Subject: [PATCH 2/2] Replace spwd in linux_shadow tests |
| 109 | + |
| 110 | +Signed-off-by: Georg Pfuetzenreuter < [email protected]> |
| 111 | +--- |
| 112 | + .../pytests/unit/modules/test_linux_shadow.py | 23 ++++++++----------- |
| 113 | + 1 file changed, 9 insertions(+), 14 deletions(-) |
| 114 | + |
| 115 | +diff --git a/tests/pytests/unit/modules/test_linux_shadow.py b/tests/pytests/unit/modules/test_linux_shadow.py |
| 116 | +index 0c742672750b..723e3a8ad26f 100644 |
| 117 | +--- a/tests/pytests/unit/modules/test_linux_shadow.py |
| 118 | ++++ b/tests/pytests/unit/modules/test_linux_shadow.py |
| 119 | +@@ -1,5 +1,5 @@ |
| 120 | + """ |
| 121 | +- :codeauthor: Erik Johnson <[email protected]> |
| 122 | ++:codeauthor: Erik Johnson <[email protected]> |
| 123 | + """ |
| 124 | + |
| 125 | + import types |
| 126 | +@@ -175,6 +175,11 @@ def test_info(password): |
| 127 | + Test if info shows the correct user information |
| 128 | + """ |
| 129 | + |
| 130 | ++ data = { |
| 131 | ++ "/etc/shadow": f"foo:{password.pw_hash}:31337:0:99999:7:::", |
| 132 | ++ "*": Exception("Attempted to open something other than /etc/shadow"), |
| 133 | ++ } |
| 134 | ++ |
| 135 | + # First test is with a succesful call |
| 136 | + expected_result = [ |
| 137 | + ("expire", -1), |
| 138 | +@@ -186,10 +191,7 @@ def test_info(password): |
| 139 | + ("passwd", password.pw_hash), |
| 140 | + ("warn", 7), |
| 141 | + ] |
| 142 | +- getspnam_return = spwd.struct_spwd( |
| 143 | +- ["foo", password.pw_hash, 31337, 0, 99999, 7, -1, -1, -1] |
| 144 | +- ) |
| 145 | +- with patch("spwd.getspnam", return_value=getspnam_return): |
| 146 | ++ with patch("salt.utils.files.fopen", mock_open(read_data=data)): |
| 147 | + result = shadow.info("foo") |
| 148 | + assert expected_result == sorted(result.items(), key=lambda x: x[0]) |
| 149 | + |
| 150 | +@@ -204,15 +206,8 @@ def test_info(password): |
| 151 | + ("passwd", ""), |
| 152 | + ("warn", ""), |
| 153 | + ] |
| 154 | +- # We get KeyError exception for non-existent users in glibc based systems |
| 155 | +- getspnam_return = KeyError |
| 156 | +- with patch("spwd.getspnam", side_effect=getspnam_return): |
| 157 | +- result = shadow.info("foo") |
| 158 | +- assert expected_result == sorted(result.items(), key=lambda x: x[0]) |
| 159 | +- # And FileNotFoundError in musl based systems |
| 160 | +- getspnam_return = FileNotFoundError |
| 161 | +- with patch("spwd.getspnam", side_effect=getspnam_return): |
| 162 | +- result = shadow.info("foo") |
| 163 | ++ with patch("salt.utils.files.fopen", mock_open(read_data=data)): |
| 164 | ++ result = shadow.info("bar") |
| 165 | + assert expected_result == sorted(result.items(), key=lambda x: x[0]) |
| 166 | + |
| 167 | + |
0 commit comments