Blame SOURCES/tests-add-test-for-gss_localname.patch

bc66de
From 0dbf450a49784e2a750c667824e0e0249be575e4 Mon Sep 17 00:00:00 2001
bc66de
From: rpm-build <rpm-build>
bc66de
Date: Wed, 27 Apr 2022 18:18:22 +0200
bc66de
Subject: [PATCH] Add test for gss_localname
bc66de
bc66de
Backport test for gss_localname implemented upstream by Simo
bc66de
---
bc66de
 tests/httpd.conf     | 13 ++++++++++
bc66de
 tests/localname.html |  1 +
bc66de
 tests/magtests.py    | 47 ++++++++++++++++++++++++++++++++-
bc66de
 tests/t_localname.py | 62 ++++++++++++++++++++++++++++++++++++++++++++
bc66de
 4 files changed, 122 insertions(+), 1 deletion(-)
bc66de
 create mode 100644 tests/localname.html
bc66de
 create mode 100755 tests/t_localname.py
bc66de
bc66de
diff --git a/tests/httpd.conf b/tests/httpd.conf
bc66de
index f76f2b671e02515e6d4effe09ab123dace90c023..b3777574d9f0547560f24eff992fc1018569b5cc 100644
bc66de
--- a/tests/httpd.conf
bc66de
+++ b/tests/httpd.conf
bc66de
@@ -274,6 +274,19 @@ CoreDumpDirectory "{HTTPROOT}"
bc66de
   Require valid-user
bc66de
 </Location>
bc66de
 
bc66de
+<Location /gss_localname>
bc66de
+  AuthType GSSAPI
bc66de
+  AuthName "Login"
bc66de
+  GssapiSSLonly Off
bc66de
+  GssapiCredStore ccache:{HTTPROOT}/tmp/httpd_krb5_ccache
bc66de
+  GssapiCredStore client_keytab:{HTTPROOT}/http.keytab
bc66de
+  GssapiCredStore keytab:{HTTPROOT}/http.keytab
bc66de
+  GssapiBasicAuth Off
bc66de
+  GssapiAllowedMech krb5
bc66de
+  GssapiLocalName On
bc66de
+  Require valid-user
bc66de
+</Location>
bc66de
+
bc66de
 <VirtualHost *:{PROXYPORT}>
bc66de
   ProxyRequests On
bc66de
   ProxyVia On
bc66de
diff --git a/tests/localname.html b/tests/localname.html
bc66de
new file mode 100644
bc66de
index 0000000000000000000000000000000000000000..abf7c507de1eb32b31b882502eed5f2bbcc5fbf3
bc66de
--- /dev/null
bc66de
+++ b/tests/localname.html
bc66de
@@ -0,0 +1 @@
bc66de
+
bc66de
diff --git a/tests/magtests.py b/tests/magtests.py
bc66de
index d0f0a67f075c6b631926e9abd91a665973d90f4a..d100413b371e7ecf4e09d944b7ff6e9bec7e316f 100755
bc66de
--- a/tests/magtests.py
bc66de
+++ b/tests/magtests.py
bc66de
@@ -58,12 +58,20 @@ def setup_wrappers(base):
bc66de
         f.write('%s %s\n' % (WRAP_IPADDR, WRAP_ALIASNAME))
bc66de
         f.write('%s %s\n' % (WRAP_IPADDR, WRAP_FAILNAME))
bc66de
 
bc66de
+    passwd_file = os.path.join(testdir, 'passwd')
bc66de
+    with open(passwd_file, 'w+') as f:
bc66de
+        f.write('root:x:0:0:root:/root:/bin/sh')
bc66de
+        f.write('maguser:x:1:1:maguser:/maguser:/bin/sh')
bc66de
+        f.write('maguser2:x:2:2:maguser2:/maguser2:/bin/sh')
bc66de
+        f.write('maguser3:x:3:3:maguser3:/maguser3:/bin/sh')
bc66de
+
bc66de
     wenv = {'LD_PRELOAD': 'libsocket_wrapper.so libnss_wrapper.so',
bc66de
             'SOCKET_WRAPPER_DIR': wrapdir,
bc66de
             'SOCKET_WRAPPER_DEFAULT_IFACE': '9',
bc66de
             'WRAP_PROXY_PORT': WRAP_PROXY_PORT,
bc66de
             'NSS_WRAPPER_HOSTNAME': WRAP_HOSTNAME,
bc66de
-            'NSS_WRAPPER_HOSTS': hosts_file}
bc66de
+            'NSS_WRAPPER_HOSTS': hosts_file,
bc66de
+            'NSS_WRAPPER_PASSWD': passwd_file}
bc66de
     return wenv
bc66de
 
bc66de
 
bc66de
@@ -744,6 +752,40 @@ def http_restart(testdir, so_dir, testenv):
bc66de
     return httpproc
bc66de
 
bc66de
 
