-
Notifications
You must be signed in to change notification settings - Fork 46
/
test_duo_openvpn.py
509 lines (467 loc) · 15.5 KB
/
test_duo_openvpn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
import email.utils
import json
import os
import tempfile
import unittest
from mox3 import mox
import six
import duo_openvpn
def mock_client_factory(mock):
"""
Return a Client-alike that uses a mock instead of an HTTP
connection. Special case: Client.__init__() and set_proxy()
arguments are verified by calling mock.duo_client_init() and
mock.duo_client_set_proxy(), respectively.
"""
class MockClient(duo_openvpn.Client):
def __init__(self, *args, **kwargs):
mock.duo_client_init(*args, **kwargs)
super(MockClient, self).__init__(*args, **kwargs)
def set_proxy(self, *args, **kwargs):
mock.duo_client_set_proxy(*args, **kwargs)
return super(MockClient, self).set_proxy(*args, **kwargs)
def _connect(self):
return mock
return MockClient
class MockResponse(six.StringIO, object):
def __init__(self, status, body, reason='some reason'):
self.status = status
self.reason = reason
super(MockResponse, self).__init__(body)
class TestIntegration(unittest.TestCase):
IKEY = 'expected ikey'
SKEY = 'expected skey'
HOST = 'expected hostname'
USERNAME = 'expected username'
PASSCODE = 'expected passcode'
IPADDR = 'expected_ipaddr'
PROXY_HOST = 'expected proxy host'
PROXY_PORT = 'expected proxy port'
EXPECTED_USER_AGENT = 'duo_openvpn/' + duo_openvpn.__version__
EXPECTED_PREAUTH_PARAMS = (
'ipaddr=expected_ipaddr'
'&user=expected+username'
)
EXPECTED_AUTH_PATH = '/rest/v1/auth'
EXPECTED_PREAUTH_PATH = '/rest/v1/preauth'
EXPECTED_AUTH_PARAMS = (
'auto=expected+passcode'
'&ipaddr=expected_ipaddr'
'&user=expected+username'
'&factor=auto'
)
def setUp(self):
self.mox = mox.Mox()
self.expected_calls = self.mox.CreateMockAnything()
def assert_auth(self, environ, expected_control,
send_control=True):
self.mox.ReplayAll()
with tempfile.NamedTemporaryFile() as control:
if send_control:
environ['control'] = control.name
with self.assertRaises(SystemExit) as cm:
duo_openvpn.main(
environ=environ,
Client=mock_client_factory(self.expected_calls),
)
self.mox.VerifyAll()
control.seek(0, os.SEEK_SET)
output = control.read()
if not isinstance(output, six.text_type):
output = output.decode('utf-8')
self.assertEqual(expected_control, output)
if expected_control == '1':
self.assertEqual(0, cm.exception.args[0])
else:
self.assertEqual(1, cm.exception.args[0])
def normal_environ(self):
environ = {
'ikey': self.IKEY,
'skey': self.SKEY,
'host': self.HOST,
'username': self.USERNAME,
'password': self.PASSCODE,
'ipaddr': self.IPADDR,
}
self.expected_calls.duo_client_init(
ikey=self.IKEY,
skey=self.SKEY,
host=self.HOST,
user_agent=('duo_openvpn/' + duo_openvpn.__version__),
)
self.expected_calls.duo_client_set_proxy(
host=None,
proxy_type=None,
)
return environ
def compare_params(self, recv_params, sent_params):
stanzas = sent_params.split('&')
return len(recv_params.split('&')) == len(stanzas) and all([s in recv_params for s in stanzas])
def expect_request(self, method, path, params, params_func=None, response=None, raises=None):
if params_func == None:
params_func = lambda p: self.compare_params(p, self.EXPECTED_PREAUTH_PARAMS)
self.expected_calls.request(method, path, mox.Func(params_func), {
'User-Agent': self.EXPECTED_USER_AGENT,
'Host': self.HOST,
'Content-type': 'application/x-www-form-urlencoded',
'Authorization': mox.Func((lambda s: s.startswith('Basic ') and not s.startswith('Basic b\''))),
'Date': mox.Func((lambda s: bool(email.utils.parsedate_tz(s))))
},
)
meth = self.expected_calls.getresponse()
if raises is not None:
meth.AndRaise(raises)
else:
meth.AndReturn(response)
self.expected_calls.close()
def expect_preauth(self, result, path=EXPECTED_PREAUTH_PATH, factor='push1'):
self.expect_request(
method='POST',
path=path,
params=self.EXPECTED_PREAUTH_PARAMS,
response=MockResponse(
status=200,
body=json.dumps({
'stat': 'OK',
'response': {
'result': result,
'status': 'expected status',
'factors': {'default': factor},
},
}),
),
)
def expect_auth(self, result, path=EXPECTED_AUTH_PATH):
self.expect_request(
method='POST',
path=path,
params=self.EXPECTED_AUTH_PARAMS,
params_func = lambda p: self.compare_params(p, self.EXPECTED_AUTH_PARAMS),
response=MockResponse(
status=200,
body=json.dumps({
'stat': 'OK',
'response': {
'result': result,
'status': 'expected status',
},
}),
),
)
def test_preauth_allow(self):
environ = self.normal_environ()
self.expect_preauth('allow')
self.assert_auth(
environ=environ,
expected_control='1',
)
def test_preauth_deny(self):
environ = self.normal_environ()
self.expect_preauth('deny')
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_preauth_enroll(self):
environ = self.normal_environ()
self.expect_preauth('enroll')
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_preauth_bogus(self):
environ = self.normal_environ()
self.expect_preauth('bogus')
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_preauth_missing_result(self):
environ = self.normal_environ()
self.expect_request(
method='POST',
path=self.EXPECTED_PREAUTH_PATH,
params=self.EXPECTED_PREAUTH_PARAMS,
response=MockResponse(
status=200,
body=json.dumps({
'stat': 'OK',
'response': {
'status': 'expected status',
},
}),
),
)
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_preauth_missing_status(self):
environ = self.normal_environ()
self.expect_request(
method='POST',
path=self.EXPECTED_PREAUTH_PATH,
params=self.EXPECTED_PREAUTH_PARAMS,
response=MockResponse(
status=200,
body=json.dumps({
'stat': 'OK',
'response': {
'result': 'deny',
},
}),
),
)
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_preauth_exception(self):
environ = self.normal_environ()
self.expect_request(
method='POST',
path=self.EXPECTED_PREAUTH_PATH,
params=self.EXPECTED_PREAUTH_PARAMS,
raises=Exception('whoops'),
)
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_auth_allow(self):
environ = self.normal_environ()
self.expect_preauth('auth')
self.expect_auth('allow')
self.assert_auth(
environ=environ,
expected_control='1',
)
def test_auth_deny(self):
environ = self.normal_environ()
self.expect_preauth('auth')
self.expect_auth('deny')
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_auth_bogus(self):
environ = self.normal_environ()
self.expect_preauth('auth')
self.expect_auth('bogus')
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_auth_missing_reason(self):
environ = self.normal_environ()
self.expect_preauth('auth')
self.expect_request(
method='POST',
path=self.EXPECTED_AUTH_PATH,
params=self.EXPECTED_AUTH_PARAMS,
params_func = lambda p: self.compare_params(p, self.EXPECTED_AUTH_PARAMS),
response=MockResponse(
status=200,
body=json.dumps({
'stat': 'OK',
'response': {
'status': 'expected status',
},
}),
),
)
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_auth_missing_status(self):
environ = self.normal_environ()
self.expect_preauth('auth')
self.expect_request(
method='POST',
path=self.EXPECTED_AUTH_PATH,
params=self.EXPECTED_AUTH_PARAMS,
params_func = lambda p: self.compare_params(p, self.EXPECTED_AUTH_PARAMS),
response=MockResponse(
status=200,
body=json.dumps({
'stat': 'OK',
'response': {
'result': 'allow',
},
}),
),
)
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_auth_exception(self):
environ = self.normal_environ()
self.expect_preauth('auth')
self.expect_request(
method='POST',
path=self.EXPECTED_AUTH_PATH,
params=self.EXPECTED_AUTH_PARAMS,
params_func = lambda p: self.compare_params(p, self.EXPECTED_AUTH_PARAMS),
raises=Exception('whoops'),
)
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_auth_no_ipaddr(self):
preauth_noip_params='ipaddr=0.0.0.0' \
'&user=expected+username'
environ = self.normal_environ()
environ.pop('ipaddr')
self.expect_request(
method='POST',
path=self.EXPECTED_PREAUTH_PATH,
params=preauth_noip_params,
params_func = lambda p: self.compare_params(p, preauth_noip_params),
response=MockResponse(
status=200,
body=json.dumps({
'stat': 'OK',
'response': {
'result': 'auth',
'status': 'expected status',
'factors': {'default': 'push1'},
},
}),
),
)
auth_noip_params='auto=expected+passcode' \
'&ipaddr=0.0.0.0' \
'&user=expected+username' \
'&factor=auto'
self.expect_request(
method='POST',
path=self.EXPECTED_AUTH_PATH,
params=auth_noip_params,
params_func = lambda p: self.compare_params(p, auth_noip_params),
response=MockResponse(
status=200,
body=json.dumps({
'stat': 'OK',
'response': {
'result': 'allow',
'status': 'expected status',
},
}),
),
)
self.assert_auth(
environ=environ,
expected_control='1',
)
def test_missing_control(self):
environ = {
'ikey': self.IKEY,
'skey': self.SKEY,
'host': self.HOST,
'password': self.PASSCODE,
'username': self.USERNAME,
'ipaddr': self.IPADDR,
}
self.assert_auth(
environ=environ,
send_control=False,
expected_control='',
)
def test_missing_username(self):
environ = {
'ikey': self.IKEY,
'skey': self.SKEY,
'host': self.HOST,
'password': self.PASSCODE,
'ipaddr': self.IPADDR,
}
self.assert_auth(
environ=environ,
expected_control='',
)
def test_missing_password(self):
environ = self.normal_environ()
del environ['password']
self.expect_preauth('auth', factor=None)
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_missing_ikey(self):
environ = {
'skey': self.SKEY,
'host': self.HOST,
'password': self.PASSCODE,
'username': self.USERNAME,
'ipaddr': self.IPADDR,
}
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_missing_skey(self):
environ = {
'ikey': self.IKEY,
'host': self.HOST,
'password': self.PASSCODE,
'username': self.USERNAME,
'ipaddr': self.IPADDR,
}
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_missing_host(self):
environ = {
'ikey': self.IKEY,
'skey': self.SKEY,
'password': self.PASSCODE,
'username': self.USERNAME,
'ipaddr': self.IPADDR,
}
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_proxy_success(self):
environ = self.normal_environ()
environ['proxy_host'] = self.PROXY_HOST
environ['proxy_port'] = self.PROXY_PORT
self.expected_calls.duo_client_set_proxy(
host=self.PROXY_HOST,
port=self.PROXY_PORT,
)
self.expect_preauth(
result='auth',
path=('https://' + self.HOST + self.EXPECTED_PREAUTH_PATH),
)
self.expect_auth(
result='allow',
path=('https://' + self.HOST + self.EXPECTED_AUTH_PATH),
)
self.assert_auth(
environ=environ,
expected_control='1',
)
def test_proxy_missing_port(self):
environ = self.normal_environ()
environ['proxy_host'] = self.PROXY_HOST
self.assert_auth(
environ=environ,
expected_control='0',
)
def test_proxy_missing_host(self):
environ = self.normal_environ()
# proxy_port is ignored if proxy_host isn't present.
environ['proxy_port'] = self.PROXY_PORT
self.expect_preauth('auth')
self.expect_auth('allow')
self.assert_auth(
environ=environ,
expected_control='1',
)
if __name__ == '__main__':
unittest.main()