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 kdcenv.update(wrapenv)
141 with (open(testlog, 'a')) as logfile:
142 ksetup = subprocess.Popen(["kdb5_util", "create", "-s",
143 "-r", TESTREALM, "-P", KDC_PASSWORD],
144 stdout=logfile, stderr=logfile,
145 env=kdcenv, preexec_fn=os.setsid)
147 if ksetup.returncode != 0:
148 raise ValueError('KDC Setup failed')
150 with (open(testlog, 'a')) as logfile:
151 kdcproc = subprocess.Popen(['krb5kdc', '-n'],
152 stdout=logfile, stderr=logfile,
153 env=kdcenv, preexec_fn=os.setsid)
155 return kdcproc, kdcenv
158 def kadmin_local(cmd, env, logfile):
159 ksetup = subprocess.Popen(["kadmin.local", "-q", cmd],
160 stdout=logfile, stderr=logfile,
161 env=env, preexec_fn=os.setsid)
163 if ksetup.returncode != 0:
164 raise ValueError('Kadmin local [%s] failed' % cmd)
169 SVC_KTNAME = "httpd/http.keytab"
170 KEY_TYPE = "aes256-cts-hmac-sha1-96:normal"
173 def setup_keys(tesdir, env):
175 testlog = os.path.join(testdir, 'kerb.log')
177 svc_name = "HTTP/%s" % WRAP_HOSTNAME
178 svc_keytab = os.path.join(testdir, SVC_KTNAME)
179 cmd = "addprinc -randkey -e %s %s" % (KEY_TYPE, svc_name)
180 with (open(testlog, 'a')) as logfile:
181 kadmin_local(cmd, env, logfile)
182 cmd = "ktadd -k %s -e %s %s" % (svc_keytab, KEY_TYPE, svc_name)
183 with (open(testlog, 'a')) as logfile:
184 kadmin_local(cmd, env, logfile)
186 cmd = "addprinc -pw %s -e %s %s" % (USR_PWD, KEY_TYPE, USR_NAME)
187 with (open(testlog, 'a')) as logfile:
188 kadmin_local(cmd, env, logfile)
190 keys_env = { "KRB5_KTNAME": svc_keytab }
196 def setup_http(testdir, wrapenv):
198 httpdir = os.path.join(testdir, 'httpd')
199 if os.path.exists(httpdir):
200 shutil.rmtree(httpdir)
202 os.mkdir(os.path.join(httpdir, 'conf.d'))
203 os.mkdir(os.path.join(httpdir, 'html'))
204 os.mkdir(os.path.join(httpdir, 'logs'))
205 os.symlink('/etc/httpd/modules', os.path.join(httpdir, 'modules'))
207 shutil.copy('src/.libs/mod_auth_gssapi.so', httpdir)
209 with open('tests/httpd.conf') as f:
210 t = Template(f.read())
211 text = t.substitute({'HTTPROOT': httpdir,
212 'HTTPNAME': WRAP_HOSTNAME,
213 'HTTPADDR': WRAP_IPADDR,
215 config = os.path.join(httpdir, 'httpd.conf')
216 with open(config, 'w+') as f:
219 httpenv = {'PATH': '/sbin:/bin:/usr/sbin:/usr/bin',
220 'MALLOC_CHECK_': '3',
221 'MALLOC_PERTURB_': str(random.randint(0, 32767) % 255 + 1)}
222 httpenv.update(wrapenv)
224 httpproc = subprocess.Popen(['httpd', '-DFOREGROUND', '-f', config],
225 env=httpenv, preexec_fn=os.setsid)
230 def kinit_user(testdir, kdcenv):
231 testlog = os.path.join(testdir, 'kinit.log')
232 ccache = os.path.join(testdir, 'k5ccache')
233 testenv = {'KRB5CCNAME': ccache}
234 testenv.update(kdcenv)
236 with (open(testlog, 'a')) as logfile:
237 kinit = subprocess.Popen(["kinit", USR_NAME],
238 stdin=subprocess.PIPE,
239 stdout=logfile, stderr=logfile,
240 env=testenv, preexec_fn=os.setsid)
241 kinit.communicate('%s\n' % USR_PWD)
243 if kinit.returncode != 0:
244 raise ValueError('kinit failed')
249 def test_spnego_auth(testdir, testenv, testlog):
251 spnegodir = os.path.join(testdir, 'httpd', 'html', 'spnego')
253 shutil.copy('tests/index.html', spnegodir)
255 with (open(testlog, 'a')) as logfile:
256 spnego = subprocess.Popen(["tests/t_spnego.py"],
257 stdout=logfile, stderr=logfile,
258 env=testenv, preexec_fn=os.setsid)
260 if spnego.returncode != 0:
261 sys.stderr.write('SPNEGO: FAILED\n')
263 sys.stderr.write('SPNEGO: SUCCESS\n')
266 if __name__ == '__main__':
270 testdir = args['path']
271 if os.path.exists(testdir):
272 shutil.rmtree(testdir)
277 testlog = os.path.join(testdir, 'tests.log')
280 wrapenv = setup_wrappers(testdir)
282 kdcproc, kdcenv = setup_kdc(testdir, wrapenv)
283 processes['KDC(%d)' % kdcproc.pid] = kdcproc
285 httpproc = setup_http(testdir, wrapenv)
286 processes['HTTPD(%d)' % httpproc.pid] = httpproc
288 keysenv = setup_keys(testdir, kdcenv)
289 testenv = kinit_user(testdir, kdcenv)
291 test_spnego_auth(testdir, testenv, testlog)
294 with (open(testlog, 'a')) as logfile:
295 for name in processes:
296 logfile.write("Killing %s\n" % name)
297 os.killpg(processes[name].pid, signal.SIGTERM)