Blame SOURCES/0029-Resolve_user_group_names_in_idoverride_-find_rhbz#1657745.patch

b01884
BEGIN EXCERPT from 8182ebc6c3ca636276fc277186cfbff4ea9cf5c6 to have user_add
b01884
in ipatests/pytest_ipa/integration/tasks.py to be able to apply the patch set.
b01884
b01884
commit 8182ebc6c3ca636276fc277186cfbff4ea9cf5c6
b01884
Author: Sergey Orlov <sorlov@redhat.com>
b01884
Date:   Wed Nov 7 11:23:05 2018 +0100
b01884
b01884
    ipatests: add test for ipa-restore in multi-master configuration
b01884
    
b01884
    Test ensures that after ipa-restore on the master, the replica can be
b01884
    re-synchronized and a new replica can be created.
b01884
    
b01884
    https://pagure.io/freeipa/issue/7455
b01884
    
b01884
    Reviewed-By: Christian Heimes <cheimes@redhat.com>
b01884
    Reviewed-By: Tibor Dudlak <tdudlak@redhat.com>
b01884
b01884
diff --git a/ipatests/pytest_ipa/integration/tasks.py b/ipatests/pytest_ipa/integration/tasks.py
b01884
index 814141b83..90da8fa62 100644
b01884
--- a/ipatests/pytest_ipa/integration/tasks.py
b01884
+++ b/ipatests/pytest_ipa/integration/tasks.py
b01884
@@ -1555,3 +1561,11 @@ def strip_cert_header(pem):
b01884
         return s.group(1)
b01884
     else:
b01884
         return pem
b01884
+
b01884
+
b01884
+def user_add(host, login):
b01884
+    host.run_command([
b01884
+        "ipa", "user-add", login,
b01884
+        "--first", "test",
b01884
+        "--last", "user"
b01884
+    ])
b01884
END EXCERPT
b01884
From 5e6cb0ca034c711fe81fcfe7c651c5af3c65aa40 Mon Sep 17 00:00:00 2001
b01884
From: Christian Heimes <cheimes@redhat.com>
b01884
Date: Dec 07 2018 15:06:13 +0000
b01884
Subject: Resolve user/group names in idoverride*-find
b01884
b01884
b01884
ipa idoverrideuser-find and ...group-find have an --anchor argument. The
b01884
anchor argument used to support only anchor UUIDs like
b01884
':IPA:domain:UUID' or ':SID:S-sid'. The find commands now detect regular
b01884
user or group names and translate them to anchors.
b01884
b01884
Fixes: https://pagure.io/freeipa/issue/6594
b01884
Signed-off-by: Christian Heimes <cheimes@redhat.com>
b01884
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
b01884
b01884
---
b01884
b01884
diff --git a/ipaserver/plugins/idviews.py b/ipaserver/plugins/idviews.py
b01884
index 3252982..5213486 100644
b01884
--- a/ipaserver/plugins/idviews.py
b01884
+++ b/ipaserver/plugins/idviews.py
b01884
@@ -766,6 +766,40 @@ class baseidoverride(LDAPObject):
b01884
                     error=_('Default Trust View cannot contain IPA users')
b01884
                     )
b01884
 
b01884
+    def filter_for_anchor(self, ldap, filter, options, obj_type):
b01884
+        """Modify filter to support user and group names
b01884
+
b01884
+        Allow users to pass in an IPA user/group name and resolve it to an
b01884
+        anchor name.
b01884
+
b01884
+        :param ldap: ldap connection
b01884
+        :param filter: pre_callback filter
b01884
+        :param options: option dict
b01884
+        :param obj_type: 'user' or 'group'
b01884
+        :return: modified or same filter
b01884
+        """
b01884
+        anchor = options.get('ipaanchoruuid', None)
b01884
+        # return original filter if anchor is absent or correct
b01884
+        if anchor is None or ANCHOR_REGEX.match(anchor):
b01884
+            return filter
b01884
+        try:
b01884
+            resolved_anchor = resolve_object_to_anchor(
b01884
+                ldap, obj_type, anchor,
b01884
+                options.get('fallback_to_ldap', False)
b01884
+            )
b01884
+        except (errors.NotFound, errors.ValidationError):
b01884
+            # anchor cannot be resolved, let it pass through
b01884
+            return filter
b01884
+        else:
b01884
+            return ldap.make_filter(
b01884
+                {
b01884
+                    'objectClass': self.object_class,
b01884
+                    'ipaanchoruuid': resolved_anchor,
b01884
+                },
b01884
+                rules=ldap.MATCH_ALL
b01884
+            )
b01884
+
b01884
+
b01884
 class baseidoverride_add(LDAPCreate):
