Skip to content

Commit dca7b51

Browse files
committed
Add test for gss_localname
Signed-off-by: Simo Sorce <[email protected]>
1 parent a47d4d4 commit dca7b51

File tree

5 files changed

+120
-3
lines changed

5 files changed

+120
-3
lines changed

.travis.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if [ -f /etc/debian_version ]; then
99
apt-get -y install $COMPILER pkg-config flake8 virtualenv \
1010
apache2-bin {apache2,libkrb5,libssl,gss-ntlmssp}-dev \
1111
$PYTHON{,-dev,-requests} lib{socket,nss}-wrapper \
12-
flex bison krb5-{kdc,admin-server,pkinit}
12+
flex bison krb5-{kdc,admin-server,pkinit} curl
1313

1414
apt-get -y install $PYTHON-requests-gssapi 2>/dev/null || true
1515

@@ -23,7 +23,7 @@ elif [ -f /etc/redhat-release ]; then
2323
fi
2424

2525
$DY -y install $COMPILER $PYTHON-{gssapi,requests} \
26-
krb5-{server,workstation,pkinit} \
26+
krb5-{server,workstation,pkinit} curl \
2727
{httpd,krb5,openssl,gssntlmssp}-devel {socket,nss}_wrapper \
2828
autoconf automake libtool which bison make $PYTHON \
2929
flex mod_session redhat-rpm-config /usr/bin/virtualenv

tests/httpd.conf

+13
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,19 @@ CoreDumpDirectory "{HTTPROOT}"
274274
Require valid-user
275275
</Location>
276276

277+
<Location /gss_localname>
278+
AuthType GSSAPI
279+
AuthName "Login"
280+
GssapiSSLonly Off
281+
GssapiCredStore ccache:{HTTPROOT}/tmp/httpd_krb5_ccache
282+
GssapiCredStore client_keytab:{HTTPROOT}/http.keytab
283+
GssapiCredStore keytab:{HTTPROOT}/http.keytab
284+
GssapiBasicAuth Off
285+
GssapiAllowedMech krb5
286+
GssapiLocalName On
287+
Require valid-user
288+
</Location>
289+
277290
<VirtualHost *:{PROXYPORT}>
278291
ProxyRequests On
279292
ProxyVia On

tests/localname.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--#echo var="REMOTE_USER" -->

tests/magtests.py

+46-1
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,20 @@ def setup_wrappers(base):
5656
f.write('%s %s\n' % (WRAP_IPADDR, WRAP_ALIASNAME))
5757
f.write('%s %s\n' % (WRAP_IPADDR, WRAP_FAILNAME))
5858

59+
passwd_file = os.path.join(testdir, 'passwd')
60+
with open(passwd_file, 'w+') as f:
61+
f.write('root:x:0:0:root:/root:/bin/sh')
62+
f.write('maguser:x:1:1:maguser:/maguser:/bin/sh')
63+
f.write('maguser2:x:2:2:maguser2:/maguser2:/bin/sh')
64+
f.write('maguser3:x:3:3:maguser3:/maguser3:/bin/sh')
65+
5966
wenv = {'LD_PRELOAD': 'libsocket_wrapper.so libnss_wrapper.so',
6067
'SOCKET_WRAPPER_DIR': wrapdir,
6168
'SOCKET_WRAPPER_DEFAULT_IFACE': '9',
6269
'WRAP_PROXY_PORT': WRAP_PROXY_PORT,
6370
'NSS_WRAPPER_HOSTNAME': WRAP_HOSTNAME,
64-
'NSS_WRAPPER_HOSTS': hosts_file}
71+
'NSS_WRAPPER_HOSTS': hosts_file,
72+
'NSS_WRAPPER_PASSWD': passwd_file}
6573
return wenv
6674

6775

@@ -660,6 +668,40 @@ def test_hostname_acceptor(testdir, testenv, logfile):
660668
return 0
661669

662670

