Blame SOURCES/0114-TESTS-Add-a-basic-SSH-responder-test.patch

75e304
From 612dda4bbd706be9e7c3674e4d0420f9ebd1ea83 Mon Sep 17 00:00:00 2001
75e304
From: Jakub Hrozek <jhrozek@redhat.com>
75e304
Date: Mon, 18 Jun 2018 09:12:13 +0200
75e304
Subject: [PATCH] TESTS: Add a basic SSH responder test
75e304
MIME-Version: 1.0
75e304
Content-Type: text/plain; charset=UTF-8
75e304
Content-Transfer-Encoding: 8bit
75e304
75e304
Adds a basic test that makes sure that a list of SSH public keys can be
75e304
retrieved. This is to make sure we don't break the SSH integration later
75e304
on.
75e304
75e304
Related:
75e304
https://pagure.io/SSSD/sssd/issue/3747
75e304
75e304
Reviewed-by: Fabiano FidĂȘncio <fidencio@redhat.com>
75e304
(cherry picked from commit 804c5b538ad89a1a3897b93f39d716fa50530842)
75e304
---
75e304
 src/tests/intg/Makefile.am        |   1 +
75e304
 src/tests/intg/test_ssh_pubkey.py | 232 ++++++++++++++++++++++++++++++++++++++
75e304
 2 files changed, 233 insertions(+)
75e304
 create mode 100644 src/tests/intg/test_ssh_pubkey.py
75e304
75e304
diff --git a/src/tests/intg/Makefile.am b/src/tests/intg/Makefile.am
75e304
index 209e5a0c73db99b11aa6967f30db23933d21acb0..285ce21f04dddb6388c595470eac8f31bc224a60 100644
75e304
--- a/src/tests/intg/Makefile.am
75e304
+++ b/src/tests/intg/Makefile.am
75e304
@@ -35,6 +35,7 @@ dist_noinst_DATA = \
75e304
     data/ad_data.ldif \
75e304
     data/ad_schema.ldif \
75e304
     test_pysss_nss_idmap.py \
75e304
+    test_ssh_pubkey.py \
75e304
     $(NULL)
75e304
 
75e304
 config.py: config.py.m4
