1079a7
From 6b70e3c49acc55b5553101cf850fc40978861979 Mon Sep 17 00:00:00 2001
1079a7
From: Anuja More <amore@redhat.com>
1079a7
Date: Mon, 17 Jan 2022 16:57:52 +0530
1079a7
Subject: [PATCH] ipatests: Tests for Autoprivate group.
1079a7
1079a7
Added tests using posix AD trust and non posix AD trust.
1079a7
For option --auto-private-groups=[hybrid/true/false]
1079a7
1079a7
Related : https://pagure.io/freeipa/issue/8807
1079a7
1079a7
Signed-off-by: Anuja More <amore@redhat.com>
1079a7
Reviewed-By: Florence Blanc-Renaud <flo@redhat.com>
1079a7
Reviewed-By: Anuja More <amore@redhat.com>
1079a7
---
1079a7
 .../nightly_ipa-4-9_latest.yaml               |   2 +-
1079a7
 .../nightly_ipa-4-9_latest_selinux.yaml       |   2 +-
1079a7
 .../nightly_ipa-4-9_previous.yaml             |   2 +-
1079a7
 ipatests/test_integration/test_trust.py       | 242 +++++++++++++++++-
1079a7
 4 files changed, 240 insertions(+), 8 deletions(-)
1079a7
1079a7
diff --git a/ipatests/prci_definitions/nightly_ipa-4-9_latest.yaml b/ipatests/prci_definitions/nightly_ipa-4-9_latest.yaml
1079a7
index 6817421b278999c52c32b3e28dd06587e30d874f..8b1f58c4d99e744e319e6c758050a62a8d35c9ee 100644
1079a7
--- a/ipatests/prci_definitions/nightly_ipa-4-9_latest.yaml
1079a7
+++ b/ipatests/prci_definitions/nightly_ipa-4-9_latest.yaml
1079a7
@@ -1627,7 +1627,7 @@ jobs:
1079a7
         build_url: '{fedora-latest-ipa-4-9/build_url}'
1079a7
         test_suite: test_integration/test_trust.py
1079a7
         template: *ci-ipa-4-9-latest
1079a7
-        timeout: 9000
1079a7
+        timeout: 10000
1079a7
         topology: *adroot_adchild_adtree_master_1client
1079a7
 
1079a7
   fedora-latest-ipa-4-9/test_backup_and_restore_TestBackupAndRestoreTrust:
1079a7
diff --git a/ipatests/prci_definitions/nightly_ipa-4-9_latest_selinux.yaml b/ipatests/prci_definitions/nightly_ipa-4-9_latest_selinux.yaml
1079a7
index 817329756dc145fa5e6bc7aa0477e5df2a6ece5b..a11376ab836e7ed2f942c29753707e5b8e88a00f 100644
1079a7
--- a/ipatests/prci_definitions/nightly_ipa-4-9_latest_selinux.yaml
1079a7
+++ b/ipatests/prci_definitions/nightly_ipa-4-9_latest_selinux.yaml
1079a7
@@ -1743,7 +1743,7 @@ jobs:
1079a7
         selinux_enforcing: True
1079a7
         test_suite: test_integration/test_trust.py
1079a7
         template: *ci-ipa-4-9-latest
1079a7
-        timeout: 9000
1079a7
+        timeout: 10000
1079a7
         topology: *adroot_adchild_adtree_master_1client
1079a7
 
1079a7
   fedora-latest-ipa-4-9/test_backup_and_restore_TestBackupAndRestoreTrust:
1079a7
diff --git a/ipatests/prci_definitions/nightly_ipa-4-9_previous.yaml b/ipatests/prci_definitions/nightly_ipa-4-9_previous.yaml
1079a7
index 4196265c772ec393ebb8f8bbdc4af845cd6d2d24..3f8ce8b7641fdfdc27278651cbf83c2b152e1a16 100644
1079a7
--- a/ipatests/prci_definitions/nightly_ipa-4-9_previous.yaml
1079a7
+++ b/ipatests/prci_definitions/nightly_ipa-4-9_previous.yaml
1079a7
@@ -1627,7 +1627,7 @@ jobs:
1079a7
         build_url: '{fedora-previous-ipa-4-9/build_url}'