671+
def test_gss_localname(testdir, testenv, logfile):
672+
hdir = os.path.join(testdir, 'httpd', 'html', 'gss_localname')
673+
os.mkdir(hdir)
674+
shutil.copy('tests/localname.html', os.path.join(hdir, 'index.html'))
675+
error_count = 0
676+
677+
# Make sure spnego is explicitly tested
678+
spnego = subprocess.Popen(["tests/t_localname.py", "SPNEGO"],
679+
stdout=logfile, stderr=logfile,
680+
env=testenv, preexec_fn=os.setsid)
681+
spnego.wait()
682+
if spnego.returncode != 0:
683+
sys.stderr.write('LOCALNAME(SPNEGO): FAILED\n')
684+
error_count += 1
685+
else:
686+
sys.stderr.write('LOCALNAME(SPNEGO): SUCCESS\n')
687+
688+
# and bare krb5 (GS2-KRB5 is the name used by SASL for it)
689+
krb5 = subprocess.Popen(["tests/t_localname.py", "GS2-KRB5"],
690+
stdout=logfile, stderr=logfile,
691+
env=testenv, preexec_fn=os.setsid)
692+
krb5.wait()
693+
if krb5.returncode != 0:
694+
if krb5.returncode == 42:
695+
sys.stderr.write('LOCALNAME(KRB5): SKIPPED\n')
696+
else:
697+
sys.stderr.write('LOCALNAME(KRB5): FAILED\n')
698+
error_count += 1
699+
else:
700+
sys.stderr.write('LOCALNAME(KRB5): SUCCESS\n')
701+
702+
return error_count
703+
704+
663705
if __name__ == '__main__':
664706
args = parse_args()
665707

@@ -701,6 +743,9 @@ def test_hostname_acceptor(testdir, testenv, logfile):
701743

702744
errs += test_bad_acceptor_name(testdir, testenv, logfile)
703745

746+
testenv['MAG_REMOTE_USER'] = USR_NAME
747+
errs += test_gss_localname(testdir, testenv, logfile)
748+
704749
rpm_path = "/usr/lib64/krb5/plugins/preauth/pkinit.so"
705750
deb_path = "/usr/lib/x86_64-linux-gnu/krb5/plugins/preauth/pkinit.so"
706751
if os.path.exists(rpm_path) or os.path.exists(deb_path):

tests/t_localname.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
# Copyright (C) 2020 - mod_auth_gssapi contributors, see COPYING for license.
3+
4+
import os
5+
import gssapi
6+
import requests
7+
import subprocess
8+
import sys
9+
from requests_gssapi import HTTPSPNEGOAuth
10+
11+
def use_requests(auth):
12+
sess = requests.Session()
13+
url = 'http://%s/gss_localname/' % os.environ['NSS_WRAPPER_HOSTNAME']
14+
15+
r = sess.get(url, auth=auth)
16+
if r.status_code != 200:
17+
raise ValueError('Localname failed')
18+
19+
if r.text.rstrip() != os.environ['MAG_REMOTE_USER']:
20+
raise ValueError('Localname, REMOTE_USER check failed')
21+
22+
23+
def use_curl():
24+
url = 'http://%s/gss_localname/' % os.environ['NSS_WRAPPER_HOSTNAME']
25+
curl = subprocess.Popen(["curl", "--negotiate", "-u:", url],
26+
stdout=subprocess.PIPE)
27+
curl.wait()
28+
if curl.returncode != 0:
29+
raise ValueError('Localname failed')
30+
31+
line = curl.stdout.read().strip(b' \t\n\r').decode('utf-8')
32+
if line != os.environ['MAG_REMOTE_USER']:
33+
raise ValueError('Localname, REMOTE_USER check failed (%s != %s)' % (
34+
line, os.environ['MAG_REMOTE_USER']))
35+
36+
37+
if __name__ == '__main__':
38+
mech_name = None
39+
if len(sys.argv) > 1:
40+
mech_name = sys.argv[1]
41+
42+
mech=None
43+
if mech_name is not None:
44+
mech = gssapi.mechs.Mechanism.from_sasl_name(mech_name)
45+
46+
try:
47+
auth = HTTPSPNEGOAuth(mech=mech)
48+
use_requests(auth)
49+
except TypeError as e:
50+
# odler version of requests that does not support mechs
51+
if mech_name == 'SPNEGO':
52+
use_curl()
53+
elif mech_name == 'GS2-KRB5':
54+
# older request versions use krb5 as the mech by default
55+
auth = HTTPSPNEGOAuth()
56+
use_requests(auth)
57+
else:
58+
sys.exit(42) # SKIP

0 commit comments

Comments
 (0)