Skip to content

Commit

Permalink
fixes #62377 groups with duplicate GIDs are not returned by get_group…
Browse files Browse the repository at this point in the history
…_list
  • Loading branch information
nicholasmhughes authored and Megan Wilhite committed Aug 8, 2022
1 parent fc7d0a9 commit eaa004d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
5 changes: 3 additions & 2 deletions salt/utils/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,9 @@ def get_group_list(user, include_default=True):
log.trace("Trying os.getgrouplist for '%s'", user)
try:
group_names = [
grp.getgrgid(grpid).gr_name
for grpid in os.getgrouplist(user, pwd.getpwnam(user).pw_gid)
_group.gr_name
for _group in grp.getgrall()
if _group.gr_gid in os.getgrouplist(user, pwd.getpwnam(user).pw_gid)
]
except Exception: # pylint: disable=broad-except
pass
Expand Down
31 changes: 31 additions & 0 deletions tests/pytests/functional/utils/user/test_get_group_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import logging

import pytest
import salt.utils.user

log = logging.getLogger(__name__)

pytestmark = [
pytest.mark.destructive_test,
pytest.mark.skip_if_not_root,
pytest.mark.skip_on_windows,
]


@pytest.fixture(scope="module")
def user():
with pytest.helpers.create_account(create_group=True) as _account:
yield _account


@pytest.fixture(scope="module")
def dupegroup(user):
grpid = user.group.info.gid
with pytest.helpers.create_group(name="dupegroup", gid=grpid) as _group:
yield _group


def test_get_group_list_with_duplicate_gid_group(user, dupegroup):
group_list = salt.utils.user.get_group_list(user)
assert user.group.info.name in group_list
assert dupegroup.name in group_list
11 changes: 8 additions & 3 deletions tests/support/pytest/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def remove_stale_proxy_minion_cache_file(proxy_minion, minion_id=None):
class TestGroup:
sminion = attr.ib(repr=False)
name = attr.ib()
gid = attr.ib()
_delete_group = attr.ib(init=False, repr=False, default=False)

@sminion.default
Expand All @@ -202,14 +203,18 @@ def _default_sminion(self):
def _default_name(self):
return random_string("group-", uppercase=False)

@gid.default
def _default_gid(self):
return None

@property
def info(self):
return types.SimpleNamespace(**self.sminion.functions.group.info(self.name))

def __enter__(self):
group = self.sminion.functions.group.info(self.name)
if not group:
ret = self.sminion.functions.group.add(self.name)
ret = self.sminion.functions.group.add(self.name, gid=self.gid)
assert ret
self._delete_group = True
log.debug("Created system group: %s", self)
Expand All @@ -231,8 +236,8 @@ def __exit__(self, *_):

@pytest.helpers.register
@contextmanager
def create_group(name=attr.NOTHING, sminion=attr.NOTHING):
with TestGroup(sminion=sminion, name=name) as group:
def create_group(name=attr.NOTHING, sminion=attr.NOTHING, gid=attr.NOTHING):
with TestGroup(sminion=sminion, name=name, gid=gid) as group:
yield group


Expand Down

0 comments on commit eaa004d

Please sign in to comment.