1079a7
         test_suite: test_integration/test_trust.py
1079a7
         template: *ci-ipa-4-9-previous
1079a7
-        timeout: 9000
1079a7
+        timeout: 10000
1079a7
         topology: *adroot_adchild_adtree_master_1client
1079a7
 
1079a7
   fedora-previous-ipa-4-9/test_backup_and_restore_TestBackupAndRestoreTrust:
1079a7
diff --git a/ipatests/test_integration/test_trust.py b/ipatests/test_integration/test_trust.py
1079a7
index 0634badbb6a9aa148db2e3062e866215e61e89e7..ff2dd9cc819e1c5620ce449384957a633ae6d1f0 100644
1079a7
--- a/ipatests/test_integration/test_trust.py
1079a7
+++ b/ipatests/test_integration/test_trust.py
1079a7
@@ -62,11 +62,12 @@ class BaseTestTrust(IntegrationTest):
1079a7
         cls.check_sid_generation()
1079a7
         tasks.sync_time(cls.master, cls.ad)
1079a7
 
1079a7
-        cls.child_ad = cls.ad_subdomains[0]
1079a7
-        cls.ad_subdomain = cls.child_ad.domain.name
1079a7
-        cls.tree_ad = cls.ad_treedomains[0]
1079a7
-        cls.ad_treedomain = cls.tree_ad.domain.name
1079a7
-
1079a7
+        if cls.num_ad_subdomains > 0:
1079a7
+            cls.child_ad = cls.ad_subdomains[0]
1079a7
+            cls.ad_subdomain = cls.child_ad.domain.name
1079a7
+        if cls.num_ad_treedomains > 0:
1079a7
+            cls.tree_ad = cls.ad_treedomains[0]
1079a7
+            cls.ad_treedomain = cls.tree_ad.domain.name
1079a7
         # values used in workaround for
1079a7
         # https://bugzilla.redhat.com/show_bug.cgi?id=1711958
1079a7
         cls.srv_gc_record_name = \
1079a7
@@ -106,6 +107,63 @@ class BaseTestTrust(IntegrationTest):
1079a7
         expected_text = 'iparangetype: %s\n' % expected_type
1079a7
         assert expected_text in result.stdout_text
1079a7
 
