Moved canaille.ldap_backend to canaille.backends.ldap

This commit is contained in:
Éloi Rivard 2023-04-08 20:09:52 +02:00
parent c4676ec572
commit fa62c16768
25 changed files with 97 additions and 85 deletions

View file

@ -1,5 +1,5 @@
include canaille/conf/*.sample.*
graft canaille/ldap_backend/schemas
graft canaille/backends/ldap/schemas
graft canaille/templates
graft canaille/themes
graft canaille/translations

View file

@ -173,7 +173,7 @@ def create_app(config=None, validate=True):
sentry_sdk = setup_sentry(app)
try:
from .oidc.oauth import setup_oauth
from .ldap_backend.backend import init_backend
from .backends.ldap.backend import init_backend
from .app.i18n import setup_i18n
setup_logging(app)

View file

@ -9,7 +9,7 @@ from flask.cli import with_appcontext
def with_backendcontext(func):
@functools.wraps(func)
def _func(*args, **kwargs):
from canaille.ldap_backend.backend import (
from canaille.backends.ldap.backend import (
setup_backend,
teardown_backend,
)

View file

@ -16,7 +16,7 @@ def validate(config, validate_remote=False):
if not validate_remote:
return
from canaille.ldap_backend.backend import validate_configuration
from canaille.backends.ldap.backend import validate_configuration
validate_configuration(config)
validate_smtp_configuration(config)

View file

@ -1,5 +1,5 @@
import ldap.filter
from canaille.ldap_backend.ldapobject import LDAPObject
from canaille.backends.ldap.ldapobject import LDAPObject
from flask import current_app
from flask import session

View file

@ -1,7 +1,7 @@
import os
from canaille.ldap_backend.installation import install_schema
from canaille.ldap_backend.installation import ldap_connection
from canaille.backends.ldap.installation import install_schema
from canaille.backends.ldap.installation import ldap_connection
from canaille.oidc.models import AuthorizationCode
from canaille.oidc.models import Client
from canaille.oidc.models import Consent
@ -54,5 +54,5 @@ def setup_schemas(config):
install_schema(
config,
os.path.dirname(os.path.dirname(__file__))
+ "/ldap_backend/schemas/oauth2-openldap.ldif",
+ "/backends/ldap/schemas/oauth2-openldap.ldif",
)

View file

@ -4,7 +4,7 @@ from authlib.oauth2.rfc6749 import AuthorizationCodeMixin
from authlib.oauth2.rfc6749 import ClientMixin
from authlib.oauth2.rfc6749 import TokenMixin
from authlib.oauth2.rfc6749 import util
from canaille.ldap_backend.ldapobject import LDAPObject
from canaille.backends.ldap.ldapobject import LDAPObject
class Client(LDAPObject, ClientMixin):

View file

@ -10,7 +10,7 @@ services:
- ./ldif/memberof-config.ldif:/container/service/slapd/assets/config/bootstrap/ldif/03-memberOf.ldif:ro
# memberof overlay is already present in openldap docker image but only for groupOfUniqueNames. We need to overwrite it (until canaille can handle groupOfUniqueNames).
# https://github.com/osixia/docker-openldap/blob/master/image/service/slapd/assets/config/bootstrap/ldif/03-memberOf.ldif
- ../canaille/ldap_backend/schemas/oauth2-openldap.ldif:/container/service/slapd/assets/config/bootstrap/ldif/custom/40-oauth2.ldif:ro
- ../canaille/backends/ldap/schemas/oauth2-openldap.ldif:/container/service/slapd/assets/config/bootstrap/ldif/custom/40-oauth2.ldif:ro
- ./ldif/bootstrap-users-tree.ldif:/container/service/slapd/assets/config/bootstrap/ldif/custom/50-bootstrap-users-tree.ldif:ro
- ./ldif/bootstrap-oidc-tree.ldif:/container/service/slapd/assets/config/bootstrap/ldif/custom/50-bootstrap-oidc-tree.ldif:ro
- ./ldif/bootstrap-users.ldif:/container/service/slapd/assets/config/bootstrap/ldif/custom/60-bootstrap-users.ldif:ro

View file

@ -14,7 +14,7 @@ slapd = slapd.Slapd(
"cosine.ldif",
"nis.ldif",
"inetorgperson.ldif",
"../canaille/ldap_backend/schemas/oauth2-openldap.ldif",
"../canaille/backends/ldap/schemas/oauth2-openldap.ldif",
"ldif/memberof-config.ldif",
),
)

View file

@ -68,8 +68,8 @@ Old fashion: Copy the schemas in your filesystem
.. code-block:: bash
test -d /etc/openldap/schema && sudo cp "$CANAILLE_INSTALL_DIR/env/lib/python*/site-packages/canaille/ldap_backend/schemas/*" /etc/openldap/schema
test -d /etc/ldap/schema && sudo cp "$CANAILLE_INSTALL_DIR/env/lib/python*/site-packages/canaille/ldap_backend/schemas/*" /etc/ldap/schema
test -d /etc/openldap/schema && sudo cp "$CANAILLE_INSTALL_DIR/env/lib/python*/site-packages/canaille/backends/ldap/schemas/*" /etc/openldap/schema
test -d /etc/ldap/schema && sudo cp "$CANAILLE_INSTALL_DIR/env/lib/python*/site-packages/canaille/backends/ldap/schemas/*" /etc/ldap/schema
sudo service slapd restart
New fashion: Use slapadd to add the schemas
@ -80,7 +80,7 @@ Be careful to stop your ldap server before running ``slapadd``
.. code-block:: bash
sudo service slapd stop
sudo -u openldap slapadd -n0 -l "$CANAILLE_INSTALL_DIR/env/lib/python*/site-packages/canaille/ldap_backend/schemas/*.ldif"
sudo -u openldap slapadd -n0 -l "$CANAILLE_INSTALL_DIR/env/lib/python*/site-packages/canaille/backends/ldap/schemas/*.ldif"
sudo service slapd start
Generate the key pair

View file

@ -1,7 +1,5 @@
import os
from unittest import mock
import ldap
import pytest
from canaille import create_app
from canaille.app.configuration import ConfigurationException
@ -9,61 +7,9 @@ from canaille.app.configuration import validate
from flask_webtest import TestApp
def test_ldap_connection_no_remote(testclient, configuration):
validate(configuration)
def test_ldap_connection_remote(testclient, configuration, slapd_connection):
validate(configuration, validate_remote=True)
def test_ldap_connection_remote_ldap_unreachable(testclient, configuration):
configuration["BACKENDS"]["LDAP"]["URI"] = "ldap://invalid-ldap.com"
with pytest.raises(
ConfigurationException,
match=r"Could not connect to the LDAP server",
):
validate(configuration, validate_remote=True)
def test_ldap_connection_remote_ldap_wrong_credentials(testclient, configuration):
configuration["BACKENDS"]["LDAP"]["BIND_PW"] = "invalid-password"
with pytest.raises(
ConfigurationException,
match=r"LDAP authentication failed with user",
):
validate(configuration, validate_remote=True)
def test_ldap_cannot_create_users(testclient, configuration, slapd_connection):
from canaille.core.models import User
def fake_init(*args, **kwarg):
raise ldap.INSUFFICIENT_ACCESS
with mock.patch.object(User, "__init__", fake_init):
with pytest.raises(
ConfigurationException,
match=r"cannot create users at",
):
validate(configuration, validate_remote=True)
def test_ldap_cannot_create_groups(testclient, configuration, slapd_connection):
from canaille.core.models import Group
def fake_init(*args, **kwarg):
raise ldap.INSUFFICIENT_ACCESS
with mock.patch.object(Group, "__init__", fake_init):
with pytest.raises(
ConfigurationException,
match=r"cannot create groups at",
):
validate(configuration, validate_remote=True)
def test_smtp_connection_remote_smtp_unreachable(testclient, configuration):
def test_smtp_connection_remote_smtp_unreachable(
testclient, slapd_connection, configuration
):
configuration["SMTP"]["HOST"] = "smtp://invalid-smtp.com"
with pytest.raises(
ConfigurationException,
@ -72,7 +18,9 @@ def test_smtp_connection_remote_smtp_unreachable(testclient, configuration):
validate(configuration, validate_remote=True)
def test_smtp_connection_remote_smtp_wrong_credentials(testclient, configuration):
def test_smtp_connection_remote_smtp_wrong_credentials(
testclient, slapd_connection, configuration
):
configuration["SMTP"]["PASSWORD"] = "invalid-password"
with pytest.raises(
ConfigurationException,
@ -81,7 +29,9 @@ def test_smtp_connection_remote_smtp_wrong_credentials(testclient, configuration
validate(configuration, validate_remote=True)
def test_smtp_connection_remote_smtp_no_credentials(testclient, configuration):
def test_smtp_connection_remote_smtp_no_credentials(
testclient, slapd_connection, configuration
):
del configuration["SMTP"]["LOGIN"]
del configuration["SMTP"]["PASSWORD"]
validate(configuration, validate_remote=True)
@ -97,7 +47,11 @@ def test_smtp_bad_tls(testclient, slapd_connection, smtpd, configuration):
@pytest.fixture
def themed_testclient(app, configuration):
def themed_testclient(
app,
configuration,
slapd_connection,
):
configuration["TESTING"] = True
root = os.path.dirname(os.path.abspath(__file__))
@ -109,7 +63,7 @@ def themed_testclient(app, configuration):
return TestApp(app)
def test_theme(testclient, themed_testclient):
def test_theme(testclient, themed_testclient, slapd_connection):
res = testclient.get("/login")
res.mustcontain(no="TEST_THEME")
@ -117,7 +71,7 @@ def test_theme(testclient, themed_testclient):
res.mustcontain("TEST_THEME")
def test_invalid_theme(configuration):
def test_invalid_theme(configuration, slapd_connection):
validate(configuration, validate_remote=False)
with pytest.raises(

View file

View file

View file

@ -1,14 +1,18 @@
import datetime
from unittest import mock
import ldap.dn
import pytest
from canaille.app.configuration import ConfigurationException
from canaille.app.configuration import validate
from canaille.backends.ldap.backend import setup_ldap_models
from canaille.backends.ldap.ldapobject import LDAPObject
from canaille.backends.ldap.ldapobject import python_attrs_to_ldap
from canaille.backends.ldap.utils import ldap_to_python
from canaille.backends.ldap.utils import python_to_ldap
from canaille.backends.ldap.utils import Syntax
from canaille.core.models import Group
from canaille.core.models import User
from canaille.ldap_backend.backend import setup_ldap_models
from canaille.ldap_backend.ldapobject import LDAPObject
from canaille.ldap_backend.ldapobject import python_attrs_to_ldap
from canaille.ldap_backend.utils import ldap_to_python
from canaille.ldap_backend.utils import python_to_ldap
from canaille.ldap_backend.utils import Syntax
def test_object_creation(slapd_connection):
@ -224,3 +228,57 @@ def test_object_class_update(slapd_connection, testclient):
user1.delete()
user2.delete()
def test_ldap_connection_no_remote(testclient, configuration):
validate(configuration)
def test_ldap_connection_remote(testclient, configuration, slapd_connection):
validate(configuration, validate_remote=True)
def test_ldap_connection_remote_ldap_unreachable(testclient, configuration):
configuration["BACKENDS"]["LDAP"]["URI"] = "ldap://invalid-ldap.com"
with pytest.raises(
ConfigurationException,
match=r"Could not connect to the LDAP server",
):
validate(configuration, validate_remote=True)
def test_ldap_connection_remote_ldap_wrong_credentials(testclient, configuration):
configuration["BACKENDS"]["LDAP"]["BIND_PW"] = "invalid-password"
with pytest.raises(
ConfigurationException,
match=r"LDAP authentication failed with user",
):
validate(configuration, validate_remote=True)
def test_ldap_cannot_create_users(testclient, configuration, slapd_connection):
from canaille.core.models import User
def fake_init(*args, **kwarg):
raise ldap.INSUFFICIENT_ACCESS
with mock.patch.object(User, "__init__", fake_init):
with pytest.raises(
ConfigurationException,
match=r"cannot create users at",
):
validate(configuration, validate_remote=True)
def test_ldap_cannot_create_groups(testclient, configuration, slapd_connection):
from canaille.core.models import Group
def fake_init(*args, **kwarg):
raise ldap.INSUFFICIENT_ACCESS
with mock.patch.object(Group, "__init__", fake_init):
with pytest.raises(
ConfigurationException,
match=r"cannot create groups at",
):
validate(configuration, validate_remote=True)

View file

@ -2,9 +2,9 @@ import ldap.ldapobject
import pytest
import slapd
from canaille import create_app
from canaille.backends.ldap.backend import setup_ldap_models
from canaille.core.models import Group
from canaille.core.models import User
from canaille.ldap_backend.backend import setup_ldap_models
from canaille.oidc.installation import setup_ldap_tree
from flask import g
from flask_webtest import TestApp
@ -52,7 +52,7 @@ def slapd_server():
slapd.init_tree()
for ldif in (
"demo/ldif/memberof-config.ldif",
"canaille/ldap_backend/schemas/oauth2-openldap.ldif",
"canaille/backends/ldap/schemas/oauth2-openldap.ldif",
"demo/ldif/bootstrap-users-tree.ldif",
"demo/ldif/bootstrap-oidc-tree.ldif",
):

View file

@ -4,8 +4,8 @@ import ldap
import pytest
from canaille import create_app
from canaille.app.installation import InstallationException
from canaille.backends.ldap.ldapobject import LDAPObject
from canaille.commands import cli
from canaille.ldap_backend.ldapobject import LDAPObject
from canaille.oidc.installation import setup_schemas
from flask_webtest import TestApp
from tests.conftest import CustomSlapdObject