From 5a7ae858c21b1d5d27c33c51bf1951281b25a43b Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 12 Nov 2020 17:19:50 +0100 Subject: [PATCH] Connecting and disconnecting from LDA servers --- lib/ldap_migration/__init__.py | 107 ++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/lib/ldap_migration/__init__.py b/lib/ldap_migration/__init__.py index d0bd187..818f54e 100644 --- a/lib/ldap_migration/__init__.py +++ b/lib/ldap_migration/__init__.py @@ -13,9 +13,11 @@ import logging import copy import sys import os +import time # 3rd party modules +from ldap3 import Server, Connection, ALL, DSA, IP_V4_PREFERRED, SAFE_SYNC from ldap3.core.exceptions import LDAPException # Own modules @@ -28,7 +30,7 @@ from fb_tools.errors import FbAppError from .config import LDAPMigrationConfiguration -__version__ = '0.2.0' +__version__ = '0.3.0' LOG = logging.getLogger(__name__) CFG_BASENAME = 'ldap-migration.ini' @@ -240,12 +242,115 @@ class LDAPMigrationApplication(BaseApplication): self.initialized = True + # ------------------------------------------------------------------------- + def connect_source(self): + + server_opts = {} + if self.config.src_use_ssl: + server_opts['use_ssl'] = True + if self.config.src_port != 636: + server_opts['port'] = self.config.src_port + else: + server_opts['use_ssl'] = False + if self.config.src_port != 389: + server_opts['port'] = self.config.src_port + server_opts['get_info'] = DSA + server_opts['mode'] = IP_V4_PREFERRED + server_opts['connect_timeout'] = self.config.timeout + LOG.info("Connecting to source server {!r} ...".format(self.config.src_server)) + if self.verbose > 1: + LOG.debug( + "Connect options to source server {!r}: \n".format(self.config.src_server) + + pp(server_opts)) + + self.src_server = Server( + self.config.src_server, **server_opts) + + if self.verbose > 2: + LOG.debug("Source server {s}: {re}".format( + s=self.src_server, re=repr(self.src_server))) + + self.source = Connection( + self.src_server, self.config.src_bind_dn, self.config.src_bind_pw, + client_strategy=SAFE_SYNC, auto_bind=True) + + if self.verbose > 1: + LOG.debug( + "Info about source server {}:\n".format(self.src_server) + + repr(self.src_server.info)) + + # ------------------------------------------------------------------------- + def connect_target(self): + + server_opts = {} + if self.config.tgt_use_ssl: + server_opts['use_ssl'] = True + if self.config.tgt_port != 636: + server_opts['port'] = self.config.tgt_port + else: + server_opts['use_ssl'] = False + if self.config.tgt_port != 389: + server_opts['port'] = self.config.tgt_port + server_opts['get_info'] = ALL + server_opts['mode'] = IP_V4_PREFERRED + server_opts['connect_timeout'] = self.config.timeout + LOG.info("Connecting to target server {!r} ...".format(self.config.tgt_server)) + if self.verbose > 1: + LOG.debug( + "Connect options to target server {!r}: \n".format(self.config.tgt_server) + + pp(server_opts)) + + self.tgt_server = Server( + self.config.tgt_server, **server_opts) + + if self.verbose > 2: + LOG.debug("Target server {s}: {re}".format( + s=self.tgt_server, re=repr(self.tgt_server))) + + self.target = Connection( + self.tgt_server, self.config.tgt_bind_dn, self.config.tgt_bind_pw, + client_strategy=SAFE_SYNC, auto_bind=True) + + if self.verbose > 1: + LOG.debug( + "Info about target server {}:\n".format(self.tgt_server) + + repr(self.tgt_server.info)) + + # ------------------------------------------------------------------------- + def disconnect(self): + + if self.target: + LOG.info("Unbinding from target server {} ...".format(self.tgt_server)) + self.target.unbind() + self.target = None + + if self.source: + LOG.info("Unbinding from source server {} ...".format(self.src_server)) + self.source.unbind() + self.source = None + + if self.tgt_server: + LOG.info("Disconnecting from target server {!r} ...".format(self.config.tgt_server)) + self.tgt_server = None + + if self.src_server: + LOG.info("Disconnecting from source server {!r} ...".format(self.config.src_server)) + self.src_server = None + # ------------------------------------------------------------------------- def _run(self): LOG.info("Starting {a!r}, version {v!r} ...".format( a=self.appname, v=self.version)) + try: + self.connect_source() + self.connect_target() + LOG.info("Sleeping ...") + time.sleep(2) + finally: + self.disconnect() + LOG.info("Ending {a!r}.".format( a=self.appname, v=self.version)) -- 2.39.5