1079a7
+    def mod_idrange_auto_private_group(
1079a7
+        self, option='false'
1079a7
+    ):
1079a7
+        """
1079a7
+        Set the auto-private-group option of the default trusted
1079a7
+        AD domain range.
1079a7
+        """
1079a7
+        tasks.kinit_admin(self.master)
1079a7
+        rangename = self.ad_domain.upper() + '_id_range'
1079a7
+        error_msg = "ipa: ERROR: no modifications to be performed"
1079a7
+        cmd = ["ipa", "idrange-mod", rangename,
1079a7
+               "--auto-private-groups", option]
1079a7
+        result = self.master.run_command(cmd, raiseonerr=False)
1079a7
+        if result.returncode != 0:
1079a7
+            tasks.assert_error(result, error_msg)
1079a7
+        tasks.clear_sssd_cache(self.master)
1079a7
+        tasks.clear_sssd_cache(self.clients[0])
1079a7
+        test = self.master.run_command(["ipa", "idrange-show", rangename])
1079a7
+        assert "Auto private groups: {0}".format(option) in test.stdout_text
1079a7
+
1079a7
+    def get_user_id(self, host, username):
1079a7
+        """
1079a7
+        User uid gid is parsed from the output of id user command.
1079a7
+        """
1079a7
+        tasks.clear_sssd_cache(self.master)
1079a7
+        tasks.clear_sssd_cache(self.clients[0])
1079a7
+        self.master.run_command(["id", username])
1079a7
+        test_id = host.run_command(["id", username])
1079a7
+        regex = r"^uid=(?P<uid>\d+).*gid=(?P<gid>\d+).*groups=(?P<groups>\d+)"
1079a7
+        match = re.match(regex, test_id.stdout_text)
1079a7
+        uid = match.group('uid')
1079a7
+        gid = match.group('gid')
1079a7
+        return uid, gid
1079a7
+
1079a7
+    @contextmanager
1079a7
+    def set_idoverrideuser(self, user, uid, gid):
1079a7
+        """
1079a7
+        Fixture to add/remove idoverrideuser for default idview,
1079a7
+        also creates idm group with the provided gid because
1079a7
+        gid overrides requires an existing group.
1079a7
+        """
1079a7
+        tasks.clear_sssd_cache(self.master)
1079a7
+        tasks.clear_sssd_cache(self.clients[0])
1079a7
+        tasks.kinit_admin(self.master)
1079a7
+        try:
1079a7
+            args = ["ipa", "idoverrideuser-add", "Default Trust View",
1079a7
+                    "--gid", gid, "--uid", uid, user]
1079a7
+            self.master.run_command(args)
1079a7
+            tasks.group_add(self.master, "idgroup",
1079a7
+                            extra_args=["--gid", gid])
1079a7
+            yield
1079a7
+        finally:
1079a7
+            self.master.run_command([
1079a7
+                "ipa", "idoverrideuser-del", "Default Trust View", user]
1079a7
+            )
1079a7
+            self.master.run_command(["ipa", "group-del", "idgroup"])
1079a7
+
1079a7
     def remove_trust(self, ad):
1079a7
         tasks.remove_trust_with_ad(self.master,
1079a7
                                    ad.domain.name, ad.hostname)
1079a7
@@ -993,3 +1051,177 @@ class TestTrust(BaseTestTrust):
1079a7
             self.master.run_command(['rm', '-f', ad_zone_file])
1079a7
             tasks.configure_dns_for_trust(self.master, self.ad)
1079a7
             self.remove_trust(self.ad)