bc66de
+def test_gss_localname(testdir, testenv, logfile):
bc66de
+    hdir = os.path.join(testdir, 'httpd', 'html', 'gss_localname')
bc66de
+    os.mkdir(hdir)
bc66de
+    shutil.copy('tests/localname.html', os.path.join(hdir, 'index.html'))
bc66de
+    error_count = 0
bc66de
+
bc66de
+    # Make sure spnego is explicitly tested
bc66de
+    spnego = subprocess.Popen(["tests/t_localname.py", "SPNEGO"],
bc66de
+                              stdout=logfile, stderr=logfile,
bc66de
+                              env=testenv, preexec_fn=os.setsid)
bc66de
+    spnego.wait()
bc66de
+    if spnego.returncode != 0:
bc66de
+        sys.stderr.write('LOCALNAME(SPNEGO): FAILED\n')
bc66de
+        error_count += 1
bc66de
+    else:
bc66de
+        sys.stderr.write('LOCALNAME(SPNEGO): SUCCESS\n')
bc66de
+
bc66de
+    # and bare krb5 (GS2-KRB5 is the name used by SASL for it)
bc66de
+    krb5 = subprocess.Popen(["tests/t_localname.py", "GS2-KRB5"],
bc66de
+                            stdout=logfile, stderr=logfile,
bc66de
+                            env=testenv, preexec_fn=os.setsid)
bc66de
+    krb5.wait()
bc66de
+    if krb5.returncode != 0:
bc66de
+        if krb5.returncode == 42:
bc66de
+            sys.stderr.write('LOCALNAME(KRB5): SKIPPED\n')
bc66de
+        else:
bc66de
+            sys.stderr.write('LOCALNAME(KRB5): FAILED\n')
bc66de
+            error_count += 1
bc66de
+    else:
bc66de
+        sys.stderr.write('LOCALNAME(KRB5): SUCCESS\n')
bc66de
+
bc66de
+    return error_count
bc66de
+
bc66de
+
bc66de
 if __name__ == '__main__':
bc66de
     args = parse_args()
bc66de
 
bc66de
@@ -781,6 +823,9 @@ if __name__ == '__main__':
bc66de
 
bc66de
         errs += test_bad_acceptor_name(testdir, testenv, logfile)
bc66de
 
bc66de
+        testenv['MAG_REMOTE_USER'] = USR_NAME
bc66de
+        errs += test_gss_localname(testdir, testenv, logfile)
bc66de
+
bc66de
         rpm_path = "/usr/lib64/krb5/plugins/preauth/pkinit.so"
bc66de
         deb_path = "/usr/lib/x86_64-linux-gnu/krb5/plugins/preauth/pkinit.so"
bc66de
         if os.path.exists(rpm_path) or os.path.exists(deb_path):
bc66de
diff --git a/tests/t_localname.py b/tests/t_localname.py
bc66de
new file mode 100755
bc66de
index 0000000000000000000000000000000000000000..e990762c42aa9b370ac71292b5019fc63622c240
bc66de
--- /dev/null
bc66de
+++ b/tests/t_localname.py
bc66de
@@ -0,0 +1,62 @@
bc66de
+#!/usr/bin/env python3
bc66de
+# Copyright (C) 2020 - mod_auth_gssapi contributors, see COPYING for license.
bc66de
+
bc66de
+import os
bc66de
+import subprocess
bc66de
+import sys
bc66de
+
bc66de
+import gssapi
bc66de
+
bc66de
+import requests
bc66de
+
bc66de
+from requests_gssapi import HTTPSPNEGOAuth
bc66de
+
bc66de
+
bc66de
+def use_requests(auth):
bc66de
+    sess = requests.Session()
bc66de
+    url = 'http://%s/gss_localname/' % os.environ['NSS_WRAPPER_HOSTNAME']
bc66de
+
bc66de
+    r = sess.get(url, auth=auth)
bc66de
+    if r.status_code != 200:
bc66de
+        raise ValueError('Localname failed')
bc66de
+
bc66de
+    if r.text.rstrip() != os.environ['MAG_REMOTE_USER']:
bc66de
+        raise ValueError('Localname, REMOTE_USER check failed')
bc66de
+
bc66de
+
bc66de
+def use_curl():
bc66de
+    url = 'http://%s/gss_localname/' % os.environ['NSS_WRAPPER_HOSTNAME']
bc66de
+    curl = subprocess.Popen(["curl", "--negotiate", "-u:", url],
bc66de
+                            stdout=subprocess.PIPE)
bc66de
+    curl.wait()
bc66de
+    if curl.returncode != 0:
bc66de
+        raise ValueError('Localname failed')
bc66de
+
bc66de
+    line = curl.stdout.read().strip(b' \t\n\r').decode('utf-8')
bc66de
+    if line != os.environ['MAG_REMOTE_USER']:
bc66de
+        raise ValueError('Localname, REMOTE_USER check failed (%s != %s)' % (
bc66de
+                         line, os.environ['MAG_REMOTE_USER']))
bc66de
+
bc66de
+
bc66de
+if __name__ == '__main__':
bc66de
+    mech_name = None
bc66de
+    if len(sys.argv) > 1:
bc66de
+        mech_name = sys.argv[1]
bc66de
+
bc66de
+    mech = None
bc66de
+    if mech_name is not None:
bc66de
+        mech = gssapi.mechs.Mechanism.from_sasl_name(mech_name)
bc66de
+
bc66de
+    try:
bc66de
+        auth = HTTPSPNEGOAuth(mech=mech)
bc66de
+        use_requests(auth)
bc66de
+    except TypeError:
bc66de
+        # odler version of requests that does not support mechs
bc66de
+        if mech_name == 'SPNEGO':
bc66de
+            use_curl()
bc66de
+        elif mech_name == 'GS2-KRB5':
bc66de
+            # older request versions use krb5 as the mech by default
bc66de
+            auth = HTTPSPNEGOAuth()
bc66de
+            use_requests(auth)
bc66de
+        else:
bc66de
+            sys.exit(42)  # SKIP
bc66de
-- 
bc66de
2.35.1
bc66de