b01884
     __doc__ = _('Add a new ID override.')
b01884
     msg_summary = _('Added ID override "%(value)s"')
b01884
@@ -1128,6 +1162,15 @@ class idoverrideuser_find(baseidoverride_find):
b01884
     msg_summary = ngettext('%(count)d User ID override matched',
b01884
                            '%(count)d User ID overrides matched', 0)
b01884
 
b01884
+    def pre_callback(self, ldap, filter, attrs_list, base_dn, scope, *args,
b01884
+                     **options):
b01884
+        result = super(idoverrideuser_find, self).pre_callback(
b01884
+            ldap, filter, attrs_list, base_dn, scope, *args, **options
b01884
+        )
b01884
+        filter, base_dn, scope = result
b01884
+        filter = self.obj.filter_for_anchor(ldap, filter, options, 'user')
b01884
+        return filter, base_dn, scope
b01884
+
b01884
     def post_callback(self, ldap, entries, truncated, *args, **options):
b01884
         truncated = super(idoverrideuser_find, self).post_callback(
b01884
             ldap, entries, truncated, *args, **options)
b01884
@@ -1173,6 +1216,15 @@ class idoverridegroup_find(baseidoverride_find):
b01884
     msg_summary = ngettext('%(count)d Group ID override matched',
b01884
                            '%(count)d Group ID overrides matched', 0)
b01884
 
b01884
+    def pre_callback(self, ldap, filter, attrs_list, base_dn, scope, *args,
b01884
+                     **options):
b01884
+        result = super(idoverridegroup_find, self).pre_callback(
b01884
+            ldap, filter, attrs_list, base_dn, scope, *args, **options
b01884
+        )
b01884
+        filter, base_dn, scope = result
b01884
+        filter = self.obj.filter_for_anchor(ldap, filter, options, 'group')
b01884
+        return filter, base_dn, scope
b01884
+
b01884
 
b01884
 @register()
b01884
 class idoverridegroup_show(baseidoverride_show):
b01884
b01884
From 11b06d24a94c5e92a0275df759bc81f0fc81d802 Mon Sep 17 00:00:00 2001
b01884
From: Christian Heimes <cheimes@redhat.com>
b01884
Date: Dec 07 2018 15:06:13 +0000
b01884
Subject: Add integration tests for idviews
b01884
b01884
b01884
Add several tests to verify new anchor override and general idview
b01884
override functionality.
b01884
b01884
Fixes: https://pagure.io/freeipa/issue/6594
b01884
Signed-off-by: Christian Heimes <cheimes@redhat.com>
b01884
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
b01884
b01884
---
b01884
b01884
diff --git a/ipatests/pytest_ipa/integration/tasks.py b/ipatests/pytest_ipa/integration/tasks.py
b01884
index 36178e8..3548f2b 100644
b01884
--- a/ipatests/pytest_ipa/integration/tasks.py
b01884
+++ b/ipatests/pytest_ipa/integration/tasks.py
b01884
@@ -1576,9 +1576,19 @@ def strip_cert_header(pem):
b01884
         return pem
b01884
 
b01884
 
