dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0009-TESTS-Add-integration-tests-for-the-auto_private_gro.patch

ced1f5
From 95053cd058a9045c45c59e002fd6078048fdca76 Mon Sep 17 00:00:00 2001
ced1f5
From: Jakub Hrozek <jhrozek@redhat.com>
ced1f5
Date: Tue, 3 Oct 2017 16:55:40 +0200
ced1f5
Subject: [PATCH 09/21] TESTS: Add integration tests for the
ced1f5
 auto_private_groups option
ced1f5
MIME-Version: 1.0
ced1f5
Content-Type: text/plain; charset=UTF-8
ced1f5
Content-Transfer-Encoding: 8bit
ced1f5
ced1f5
Related:
ced1f5
    https://pagure.io/SSSD/sssd/issue/1872
ced1f5
ced1f5
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
ced1f5
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
ced1f5
(cherry picked from commit 6c802b2009c1b6dd0c3306ba97056e64acc0ec9e)
ced1f5
---
ced1f5
 src/tests/intg/test_enumeration.py |  79 +++++++++++++-
ced1f5
 src/tests/intg/test_ldap.py        | 214 +++++++++++++++++++++++++++++++++++++
ced1f5
 2 files changed, 290 insertions(+), 3 deletions(-)
ced1f5
ced1f5
diff --git a/src/tests/intg/test_enumeration.py b/src/tests/intg/test_enumeration.py
ced1f5
index fdb8d376879f756957f8f25fd28b37d7178aeff5..c7d78155c64dc6c85cb4dc070b205bdcfceff6af 100644
ced1f5
--- a/src/tests/intg/test_enumeration.py
ced1f5
+++ b/src/tests/intg/test_enumeration.py
ced1f5
@@ -237,9 +237,7 @@ def sanity_rfc2307(request, ldap_conn):
ced1f5
     create_sssd_fixture(request)
ced1f5
     return None
ced1f5
 
ced1f5
-
ced1f5
-@pytest.fixture
ced1f5
-def sanity_rfc2307_bis(request, ldap_conn):
ced1f5
+def populate_rfc2307bis(request, ldap_conn):
ced1f5
     ent_list = ldap_ent.List(ldap_conn.ds_inst.base_dn)
ced1f5
     ent_list.add_user("user1", 1001, 2001)
ced1f5
     ent_list.add_user("user2", 1002, 2002)
ced1f5
@@ -266,6 +264,11 @@ def sanity_rfc2307_bis(request, ldap_conn):
ced1f5
                            [], ["one_user_group1", "one_user_group2"])
ced1f5
 
ced1f5
     create_ldap_fixture(request, ldap_conn, ent_list)
ced1f5
+
ced1f5
+
ced1f5
+@pytest.fixture
ced1f5
+def sanity_rfc2307_bis(request, ldap_conn):
ced1f5
+    populate_rfc2307bis(request, ldap_conn)
ced1f5
     conf = format_basic_conf(ldap_conn, SCHEMA_RFC2307_BIS)
ced1f5
     create_conf_fixture(request, conf)
ced1f5
     create_sssd_fixture(request)
ced1f5
@@ -695,3 +698,73 @@ def test_vetoed_shells(vetoed_shells):
ced1f5
                  shell="/bin/default")
ced1f5
         )
ced1f5
     )
