ldap_local_override_test.py revision 3569ade3eaf9bf13c522d228019da228de55398a
641f68d427629200c29aa62c95e18d46fce434abMark Andrews# integration test for sss_override tool
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence# Copyright (c) 2015 Red Hat, Inc.
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater# Author: Pavel Reichl <preichl@redhat.com>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson# This is free software; you can redistribute it and/or modify it
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence# under the terms of the GNU General Public License as published by
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews# the Free Software Foundation; version 2 only
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews# This program is distributed in the hope that it will be useful, but
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews# WITHOUT ANY WARRANTY; without even the implied warranty of
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews# General Public License for more details.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson# You should have received a copy of the GNU General Public License
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson# along with this program. If not, see <http://www.gnu.org/licenses/>.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence # In python 2.6 , the module subprocess does not have the function
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafsson # check_output. This is a falback implementation
7693d4de8fca501dfe6989a7f30d8d3c86fe096aAndreas Gustafsson raise ValueError('stdout argument not allowed, it will be '
7693d4de8fca501dfe6989a7f30d8d3c86fe096aAndreas Gustafsson 'overridden.')
7693d4de8fca501dfe6989a7f30d8d3c86fe096aAndreas Gustafsson process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs,
7693d4de8fca501dfe6989a7f30d8d3c86fe096aAndreas Gustafsson raise subprocess.CalledProcessError(retcode, cmd, output=output)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence """LDAP server instance fixture"""
7693d4de8fca501dfe6989a7f30d8d3c86fe096aAndreas Gustafsson request.addfinalizer(lambda: ds_inst.teardown())
7693d4de8fca501dfe6989a7f30d8d3c86fe096aAndreas Gustafsson """LDAP server connection fixture"""
7693d4de8fca501dfe6989a7f30d8d3c86fe096aAndreas Gustafsson request.addfinalizer(lambda: ldap_conn.unbind_s())
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafssondef create_ldap_fixture(request, ldap_conn, ent_list):
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafsson """Add LDAP entries and add teardown for removing them"""
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafssondef create_conf_fixture(request, contents):
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafsson """Generate sssd.conf and add teardown for removing it"""
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafsson os.chmod(config.CONF_PATH, stat.S_IRUSR | stat.S_IWUSR)
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafsson request.addfinalizer(lambda: os.unlink(config.CONF_PATH))
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafsson pid_file = open(config.PIDFILE_PATH, "r")
7693d4de8fca501dfe6989a7f30d8d3c86fe096aAndreas Gustafsson """Start sssd"""
7693d4de8fca501dfe6989a7f30d8d3c86fe096aAndreas Gustafsson if subprocess.call(["sssd", "-D", "-f"]) != 0:
7693d4de8fca501dfe6989a7f30d8d3c86fe096aAndreas Gustafsson """Start sssd and add teardown for stopping it and removing state"""
7693d4de8fca501dfe6989a7f30d8d3c86fe096aAndreas Gustafsson if subprocess.call(["sssd", "-D", "-f"]) != 0:
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssondef prepare_sssd(request, ldap_conn, use_fully_qualified_names=False):
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson """Prepare SSSD with defaults"""
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence domains = LDAP
ba7ea2326d98edb4296098749fc9cf44b5157643David Lawrence services = nss
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson memcache_timeout = 1
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson ldap_auth_disable_tls_never_use_in_production = true
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson ldap_schema = rfc2307
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson id_provider = ldap
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson auth_provider = ldap
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson sudo_provider = ldap
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrews ldap_uri = {ldap_conn.ds_inst.ldap_url}
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews ldap_search_base = {ldap_conn.ds_inst.base_dn}
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews use_fully_qualified_names = {use_fully_qualified_names}
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrews # remove user export file
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrews# Common asserts for users
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrews # Assert entries are not overriden
6eccf5bd07eb9abf65cc08fec4a8fc97b62c0e1bBrian Wellington user1 = dict(name='user1', passwd='*', uid=10001, gid=20001,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson user2 = dict(name='user2', passwd='*', uid=10002, gid=20001,
6eccf5bd07eb9abf65cc08fec4a8fc97b62c0e1bBrian Wellington ent.assert_passwd_by_name('user1@LDAP', user1)
6eccf5bd07eb9abf65cc08fec4a8fc97b62c0e1bBrian Wellington ent.assert_passwd_by_name('user2@LDAP', user2)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson user1 = dict(name='ov_user1', passwd='*', uid=10010, gid=20010,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson user2 = dict(name='ov_user2', passwd='*', uid=10020, gid=20020,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ent.assert_passwd_by_name('user1', user1)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ent.assert_passwd_by_name('user1@LDAP', user1)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ent.assert_passwd_by_name('ov_user1', user1)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ent.assert_passwd_by_name('ov_user1@LDAP', user1)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ent.assert_passwd_by_name('user2', user2)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ent.assert_passwd_by_name('user2@LDAP', user2)
ba7ea2326d98edb4296098749fc9cf44b5157643David Lawrence ent.assert_passwd_by_name('ov_user2@LDAP', user2)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson# Common fixtures for users
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssondef env_two_users_and_group(request, ldap_conn):
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ent_list = ldap_ent.List(ldap_conn.ds_inst.base_dn)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson create_ldap_fixture(request, ldap_conn, ent_list)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson # Assert entries are not overriden
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssondef env_two_users_and_group_overriden(request, ldap_conn,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson subprocess.check_call(["sss_override", "user-add", "user1",
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson subprocess.check_call(["sss_override", "user-add", "user2@LDAP",
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson # Restart SSSD so the override might take effect
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence # Assert entries are overriden
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson# Simple user override
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssondef env_simple_user_override(request, ldap_conn, env_two_users_and_group):
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson subprocess.check_call(["sss_override", "user-add", "user1",
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson subprocess.check_call(["sss_override", "user-add", "user2@LDAP",
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson # Restart SSSD so the override might take effect
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssondef test_simple_user_override(ldap_conn, env_simple_user_override):
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson """Test entries are overriden"""
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson# Root user override
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrewsdef env_root_user_override(request, ldap_conn, env_two_users_and_group):
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrews # Assert entries are not overriden
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson ent.assert_passwd_by_uid(0, dict(name="root"))
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson subprocess.check_call(["sss_override", "user-add", "user1",
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews subprocess.check_call(["sss_override", "user-add", "user2",
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson # Restart SSSD so the override might take effect
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssondef test_root_user_override(ldap_conn, env_root_user_override):
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafsson """Test entries are not overriden to root"""
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews # Override does not have to happen completly, trying to set uid or gid
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson # to 0 is simply ignored.
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson dict(name='ov_user1', passwd='*', uid=10001, gid=20001,
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson # We can create override with name root. This test is just for tracking
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson # that this particular behavior won't change.
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson dict(name='root', passwd='*', uid=10020, gid=20020,
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson ent.assert_passwd_by_uid(0, dict(name="root"))
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson# Override replaces previous override
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafssondef env_replace_user_override(request, ldap_conn):
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson ent_list = ldap_ent.List(ldap_conn.ds_inst.base_dn)
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson create_ldap_fixture(request, ldap_conn, ent_list)
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson # Assert entries are not overriden
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson dict(name='user1', passwd='*', uid=10001, gid=20001,
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson subprocess.check_call(["sss_override", "user-add", "user1",
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafsson # Restart SSSD so the override might take effect
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafsson # Assert entries are overriden
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafsson dict(name='ov_user1', passwd='*', uid=10010, gid=20010,
6eccf5bd07eb9abf65cc08fec4a8fc97b62c0e1bBrian Wellington # Override of override
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafsson subprocess.check_call(["sss_override", "user-add", "user1",
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence # Restart SSSD so the override might take effect
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafssondef test_replace_user_override(ldap_conn, env_replace_user_override):
6eccf5bd07eb9abf65cc08fec4a8fc97b62c0e1bBrian Wellington user = dict(name='ov2_user1', passwd='*', uid=10100, gid=20100,
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafsson ent.assert_passwd_by_name('ov2_user1', user)
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafsson ent.assert_passwd_by_name('ov2_user1@LDAP', user)
"/home/ov/user1:/bin/ov_user1_shell\n"
"/home/ov/user2:/bin/ov_user2_shell\n"
'/home/ov/user1:/bin/ov_user1_shell',
'/home/ov/user2:/bin/ov_user2_shell']
def assert_group_overriden():
def assert_group_default():