refactor: LDAP backend initialization

This commit is contained in:
Éloi Rivard 2023-12-27 10:57:22 +01:00
parent a237fa0198
commit e8b620588e
No known key found for this signature in database
GPG key ID: 7EDA204EA57DD184
10 changed files with 15 additions and 34 deletions

View file

@ -128,8 +128,6 @@ def create_app(config=None, validate=True, backend=None):
try:
setup_logging(app)
backend = setup_backend(app, backend)
if app.debug:
backend.install(app.config, True)
setup_flask_converters(app)
setup_blueprints(app)
setup_jinja(app)

View file

@ -8,4 +8,4 @@ class InstallationException(Exception):
def install(config, debug=False):
install_oidc(config, debug=debug)
BaseBackend.get().install(config, debug=debug)
BaseBackend.get().install(config)

View file

@ -33,7 +33,7 @@ class BaseBackend:
self.teardown()
@classmethod
def install(self, config, debug=False):
def install(self, config):
"""
This methods prepares the database to host canaille data.
"""

View file

@ -54,7 +54,7 @@ class Backend(BaseBackend):
setup_ldap_models(config)
@classmethod
def install(cls, config, debug=False):
def install(cls, config):
cls.setup_schemas(config)
with cls(config).session():
models.Token.install()

View file

@ -3,7 +3,7 @@ from canaille.backends import BaseBackend
class Backend(BaseBackend):
@classmethod
def install(cls, config, debug=False):
def install(cls, config):
pass
def setup(self):

View file

@ -19,7 +19,7 @@ class Backend(BaseBackend):
db_session = None
@classmethod
def install(cls, config, debug=False): # pragma: no cover
def install(cls, config): # pragma: no cover
engine = create_engine(
config["BACKENDS"]["SQL"]["SQL_DATABASE_URI"],
echo=False,

View file

@ -17,6 +17,7 @@ schemas = [
"ldif/memberof-config.ldif",
"ldif/refint-config.ldif",
"ldif/ppolicy-config.ldif",
"../canaille/backends/ldap/schemas/oauth2-openldap.ldif",
]
slapd = slapd.Slapd(
@ -30,35 +31,18 @@ slapd = slapd.Slapd(
slapd.start()
try:
suffix_dc = slapd.suffix.split(",")[0][3:]
slapd.ldapadd(
"\n".join(
[
"dn: " + slapd.suffix,
"objectClass: dcObject",
"objectClass: organization",
"dc: " + suffix_dc,
"o: " + suffix_dc,
"",
"dn: " + slapd.root_dn,
"objectClass: applicationProcess",
"cn: " + slapd.root_cn,
]
)
+ "\n"
)
slapd.init_tree()
for ldif in (
"ldif/ppolicy.ldif",
"ldif/bootstrap-users-tree.ldif",
"ldif/bootstrap-oidc-tree.ldif",
):
with open(ldif) as fd:
try:
slapd.ldapadd(fd.read())
slapd.ldapadd(None, ["-f", ldif])
except RuntimeError:
pass
slapd.logger.info("slapd initialized: all ldif files loaded")
slapd.wait()
finally:
slapd.stop()

View file

@ -18,8 +18,7 @@ def slapd_server():
"demo/ldif/bootstrap-users-tree.ldif",
"demo/ldif/bootstrap-oidc-tree.ldif",
):
with open(ldif) as fd:
slapd.ldapadd(fd.read())
slapd.ldapadd(None, ["-f", ldif])
yield slapd
finally:
slapd.stop()

View file

@ -25,8 +25,7 @@ def slapd_server():
"demo/ldif/bootstrap-users-tree.ldif",
"demo/ldif/bootstrap-users.ldif",
):
with open(ldif) as fd:
slapd.ldapadd(fd.read())
slapd.ldapadd(None, ["-f", ldif])
yield slapd
finally:

View file

@ -127,6 +127,7 @@ def configuration(smtpd):
@pytest.fixture
def app(configuration, backend):
app = create_app(configuration, backend=backend)
backend.install(app.config)
with app.test_request_context():
yield app