From c63b74bd0290b5adcc913b9bf0f5465ae79d5d77 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Mon, 19 Aug 2024 12:11:31 +0100 Subject: [PATCH] Allow loading the bot without LDAP env vars set --- arthur/config.py | 10 +++++----- arthur/exts/directory/ldap.py | 19 ++++++++++++++++--- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/arthur/config.py b/arthur/config.py index 4d31103..bed026c 100644 --- a/arthur/config.py +++ b/arthur/config.py @@ -36,18 +36,18 @@ class Config( enable_ldap: bool = False - ldap_host: pydantic.AnyUrl + ldap_host: pydantic.AnyUrl | None = None ldap_bind_user: str = "uid=kingarthur,cn=users,cn=accounts,dc=box,dc=pydis,dc=wtf" - ldap_bind_password: pydantic.SecretStr + ldap_bind_password: pydantic.SecretStr | None = None ldap_base_dn: str = "dc=box,dc=pydis,dc=wtf" - ldap_certificate_location: pydantic.FilePath + ldap_certificate_location: pydantic.FilePath | None = None # Keycloak - keycloak_address: pydantic.AnyUrl + keycloak_address: pydantic.AnyUrl | None = None keycloak_username: str = "kingarthur" - keycloak_password: pydantic.SecretStr + keycloak_password: pydantic.SecretStr | None = None keycloak_user_realm: str = "pydis" diff --git a/arthur/exts/directory/ldap.py b/arthur/exts/directory/ldap.py index dd2fc6c..70fe1ab 100644 --- a/arthur/exts/directory/ldap.py +++ b/arthur/exts/directory/ldap.py @@ -427,10 +427,23 @@ async def sync(self, ctx: commands.Context) -> None: async def setup(bot: KingArthur) -> None: """Add the extension to the bot.""" - if ldap.BONSAI_AVAILABLE and freeipa.BONSAI_AVAILABLE and CONFIG.enable_ldap: - await bot.add_cog(LDAP(bot)) - else: + if not all(ldap.BONSAI_AVAILABLE, freeipa.BONSAI_AVAILABLE, CONFIG.enable_ldap): logger.warning( "Not loading LDAP sync utilities as LDAP dependencies are not available " "or LDAP is disabled by config, see README.md for more." ) + return + if not all( + CONFIG.ldap_host, + CONFIG.ldap_bind_password, + CONFIG.ldap_certificate_location, + CONFIG.keycloak_address, + CONFIG.keycloak_password, + ): + logger.warning( + "Not loading LDAP sync utilities as one or more LDAP environment variables" + "are not set, see README.md for more." + ) + return + + await bot.add_cog(LDAP(bot))