Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

ldap: Move ldap log messages from root logger to zulip.ldap.

Alex Vandiverd296bc530e5d
diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py
index 65150013741..acca7725c77 100644
--- a/zerver/tests/test_auth_backends.py
+++ b/zerver/tests/test_auth_backends.py
@@ -1816,7 +1816,7 @@ class SocialAuthBase(DesktopFlowTestingLib, ZulipTestCase, ABC):
"zproject.backends.ZulipDummyBackend",
),
),
- self.assertLogs(level="WARNING") as log_warn,
+ self.assertLogs("zulip.ldap", level="WARNING") as log_warn,
):
result = self.social_auth_test(
account_data_dict,
@@ -1857,7 +1857,8 @@ class SocialAuthBase(DesktopFlowTestingLib, ZulipTestCase, ABC):
skip_registration_form=self.BACKEND_CLASS.full_name_validated,
)
self.assertEqual(
- log_warn.output, [f"WARNING:root:New account email {email} could not be found in LDAP"]
+ log_warn.output,
+ [f"WARNING:zulip.ldap:New account email {email} could not be found in LDAP"],
)
@override_settings(TERMS_OF_SERVICE_VERSION=None)
@@ -1887,7 +1888,6 @@ class SocialAuthBase(DesktopFlowTestingLib, ZulipTestCase, ABC):
),
),
self.assertLogs("zulip.ldap", level="DEBUG") as log_debug,
- self.assertLogs(level="WARNING") as log_warn,
):
account_data_dict = self.get_account_data_dict(email=email, name=name)
result = self.social_auth_test(
@@ -1907,13 +1907,11 @@ class SocialAuthBase(DesktopFlowTestingLib, ZulipTestCase, ABC):
name,
skip_registration_form=self.BACKEND_CLASS.full_name_validated,
)
- self.assertEqual(
- log_warn.output, [f"WARNING:root:New account email {email} could not be found in LDAP"]
- )
self.assertEqual(
log_debug.output,
[
- f"DEBUG:zulip.ldap:ZulipLDAPAuthBackend: No LDAP user matching django_to_ldap_username result: {email}. Input username: {email}"
+ f"WARNING:zulip.ldap:New account email {email} could not be found in LDAP",
+ f"DEBUG:zulip.ldap:ZulipLDAPAuthBackend: No LDAP user matching django_to_ldap_username result: {email}. Input username: {email}",
],
)
@@ -3672,7 +3670,6 @@ class SAMLAuthBackendTest(SocialAuthBase):
SOCIAL_AUTH_SYNC_ATTRS_DICT=sync_custom_attrs_dict,
),
self.assertLogs(self.logger_string) as mock_log,
- self.assertLogs("", level="WARNING") as mock_root_logger,
):
account_data_dict = self.get_account_data_dict(email=self.email, name=self.name)
result = self.social_auth_test(
@@ -3740,9 +3737,12 @@ class SAMLAuthBackendTest(SocialAuthBase):
),
mock_log.output,
)
- self.assertEqual(
- mock_root_logger.output[0],
- "WARNING:root:ensure_missing_groups: received invalid groups names: ['@@@@']",
+ self.assertIn(
+ self.logger_output(
+ "ensure_missing_groups: received invalid groups names: ['@@@@']",
+ type="warning",
+ ),
+ mock_log.output,
)
@override_settings(TERMS_OF_SERVICE_VERSION=None)
@@ -3797,7 +3797,7 @@ class SAMLAuthBackendTest(SocialAuthBase):
),
)
- with self.assertLogs("", level="INFO") as mock_root_logger:
+ with self.assertLogs("zulip.ldap", level="INFO") as mock_ldap_logger:
self.stage_two_of_registration(
result, realm, subdomain, email, name, name, self.BACKEND_CLASS.full_name_validated
)
@@ -3849,13 +3849,14 @@ class SAMLAuthBackendTest(SocialAuthBase):
prereg_user = PreregistrationUser.objects.last()
assert prereg_user is not None
self.assertEqual(
- mock_root_logger.output[0],
- f"INFO:root:PreregistrationUser {prereg_user.id} should be added to groups ['newtestgroup'], but they don't exist. Creating them first.",
+ mock_ldap_logger.output[0],
+ f"INFO:zulip.ldap:PreregistrationUser {prereg_user.id} should be added to groups ['newtestgroup'], but they don't exist. Creating them first.",
)
+
self.assertEqual(
- f"INFO:root:Synced user groups for PreregistrationUser {prereg_user.id} in {realm.id}: "
+ mock_ldap_logger.output[1],
+ f"INFO:zulip.ldap:Synced user groups for PreregistrationUser {prereg_user.id} in {realm.id}: "
'{"newtestgroup": true, "testgroup1": true, "testgroup2": false}. Final groups set: [\'newtestgroup\', \'testgroup1\']',
- mock_root_logger.output[1],
)
@override_settings(TERMS_OF_SERVICE_VERSION=None)
@@ -3911,7 +3912,7 @@ class SAMLAuthBackendTest(SocialAuthBase):
zulip_groups=["testgroup1"],
),
)
- with self.assertLogs("", level="INFO") as mock_root_logger:
+ with self.assertLogs("zulip.ldap", level="INFO") as mock_logger:
self.stage_two_of_registration(
result, realm, subdomain, email, name, name, self.BACKEND_CLASS.full_name_validated
)
@@ -3935,9 +3936,9 @@ class SAMLAuthBackendTest(SocialAuthBase):
prereg_user = PreregistrationUser.objects.last()
assert prereg_user is not None
self.assertEqual(
- f"INFO:root:Synced user groups for PreregistrationUser {prereg_user.id} in {realm.id}: "
+ f"INFO:zulip.ldap:Synced user groups for PreregistrationUser {prereg_user.id} in {realm.id}: "
'{"testgroup1": true, "testgroup2": false}. Final groups set: [\'testgroup1\']',
- mock_root_logger.output[0],
+ mock_logger.output[0],
)
@override_settings(TERMS_OF_SERVICE_VERSION=None)
@@ -6996,12 +6997,15 @@ class DjangoToLDAPUsernameTests(ZulipTestCase):
self.backend.django_to_ldap_username("[email protected]"), self.ldap_username("aaron")
)
- with self.assertLogs(level="WARNING") as m, self.assertRaises(NoMatchingLDAPUserError):
+ with (
+ self.assertLogs("zulip.ldap", level="WARNING") as m,
+ self.assertRaises(NoMatchingLDAPUserError),
+ ):
self.backend.django_to_ldap_username("[email protected]")
self.assertEqual(
m.output,
[
- "WARNING:root:Multiple users with email {} found in LDAP.".format(
+ "WARNING:zulip.ldap:Multiple users with email {} found in LDAP.".format(
)
],
@@ -8048,12 +8052,12 @@ class TestZulipLDAPUserPopulator(ZulipLDAPTestCase):
# Try to use invalid data as the image:
self.change_ldap_user_attr("hamlet", "jpegPhoto", b"00" + test_image_data)
with self.settings(AUTH_LDAP_USER_ATTR_MAP={"full_name": "cn", "avatar": "jpegPhoto"}):
- with self.assertLogs(level="WARNING") as m:
+ with self.assertLogs("zulip.ldap", level="WARNING") as m:
self.perform_ldap_sync(self.example_user("hamlet"))
self.assertEqual(
m.output,
[
- "WARNING:root:Could not parse {} field for user {}".format(
+ "WARNING:zulip.ldap:Could not parse {} field for user {}".format(
"jpegPhoto", hamlet.id
)
],
diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py
index 3ac8128e444..789942596ab 100644
--- a/zerver/tests/test_signup.py
+++ b/zerver/tests/test_signup.py
@@ -2589,7 +2589,6 @@ class UserSignUpTest(ZulipTestCase):
AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map,
),
self.assertLogs("zulip.ldap", level="DEBUG") as ldap_logs,
- self.assertLogs(level="WARNING") as root_logs,
):
# Click confirmation link
result = self.submit_reg_form_for_user(
@@ -2621,15 +2620,10 @@ class UserSignUpTest(ZulipTestCase):
self.assertEqual(
result["Location"], "/accounts/login/?email=no_such_user_in_ldap%40example.com"
)
- self.assertEqual(
- root_logs.output,
- [
- "WARNING:root:New account email [email protected] could not be found in LDAP",
- ],
- )
self.assertEqual(
ldap_logs.output,
[
+ "WARNING:zulip.ldap:New account email [email protected] could not be found in LDAP",
"DEBUG:zulip.ldap:ZulipLDAPAuthBackend: Email [email protected] does not match LDAP domain zulip.com.",
],
)
@@ -3005,7 +2999,7 @@ class UserSignUpTest(ZulipTestCase):
LDAP_APPEND_DOMAIN="example.com",
AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map,
):
- with self.assertLogs(level="WARNING") as m:
+ with self.assertLogs("zulip.ldap", level="WARNING") as m:
result = self.submit_reg_form_for_user(
email,
password,
@@ -3016,7 +3010,9 @@ class UserSignUpTest(ZulipTestCase):
self.assertEqual(result.status_code, 200)
self.assertEqual(
m.output,
- ["WARNING:root:New account email [email protected] could not be found in LDAP"],
+ [
+ "WARNING:zulip.ldap:New account email [email protected] could not be found in LDAP"
+ ],
)
with self.assertLogs("zulip.ldap", "DEBUG") as debug_log:
result = self.submit_reg_form_for_user(
@@ -3112,7 +3108,7 @@ class UserSignUpTest(ZulipTestCase):
LDAP_EMAIL_ATTR="mail",
AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map,
):
- with self.assertLogs(level="WARNING") as m:
+ with self.assertLogs("zulip.ldap", level="WARNING") as m:
result = self.submit_reg_form_for_user(
email,
password,
@@ -3124,7 +3120,7 @@ class UserSignUpTest(ZulipTestCase):
self.assertEqual(
m.output,
[
- "WARNING:root:New account email [email protected] could not be found in LDAP"
+ "WARNING:zulip.ldap:New account email [email protected] could not be found in LDAP"
],
)
diff --git a/zerver/views/registration.py b/zerver/views/registration.py
index 4b35fe6c342..6e9e6bea848 100644
--- a/zerver/views/registration.py
+++ b/zerver/views/registration.py
@@ -147,7 +147,7 @@ from zproject.backends import (
password_auth_enabled,
)
-logger = logging.getLogger("")
+ldap_logger = logging.getLogger("zulip.ldap")
@typed_endpoint
@@ -536,7 +536,9 @@ def registration_helper(
try:
ldap_username = backend.django_to_ldap_username(email)
except NoMatchingLDAPUserError:
- logger.warning("New account email %s could not be found in LDAP", email)
+ ldap_logger.warning(
+ "New account email %s could not be found in LDAP", email
+ )
break
# Note that this `ldap_user` object is not a
@@ -864,7 +866,7 @@ def registration_helper(
)
if return_data.get("invalid_subdomain"):
# By construction, this should never happen.
- logger.error(
+ logging.error(
"Subdomain mismatch in registration %s: %s",
realm.subdomain,
user_profile.delivery_email,
@@ -1371,7 +1373,7 @@ def create_realm(request: HttpRequest, confirmation_key: str | None = None) -> H
request=request,
)
except EmailNotDeliveredError:
- logger.exception("Failed to deliver email during realm creation")
+ logging.exception("Failed to deliver email during realm creation")
if settings.CORPORATE_ENABLED:
return render(request, "500.html", status=500)
return config_error(request, "smtp")
@@ -1705,7 +1707,7 @@ def accounts_home(
try:
send_confirm_registration_email(email, activation_url, request=request, realm=realm)
except EmailNotDeliveredError:
- logger.exception("Failed to deliver email during user registration")
+ logging.exception("Failed to deliver email during user registration")
if settings.CORPORATE_ENABLED:
return render(request, "500.html", status=500)
return config_error(request, "smtp")
diff --git a/zproject/backends.py b/zproject/backends.py
index 13ed536c69d..d94b5c0963f 100644
--- a/zproject/backends.py
+++ b/zproject/backends.py
@@ -815,7 +815,7 @@ class ZulipLDAPAuthBackendBase(ZulipAuthMixin, LDAPBackend):
# This is possible, but strange, so worth logging a warning about.
# We can't translate the email to a unique username,
# so we don't do anything else here.
- logging.warning("Multiple users with email %s found in LDAP.", username)
+ ldap_logger.warning("Multiple users with email %s found in LDAP.", username)
result = username
if _LDAPUser(self, result).attrs is None:
@@ -896,7 +896,7 @@ class ZulipLDAPAuthBackendBase(ZulipAuthMixin, LDAPBackend):
user.avatar_hash = user_avatar_content_hash(ldap_avatar)
user.save(update_fields=["avatar_hash"])
else:
- logging.warning("Could not parse %s field for user %s", avatar_attr_name, user.id)
+ ldap_logger.warning("Could not parse %s field for user %s", avatar_attr_name, user.id)
def is_user_disabled_in_ldap(self, ldap_user: _LDAPUser) -> bool:
"""Implements checks for whether a user has been
@@ -1841,12 +1841,12 @@ def sync_groups_for_prereg_user(
missing_group_names = set(group_names_to_ensure_member) - {
group.name for group in groups_to_ensure_member
}
- logging.info(
+ ldap_logger.info(
"PreregistrationUser %s should be added to groups %s, but they don't exist. Creating them first.",
prereg_user.id,
sorted(missing_group_names),
)
- groups_to_ensure_member += ensure_missing_groups(missing_group_names, realm)
+ groups_to_ensure_member += ensure_missing_groups(missing_group_names, realm, ldap_logger)
groups_to_ensure_not_member = list(
NamedUserGroup.objects.filter(
@@ -1860,7 +1860,7 @@ def sync_groups_for_prereg_user(
final_group_names = set(prereg_user.groups.all().values_list("name", flat=True))
stringified_dict = json.dumps(group_memberships_sync_map, sort_keys=True)
- logging.info(
+ ldap_logger.info(
"Synced user groups for PreregistrationUser %s in %s: %s. Final groups set: %s",
prereg_user.id,
realm.id,
@@ -1924,7 +1924,7 @@ def sync_groups(
user_id,
sorted(missing_group_names),
)
- add_groups += ensure_missing_groups(missing_group_names, realm)
+ add_groups += ensure_missing_groups(missing_group_names, realm, logger)
bulk_add_members_to_user_groups(add_groups, [user_id], acting_user=None)
@@ -1939,7 +1939,9 @@ def sync_groups(
@transaction.atomic(savepoint=False)
-def ensure_missing_groups(missing_group_names: set[str], realm: Realm) -> list[NamedUserGroup]:
+def ensure_missing_groups(
+ missing_group_names: set[str], realm: Realm, logger: logging.Logger
+) -> list[NamedUserGroup]:
system_groups_name_dict = get_role_based_system_groups_dict(realm)
group_owners = system_groups_name_dict[SystemGroups.OWNERS].usergroup_ptr
group_settings_map = dict(
@@ -1954,7 +1956,7 @@ def ensure_missing_groups(missing_group_names: set[str], realm: Realm) -> list[N
continue
valid_groups_names.add(group_name)
if valid_groups_names != missing_group_names:
- logging.warning(
+ logger.warning(
"ensure_missing_groups: received invalid groups names: %s",
sorted(missing_group_names - valid_groups_names),
)