>From 174b432e59d01c087cce6e279f5cd4c392f5c173 Mon Sep 17 00:00:00 2001
From: Pavel Reichl <preichl@redhat.com>
Date: Mon, 5 Oct 2015 10:02:05 -0400
Subject: [PATCH] intg: Add test for user local overrides

Introduce a new integration test for local view overrides.

Resolves:
https://fedorahosted.org/sssd/ticket/2732
---
 src/tests/intg/ldap_local_override_test.py | 567 +++++++++++++++++++++++++++++
 1 file changed, 567 insertions(+)
 create mode 100644 src/tests/intg/ldap_local_override_test.py

diff --git a/src/tests/intg/ldap_local_override_test.py b/src/tests/intg/ldap_local_override_test.py
new file mode 100644
index 0000000000000000000000000000000000000000..5d19fd30f5c0401614984f26c354285e622c6917
--- /dev/null
+++ b/src/tests/intg/ldap_local_override_test.py
@@ -0,0 +1,567 @@
+#
+# integration test for sss_override tool
+#
+# Copyright (c) 2015 Red Hat, Inc.
+# Author: Pavel Reichl  <preichl@redhat.com>
+#
+# This is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 only
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+import os
+import stat
+import ent
+import grp
+import pwd
+import config
+import signal
+import subprocess
+import time
+import pytest
+import ds_openldap
+import ldap_ent
+import sssd_id
+from util import unindent
+
+LDAP_BASE_DN = "dc=example,dc=com"
+
+
+@pytest.fixture(scope="module")
+def ds_inst(request):
+    """LDAP server instance fixture"""
+    ds_inst = ds_openldap.DSOpenLDAP(
+        config.PREFIX, 10389, LDAP_BASE_DN,
+        "cn=admin", "Secret123")
+    try:
+        ds_inst.setup()
+    except:
+        ds_inst.teardown()
+        raise
+    request.addfinalizer(lambda: ds_inst.teardown())
+    return ds_inst
+
+
+@pytest.fixture(scope="module")
+def ldap_conn(request, ds_inst):
+    """LDAP server connection fixture"""
+    ldap_conn = ds_inst.bind()
+    ldap_conn.ds_inst = ds_inst
+    request.addfinalizer(lambda: ldap_conn.unbind_s())
+    return ldap_conn
+
+
+def create_ldap_fixture(request, ldap_conn, ent_list):
+    """Add LDAP entries and add teardown for removing them"""
+    for entry in ent_list:
+        ldap_conn.add_s(entry[0], entry[1])
+
+    def teardown():
+        for entry in ent_list:
+            ldap_conn.delete_s(entry[0])
+    request.addfinalizer(teardown)
+
+
+def create_conf_fixture(request, contents):
+    """Generate sssd.conf and add teardown for removing it"""
+    conf = open(config.CONF_PATH, "w")
+    conf.write(contents)
+    conf.close()
+    os.chmod(config.CONF_PATH, stat.S_IRUSR | stat.S_IWUSR)
+    request.addfinalizer(lambda: os.unlink(config.CONF_PATH))
+
+
+def stop_sssd():
+    pid_file = open(config.PIDFILE_PATH, "r")
+    pid = int(pid_file.read())
+    os.kill(pid, signal.SIGTERM)
+    while True:
+        try:
+            os.kill(pid, signal.SIGCONT)
+        except:
+            break
+        time.sleep(1)
+
+
+def start_sssd():
+    """Start sssd"""
+    if subprocess.call(["sssd", "-D", "-f"]) != 0:
+        raise Exception("sssd start failed")
+
+
+def restart_sssd():
+    stop_sssd()
+    start_sssd()
+
+
+def create_sssd_fixture(request):
+    """Start sssd and add teardown for stopping it and removing state"""
+    if subprocess.call(["sssd", "-D", "-f"]) != 0:
+        raise Exception("sssd start failed")
+
+    def teardown():
+        try:
+            stop_sssd()
+        except:
+            pass
+        subprocess.call(["sss_cache", "-E"])
+        for path in os.listdir(config.DB_PATH):
+            os.unlink(config.DB_PATH + "/" + path)
+        for path in os.listdir(config.MCACHE_PATH):
+            os.unlink(config.MCACHE_PATH + "/" + path)
+    request.addfinalizer(teardown)
+
+
+override_user_filename = "user_export_file"
+
+def prepare_sssd(request, ldap_conn):
+    """Prepare SSSD woth defaults"""
+    conf = unindent("""\
+        [sssd]
+        domains             = LDAP
+        services            = nss
+
+        [domain/LDAP]
+        ldap_auth_disable_tls_never_use_in_production = true
+        ldap_schema         = rfc2307
+        id_provider         = ldap
+        auth_provider       = ldap
+        sudo_provider       = ldap
+        ldap_uri            = {ldap_conn.ds_inst.ldap_url}
+        ldap_search_base    = {ldap_conn.ds_inst.base_dn}
+    """).format(**locals())
+    create_conf_fixture(request, conf)
+    create_sssd_fixture(request)
+
+#
+# Simple user override
+#
+
+@pytest.fixture
+def env_simple_user_override(request, ldap_conn):
+
+    prepare_sssd(request, ldap_conn)
+
+    # Add entries
+    ent_list = ldap_ent.List(LDAP_BASE_DN)
+    ent_list.add_user("user1", 10001, 20001,
+                      gecos='User Number 1',
+                      loginShell='/bin/user1_shell',
+                      homeDirectory='/home/user1')
+
+    ent_list.add_user("user2", 10002, 20001,
+                      gecos='User Number 2',
+                      loginShell='/bin/user2_shell',
+                      homeDirectory='/home/user2')
+
+    ent_list.add_group("group", 2001,
+                       ["user2", "user1"])
+
+    create_ldap_fixture(request, ldap_conn, ent_list)
+
+    # Assert entries are not overriden
+    with pytest.raises(KeyError):
+        pwd.getpwnam('ov_user1')
+    with pytest.raises(KeyError):
+        pwd.getpwnam('ov_user2')
+    with pytest.raises(KeyError):
+        pwd.getpwnam('ov_user1@LDAP')
+    with pytest.raises(KeyError):
+        pwd.getpwnam('ov_user2@LDAP')
+
+    # Override
+    subprocess.check_call(["sss_override", "user-add", "user1",
+                           "-u", "10010",
+                           "-g", "20010",
+                           "-n", "ov_user1",
+                           "-c", "Overriden User 1",
+                           "-h", "/home/ov/user1",
+                           "-s", "/bin/ov_user1_shell"])
+
+    subprocess.check_call(["sss_override", "user-add", "user2@LDAP",
+                           "-u", "10020",
+                           "-g", "20020",
+                           "-n", "ov_user2",
+                           "-c", "Overriden User 2",
+                           "-h", "/home/ov/user2",
+                           "-s", "/bin/ov_user2_shell"])
+
+    # Restart SSSD so the override might take effect
+    restart_sssd()
+
+    return None
+
+def test_user_simple_override(ldap_conn, env_simple_user_override):
+    """Test entries are overriden"""
+
+    ent.assert_passwd_by_name(
+        'ov_user1',
+        dict(name='ov_user1', passwd='*', uid=10010, gid=20010,
+             gecos='Overriden User 1',
+             dir='/home/ov/user1',
+             shell='/bin/ov_user1_shell'))
+
+    ent.assert_passwd_by_name(
+        'user1',
+        dict(name='ov_user1', passwd='*', uid=10010, gid=20010,
+             gecos='Overriden User 1',
+             dir='/home/ov/user1',
+             shell='/bin/ov_user1_shell'))
+
+    ent.assert_passwd_by_name(
+        'ov_user1@LDAP',
+        dict(name='ov_user1', passwd='*', uid=10010, gid=20010,
+             gecos='Overriden User 1',
+             dir='/home/ov/user1',
+             shell='/bin/ov_user1_shell'))
+
+    ent.assert_passwd_by_name(
+        'user1@LDAP',
+        dict(name='ov_user1', passwd='*', uid=10010, gid=20010,
+             gecos='Overriden User 1',
+             dir='/home/ov/user1',
+             shell='/bin/ov_user1_shell'))
+
+    ent.assert_passwd_by_name(
+        'ov_user2@LDAP',
+        dict(name='ov_user2', passwd='*', uid=10020, gid=20020,
+             gecos='Overriden User 2',
+             dir='/home/ov/user2',
+             shell='/bin/ov_user2_shell'))
+
+    ent.assert_passwd_by_name(
+        'user2',
+        dict(name='ov_user2', passwd='*', uid=10020, gid=20020,
+             gecos='Overriden User 2',
+             dir='/home/ov/user2',
+             shell='/bin/ov_user2_shell'))
+
+#
+# Root user override
+#
+
+def assert_root_user_override():
+    ent.assert_passwd_by_name(
+        'user1',
+        dict(name='user1', passwd='*', uid=10001, gid=20001,
+             gecos='User Number 1',
+             dir='/home/user1',
+             shell='/bin/user1_shell'))
+
+    ent.assert_passwd_by_name(
+        'user2',
+        dict(name='user2', passwd='*', uid=10002, gid=20001,
+             gecos='User Number 2',
+             dir='/home/user2',
+             shell='/bin/user2_shell'))
+
+    ent.assert_passwd_by_name(
+        'root',
+        dict(name='root', uid=0, gid=0))
+
+    ent.assert_passwd_by_uid(0, dict(name="root"))
+
+
+@pytest.fixture
+def env_root_user_override(request, ldap_conn):
+
+    prepare_sssd(request, ldap_conn);
+
+    # Add entries
+    ent_list = ldap_ent.List(LDAP_BASE_DN)
+    ent_list.add_user("user1", 10001, 20001,
+                      gecos='User Number 1',
+                      loginShell='/bin/user1_shell',
+                      homeDirectory='/home/user1')
+    ent_list.add_user("user2", 10002, 20001,
+                      gecos='User Number 2',
+                      loginShell='/bin/user2_shell',
+                      homeDirectory='/home/user2')
+
+    create_ldap_fixture(request, ldap_conn, ent_list)
+
+    # Assert
+    assert_root_user_override()
+
+    # Override
+    subprocess.check_call(["sss_override", "user-add", "user1",
+                           "-u", "0",
+                           "-g", "0",
+                           "-n", "ov_user1",
+                           "-c", "Overriden User 1",
+                           "-h", "/home/ov/user1",
+                           "-s", "/bin/ov_user1_shell"])
+
+    subprocess.check_call(["sss_override", "user-add", "user2@LDAP",
+                           "-u", "10020",
+                           "-g", "20020",
+                           "-n", "root",
+                           "-c", "Overriden User 2",
+                           "-h", "/home/ov/user2",
+                           "-s", "/bin/ov_user2_shell"])
+
+    # Restart SSSD so the override might take effect
+    restart_sssd()
+
+    return None
+
+
+def test_user_root_override(ldap_conn, env_root_user_override):
+    """Test entries are not overriden to root"""
+
+    # I have to discuss this with author. I think override should
+    # happen completly or not at all.
+    ent.assert_passwd_by_name(
+        'ov_user1',
+        dict(name='ov_user1', passwd='*', uid=10001, gid=20001,
+             gecos='Overriden User 1',
+             dir='/home/ov/user1',
+             shell='/bin/ov_user1_shell'))
+
+    # We can create override with name root, interesting.
+    ent.assert_passwd_by_name(
+        'user2',
+        dict(name='root', passwd='*', uid=10020, gid=20020,
+             gecos='Overriden User 2',
+             dir='/home/ov/user2',
+             shell='/bin/ov_user2_shell'))
+
+    ent.assert_passwd_by_uid(0, dict(name="root"))
+
+    # result of this command is probably non deterministic as 2 users with name root
+    # can be looked up
+    ent.assert_passwd_by_name(
+        'root',
+        dict(name='root', uid=0, gid=0))
+
+#
+# Override of override
+#
+
+@pytest.fixture
+def env_override_of_override_user_override(request, ldap_conn):
+
+    prepare_sssd(request, ldap_conn);
+
+    # Add entries
+    ent_list = ldap_ent.List(LDAP_BASE_DN)
+    ent_list.add_user("user1", 10001, 20001,
+                      gecos='User Number 1',
+                      loginShell='/bin/user1_shell',
+                      homeDirectory='/home/user1')
+
+    create_ldap_fixture(request, ldap_conn, ent_list)
+
+    # Assert entries are not overriden
+    ent.assert_passwd_by_name(
+        'user1',
+        dict(name='user1', passwd='*', uid=10001, gid=20001,
+             gecos='User Number 1',
+             dir='/home/user1',
+             shell='/bin/user1_shell'))
+
+    # Override
+    subprocess.check_call(["sss_override", "user-add", "user1",
+                           "-u", "10010",
+                           "-g", "20010",
+                           "-n", "ov_user1",
+                           "-c", "Overriden User 1",
+                           "-h", "/home/ov/user1",
+                           "-s", "/bin/ov_user1_shell"])
+
+    # Restart SSSD so the override might take effect
+    restart_sssd()
+
+    # Assert entries are overriden
+    ent.assert_passwd_by_name(
+        'user1',
+        dict(name='ov_user1', passwd='*', uid=10010, gid=20010,
+             gecos='Overriden User 1',
+             dir='/home/ov/user1',
+             shell='/bin/ov_user1_shell'))
+
+    # Override of override
+    subprocess.check_call(["sss_override", "user-add", "user1",
+                           "-u", "10100",
+                           "-g", "20100",
+                           "-n", "ov2_user1",
+                           "-c", "Overriden2 User 1",
+                           "-h", "/home/ov2/user1",
+                           "-s", "/bin/ov2_user1_shell"])
+
+    # Restart SSSD so the override might take effect
+    restart_sssd()
+
+    return None
+
+
+def test_user_override_of_override(ldap_conn, env_override_of_override_user_override):
+
+    ent.assert_passwd_by_name(
+        'ov2_user1',
+        dict(name='ov2_user1', passwd='*', uid=10100, gid=20100,
+             gecos='Overriden2 User 1',
+             dir='/home/ov2/user1',
+             shell='/bin/ov2_user1_shell'))
+
+    with pytest.raises(KeyError):
+        pwd.getpwnam('ov_user1')
+
+
+#
+# End for now
+#
+
+# def test_local_override_user(ldap_conn, test_user_env):
+#     """ test commands: user-add, user-del, user-import, user export """
+
+#     def perform_override_1():
+#         subprocess.check_call(["sss_override", "user-add", "user1",
+#                                "-u", "10010",
+#                                "-g", "20010",
+#                                "-n", "ov_user1",
+#                                "-c", "Overriden User 1",
+#                                "-h", "/home/ov/user1",
+#                                "-s", "/bin/ov_user1_shell"])
+
+#         # fqdn
+#         subprocess.check_call(["sss_override", "user-add", "user2@LDAP",
+#                                "-u", "10020",
+#                                "-g", "20020",
+#                                "-n", "ov_user2",
+#                                "-c", "Overriden User 2",
+#                                "-h", "/home/ov/user2",
+#                                "-s", "/bin/ov_user2_shell"])
+
+#     def check_users_after_override_1():
+
+#         ent.assert_passwd_by_name(
+#             'ov_user1',
+#             dict(name='ov_user1', passwd='*', uid=10010, gid=20010,
+#                  gecos='Overriden User 1',
+#                  dir='/home/ov/user1',
+#                  shell='/bin/ov_user1_shell'))
+
+#         ent.assert_passwd_by_name(
+#             'user1',
+#             dict(name='ov_user1', passwd='*', uid=10010, gid=20010,
+#                  gecos='Overriden User 1',
+#                  dir='/home/ov/user1',
+#                  shell='/bin/ov_user1_shell'))
+#         # fqdn
+#         ent.assert_passwd_by_name(
+#             'ov_user1@LDAP',
+#             dict(name='ov_user1', passwd='*', uid=10010, gid=20010,
+#                  gecos='Overriden User 1',
+#                  dir='/home/ov/user1',
+#                  shell='/bin/ov_user1_shell'))
+
+#         # fqdn
+#         ent.assert_passwd_by_name(
+#             'user1@LDAP',
+#             dict(name='ov_user1', passwd='*', uid=10010, gid=20010,
+#                  gecos='Overriden User 1',
+#                  dir='/home/ov/user1',
+#                  shell='/bin/ov_user1_shell'))
+
+#         # fqdn
+#         ent.assert_passwd_by_name(
+#             'ov_user2@LDAP',
+#             dict(name='ov_user2', passwd='*', uid=10020, gid=20020,
+#                  gecos='Overriden User 2',
+#                  dir='/home/ov/user2',
+#                  shell='/bin/ov_user2_shell'))
+
+#         ent.assert_passwd_by_name(
+#             'user2',
+#             dict(name='ov_user2', passwd='*', uid=10020, gid=20020,
+#                  gecos='Overriden User 2',
+#                  dir='/home/ov/user2',
+#                  shell='/bin/ov_user2_shell'))
+
+#     def subtest_override_override():
+#         subprocess.check_call(["sss_override", "user-add", "user1",
+#                                "-c", "Override of Overriden User 1"])
+
+#         restart_sssd()
+
+#         # user1 got restored to initial status (state in ldap) except for gecos
+#         ent.assert_passwd_by_name(
+#             'user1',
+#             dict(name='user1', passwd='*', uid=10001, gid=20001,
+#                  gecos='Override of Overriden User 1',
+#                  shell='/bin/user1_shell',
+#                  dir='/home/user1'))
+
+#         ent.assert_passwd_by_name(
+#             'user1@LDAP',
+#             dict(name='user1', passwd='*', uid=10001, gid=20001,
+#                  gecos='Override of Overriden User 1',
+#                  shell='/bin/user1_shell',
+#                  dir='/home/user1'))
+
+#         # user1's name override to ov_user1 was removed
+#         with pytest.raises(KeyError):
+#             pwd.getpwnam('ov_user1')
+
+#     def subtest_override_to_root():
+#         subprocess.check_call(["sss_override", "user-add", "user1",
+#                                "-u", "0"])
+
+#         restart_sssd()
+
+#         # nothing changed
+#         ent.assert_passwd_by_name(
+#             'user1@LDAP',
+#             dict(name='user1', passwd='*', uid=10001, gid=20001,
+#                  gecos='Override of Overriden User 1',
+#                  shell='/bin/user1_shell',
+#                  dir='/home/user1'))
+
+#     def drop_all_overrides():
+#         subprocess.check_call(["sss_override", "user-del", "user1"])
+#         # fqdn
+#         subprocess.check_call(["sss_override", "user-del", "user2@LDAP"])
+
+#     # end of local functions
+
+#     perform_override_1()
+#     restart_sssd()
+
+#     check_users_after_override_1()
+
+#     # save overrides
+#     subprocess.call(["sss_override", "user-export", override_user_filename])
+
+#     # override already overriden user
+#     subtest_override_override()
+
+#     # depends on outcome of ticket #2833 - commented for now
+#     # subtest_override_to_root()
+
+#     drop_all_overrides()
+#     restart_sssd()
+
+#     check_users_from_ldap()
+
+#     # reload overrides
+#     subprocess.call(["sss_override", "user-import", override_user_filename])
+
+#     restart_sssd()
+
+#     check_users_after_override_1()
+
+
+# def test_local_override_user_cached(ldap_conn, test_user_env):
+
+#     # put object to sysdb cache before perfoming tests
+#     check_users_from_ldap()
+
+#     test_local_override_user(ldap_conn, test_user_env)
-- 
2.4.3

