Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
checks: Move existing SOCIAL_AUTH_* asserts to checks.
Alex Vandiver09f194b6172c
diff --git a/zerver/apps.py b/zerver/apps.pyindex 1ae9ea6d764..1ca1d55afce 100644--- a/zerver/apps.py+++ b/zerver/apps.py@@ -9,7 +9,7 @@ from django.core.checks import registerfrom django.db.models.signals import post_migratefrom typing_extensions import override-from zerver.checks import check_external_host_setting, check_required_settings+from zerver.checks import check_auth_settings, check_external_host_setting, check_required_settingsdef flush_cache(sender: AppConfig | None, **kwargs: Any) -> None:@@ -24,6 +24,7 @@ class ZerverConfig(AppConfig):def ready(self) -> None:register(check_required_settings)register(check_external_host_setting)+ register(check_auth_settings)if settings.SENTRY_DSN: # nocoveragefrom zproject.config import get_configdiff --git a/zerver/checks.py b/zerver/checks.pyindex b7681911e04..42008096ca1 100644--- a/zerver/checks.py+++ b/zerver/checks.py@@ -99,3 +99,33 @@ def check_external_host_setting())return errors+++def check_auth_settings(+ app_configs: Sequence[AppConfig] | None,+ databases: Sequence[str] | None,+ **kwargs: Any,+) -> Iterable[checks.CheckMessage]:+ errors = []+ for idp_name, idp_dict in settings.SOCIAL_AUTH_SAML_ENABLED_IDPS.items():+ if "zulip_groups" in idp_dict.get("extra_attrs", []):+ errors.append(+ checks.Error(+ "zulip_groups can't be listed in extra_attrs",+ obj=f'settings.SOCIAL_AUTH_SAML_ENABLED_IDPS["{idp_name}"]["extra_attrs"]',+ id="zulip.E003",+ )+ )++ for subdomain, config_dict in settings.SOCIAL_AUTH_SYNC_ATTRS_DICT.items():+ for auth_name, attrs_map in config_dict.items():+ for attr_key, attr_value in attrs_map.items():+ if attr_value == "zulip_groups":+ errors.append(+ checks.Error(+ "zulip_groups can't be listed as a SAML attribute",+ obj=f'settings.SOCIAL_AUTH_SYNC_ATTRS_DICT["{subdomain}"]["{auth_name}"]["{attr_key}"]',+ id="zulip.E004",+ )+ )+ return errorsdiff --git a/zerver/tests/test_checks.py b/zerver/tests/test_checks.pyindex 2dbfe8d7e29..015506960db 100644--- a/zerver/tests/test_checks.py+++ b/zerver/tests/test_checks.py@@ -79,3 +79,40 @@ class TestChecks(ZulipTestCase):"EXTERNAL_HOST (-bogus-.example.com) does not validate as a hostname",EXTERNAL_HOST="-bogus-.example.com:443",)++ def test_checks_auth(self) -> None:+ self.assert_check_with_error(+ (+ 'SOCIAL_AUTH_SAML_ENABLED_IDPS["idp_name"]["extra_attrs"]: '+ "(zulip.E003) zulip_groups can't be listed in extra_attrs"+ ),+ SOCIAL_AUTH_SAML_ENABLED_IDPS={+ "idp_name": {+ "entity_id": "https://idp.testshib.org/idp/shibboleth",+ "url": "https://idp.testshib.org/idp/profile/SAML2/Redirect/SSO",+ "attr_user_permanent_id": "email",+ "attr_first_name": "first_name",+ "attr_last_name": "last_name",+ "attr_username": "email",+ "attr_email": "email",+ "extra_attrs": ["title", "mobilePhone", "zulip_role", "zulip_groups"],+ }+ },+ )++ self.assert_check_with_error(+ (+ 'settings.SOCIAL_AUTH_SYNC_ATTRS_DICT["example_org"]["saml"]["custom__groups"]: '+ "(zulip.E004) zulip_groups can't be listed as a SAML attribute"+ ),+ SOCIAL_AUTH_SYNC_ATTRS_DICT={+ "example_org": {+ "saml": {+ "role": "zulip_role",+ "custom__groups": "zulip_groups",+ "custom__title": "title",+ "groups": ["group1", "group2", ("samlgroup3", "zulipgroup3"), "group4"],+ }+ }+ },+ )diff --git a/zproject/computed_settings.py b/zproject/computed_settings.pyindex 88e250c1e94..42ee52cb8db 100644--- a/zproject/computed_settings.py+++ b/zproject/computed_settings.py@@ -64,7 +64,6 @@ from .configured_settings import (SOCIAL_AUTH_SAML_ENABLED_IDPS,SOCIAL_AUTH_SAML_SECURITY_CONFIG,SOCIAL_AUTH_SUBDOMAIN,- SOCIAL_AUTH_SYNC_ATTRS_DICT,STATIC_URL,SUBMIT_USAGE_STATISTICS,TORNADO_PORTS,@@ -1211,16 +1210,6 @@ for idp_name, idp_dict in SOCIAL_AUTH_SAML_ENABLED_IDPS.items():path = f"/etc/zulip/saml/idps/{idp_name}.crt"idp_dict["x509cert"] = get_from_file_if_exists(path)- if "zulip_groups" in idp_dict.get("extra_attrs", []):- raise AssertionError("zulip_groups can't be listed in extra_attrs in the IdP config.")---for dict_for_subdomain in SOCIAL_AUTH_SYNC_ATTRS_DICT.values():- for attrs_map in dict_for_subdomain.values():- if "zulip_groups" in attrs_map.values():- raise AssertionError(- "zulip_groups can't be listed as a SAML attribute in SOCIAL_AUTH_SYNC_ATTRS_DICT"- )SOCIAL_AUTH_PIPELINE = ["social_core.pipeline.social_auth.social_details",