75e304
diff --git a/src/tests/intg/test_ssh_pubkey.py b/src/tests/intg/test_ssh_pubkey.py
75e304
new file mode 100644
75e304
index 0000000000000000000000000000000000000000..fbf55566e341373873057ec4e3af1d7f83202aa7
75e304
--- /dev/null
75e304
+++ b/src/tests/intg/test_ssh_pubkey.py
75e304
@@ -0,0 +1,232 @@
75e304
+#
75e304
+# ssh public key integration test
75e304
+#
75e304
+# Copyright (c) 2018 Red Hat, Inc.
75e304
+#
75e304
+# This is free software; you can redistribute it and/or modify it
75e304
+# under the terms of the GNU General Public License as published by
75e304
+# the Free Software Foundation; version 2 only
75e304
+#
75e304
+# This program is distributed in the hope that it will be useful, but
75e304
+# WITHOUT ANY WARRANTY; without even the implied warranty of
75e304
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
75e304
+# General Public License for more details.
75e304
+#
75e304
+# You should have received a copy of the GNU General Public License
75e304
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
75e304
+#
75e304
+
75e304
+import os
75e304
+import stat
75e304
+import signal
75e304
+import subprocess
75e304
+import time
75e304
+import ldap
75e304
+import ldap.modlist
75e304
+import pytest
75e304
+
75e304
+import config
75e304
+import ds_openldap
75e304
+import ent
75e304
+import ldap_ent
75e304
+from util import unindent, get_call_output
75e304
+
75e304
+LDAP_BASE_DN = "dc=example,dc=com"
75e304
+
75e304
+USER1_PUBKEY1 = "ssh-dss AAAAB3NzaC1kc3MAAACBAPMkvcU53RVhBtjwiC3IqeRIWR9Qwdv8\
75e304
+DmZzEsDD3Csd6jYxMsPZoXcPrHqwYcEj1s5MVqhdSFS0Cjz13e7gO6OMLInO3xMBSSFHjfp9RE1H\
75e304
+pgc4WisazzyJaW9EMkQo/DqvkFkKh31oqAmxcSbLAFJRg4TTIqm18qu8IRKS6m/RAAAAFQC97TA5\
75e304
+JSsMsaX1bRszC7y4PhMBvQAAAIEAt9Yo9v/h9W4nDbzUdkGwNRszlPEK+T12bJv0O9Fk6subD3Do\
75e304
+6A4Qru/Nr6voXoq8b018Wb7iFWvKOoz5uT/plWBKLXL2NN7ovTR+dUJIzvwurQZroukmU1EghNey\
75e304
+lkSHmDlxSoMK6Nh21uGu6l+b6x5pXNaZHMpsywG4kY8SoC0AAACAAWLHneEGvqkYA8La4Eob+Hjj\
75e304
+mAKilx8byxm3Kfb1XO+ZrR6XxadofZOaUYRMpPKgFjKAKPxJftPLiDjWM7lSe6h8df0dUMLVXt6m\
75e304
+eA83kE0uK5JOOGJfJDqmRed2YnfxUDNNFQGT4xFWGrNtYNbGyw9BWKbkooAsLqaO04zP3Rs= \
75e304
+user1@LDAP"
75e304
+
75e304
+USER1_PUBKEY2 = "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAwHUUF3HPH+DkU6j8k7Q1wHG\
75e304
+RJY9NeLqSav3h95mTSCQYPSC7I9RTJ4OORgqCbEzrP/DYrrn4TtQ9dhRJar3ZY+F36SH5yFIXORb\
75e304
+lAIbFU+/anahBuFS9vHi1MqFPckGmwJ4QCpjQhdYxo1ro0e1RuGSaQNp/w9N6S/fDz4Cj4I99xDz\
75e304
+SeQeGHxYv0e60plQ8dUajmnaGmYRJHF9a6Ban7IWySActCja7eQP2zIRXEZMpuhl1E0U4y+gHTFI\
75e304
+gD3zQai3QrXm8RUrQURIJ0u6BlGS910OPbHqLpLTFWG08L8sNUcYzC+DY6yoCSO0n/Df3pVRS4C9\
75e304
+5Krf3FqppMTjdfQ== user1@LDAP"
75e304
+
75e304
+
75e304
+@pytest.fixture(scope="module")
75e304
+def ds_inst(request):
75e304
+    """LDAP server instance fixture"""
75e304
+    ds_inst = ds_openldap.DSOpenLDAP(
75e304
+        config.PREFIX, 10389, LDAP_BASE_DN,
75e304
+        "cn=admin", "Secret123"
75e304
+    )
75e304
+
75e304
+    try:
75e304
+        ds_inst.setup()
75e304
+    except:
75e304
+        ds_inst.teardown()
75e304
+        raise
75e304
+    request.addfinalizer(ds_inst.teardown)
75e304
+    return ds_inst
75e304
+
75e304
+
75e304
+@pytest.fixture(scope="module")
75e304
+def ldap_conn(request, ds_inst):
75e304
+    """LDAP server connection fixture"""
75e304
+    ldap_conn = ds_inst.bind()
75e304
+    ldap_conn.ds_inst = ds_inst
75e304
+    request.addfinalizer(ldap_conn.unbind_s)
75e304
+    return ldap_conn
75e304
+
75e304
+
75e304
+def create_ldap_entries(ldap_conn, ent_list=None):
75e304
+    """Add LDAP entries from ent_list"""
75e304
+    if ent_list is not None:
75e304
+        for entry in ent_list:
75e304
+            ldap_conn.add_s(entry[0], entry[1])
75e304
+
75e304
+
75e304
+def cleanup_ldap_entries(ldap_conn, ent_list=None):
75e304
+    """Remove LDAP entries added by create_ldap_entries"""
75e304
+    if ent_list is None:
75e304
+        for ou in ("Users", "Groups", "Netgroups", "Services", "Policies"):
75e304
+            for entry in ldap_conn.search_s("ou=" + ou + "," +
75e304
+                                            ldap_conn.ds_inst.base_dn,
75e304
+                                            ldap.SCOPE_ONELEVEL,
75e304
+                                            attrlist=[]):
75e304
+                ldap_conn.delete_s(entry[0])
75e304
+    else:
75e304
+        for entry in ent_list:
75e304
+            ldap_conn.delete_s(entry[0])
75e304
+
75e304
+
75e304
+def create_ldap_cleanup(request, ldap_conn, ent_list=None):
75e304
+    """Add teardown for removing all user/group LDAP entries"""
75e304
+    request.addfinalizer(lambda: cleanup_ldap_entries(ldap_conn, ent_list))
75e304
+
75e304
+
75e304
+def create_ldap_fixture(request, ldap_conn, ent_list=None):
75e304
+    """Add LDAP entries and add teardown for removing them"""
75e304
+    create_ldap_entries(ldap_conn, ent_list)
75e304
+    create_ldap_cleanup(request, ldap_conn, ent_list)
75e304
+
75e304
+
75e304
+SCHEMA_RFC2307_BIS = "rfc2307bis"
75e304
+
75e304
+
75e304
+def format_basic_conf(ldap_conn, schema):
75e304
+    """Format a basic SSSD configuration"""
75e304
+    schema_conf = "ldap_schema         = " + schema + "\n"
75e304
+    schema_conf += "ldap_group_object_class = groupOfNames\n"
75e304
+    return unindent("""\
75e304
+        [sssd]
75e304
+        domains             = LDAP
75e304
+        services            = nss, ssh
75e304
+
75e304
+        [nss]
75e304
+
75e304
+        [ssh]
75e304
+        debug_level=10
75e304
+
75e304
+        [domain/LDAP]
75e304
+        {schema_conf}
75e304
+        id_provider         = ldap
75e304
+        auth_provider       = ldap
75e304
+        ldap_uri            = {ldap_conn.ds_inst.ldap_url}
75e304
+        ldap_search_base    = {ldap_conn.ds_inst.base_dn}
75e304
+        ldap_sudo_use_host_filter = false
75e304
+        debug_level=10
75e304
+    """).format(**locals())
75e304
+
75e304
+
75e304
+def create_conf_file(contents):
75e304
+    """Create sssd.conf with specified contents"""
75e304
+    conf = open(config.CONF_PATH, "w")
75e304
+    conf.write(contents)
75e304
+    conf.close()
75e304
+    os.chmod(config.CONF_PATH, stat.S_IRUSR | stat.S_IWUSR)
75e304
+
75e304
+
75e304
+def cleanup_conf_file():
75e304
+    """Remove sssd.conf, if it exists"""
75e304
+    if os.path.lexists(config.CONF_PATH):
75e304
+        os.unlink(config.CONF_PATH)
75e304
+
75e304
+
75e304
+def create_conf_cleanup(request):
75e304
+    """Add teardown for removing sssd.conf"""
75e304
+    request.addfinalizer(cleanup_conf_file)
75e304
+
75e304
+
75e304
+def create_conf_fixture(request, contents):
75e304
+    """
75e304
+    Create sssd.conf with specified contents and add teardown for removing it
75e304
+    """
75e304
+    create_conf_file(contents)
75e304
+    create_conf_cleanup(request)
75e304
+
75e304
+
75e304
+def create_sssd_process():
75e304
+    """Start the SSSD process"""
75e304
+    if subprocess.call(["sssd", "-D", "-f"]) != 0:
75e304
+        raise Exception("sssd start failed")
75e304
+
75e304
+
75e304
+def get_sssd_pid():
75e304
+    pid_file = open(config.PIDFILE_PATH, "r")
75e304
+    pid = int(pid_file.read())
75e304
+    return pid
75e304
+
75e304
+
75e304
+def cleanup_sssd_process():
75e304
+    """Stop the SSSD process and remove its state"""
75e304
+    try:
75e304
+        pid = get_sssd_pid()
75e304
+        os.kill(pid, signal.SIGTERM)
75e304
+        while True:
75e304
+            try:
75e304
+                os.kill(pid, signal.SIGCONT)
75e304
+            except:
75e304
+                break
75e304
+            time.sleep(1)
75e304
+    except:
75e304
+        pass
75e304
+    for path in os.listdir(config.DB_PATH):
75e304
+        os.unlink(config.DB_PATH + "/" + path)
75e304
+    for path in os.listdir(config.MCACHE_PATH):
75e304
+        os.unlink(config.MCACHE_PATH + "/" + path)
75e304
+
75e304
+
75e304
+def create_sssd_fixture(request):
75e304
+    """Start SSSD and add teardown for stopping it and removing its state"""
75e304
+    create_sssd_process()
75e304
+    create_sssd_cleanup(request)
75e304
+
75e304
+
75e304
+def create_sssd_cleanup(request):
75e304
+    """Add teardown for stopping SSSD and removing its state"""
75e304
+    request.addfinalizer(cleanup_sssd_process)
75e304
+
75e304
+
75e304
+@pytest.fixture
75e304
+def add_user_with_ssh_key(request, ldap_conn):
75e304
+    ent_list = ldap_ent.List(ldap_conn.ds_inst.base_dn)
75e304
+    ent_list.add_user("user1", 1001, 2001,
75e304
+                      sshPubKey=(USER1_PUBKEY1, USER1_PUBKEY2))
75e304
+    ent_list.add_user("user2", 1002, 2001)
75e304
+    create_ldap_fixture(request, ldap_conn, ent_list)
75e304
+
75e304
+    conf = format_basic_conf(ldap_conn, SCHEMA_RFC2307_BIS)
75e304
+    create_conf_fixture(request, conf)
75e304
+    create_sssd_fixture(request)
75e304
+    return None
75e304
+
75e304
+
75e304
+def test_ssh_pubkey_retrieve(add_user_with_ssh_key):
75e304
+    """
75e304
+    Test that we can retrieve an SSH public key for a user who has one
75e304
+    and can't retrieve a key for a user who does not have one.
75e304
+    """
75e304
+    sshpubkey = get_call_output(["sss_ssh_authorizedkeys", "user1"])
75e304
+    assert sshpubkey == USER1_PUBKEY1 + '\n' + USER1_PUBKEY2 + '\n'
75e304
+
75e304
+    sshpubkey = get_call_output(["sss_ssh_authorizedkeys", "user2"])
75e304
+    assert len(sshpubkey) == 0
75e304
-- 
75e304
2.14.4
75e304