ced1f5
+
ced1f5
+
ced1f5
+@pytest.fixture
ced1f5
+def sanity_rfc2307_bis_mpg(request, ldap_conn):
ced1f5
+    populate_rfc2307bis(request, ldap_conn)
ced1f5
+
ced1f5
+    ent_list = ldap_ent.List(ldap_conn.ds_inst.base_dn)
ced1f5
+    ent_list.add_group_bis("conflict1", 1001)
ced1f5
+    ent_list.add_group_bis("conflict2", 1002)
ced1f5
+    create_ldap_fixture(request, ldap_conn, ent_list)
ced1f5
+
ced1f5
+    conf = \
ced1f5
+        format_basic_conf(ldap_conn, SCHEMA_RFC2307_BIS) + \
ced1f5
+        unindent("""
ced1f5
+            [domain/LDAP]
ced1f5
+            auto_private_groups = True
ced1f5
+        """).format(**locals())
ced1f5
+    create_conf_fixture(request, conf)
ced1f5
+    create_sssd_fixture(request)
ced1f5
+    return None
ced1f5
+
ced1f5
+
ced1f5
+def test_ldap_auto_private_groups_enumerate(ldap_conn,
ced1f5
+                                            sanity_rfc2307_bis_mpg):
ced1f5
+    """
ced1f5
+    Test the auto_private_groups together with enumeration
ced1f5
+    """
ced1f5
+    passwd_pattern = ent.contains_only(
ced1f5
+        dict(name='user1', passwd='*', uid=1001, gid=1001, gecos='1001',
ced1f5
+             dir='/home/user1', shell='/bin/bash'),
ced1f5
+        dict(name='user2', passwd='*', uid=1002, gid=1002, gecos='1002',
ced1f5
+             dir='/home/user2', shell='/bin/bash'),
ced1f5
+        dict(name='user3', passwd='*', uid=1003, gid=1003, gecos='1003',
ced1f5
+             dir='/home/user3', shell='/bin/bash')
ced1f5
+    )
ced1f5
+    ent.assert_passwd(passwd_pattern)
ced1f5
+
ced1f5
+    group_pattern = ent.contains_only(
ced1f5
+        dict(name='user1', passwd='*', gid=1001, mem=ent.contains_only()),
ced1f5
+        dict(name='user2', passwd='*', gid=1002, mem=ent.contains_only()),
ced1f5
+        dict(name='user3', passwd='*', gid=1003, mem=ent.contains_only()),
ced1f5
+        dict(name='group1', passwd='*', gid=2001, mem=ent.contains_only()),
ced1f5
+        dict(name='group2', passwd='*', gid=2002, mem=ent.contains_only()),
ced1f5
+        dict(name='group3', passwd='*', gid=2003, mem=ent.contains_only()),
ced1f5
+        dict(name='empty_group1', passwd='*', gid=2010,
ced1f5
+             mem=ent.contains_only()),
ced1f5
+        dict(name='empty_group2', passwd='*', gid=2011,
ced1f5
+             mem=ent.contains_only()),
ced1f5
+        dict(name='two_user_group', passwd='*', gid=2012,
ced1f5
+             mem=ent.contains_only("user1", "user2")),
ced1f5
+        dict(name='group_empty_group', passwd='*', gid=2013,
ced1f5
+             mem=ent.contains_only()),
ced1f5
+        dict(name='group_two_empty_groups', passwd='*', gid=2014,
ced1f5
+             mem=ent.contains_only()),
ced1f5
+        dict(name='one_user_group1', passwd='*', gid=2015,
ced1f5
+             mem=ent.contains_only("user1")),
ced1f5
+        dict(name='one_user_group2', passwd='*', gid=2016,
ced1f5
+             mem=ent.contains_only("user2")),
ced1f5
+        dict(name='group_one_user_group', passwd='*', gid=2017,
ced1f5
+             mem=ent.contains_only("user1")),
ced1f5
+        dict(name='group_two_user_group', passwd='*', gid=2018,
ced1f5
+             mem=ent.contains_only("user1", "user2")),
ced1f5
+        dict(name='group_two_one_user_groups', passwd='*', gid=2019,
ced1f5
+             mem=ent.contains_only("user1", "user2"))
ced1f5
+    )
ced1f5
+    ent.assert_group(group_pattern)
ced1f5
+
ced1f5
+    with pytest.raises(KeyError):
ced1f5
+        grp.getgrnam("conflict1")
ced1f5
+    ent.assert_group_by_gid(1002, dict(name="user2", mem=ent.contains_only()))
ced1f5
diff --git a/src/tests/intg/test_ldap.py b/src/tests/intg/test_ldap.py
ced1f5
index f2467f1ffe9890049ad73bba6432102d029510e8..a6659b1b78df4d72eb98c208d67ee5d10c9c88ea 100644
ced1f5
--- a/src/tests/intg/test_ldap.py
ced1f5
+++ b/src/tests/intg/test_ldap.py
ced1f5
@@ -1169,3 +1169,217 @@ def test_nss_filters_cached(ldap_conn, sanity_nss_filter_cached):
ced1f5
 
ced1f5
     res, _ = call_sssd_getgrgid(0)
ced1f5
     assert res == NssReturnCode.NOTFOUND