1079a7
+
1079a7
+
1079a7
+class TestNonPosixAutoPrivateGroup(BaseTestTrust):
1079a7
+    """
1079a7
+    Tests for auto-private-groups option with non posix AD trust
1079a7
+    Related : https://pagure.io/freeipa/issue/8807
1079a7
+    """
1079a7
+    topology = 'line'
1079a7
+    num_ad_domains = 1
1079a7
+    num_clients = 1
1079a7
+    num_ad_subdomains = 0
1079a7
+    num_ad_treedomains = 0
1079a7
+    uid_override = "99999999"
1079a7
+    gid_override = "78878787"
1079a7
+
1079a7
+    def test_add_nonposix_trust(self):
1079a7
+        tasks.configure_dns_for_trust(self.master, self.ad)
1079a7
+        tasks.establish_trust_with_ad(
1079a7
+            self.master, self.ad_domain,
1079a7
+            extra_args=['--range-type', 'ipa-ad-trust'])
1079a7
+
1079a7
+    @pytest.mark.parametrize('type', ['hybrid', 'true', "false"])
1079a7
+    def test_auto_private_groups_default_trusted_range(self, type):
1079a7
+        """
1079a7
+        Modify existing range for default trusted AD domain range
1079a7
+        with auto-private-groups set as true/hybrid/false and test
1079a7
+        user with no posix attributes.
1079a7
+        """
1079a7
+        self.mod_idrange_auto_private_group(type)
1079a7
+        nonposixuser = "nonposixuser@%s" % self.ad_domain
1079a7
+        (uid, gid) = self.get_user_id(self.clients[0], nonposixuser)
1079a7
+        if type == "true":
1079a7
+            assert uid == gid
1079a7
+        else:
1079a7
+            test_group = self.clients[0].run_command(["id", nonposixuser])
1079a7
+            gid_str = "gid={0}(domain users@{1})".format(gid, self.ad_domain)
1079a7
+            grp_str = "groups={0}(domain users@{1})".format(gid,
1079a7
+                                                            self.ad_domain)
1079a7
+            assert gid_str in test_group.stdout_text
1079a7
+            assert grp_str in test_group.stdout_text
1079a7
+            assert uid != gid
1079a7
+
1079a7
+    @pytest.mark.parametrize('type', ['hybrid', 'true', "false"])
1079a7
+    def test_idoverride_with_auto_private_group(self, type):
1079a7
+        """
1079a7
+        Override ad trusted user in default trust view
1079a7
+        and set auto-private-groups=[hybrid,true,false]
1079a7
+        and ensure that overridden values takes effect.
1079a7
+        """
1079a7
+        nonposixuser = "nonposixuser@%s" % self.ad_domain
1079a7
+        with self.set_idoverrideuser(nonposixuser,
1079a7
+                                     self.uid_override,
1079a7
+                                     self.gid_override
1079a7
+                                     ):
1079a7
+            self.mod_idrange_auto_private_group(type)
1079a7
+            (uid, gid) = self.get_user_id(self.clients[0], nonposixuser)
1079a7
+            assert (uid == self.uid_override and gid == self.gid_override)
1079a7
+            test_group = self.clients[0].run_command(
1079a7
+                ["id", nonposixuser]).stdout_text
1079a7
+            assert "domain users@{0}".format(self.ad_domain) in test_group
1079a7
+
1079a7
+    @pytest.mark.parametrize('type', ['hybrid', 'true', "false"])
1079a7
+    def test_nonposixuser_nondefault_primary_group(self, type):
1079a7
+        """
1079a7
+        Test for non default primary group.
1079a7
+        For hybrid/false gid corresponds to the group testgroup1.
1079a7
+        """
1079a7
+        nonposixuser1 = "nonposixuser1@%s" % self.ad_domain
1079a7
+        self.mod_idrange_auto_private_group(type)
1079a7
+        (uid, gid) = self.get_user_id(self.clients[0], nonposixuser1)
1079a7
+        if type == "true":
1079a7
+            assert uid == gid
1079a7
+        else:
1079a7
+            test_group = self.clients[0].run_command(["id", nonposixuser1])
1079a7
+            gid_str = "gid={0}(testgroup1@{1})".format(gid, self.ad_domain)
1079a7
+            group = "groups={0}(testgroup1@{1})".format(gid, self.ad_domain)
1079a7
+            assert (gid_str in test_group.stdout_text
1079a7
+                    and group in test_group.stdout_text)
1079a7
+
1079a7
+
1079a7
+class TestPosixAutoPrivateGroup(BaseTestTrust):
1079a7
+    """
1079a7
+    Tests for auto-private-groups option with posix AD trust
1079a7
+    Related : https://pagure.io/freeipa/issue/8807
1079a7
+    """
1079a7
+    topology = 'line'
1079a7
+    num_ad_domains = 1
1079a7
+    num_clients = 1
1079a7
+    num_ad_subdomains = 0
1079a7
+    num_ad_treedomains = 0
1079a7
+    uid_override = "99999999"
1079a7
+    gid_override = "78878787"
1079a7
+
1079a7
+    def test_add_posix_trust(self):
1079a7
+        tasks.configure_dns_for_trust(self.master, self.ad)
1079a7
+        tasks.establish_trust_with_ad(
1079a7
+            self.master, self.ad_domain,
1079a7
+            extra_args=['--range-type', 'ipa-ad-trust-posix'])
1079a7
+
1079a7
+    @pytest.mark.parametrize('type', ['hybrid', 'true', "false"])
1079a7
+    def test_gidnumber_not_corresponding_existing_group(self, type):
1079a7
+        """
1079a7
+        Test checks that sssd can resolve AD users which
1079a7
+        contain posix attributes (uidNumber and gidNumber)
1079a7
+        but there is no group with the corresponding gidNumber.
1079a7
+        """
1079a7
+        posixuser = "testuser2@%s" % self.ad_domain
1079a7
+        self.mod_idrange_auto_private_group(type)
1079a7
+        if type != "true":
1079a7
+            result = self.clients[0].run_command(['id', posixuser],
1079a7
+                                                 raiseonerr=False)
1079a7
+            tasks.assert_error(result, "no such user")
1079a7
+        else:
1079a7
+            (uid, gid) = self.get_user_id(self.clients[0], posixuser)
1079a7
+            assert uid == gid
1079a7
+            assert uid == '10060'
1079a7
+
1079a7
+    @pytest.mark.parametrize('type', ['hybrid', 'true', "false"])
1079a7
+    def test_only_uid_number_auto_private_group_default(self, type):
1079a7
+        """
1079a7
+        Test checks that posix user with only uidNumber defined
1079a7
+        and gidNumber not set, auto-private-group
1079a7
+        is set to false/true/hybrid
1079a7
+        """
1079a7
+        posixuser = "testuser1@%s" % self.ad_domain
1079a7
+        self.mod_idrange_auto_private_group(type)
1079a7
+        if type == "true":
1079a7
+            (uid, gid) = self.get_user_id(self.clients[0], posixuser)
1079a7
+            assert uid == gid
1079a7
+        else:
1079a7
+            for host in [self.master, self.clients[0]]:
1079a7
+                result = host.run_command(['id', posixuser], raiseonerr=False)
1079a7
+                tasks.assert_error(result, "no such user")
1079a7
+
1079a7
+    @pytest.mark.parametrize('type', ['hybrid', 'true', "false"])
1079a7
+    def test_auto_private_group_primary_group(self, type):
1079a7
+        """
1079a7
+        Test checks that AD users which contain posix attributes
1079a7
+        (uidNumber and gidNumber) and there is primary group
1079a7
+        with gid number defined.
1079a7
+        """
1079a7
+        posixuser = "testuser@%s" % self.ad_domain
1079a7
+        self.mod_idrange_auto_private_group(type)
1079a7
+        (uid, gid) = self.get_user_id(self.clients[0], posixuser)
1079a7
+        test_grp = self.clients[0].run_command(["id", posixuser])
1079a7
+        assert uid == '10042'
1079a7
+        if type == "true":
1079a7
+            assert uid == gid
1079a7
+            groups = "groups=10042(testuser@{0}),10047(testgroup@{1})".format(
1079a7
+                self.ad_domain, self.ad_domain)
1079a7
+            assert groups in test_grp.stdout_text
1079a7
+        else:
1079a7
+            assert gid == '10047'
1079a7
+            groups = "10047(testgroup@{0})".format(self.ad_domain)
1079a7
+            assert groups in test_grp.stdout_text
1079a7
+
1079a7
+    @pytest.mark.parametrize('type', ['hybrid', 'true', "false"])
1079a7
+    def test_idoverride_with_auto_private_group(self, type):
1079a7
+        """
1079a7
+        Override ad trusted user in default trust view
1079a7
+        and set auto-private-groups=[hybrid,true,false]
1079a7
+        and ensure that overridden values takes effect.
1079a7
+        """
1079a7
+        posixuser = "testuser@%s" % self.ad_domain
1079a7
+        with self.set_idoverrideuser(posixuser,
1079a7
+                                     self.uid_override,
1079a7
+                                     self.gid_override):
1079a7
+            self.mod_idrange_auto_private_group(type)
1079a7
+            (uid, gid) = self.get_user_id(self.clients[0], posixuser)
1079a7
+            assert(uid == self.uid_override
1079a7
+                   and gid == self.gid_override)
1079a7
+            result = self.clients[0].run_command(['id', posixuser])
1079a7
+            assert "10047(testgroup@{0})".format(
1079a7
+                self.ad_domain) in result.stdout_text
1079a7
-- 
1079a7
2.34.1
1079a7