2018-12-13 22:46:37 +01:00
|
|
|
import glob
|
2019-01-14 16:08:58 +01:00
|
|
|
import logging
|
2018-12-13 22:46:37 +01:00
|
|
|
import os
|
2019-01-16 14:09:30 +01:00
|
|
|
from typing import Any, Dict, List, Optional
|
2018-12-13 22:46:37 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2019-07-17 02:29:08 +02:00
|
|
|
from zerver.lib.storage import static_path
|
2018-12-13 22:46:37 +01:00
|
|
|
|
2018-12-30 01:33:11 +01:00
|
|
|
# See https://jackstromberg.com/2013/01/useraccountcontrol-attributeflag-values/
|
|
|
|
# for docs on what these values mean.
|
2021-02-12 08:20:45 +01:00
|
|
|
LDAP_USER_ACCOUNT_CONTROL_NORMAL = "512"
|
|
|
|
LDAP_USER_ACCOUNT_CONTROL_DISABLED = "514"
|
2018-12-13 23:58:26 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def generate_dev_ldap_dir(mode: str, num_users: int = 8) -> Dict[str, Dict[str, Any]]:
|
2018-12-13 22:46:37 +01:00
|
|
|
mode = mode.lower()
|
2019-01-29 14:49:53 +01:00
|
|
|
ldap_data = []
|
2021-02-12 08:19:30 +01:00
|
|
|
for i in range(1, num_users + 1):
|
2021-02-12 08:20:45 +01:00
|
|
|
name = f"LDAP User {i}"
|
|
|
|
email = f"ldapuser{i}@zulip.com"
|
|
|
|
phone_number = f"999999999{i}"
|
|
|
|
birthdate = f"19{i:02}-{i:02}-{i:02}"
|
2019-01-29 14:49:53 +01:00
|
|
|
ldap_data.append((name, email, phone_number, birthdate))
|
2018-12-13 22:46:37 +01:00
|
|
|
|
2020-10-24 09:33:54 +02:00
|
|
|
profile_images = []
|
2022-07-13 08:21:48 +02:00
|
|
|
for path in glob.glob(os.path.join(static_path("images/landing-page/team"), "*")):
|
2020-10-24 09:33:54 +02:00
|
|
|
with open(path, "rb") as f:
|
|
|
|
profile_images.append(f.read())
|
2018-12-13 22:46:37 +01:00
|
|
|
ldap_dir = {}
|
2019-01-29 14:49:53 +01:00
|
|
|
for i, user_data in enumerate(ldap_data):
|
|
|
|
email = user_data[1].lower()
|
2021-02-12 08:20:45 +01:00
|
|
|
email_username = email.split("@")[0]
|
2019-01-29 14:49:53 +01:00
|
|
|
common_data = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"cn": [user_data[0]],
|
|
|
|
"userPassword": [email_username],
|
|
|
|
"phoneNumber": [user_data[2]],
|
|
|
|
"birthDate": [user_data[3]],
|
2019-01-29 14:49:53 +01:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
if mode == "a":
|
|
|
|
ldap_dir["uid=" + email + ",ou=users,dc=zulip,dc=com"] = dict(
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
uid=[email],
|
|
|
|
thumbnailPhoto=[profile_images[i % len(profile_images)]],
|
|
|
|
userAccountControl=[LDAP_USER_ACCOUNT_CONTROL_NORMAL],
|
2021-02-12 08:19:30 +01:00
|
|
|
**common_data,
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
elif mode == "b":
|
|
|
|
ldap_dir["uid=" + email_username + ",ou=users,dc=zulip,dc=com"] = dict(
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
uid=[email_username],
|
|
|
|
jpegPhoto=[profile_images[i % len(profile_images)]],
|
2021-02-12 08:19:30 +01:00
|
|
|
**common_data,
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
elif mode == "c":
|
|
|
|
ldap_dir["uid=" + email_username + ",ou=users,dc=zulip,dc=com"] = dict(
|
2021-02-12 08:19:30 +01:00
|
|
|
uid=[email_username], email=[email], **common_data
|
|
|
|
)
|
2018-12-13 22:46:37 +01:00
|
|
|
|
|
|
|
return ldap_dir
|
2019-01-12 18:12:11 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def init_fakeldap(
|
|
|
|
directory: Optional[Dict[str, Dict[str, List[str]]]] = None
|
|
|
|
) -> None: # nocoverage
|
2019-01-12 18:12:11 +01:00
|
|
|
# We only use this in development. Importing mock inside
|
|
|
|
# this function is an import time optimization, which
|
|
|
|
# avoids the expensive import of the mock module (slow
|
|
|
|
# because its dependency pbr uses pkgresources, which is
|
|
|
|
# really slow to import.)
|
2020-05-26 07:16:25 +02:00
|
|
|
from unittest import mock
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2019-01-12 18:12:11 +01:00
|
|
|
from fakeldap import MockLDAP
|
|
|
|
|
2019-01-14 16:08:58 +01:00
|
|
|
# Silent `django_auth_ldap` logger in dev mode to avoid
|
|
|
|
# spammy user not found log messages.
|
2021-02-12 08:20:45 +01:00
|
|
|
ldap_auth_logger = logging.getLogger("django_auth_ldap")
|
2019-01-14 16:08:58 +01:00
|
|
|
ldap_auth_logger.setLevel(logging.CRITICAL)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
fakeldap_logger = logging.getLogger("fakeldap")
|
2019-01-14 18:52:25 +01:00
|
|
|
fakeldap_logger.setLevel(logging.CRITICAL)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
ldap_patcher = mock.patch("django_auth_ldap.config.ldap.initialize")
|
2019-01-12 18:12:11 +01:00
|
|
|
mock_initialize = ldap_patcher.start()
|
|
|
|
mock_ldap = MockLDAP()
|
|
|
|
mock_initialize.return_value = mock_ldap
|
|
|
|
|
2022-07-06 06:58:43 +02:00
|
|
|
assert settings.FAKE_LDAP_MODE is not None
|
2021-02-12 08:19:30 +01:00
|
|
|
mock_ldap.directory = directory or generate_dev_ldap_dir(
|
|
|
|
settings.FAKE_LDAP_MODE, settings.FAKE_LDAP_NUM_USERS
|
|
|
|
)
|