2021-10-12 18:36:31 +00:00
|
|
|
import ldap
|
2021-10-12 16:24:51 +00:00
|
|
|
import os
|
2021-10-13 08:12:44 +00:00
|
|
|
import smtplib
|
|
|
|
import socket
|
2021-10-26 20:49:36 +00:00
|
|
|
import uuid
|
2021-10-12 16:24:51 +00:00
|
|
|
|
2021-10-28 13:24:34 +00:00
|
|
|
ROOT = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
2021-10-12 16:24:51 +00:00
|
|
|
|
2021-10-13 08:12:44 +00:00
|
|
|
class ConfigurationException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-10-12 16:24:51 +00:00
|
|
|
def validate(config, validate_remote=False):
|
2021-10-28 13:24:34 +00:00
|
|
|
validate_keypair(config)
|
|
|
|
validate_theme(config)
|
|
|
|
|
|
|
|
if not validate_remote:
|
|
|
|
return
|
|
|
|
|
|
|
|
validate_ldap_configuration(config)
|
|
|
|
validate_smtp_configuration(config)
|
|
|
|
|
|
|
|
|
|
|
|
def validate_keypair(config):
|
2021-10-12 16:24:51 +00:00
|
|
|
if not os.path.exists(config["JWT"]["PUBLIC_KEY"]):
|
2021-10-13 08:12:44 +00:00
|
|
|
raise ConfigurationException(
|
|
|
|
f'Public key does not exist {config["JWT"]["PUBLIC_KEY"]}'
|
|
|
|
)
|
2021-10-12 16:24:51 +00:00
|
|
|
|
|
|
|
if not os.path.exists(config["JWT"]["PRIVATE_KEY"]):
|
2021-10-13 08:12:44 +00:00
|
|
|
raise ConfigurationException(
|
|
|
|
f'Private key does not exist {config["JWT"]["PRIVATE_KEY"]}'
|
|
|
|
)
|
2021-10-12 16:24:51 +00:00
|
|
|
|
2021-10-13 08:12:44 +00:00
|
|
|
|
|
|
|
def validate_ldap_configuration(config):
|
2021-10-26 20:49:36 +00:00
|
|
|
from canaille.models import User, Group
|
|
|
|
|
2021-10-13 08:12:44 +00:00
|
|
|
try:
|
|
|
|
conn = ldap.initialize(config["LDAP"]["URI"])
|
|
|
|
if config["LDAP"].get("TIMEOUT"):
|
|
|
|
conn.set_option(ldap.OPT_NETWORK_TIMEOUT, config["LDAP"]["TIMEOUT"])
|
|
|
|
conn.simple_bind_s(config["LDAP"]["BIND_DN"], config["LDAP"]["BIND_PW"])
|
|
|
|
|
|
|
|
except ldap.SERVER_DOWN as exc:
|
|
|
|
raise ConfigurationException(
|
|
|
|
f'Could not connect to the LDAP server \'{config["LDAP"]["URI"]}\''
|
|
|
|
) from exc
|
|
|
|
|
|
|
|
except ldap.INVALID_CREDENTIALS as exc:
|
|
|
|
raise ConfigurationException(
|
|
|
|
f'LDAP authentication failed with user \'{config["LDAP"]["BIND_DN"]}\''
|
|
|
|
) from exc
|
|
|
|
|
2021-10-26 20:49:36 +00:00
|
|
|
try:
|
|
|
|
User.ocs_by_name(conn)
|
|
|
|
user = User(
|
|
|
|
objectClass=["inetOrgPerson"],
|
|
|
|
cn=f"canaille_{uuid.uuid4()}",
|
|
|
|
sn=f"canaille_{uuid.uuid4()}",
|
|
|
|
uid=f"canaille_{uuid.uuid4()}",
|
|
|
|
mail=f"canaille_{uuid.uuid4()}@mydomain.tld",
|
|
|
|
userPassword="{SSHA}fw9DYeF/gHTHuVMepsQzVYAkffGcU8Fz",
|
|
|
|
)
|
|
|
|
user.save(conn)
|
|
|
|
user.delete(conn)
|
|
|
|
|
|
|
|
except ldap.INSUFFICIENT_ACCESS as exc:
|
|
|
|
raise ConfigurationException(
|
2021-10-26 20:54:34 +00:00
|
|
|
f'LDAP user \'{config["LDAP"]["BIND_DN"]}\' cannot create '
|
2021-10-26 20:49:36 +00:00
|
|
|
f'users at \'{config["LDAP"]["USER_BASE"]}\''
|
|
|
|
) from exc
|
|
|
|
|
|
|
|
try:
|
|
|
|
Group.ocs_by_name(conn)
|
|
|
|
|
|
|
|
user = User(
|
|
|
|
objectClass=["inetOrgPerson"],
|
|
|
|
cn=f"canaille_{uuid.uuid4()}",
|
|
|
|
sn=f"canaille_{uuid.uuid4()}",
|
|
|
|
uid=f"canaille_{uuid.uuid4()}",
|
|
|
|
mail=f"canaille_{uuid.uuid4()}@mydomain.tld",
|
|
|
|
userPassword="{SSHA}fw9DYeF/gHTHuVMepsQzVYAkffGcU8Fz",
|
|
|
|
)
|
|
|
|
user.save(conn)
|
|
|
|
|
|
|
|
group = Group(
|
|
|
|
objectClass=["groupOfNames"],
|
|
|
|
cn=f"canaille_{uuid.uuid4()}",
|
|
|
|
member=[user.dn],
|
|
|
|
)
|
|
|
|
group.save(conn)
|
|
|
|
group.delete(conn)
|
|
|
|
|
|
|
|
except ldap.INSUFFICIENT_ACCESS as exc:
|
|
|
|
raise ConfigurationException(
|
2021-10-26 20:54:34 +00:00
|
|
|
f'LDAP user \'{config["LDAP"]["BIND_DN"]}\' cannot create '
|
2021-10-26 20:49:36 +00:00
|
|
|
f'groups at \'{config["LDAP"]["GROUP_BASE"]}\''
|
|
|
|
) from exc
|
|
|
|
|
|
|
|
finally:
|
|
|
|
user.delete(conn)
|
|
|
|
|
|
|
|
conn.unbind_s()
|
|
|
|
|
2021-10-13 08:12:44 +00:00
|
|
|
|
|
|
|
def validate_smtp_configuration(config):
|
|
|
|
try:
|
|
|
|
with smtplib.SMTP(
|
|
|
|
host=config["SMTP"]["HOST"],
|
|
|
|
port=config["SMTP"]["PORT"],
|
|
|
|
) as smtp:
|
|
|
|
if config["SMTP"].get("TLS"):
|
|
|
|
smtp.starttls()
|
|
|
|
|
|
|
|
if config["SMTP"].get("LOGIN"):
|
|
|
|
smtp.login(
|
|
|
|
user=config["SMTP"]["LOGIN"],
|
|
|
|
password=config["SMTP"].get("PASSWORD"),
|
|
|
|
)
|
|
|
|
except (socket.gaierror, ConnectionRefusedError) as exc:
|
|
|
|
raise ConfigurationException(
|
|
|
|
f'Could not connect to the SMTP server \'{config["SMTP"]["HOST"]}\''
|
|
|
|
) from exc
|
|
|
|
|
|
|
|
except smtplib.SMTPAuthenticationError as exc:
|
|
|
|
raise ConfigurationException(
|
|
|
|
f'SMTP authentication failed with user \'{config["SMTP"]["LOGIN"]}\''
|
|
|
|
) from exc
|
2021-10-12 18:36:31 +00:00
|
|
|
|
2021-10-12 16:24:51 +00:00
|
|
|
|
2021-10-28 13:24:34 +00:00
|
|
|
def validate_theme(config):
|
|
|
|
if not config.get("THEME"):
|
|
|
|
return
|
|
|
|
|
|
|
|
if not os.path.exists(config["THEME"]) and not os.path.exists(
|
|
|
|
os.path.join(ROOT, "themes", config["THEME"])
|
|
|
|
):
|
|
|
|
raise ConfigurationException(f'Cannot find theme \'{config["THEME"]}\'')
|