-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ldap_hooks.py
530 lines (458 loc) · 17.1 KB
/
test_ldap_hooks.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import docker
import os
import requests
import logging
import pytest
from urllib.parse import urljoin
from ldap3 import ALL_ATTRIBUTES
from ldap_hooks import search_for, ConnectionManager
from os.path import join, dirname, realpath
from docker.types import Mount
from .util import (
wait_for_site,
get_container,
get_container_env,
get_container_user,
delete,
wait_for_container,
)
# root dir
docker_path = dirname(dirname(realpath(__file__)))
# Logger
logging.basicConfig(level=logging.INFO)
test_logger = logging.getLogger()
JHUB_IMAGE_NAME = "jupyterhub-ldap-hooks"
JHUB_IMAGE_TAG = "test"
JHUB_IMAGE = "".join([JHUB_IMAGE_NAME, ":", JHUB_IMAGE_TAG])
PORT = 8000
jhub_image_spec = {"path": docker_path, "tag": JHUB_IMAGE, "rm": "True", "pull": "True"}
LDAP_IMAGE_PATH = "osixia/openldap"
LDAP_IMAGE_NAME = "openldap"
LDAP_IMAGE_TAG = "1.2.3"
LDAP_IMAGE = "".join([LDAP_IMAGE_PATH, ":", LDAP_IMAGE_TAG])
JHUB_URL = "http://127.0.0.1:{}".format(PORT)
LDAP_URL = "http://openldap"
# Config setup
config_path = join(dirname(realpath(__file__)), "configs")
jhub_config_path = join(config_path, "jhub", "ldap_person_hook.py")
jhub_target_config = os.path.join(os.sep, "etc", "jupyterhub", "jupyterhub_config.py")
ldap_schema = join(config_path, "openldap", "mount_schema")
ldap_target_schema = "/container/service/slapd/assets/config/bootstrap/schema"
ldap_servers = join(config_path, "openldap", "openldap-servers")
ldap_target_servers = "/opt/openldap-servers"
LDAP_NETWORK_NAME = "jhub_ldap_network"
ldap_network_config = {
"name": LDAP_NETWORK_NAME,
"driver": "bridge",
"attachable": True,
}
docker_mount = Mount(
source=os.path.join(os.sep, "var", "run", "docker.sock"),
target=os.path.join(os.sep, "var", "run", "docker.sock"),
read_only=True,
type="bind",
)
# container cmd
jhub_cont = {
"image": JHUB_IMAGE,
"name": JHUB_IMAGE_NAME,
"mounts": [
Mount(
source=jhub_config_path,
target=jhub_target_config,
read_only=True,
type="bind",
),
docker_mount,
],
"ports": {PORT: PORT},
"network": LDAP_NETWORK_NAME,
"detach": "True",
"command": "jupyterhub --debug -f " + jhub_target_config,
}
LDAP_DOMAIN = "example.org"
LDAP_USER = "cn=admin,dc=example,dc=org"
LDAP_PASSWORD = "dummyldap_password"
ldap_cont = {
"image": LDAP_IMAGE,
"name": LDAP_IMAGE_NAME,
"mounts": [
Mount(
source=ldap_schema, target=ldap_target_schema, read_only=False, type="bind"
),
Mount(
source=ldap_servers,
target=ldap_target_servers,
read_only=False,
type="bind",
),
],
"ports": {389: 389, 636: 636},
"network": LDAP_NETWORK_NAME,
"detach": "True",
"environment": {
"LDAP_DOMAIN": LDAP_DOMAIN,
"LDAP_ADMIN_PASSWORD": LDAP_PASSWORD,
"LDAP_CONFIG_PASSWORD": LDAP_PASSWORD,
"LDAP_RFC2307BIS_SCHEMA": "true",
},
"command": "--copy-service",
}
@pytest.mark.parametrize("build_image", [jhub_image_spec], indirect=["build_image"])
@pytest.mark.parametrize("network", [ldap_network_config], indirect=["network"])
@pytest.mark.parametrize(
"containers", [(jhub_cont, ldap_cont)], indirect=["containers"]
)
def test_ldap_person_hook(build_image, network, containers):
"""
Test that the ldap_person_hook is able to create an LDAP DIT entry,
with the provided JupyterHub Spawner attribute.
"""
test_logger.info("Start of ldap person hook testing")
client = docker.from_env()
username = "ldap-user"
auth_headers = {"Remote-User": username}
assert wait_for_site(JHUB_URL, valid_status_code=401) is True
with requests.Session() as session:
# Refresh cookies
session.get(JHUB_URL)
# Login
login_response = session.post(
JHUB_URL + "/hub/login",
headers=auth_headers,
params={'_xsrf': session.cookies['_xsrf']}
)
assert login_response.status_code == 200
resp = session.get(JHUB_URL + "/hub/home")
assert resp.status_code == 200
dn_str = "/telephoneNumber=23012303403/SN=My Surname/CN=" + username
# Pass LDAP DN for creation on spawn
post_dn = session.post(
JHUB_URL + "/hub/set-user-data",
json={"data": {"PersonDN": dn_str}},
params={'_xsrf': session.cookies['_xsrf']},
)
assert post_dn.status_code == 200
# Spawn notebook
spawn_response = session.post(
JHUB_URL + "/hub/spawn",
params={'_xsrf': session.cookies['_xsrf']}
)
assert spawn_response.status_code == 200
container_name = "{}-{}".format("jupyter", username)
wait_min = 5
if not wait_for_container(client, container_name, minutes=wait_min):
raise RuntimeError(
"No container with name: {} appeared within: {} minutes".format(
container_name, wait_min
)
)
spawned_container = get_container(client, container_name)
assert spawned_container is not None
# Search openldap for person
search_base = "dc=example,dc=org"
search_filter = (
"(&(objectclass=Person)(telephoneNumber=23012303403)"
"(SN=My Surname)(CN=" + username + "))"
)
conn_manager = ConnectionManager(
"127.0.0.1", user=LDAP_USER, password=LDAP_PASSWORD
)
conn_manager.connect()
assert conn_manager.is_connected()
success = search_for(
conn_manager.get_connection(),
search_base,
search_filter,
attributes=ALL_ATTRIBUTES,
)
assert success
attributes = conn_manager.get_response_attributes()
assert attributes["objectClass"] == ["person"]
assert attributes["telephoneNumber"] == ["23012303403"]
assert attributes["sn"] == ["My Surname"]
assert attributes["cn"] == [username]
assert attributes["description"] == ["A default person account"]
# Shutdown the container
# Delete the spawned service
jhub_user = get_container_user(spawned_container)
assert jhub_user is not None
assert username == jhub_user
delete_url = urljoin(JHUB_URL, "/hub/api/users/{}/server".format(jhub_user))
deleted = delete(session, delete_url)
assert deleted
# Remove the stopped container
spawned_container.stop()
spawned_container.wait()
spawned_container.remove()
deleted_container = get_container(client, container_name)
assert deleted_container is None
jhub_dynamic_config_path = join(config_path, "jhub", "ldap_person_dynamic_attr_hook.py")
jhub_dynamic_target_config = "/etc/jupyterhub/jupyterhub_config.py"
jhub_dynamic_cont = {
"image": JHUB_IMAGE,
"name": JHUB_IMAGE_NAME,
"mounts": [
Mount(
source=jhub_dynamic_config_path,
target=jhub_dynamic_target_config,
read_only=True,
type="bind",
),
docker_mount,
],
"ports": {PORT: PORT},
"network": LDAP_NETWORK_NAME,
"detach": "True",
"command": "jupyterhub --debug -f " + jhub_target_config,
}
@pytest.mark.parametrize("build_image", [jhub_image_spec], indirect=["build_image"])
@pytest.mark.parametrize("network", [ldap_network_config], indirect=["network"])
@pytest.mark.parametrize(
"containers", [(jhub_dynamic_cont, ldap_cont)], indirect=["containers"]
)
def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
"""
Test that the ldap_person_hook is able to create an LDAP DIT entry,
with a dynamic provided spawner attribute
"""
test_logger.info("Start of ldap person dynamic attribute hook")
client = docker.from_env()
username = "a-new-dynamic-user"
auth_headers = {"Remote-User": username}
assert wait_for_site(JHUB_URL, valid_status_code=401) is True
with requests.Session() as session:
# Refresh cookies
session.get(JHUB_URL)
# Login
login_response = session.post(
JHUB_URL + "/hub/login",
headers=auth_headers,
params={'_xsrf': session.cookies['_xsrf']},
)
assert login_response.status_code == 200
resp = session.get(JHUB_URL + "/hub/home")
assert resp.status_code == 200
desc = "The first description"
dn_str = (
"/description="
+ desc
+ "/telephoneNumber=23012303403/SN=My Surname/CN="
+ username
)
# Pass LDAP DN for creation on spawn
post_dn = session.post(
JHUB_URL + "/hub/set-user-data",
json={"data": {"PersonDN": dn_str}},
params={'_xsrf': session.cookies['_xsrf']},
)
assert post_dn.status_code == 200
# Spawn notebook
spawn_response = session.post(
JHUB_URL + "/hub/spawn",
params={'_xsrf': session.cookies['_xsrf']}
)
assert spawn_response.status_code == 200
container_name = "{}-{}".format("jupyter", username)
wait_min = 5
if not wait_for_container(client, container_name, minutes=wait_min):
raise RuntimeError(
"No container with name: {} appeared within: {} minutes".format(
container_name, wait_min
)
)
spawned_container = get_container(client, container_name)
assert spawned_container is not None
# Search openldap for person
search_base = "dc=example,dc=org"
search_filter = (
"(&(objectclass=Person)(description="
+ desc
+ ")(telephoneNumber=23012303403)(SN=My Surname)(CN="
+ username
+ "))"
)
conn_manager = ConnectionManager(
"127.0.0.1", user=LDAP_USER, password=LDAP_PASSWORD
)
conn_manager.connect()
assert conn_manager.is_connected()
success = search_for(
conn_manager.get_connection(),
search_base,
search_filter,
attributes=ALL_ATTRIBUTES,
)
assert success
attributes = conn_manager.get_response_attributes()
assert attributes["objectClass"] == ["person"]
assert attributes["telephoneNumber"] == ["23012303403"]
assert attributes["sn"] == ["My Surname"]
assert attributes["cn"] == [username]
assert attributes["description"] == [desc]
# Check that the notebook has the description env
spawned_container = get_container(client, container_name)
container_desc = get_container_env(spawned_container, env_key="description")
assert desc == container_desc
container_static_desc = get_container_env(
spawned_container, env_key="static_description"
)
assert "Static description" == container_static_desc
# Shutdown the container
jhub_user = get_container_user(spawned_container)
assert jhub_user is not None
assert username == jhub_user
delete_url = urljoin(JHUB_URL, "/hub/api/users/{}/server".format(jhub_user))
deleted = delete(session, delete_url)
assert deleted
# Remove the stopped container
spawned_container.stop()
spawned_container.wait()
spawned_container.remove()
deleted_container = get_container(client, container_name)
assert deleted_container is None
jhub_obj_spw_config_path = join(config_path, "jhub", "ldap_object_spawner_hook.py")
jhub_obj_spw_target_config = "/etc/jupyterhub/jupyterhub_config.py"
jhub_obj_spw_cont = {
"image": JHUB_IMAGE,
"name": JHUB_IMAGE_NAME,
"mounts": [
Mount(
source=jhub_obj_spw_config_path,
target=jhub_obj_spw_target_config,
read_only=True,
type="bind",
),
docker_mount,
],
"ports": {PORT: PORT},
"network": LDAP_NETWORK_NAME,
"detach": "True",
"command": "jupyterhub --debug -f " + jhub_obj_spw_target_config,
}
@pytest.mark.parametrize("build_image", [jhub_image_spec], indirect=["build_image"])
@pytest.mark.parametrize("network", [ldap_network_config], indirect=["network"])
@pytest.mark.parametrize(
"containers", [(jhub_obj_spw_cont, ldap_cont)], indirect=["containers"]
)
def test_dynamic_object_spawner_attributes(build_image, network, containers):
"""
Test that the ldap_person_hook is able to create an LDAP DIT entry,
with a dynamic provided spawner attribute
"""
test_logger.info("Start of ldap dynamic object spawner attributes testing")
client = docker.from_env()
username = "mynewuser"
auth_headers = {"Remote-User": username}
assert wait_for_site(JHUB_URL, valid_status_code=401) is True
with requests.Session() as session:
# Refresh cookies
session.get(JHUB_URL)
# Login
login_response = session.post(
JHUB_URL + "/hub/login",
headers=auth_headers,
params={'_xsrf': session.cookies['_xsrf']}
)
assert login_response.status_code == 200
resp = session.get(JHUB_URL + "/hub/home")
assert resp.status_code == 200
desc = "The first description"
dn_str = (
"/description="
+ desc
+ "/telephoneNumber=23012303403/SN=My Surname/CN="
+ username
)
# Pass LDAP DN for creation on spawn
post_dn = session.post(
JHUB_URL + "/hub/set-user-data",
json={"data": {"PersonDN": dn_str}},
params={'_xsrf': session.cookies['_xsrf']},
)
assert post_dn.status_code == 200
# Spawn notebook
spawn_response = session.post(
JHUB_URL + "/hub/spawn",
params={'_xsrf': session.cookies['_xsrf']}
)
assert spawn_response.status_code == 200
container_name = "{}-{}".format("jupyter", username)
wait_min = 5
if not wait_for_container(client, container_name, minutes=wait_min):
raise RuntimeError(
"No container with name: {} appeared within: {} minutes".format(
container_name, wait_min
)
)
spawned_container = get_container(client, container_name)
assert spawned_container is not None
# Shutdown the container
jhub_user = get_container_user(spawned_container)
assert jhub_user is not None
assert username == jhub_user
delete_url = urljoin(JHUB_URL, "/hub/api/users/{}/server".format(jhub_user))
deleted = delete(session, delete_url)
assert deleted
# Remove the stopped container
spawned_container.stop()
spawned_container.wait()
spawned_container.remove()
deleted_container = get_container(client, container_name)
assert deleted_container is None
####
# Respawn, ensure that it is loaded correctly from DIT
spawn_response = session.post(
JHUB_URL + "/hub/spawn",
params={'_xsrf': session.cookies['_xsrf']}
)
assert spawn_response.status_code == 200
# Validate that the env is still correct
spawned_container = get_container(client, container_name)
jhub_user = get_container_user(spawned_container)
assert jhub_user is not None
assert username == jhub_user
# Validate that the ldap DIT still only has 1 entry
search_base = "dc=example,dc=org"
search_filter = (
"(&(objectclass=inetOrgPerson)"
"(objectclass=posixAccount)(uid="
+ username
+ ")(telephoneNumber=23012303403)(SN=My Surname)(CN="
+ username
+ "))"
)
conn_manager = ConnectionManager(
"127.0.0.1", user=LDAP_USER, password=LDAP_PASSWORD
)
conn_manager.connect()
assert conn_manager.is_connected()
success = search_for(
conn_manager.get_connection(),
search_base,
search_filter,
attributes=ALL_ATTRIBUTES,
)
assert success
# 1 entry
assert len(conn_manager.get_response()) == 1
# Extract attributes from entry
attributes = conn_manager.get_response_attributes()
assert attributes["objectClass"] == ["inetOrgPerson", "posixAccount"]
assert attributes["telephoneNumber"] == ["23012303403"]
assert attributes["sn"] == ["My Surname"]
assert attributes["cn"] == [username]
assert attributes["uid"] == [username]
# Shutdown the container
# Delete the spawned service
jhub_user = get_container_user(spawned_container)
assert jhub_user is not None
delete_url = urljoin(JHUB_URL, "/hub/api/users/{}/server".format(jhub_user))
deleted = delete(session, delete_url)
assert deleted
# Remove the stopped container
spawned_container.stop()
spawned_container.wait()
spawned_container.remove()
deleted_container = get_container(client, container_name)
assert deleted_container is None