b01884
-def user_add(host, login):
b01884
-    host.run_command([
b01884
+def user_add(host, login, first='test', last='user', extra_args=()):
b01884
+    cmd = [
b01884
         "ipa", "user-add", login,
b01884
-        "--first", "test",
b01884
-        "--last", "user"
b01884
-    ])
b01884
+        "--first", first,
b01884
+        "--last", last
b01884
+    ]
b01884
+    cmd.extend(extra_args)
b01884
+    return host.run_command(cmd)
b01884
+
b01884
+
b01884
+def group_add(host, groupname, extra_args=()):
b01884
+    cmd = [
b01884
+        "ipa", "group-add", groupname,
b01884
+    ]
b01884
+    cmd.extend(extra_args)
b01884
+    return host.run_command(cmd)
b01884
diff --git a/ipatests/test_integration/test_idviews.py b/ipatests/test_integration/test_idviews.py
b01884
index 9a8f379..6ede4d0 100644
b01884
--- a/ipatests/test_integration/test_idviews.py
b01884
+++ b/ipatests/test_integration/test_idviews.py
b01884
@@ -165,6 +165,7 @@ class TestRulesWithServicePrincipals(IntegrationTest):
b01884
 
b01884
     topology = 'star'
b01884
     num_replicas = 0
b01884
+    num_clients = 0
b01884
     service_certprofile = 'caIPAserviceCert'
b01884
     caacl = 'test_caacl'
b01884
     keytab = "replica.keytab"
b01884
@@ -238,3 +239,133 @@ EOF
b01884
                                          raiseonerr=False)
b01884
         assert(result.returncode == 0), (
b01884
             'Failed to add a cert to custom certprofile')
