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

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