ced1f5
+
ced1f5
+
ced1f5
+@pytest.fixture
ced1f5
+def mpg_setup(request, ldap_conn):
ced1f5
+    ent_list = ldap_ent.List(ldap_conn.ds_inst.base_dn)
ced1f5
+    ent_list.add_user("user1", 1001, 2001)
ced1f5
+    ent_list.add_user("user2", 1002, 2002)
ced1f5
+    ent_list.add_user("user3", 1003, 2003)
ced1f5
+
ced1f5
+    ent_list.add_group_bis("group1", 2001)
ced1f5
+    ent_list.add_group_bis("group2", 2002)
ced1f5
+    ent_list.add_group_bis("group3", 2003)
ced1f5
+
ced1f5
+    ent_list.add_group_bis("two_user_group", 2012, ["user1", "user2"])
ced1f5
+    ent_list.add_group_bis("one_user_group1", 2015, ["user1"])
ced1f5
+    ent_list.add_group_bis("one_user_group2", 2016, ["user2"])
ced1f5
+
ced1f5
+    create_ldap_entries(ldap_conn, ent_list)
ced1f5
+    create_ldap_cleanup(request, ldap_conn, None)
ced1f5
+
ced1f5
+    conf = \
ced1f5
+        format_basic_conf(ldap_conn, SCHEMA_RFC2307_BIS) + \
ced1f5
+        unindent("""
ced1f5
+            [domain/LDAP]
ced1f5
+            auto_private_groups = True
ced1f5
+        """).format(**locals())
ced1f5
+    create_conf_fixture(request, conf)
ced1f5
+    create_sssd_fixture(request)
ced1f5
+    return None
ced1f5
+
ced1f5
+
ced1f5
+def test_ldap_auto_private_groups_direct(ldap_conn, mpg_setup):
ced1f5
+    """
ced1f5
+    Integration test for auto_private_groups
ced1f5
+
ced1f5
+    See also ticket https://pagure.io/SSSD/sssd/issue/1872
ced1f5
+    """
ced1f5
+    # Make sure the user's GID is taken from their uidNumber
ced1f5
+    ent.assert_passwd_by_name("user1", dict(name="user1", uid=1001, gid=1001))
ced1f5
+    # Make sure the private group is resolvable by name and by GID
ced1f5
+    ent.assert_group_by_name("user1", dict(gid=1001, mem=ent.contains_only()))
ced1f5
+    ent.assert_group_by_gid(1001, dict(name="user1", mem=ent.contains_only()))
ced1f5
+
ced1f5
+    # The group referenced in user's gidNumber attribute should be still
ced1f5
+    # visible, but it's fine that it doesn't contain the user as a member
ced1f5
+    # as the group is currently added during the initgroups operation only
ced1f5
+    ent.assert_group_by_name("group1", dict(gid=2001, mem=ent.contains_only()))
ced1f5
+    ent.assert_group_by_gid(2001, dict(name="group1", mem=ent.contains_only()))
ced1f5
+
ced1f5
+    # The user's secondary groups list must be correct as well
ced1f5
+    # Note that the original GID is listed as well -- this is correct and expected
ced1f5
+    # because we save the original GID in the SYSDB_PRIMARY_GROUP_GIDNUM attribute
ced1f5
+    user1_expected_gids = [1001, 2001, 2012, 2015]
ced1f5
+    (res, errno, gids) = sssd_id.call_sssd_initgroups("user1", 1001)
ced1f5
+    assert res == sssd_id.NssReturnCode.SUCCESS
ced1f5
+
ced1f5
+    assert sorted(gids) == sorted(user1_expected_gids), \
ced1f5
+        "result: %s\n expected %s" % (
ced1f5
+            ", ".join(["%s" % s for s in sorted(gids)]),
ced1f5
+            ", ".join(["%s" % s for s in sorted(user1_expected_gids)])
ced1f5
+        )
ced1f5
+
ced1f5
+    # Request user2's private group by GID without resolving the user first.
ced1f5
+    # This must trigger user resolution through by-GID resolution, since the GID
ced1f5
+    # doesn't exist on its own in LDAP
ced1f5
+    ent.assert_group_by_gid(1002, dict(name="user2", mem=ent.contains_only()))
ced1f5
+
ced1f5
+    # Test supplementary groups for user2 as well
ced1f5
+    user1_expected_gids = [1002, 2002, 2012, 2016]
ced1f5
+    (res, errno, gids) = sssd_id.call_sssd_initgroups("user2", 1002)
ced1f5
+    assert res == sssd_id.NssReturnCode.SUCCESS
ced1f5
+
ced1f5
+    assert sorted(gids) == sorted(user1_expected_gids), \
ced1f5
+        "result: %s\n expected %s" % (
ced1f5
+            ", ".join(["%s" % s for s in sorted(gids)]),
ced1f5
+            ", ".join(["%s" % s for s in sorted(user1_expected_gids)])
ced1f5
+        )
ced1f5
+
ced1f5
+    # Request user3's private group by name without resolving the user first
ced1f5
+    # This must trigger user resolution through by-name resolution, since the
ced1f5
+    # name doesn't exist on its own in LDAP
ced1f5
+    ent.assert_group_by_name("user3", dict(gid=1003, mem=ent.contains_only()))
ced1f5
+
ced1f5
+    # Remove entries and request them again to make sure they are not
ced1f5
+    # resolvable anymore
ced1f5
+    cleanup_ldap_entries(ldap_conn, None)
ced1f5
+
ced1f5
+    if subprocess.call(["sss_cache", "-GU"]) != 0:
ced1f5
+        raise Exception("sssd_cache failed")
ced1f5
+
ced1f5
+    with pytest.raises(KeyError):
ced1f5
+        pwd.getpwnam("user1")
ced1f5
+    with pytest.raises(KeyError):
ced1f5
+        grp.getgrnam("user1")
ced1f5
+    with pytest.raises(KeyError):
ced1f5
+        grp.getgrgid(1002)
ced1f5
+    with pytest.raises(KeyError):
ced1f5
+        grp.getgrnam("user3")
ced1f5
+
ced1f5
+
ced1f5
+@pytest.fixture
ced1f5
+def mpg_setup_conflict(request, ldap_conn):
ced1f5
+    ent_list = ldap_ent.List(ldap_conn.ds_inst.base_dn)
ced1f5
+    ent_list.add_user("user1", 1001, 2001)
ced1f5
+    ent_list.add_user("user2", 1002, 2002)
ced1f5
+    ent_list.add_user("user3", 1003, 1003)
ced1f5
+    ent_list.add_group_bis("group1", 1001)
ced1f5
+    ent_list.add_group_bis("group2", 1002)
ced1f5
+    ent_list.add_group_bis("group3", 1003)
ced1f5
+    ent_list.add_group_bis("supp_group", 2015, ["user3"])
ced1f5
+    create_ldap_fixture(request, ldap_conn, ent_list)
ced1f5
+
ced1f5
+    conf = \
ced1f5
+        format_basic_conf(ldap_conn, SCHEMA_RFC2307_BIS) + \
ced1f5
+        unindent("""
ced1f5
+            [domain/LDAP]
ced1f5
+            auto_private_groups = True
ced1f5
+        """).format(**locals())
ced1f5
+    create_conf_fixture(request, conf)
ced1f5
+    create_sssd_fixture(request)
ced1f5
+    return None
ced1f5
+
ced1f5
+
ced1f5
+def test_ldap_auto_private_groups_conflict(ldap_conn, mpg_setup_conflict):
ced1f5
+    """
ced1f5
+    Make sure that conflicts between groups that are auto-created with the
ced1f5
+    help of the auto_private_groups option and between 'real' LDAP groups
ced1f5
+    are handled in a predictable manner.
ced1f5
+    """
ced1f5
+    # Make sure the user's GID is taken from their uidNumber
ced1f5
+    ent.assert_passwd_by_name("user1", dict(name="user1", uid=1001, gid=1001))
ced1f5
+    # Make sure the private group is resolvable by name and by GID
ced1f5
+    ent.assert_group_by_name("user1", dict(gid=1001, mem=ent.contains_only()))
ced1f5
+    ent.assert_group_by_gid(1001, dict(name="user1", mem=ent.contains_only()))
ced1f5
+
ced1f5
+    # Let's request the group with the same ID as user2's private group
ced1f5
+    # The request should match the 'real' group
ced1f5
+    ent.assert_group_by_gid(1002, dict(name="group2", mem=ent.contains_only()))
ced1f5
+    # But because of the GID conflict, the user cannot be resolved
ced1f5
+    with pytest.raises(KeyError):
ced1f5
+        pwd.getpwnam("user2")
ced1f5
+
ced1f5
+    # This user's GID is the same as the UID in this entry. The most important
ced1f5
+    # thing here is that the supplementary groups are correct and the GID
ced1f5
+    # resolves to the private group (as long as the user was requested first)
ced1f5
+    user3_expected_gids = [1003, 2015]
ced1f5
+    ent.assert_passwd_by_name("user3", dict(name="user3", uid=1003, gid=1003))
ced1f5
+    (res, errno, gids) = sssd_id.call_sssd_initgroups("user3", 1003)
ced1f5
+    assert res == sssd_id.NssReturnCode.SUCCESS
ced1f5
+
ced1f5
+    assert sorted(gids) == sorted(user3_expected_gids), \
ced1f5
+        "result: %s\n expected %s" % (
ced1f5
+            ", ".join(["%s" % s for s in sorted(gids)]),
ced1f5
+            ", ".join(["%s" % s for s in sorted(user3_expected_gids)])
ced1f5
+        )
ced1f5
+    # Make sure the private group is resolvable by name and by GID
ced1f5
+    ent.assert_group_by_gid(1003, dict(name="user3", mem=ent.contains_only()))
ced1f5
+    ent.assert_group_by_name("user3", dict(gid=1003, mem=ent.contains_only()))
ced1f5
+
ced1f5
+
ced1f5
+@pytest.fixture
ced1f5
+def mpg_setup_no_gid(request, ldap_conn):
ced1f5
+    ent_list = ldap_ent.List(ldap_conn.ds_inst.base_dn)
ced1f5
+    ent_list.add_user("user1", 1001, 2001)
ced1f5
+
ced1f5
+    ent_list.add_group_bis("group1", 2001)
ced1f5
+    ent_list.add_group_bis("one_user_group1", 2015, ["user1"])
ced1f5
+
ced1f5
+    create_ldap_entries(ldap_conn, ent_list)
ced1f5
+    create_ldap_cleanup(request, ldap_conn, None)
ced1f5
+
ced1f5
+    conf = \
ced1f5
+        format_basic_conf(ldap_conn, SCHEMA_RFC2307_BIS) + \
ced1f5
+        unindent("""
ced1f5
+            [domain/LDAP]
ced1f5
+            auto_private_groups = True
ced1f5
+            ldap_user_gid_number = no_such_attribute
ced1f5
+        """).format(**locals())
ced1f5
+    create_conf_fixture(request, conf)
ced1f5
+    create_sssd_fixture(request)
ced1f5
+    return None
ced1f5
+
ced1f5
+
ced1f5
+def test_ldap_auto_private_groups_direct_no_gid(ldap_conn, mpg_setup_no_gid):
ced1f5
+    """
ced1f5
+    Integration test for auto_private_groups - test that even a user with
ced1f5
+    no GID assigned at all can be resolved including their autogenerated
ced1f5
+    primary group.
ced1f5
+
ced1f5
+    See also ticket https://pagure.io/SSSD/sssd/issue/1872
ced1f5
+    """
ced1f5
+    # Make sure the user's GID is taken from their uidNumber
ced1f5
+    ent.assert_passwd_by_name("user1", dict(name="user1", uid=1001, gid=1001))
ced1f5
+    # Make sure the private group is resolvable by name and by GID
ced1f5
+    ent.assert_group_by_name("user1", dict(gid=1001, mem=ent.contains_only()))
ced1f5
+    ent.assert_group_by_gid(1001, dict(name="user1", mem=ent.contains_only()))
ced1f5
+
ced1f5
+    # The group referenced in user's gidNumber attribute should be still
ced1f5
+    # visible, but shouldn't have any relation to the user
ced1f5
+    ent.assert_group_by_name("group1", dict(gid=2001, mem=ent.contains_only()))
ced1f5
+    ent.assert_group_by_gid(2001, dict(name="group1", mem=ent.contains_only()))
ced1f5
+
ced1f5
+    # The user's secondary groups list must be correct as well. This time only
ced1f5
+    # the generated group and the explicit secondary group are added, since
ced1f5
+    # there is no original GID
ced1f5
+    user1_expected_gids = [1001, 2015]
ced1f5
+    (res, errno, gids) = sssd_id.call_sssd_initgroups("user1", 1001)
ced1f5
+    assert res == sssd_id.NssReturnCode.SUCCESS
ced1f5
+
ced1f5
+    assert sorted(gids) == sorted(user1_expected_gids), \
ced1f5
+        "result: %s\n expected %s" % (
ced1f5
+            ", ".join(["%s" % s for s in sorted(gids)]),
ced1f5
+            ", ".join(["%s" % s for s in sorted(user1_expected_gids)])
ced1f5
+        )
ced1f5
-- 
ced1f5
2.13.5
ced1f5