b01884
+
b01884
+
b01884
+class TestIDViews(IntegrationTest):
b01884
+    topology = 'star'
b01884
+    num_replicas = 0
b01884
+    num_clients = 1
b01884
+
b01884
+    user1 = 'testuser1'
b01884
+    user1_uid = 10001
b01884
+    user1_gid = 10001
b01884
+    user1_uid_override = 5001
b01884
+    user1_gid_override = 6001
b01884
+
b01884
+    user2 = 'testuser2'
b01884
+    user2_uid = 10002
b01884
+    user2_gid = 10002
b01884
+
b01884
+    group1 = 'testgroup1'
b01884
+    group1_gid = 11001
b01884
+    group1_gid_override = 7001
b01884
+
b01884
+    idview = 'testview'
b01884
+
b01884
+    @classmethod
b01884
+    def install(cls, mh):
b01884
+        super(TestIDViews, cls).install(mh)
b01884
+        master = cls.master
b01884
+        client = cls.clients[0]
b01884
+        tasks.kinit_admin(master)
b01884
+
b01884
+        tasks.user_add(
b01884
+            master, cls.user1, first='Test1',
b01884
+            extra_args=[
b01884
+                '--uid', str(cls.user1_uid),
b01884
+                '--gidnumber', str(cls.user1_gid),
b01884
+            ]
b01884
+        )
b01884
+        tasks.user_add(
b01884
+            master, cls.user2, first='Test2',
b01884
+            extra_args=[
b01884
+                '--uid', str(cls.user2_uid),
b01884
+                '--gidnumber', str(cls.user2_gid),
b01884
+            ]
b01884
+        )
b01884
+        tasks.group_add(
b01884
+            master, cls.group1, extra_args=['--gid', str(cls.group1_gid)]
b01884
+        )
b01884
+
b01884
+        master.run_command(['ipa', 'idview-add', cls.idview])
b01884
+
b01884
+        # add overrides for user1 and its default user group
b01884
+        master.run_command([
b01884
+            'ipa', 'idoverrideuser-add', cls.idview, cls.user1,
b01884
+            '--uid', str(cls.user1_uid_override),
b01884
+            '--gid', str(cls.user1_gid_override),
b01884
+            '--homedir', '/special-home/{}'.format(cls.user1),
b01884
+            '--shell', '/bin/special'
b01884
+        ])
b01884
+        master.run_command([
b01884
+            'ipa', 'idoverridegroup-add', cls.idview, cls.group1,
b01884
+            '--gid', str(cls.group1_gid_override),
b01884
+        ])
b01884
+
b01884
+        # ID view overrides don't work on IPA masters
b01884
+        master.run_command([
b01884
+            'ipa', 'idview-apply', cls.idview,
b01884
+            '--hosts', client.hostname
b01884
+        ])
b01884
+        # finally restart SSSD to materialize idviews
b01884
+        client.run_command(['systemctl', 'restart', 'sssd.service'])
b01884
+
b01884
+    def test_useroverride(self):
b01884
+        result = self.clients[0].run_command(['id', self.user1])
b01884
+        assert 'uid={}'.format(self.user1_uid_override) in result.stdout_text
b01884
+        assert 'gid={}'.format(self.user1_gid_override) in result.stdout_text
b01884
+
b01884
+        result = self.clients[0].run_command(
b01884
+            ['getent', 'passwd', str(self.user1_uid_override)]
b01884
+        )
b01884
+        expected = '{}:*:{}:{}'.format(
b01884
+            self.user1, self.user1_uid_override, self.user1_gid_override
b01884
+        )
b01884
+        assert expected in result.stdout_text
b01884
+
b01884
+        result = self.master.run_command(['id', self.user1])
b01884
+        assert 'uid={}'.format(self.user1_uid) in result.stdout_text
b01884
+        assert 'gid={}'.format(self.user1_gid) in result.stdout_text
b01884
+
b01884
+    def test_useroverride_original_uid(self):
b01884
+        # It's still possible to request the user with its original UID. In
b01884
+        # this case the getent command returns the user with override uid.
b01884
+        result = self.clients[0].run_command(
b01884
+            ['getent', 'passwd', str(self.user1_uid)]
b01884
+        )
b01884
+        expected = '{}:*:{}:{}'.format(
b01884
+            self.user1, self.user1_uid_override, self.user1_gid_override
b01884
+        )
b01884
+        assert expected in result.stdout_text
b01884
+
b01884
+    def test_anchor_username(self):
b01884
+        result = self.master.run_command([
b01884
+            'ipa', 'idoverrideuser-find', self.idview, '--anchor', self.user1
b01884
+        ])
b01884
+        expected = "Anchor to override: {}".format(self.user1)
b01884
+        assert expected in result.stdout_text
b01884
+
b01884
+    def test_groupoverride(self):
b01884
+        result = self.clients[0].run_command(['getent', 'group', self.group1])
b01884
+        assert ':{}:'.format(self.group1_gid_override) in result.stdout_text
b01884
+
b01884
+        result = self.master.run_command(['getent', 'group', self.group1])
b01884
+        assert ':{}:'.format(self.group1_gid) in result.stdout_text
b01884
+
b01884
+    def test_groupoverride_system_objects(self):
b01884
+        # group override for user group should fail
b01884
+        result = self.master.run_command(
b01884
+            ['ipa', 'idoverridegroup-add', self.idview, self.user1,
b01884
+             '--gid', str(self.user1_gid_override)],
b01884
+            raiseonerr=False
b01884
+        )
b01884
+        assert result.returncode == 1
b01884
+        assert "cannot be overridden" in result.stderr_text
b01884
+
b01884
+    def test_anchor_groupname(self):
b01884
+        result = self.master.run_command([
b01884
+            'ipa', 'idoverridegroup-find', self.idview,
b01884
+            '--anchor', self.group1
b01884
+        ])
b01884
+        expected = "Anchor to override: {}".format(self.group1)
b01884
+        assert expected in result.stdout_text
b01884
b01884
ONLY APPLYING TO ipatests/prci_definitions/nightly_rawhide.yaml, other
b01884
files are not available or compatible
b01884
b01884
From e86498ea2f8259118025e622cc5f1cf2c26f2757 Mon Sep 17 00:00:00 2001
b01884
From: Christian Heimes <cheimes@redhat.com>
b01884
Date: Dec 07 2018 15:06:13 +0000
b01884
Subject: Run idviews integration tests in nightly
b01884
b01884
b01884
See: https://pagure.io/freeipa/issue/6594
b01884
Signed-off-by: Christian Heimes <cheimes@redhat.com>
b01884
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
b01884
b01884
---
b01884
b01884
#diff --git a/ipatests/prci_definitions/nightly_f28.yaml b/ipatests/prci_definitions/nightly_f28.yaml
b01884
#index 8462c14..ac792f1 100644
b01884
#--- a/ipatests/prci_definitions/nightly_f28.yaml
b01884
#+++ b/ipatests/prci_definitions/nightly_f28.yaml
b01884
#@@ -195,6 +195,18 @@ jobs:
b01884
#         timeout: 10800
b01884
#         topology: *master_1repl
b01884
# 
b01884
#+  fedora-28/test_idviews:
b01884
#+    requires: [fedora-28/build]
b01884
#+    priority: 50
b01884
#+    job:
b01884
#+      class: RunPytest
b01884
#+      args:
b01884
#+        build_url: '{fedora-28/build_url}'
b01884
#+        test_suite: test_integration/test_idviews.py::TestIDViews
b01884
#+        template: *ci-master-f28
b01884
#+        timeout: 3600
b01884
#+        topology: *master_1repl_1client
b01884
#+
b01884
#   fedora-28/test_caless_TestServerInstall:
b01884
#     requires: [fedora-28/build]
b01884
#     priority: 50
b01884
#diff --git a/ipatests/prci_definitions/nightly_master.yaml b/ipatests/prci_definitions/nightly_master.yaml
b01884
#index 3f2b346..953a60e 100644
b01884
#--- a/ipatests/prci_definitions/nightly_master.yaml
b01884
#+++ b/ipatests/prci_definitions/nightly_master.yaml
b01884
#@@ -195,6 +195,18 @@ jobs:
b01884
#         timeout: 10800
b01884
#         topology: *master_1repl
b01884
# 
b01884
#+  fedora-28/test_idviews:
b01884
#+    requires: [fedora-29/build]
b01884
#+    priority: 50
b01884
#+    job:
b01884
#+      class: RunPytest
b01884
#+      args:
b01884
#+        build_url: '{fedora-29/build_url}'
b01884
#+        test_suite: test_integration/test_idviews.py::TestIDViews
b01884
#+        template: *ci-master-f29
b01884
#+        timeout: 3600
b01884
#+        topology: *master_1repl_1client
b01884
#+
b01884
#   fedora-29/test_caless_TestServerInstall:
b01884
#     requires: [fedora-29/build]
b01884
#     priority: 50
b01884
diff --git a/ipatests/prci_definitions/nightly_rawhide.yaml b/ipatests/prci_definitions/nightly_rawhide.yaml
b01884
index bdc34d2..e74e5f6 100644
b01884
--- a/ipatests/prci_definitions/nightly_rawhide.yaml
b01884
+++ b/ipatests/prci_definitions/nightly_rawhide.yaml
b01884
@@ -195,6 +195,18 @@ jobs:
b01884
         timeout: 10800
b01884
         topology: *master_1repl
b01884
 
b01884
+  fedora-28/test_idviews:
b01884
+    requires: [fedora-rawhide/build]
b01884
+    priority: 50
b01884
+    job:
b01884
+      class: RunPytest
b01884
+      args:
b01884
+        build_url: '{fedora-rawhide/build_url}'
b01884
+        test_suite: test_integration/test_idviews.py::TestIDViews
b01884
+        template: *ci-master-frawhide
b01884
+        timeout: 3600
b01884
+        topology: *master_1repl_1client
b01884
+
b01884
   fedora-rawhide/test_caless_TestServerInstall:
b01884
     requires: [fedora-rawhide/build]
b01884
     priority: 50
b01884