2 # Copyright (C) 2015 - mod_auth_gssapi contributors, see COPYING for license.
10 from string import Template
17 parser = argparse.ArgumentParser(description='Mod Auth GSSAPI Tests Environment')
18 parser.add_argument('--path', default='%s/scratchdir' % os.getcwd(),
19 help="Directory in which tests are run")
21 return vars(parser.parse_args())
24 WRAP_HOSTNAME = "kdc.mag.dev"
25 WRAP_IPADDR = '127.0.0.9'
27 def setup_wrappers(base):
29 pkgcfg = subprocess.Popen(['pkg-config', '--exists', 'socket_wrapper'])
31 if pkgcfg.returncode != 0:
32 raise ValueError('Socket Wrappers not available')
34 pkgcfg = subprocess.Popen(['pkg-config', '--exists', 'nss_wrapper'])
36 if pkgcfg.returncode != 0:
37 raise ValueError('Socket Wrappers not available')
39 wrapdir = os.path.join(base, 'wrapdir')
40 if not os.path.exists(wrapdir):
43 hosts_file = os.path.join(testdir, 'hosts')
44 with open(hosts_file, 'w+') as f:
45 f.write('%s %s' % (WRAP_IPADDR, WRAP_HOSTNAME))
47 wenv = {'LD_PRELOAD': 'libsocket_wrapper.so libnss_wrapper.so',
48 'SOCKET_WRAPPER_DIR': wrapdir,
49 'SOCKET_WRAPPER_DEFAULT_IFACE': '9',
50 'NSS_WRAPPER_HOSTNAME': WRAP_HOSTNAME,
51 'NSS_WRAPPER_HOSTS': hosts_file}
57 KDC_DBNAME = 'db.file'
58 KDC_STASH = 'stash.file'
59 KDC_PASSWORD = 'modauthgssapi'
60 KRB5_CONF_TEMPLATE = '''
62 default_realm = ${TESTREALM}
63 dns_lookup_realm = false
64 dns_lookup_kdc = false
68 default_ccache_name = FILE://${TESTDIR}/ccaches/krb5_ccache_XXXXXX
81 database_name = ${KDCDIR}/${KDC_DBNAME}
84 KDC_CONF_TEMPLATE = '''
88 restrict_anonymous_to_tgt = true
92 master_key_type = aes256-cts
94 max_renewable_life = 14d
95 acl_file = ${KDCDIR}/kadm5.acl
96 dict_file = /usr/share/dict/words
97 default_principal_flags = +preauth
98 admin_keytab = ${TESTREALM}/kadm5.keytab
99 key_stash_file = ${KDCDIR}/${KDC_STASH}
106 def setup_kdc(testdir, wrapenv):
108 # setup kerberos environment
109 testlog = os.path.join(testdir, 'kerb.log')
110 krb5conf = os.path.join(testdir, 'krb5.conf')
111 kdcconf = os.path.join(testdir, 'kdc.conf')
112 kdcdir = os.path.join(testdir, 'kdc')
113 kdcstash = os.path.join(kdcdir, KDC_STASH)
114 kdcdb = os.path.join(kdcdir, KDC_DBNAME)
115 if os.path.exists(kdcdir):
116 shutil.rmtree(kdcdir)
119 t = Template(KRB5_CONF_TEMPLATE)
120 text = t.substitute({'TESTREALM': TESTREALM,
123 'KDC_DBNAME': KDC_DBNAME,
124 'WRAP_HOSTNAME': WRAP_HOSTNAME})
125 with open(krb5conf, 'w+') as f:
128 t = Template(KDC_CONF_TEMPLATE)
129 text = t.substitute({'TESTREALM': TESTREALM,
132 'KDC_STASH': KDC_STASH})
133 with open(kdcconf, 'w+') as f:
136 kdcenv = {'PATH': '/sbin:/bin:/usr/sbin:/usr/bin',
137 'KRB5_CONFIG': krb5conf,
138 'KRB5_KDC_PROFILE': kdcconf,
139 'KRB5_TRACE': os.path.join(testdir, 'krbtrace.log')}
140 kdcenv.update(wrapenv)
142 with (open(testlog, 'a')) as logfile:
143 ksetup = subprocess.Popen(["kdb5_util", "create", "-s",
144 "-r", TESTREALM, "-P", KDC_PASSWORD],
145 stdout=logfile, stderr=logfile,
146 env=kdcenv, preexec_fn=os.setsid)
148 if ksetup.returncode != 0:
149 raise ValueError('KDC Setup failed')
151 with (open(testlog, 'a')) as logfile:
152 kdcproc = subprocess.Popen(['krb5kdc', '-n'],
153 stdout=logfile, stderr=logfile,
154 env=kdcenv, preexec_fn=os.setsid)
156 return kdcproc, kdcenv
159 def kadmin_local(cmd, env, logfile):
160 ksetup = subprocess.Popen(["kadmin.local", "-q", cmd],
161 stdout=logfile, stderr=logfile,
162 env=env, preexec_fn=os.setsid)
164 if ksetup.returncode != 0:
165 raise ValueError('Kadmin local [%s] failed' % cmd)
170 SVC_KTNAME = "httpd/http.keytab"
171 KEY_TYPE = "aes256-cts-hmac-sha1-96:normal"
174 def setup_keys(tesdir, env):
176 testlog = os.path.join(testdir, 'kerb.log')
178 svc_name = "HTTP/%s" % WRAP_HOSTNAME
179 svc_keytab = os.path.join(testdir, SVC_KTNAME)
180 cmd = "addprinc -randkey -e %s %s" % (KEY_TYPE, svc_name)
181 with (open(testlog, 'a')) as logfile:
182 kadmin_local(cmd, env, logfile)
183 cmd = "ktadd -k %s -e %s %s" % (svc_keytab, KEY_TYPE, svc_name)
184 with (open(testlog, 'a')) as logfile:
185 kadmin_local(cmd, env, logfile)
187 cmd = "addprinc -pw %s -e %s %s" % (USR_PWD, KEY_TYPE, USR_NAME)
188 with (open(testlog, 'a')) as logfile:
189 kadmin_local(cmd, env, logfile)
191 keys_env = { "KRB5_KTNAME": svc_keytab }
197 def setup_http(testdir, wrapenv):
199 httpdir = os.path.join(testdir, 'httpd')
200 if os.path.exists(httpdir):
201 shutil.rmtree(httpdir)
203 os.mkdir(os.path.join(httpdir, 'conf.d'))
204 os.mkdir(os.path.join(httpdir, 'html'))
205 os.mkdir(os.path.join(httpdir, 'logs'))
206 os.symlink('/etc/httpd/modules', os.path.join(httpdir, 'modules'))
208 shutil.copy('src/.libs/mod_auth_gssapi.so', httpdir)
210 with open('tests/httpd.conf') as f:
211 t = Template(f.read())
212 text = t.substitute({'HTTPROOT': httpdir,
213 'HTTPNAME': WRAP_HOSTNAME,
214 'HTTPADDR': WRAP_IPADDR,
216 config = os.path.join(httpdir, 'httpd.conf')
217 with open(config, 'w+') as f:
220 httpenv = {'PATH': '/sbin:/bin:/usr/sbin:/usr/bin',
221 'MALLOC_CHECK_': '3',
222 'MALLOC_PERTURB_': str(random.randint(0, 32767) % 255 + 1)}
223 httpenv.update(wrapenv)
225 httpproc = subprocess.Popen(['httpd', '-DFOREGROUND', '-f', config],
226 env=httpenv, preexec_fn=os.setsid)
231 def kinit_user(testdir, kdcenv):
232 testlog = os.path.join(testdir, 'kinit.log')
233 ccache = os.path.join(testdir, 'k5ccache')
234 testenv = {'KRB5CCNAME': ccache}
235 testenv.update(kdcenv)
237 with (open(testlog, 'a')) as logfile:
238 kinit = subprocess.Popen(["kinit", USR_NAME],
239 stdin=subprocess.PIPE,
240 stdout=logfile, stderr=logfile,
241 env=testenv, preexec_fn=os.setsid)
242 kinit.communicate('%s\n' % USR_PWD)
244 if kinit.returncode != 0:
245 raise ValueError('kinit failed')
250 def test_spnego_auth(testdir, testenv, testlog):
252 spnegodir = os.path.join(testdir, 'httpd', 'html', 'spnego')
254 shutil.copy('tests/index.html', spnegodir)
256 with (open(testlog, 'a')) as logfile:
257 spnego = subprocess.Popen(["tests/t_spnego.py"],
258 stdout=logfile, stderr=logfile,
259 env=testenv, preexec_fn=os.setsid)
261 if spnego.returncode != 0:
262 sys.stderr.write('SPNEGO: FAILED\n')
264 sys.stderr.write('SPNEGO: SUCCESS\n')
267 def test_basic_auth_krb5(testdir, testenv, testlog):
269 basicdir = os.path.join(testdir, 'httpd', 'html', 'basic_auth_krb5')
271 shutil.copy('tests/index.html', basicdir)
273 with (open(testlog, 'a')) as logfile:
274 basick5 = subprocess.Popen(["tests/t_basic_k5.py"],
275 stdout=logfile, stderr=logfile,
276 env=testenv, preexec_fn=os.setsid)
278 if basick5.returncode != 0:
279 sys.stderr.write('BASIC-AUTH: FAILED\n')
281 sys.stderr.write('BASIC-AUTH: SUCCESS\n')
284 if __name__ == '__main__':
288 testdir = args['path']
289 if os.path.exists(testdir):
290 shutil.rmtree(testdir)
295 testlog = os.path.join(testdir, 'tests.log')
298 wrapenv = setup_wrappers(testdir)
300 kdcproc, kdcenv = setup_kdc(testdir, wrapenv)
301 processes['KDC(%d)' % kdcproc.pid] = kdcproc
303 httpproc = setup_http(testdir, kdcenv)
304 processes['HTTPD(%d)' % httpproc.pid] = httpproc
306 keysenv = setup_keys(testdir, kdcenv)
307 testenv = kinit_user(testdir, kdcenv)
309 test_spnego_auth(testdir, testenv, testlog)
312 testenv = {'MAG_USER_NAME': USR_NAME,
313 'MAG_USER_PASSWORD': USR_PWD}
314 testenv.update(kdcenv)
315 test_basic_auth_krb5(testdir, testenv, testlog)
318 with (open(testlog, 'a')) as logfile:
319 for name in processes:
320 logfile.write("Killing %s\n" % name)
321 os.killpg(processes[name].pid, signal.SIGTERM)