def handle(self, *args, **options): try: backend = BackendHelper() target = TargetHelper() except Exception as e: print >>sys.stderr, e sys.exit(1) for lbeObjectTemplate in LBEObjectTemplate.objects.all(): now = datetime.datetime.now(utc) # the searchNewObjectcs methods search for backend object where createTimestamp > objectTemplate.imported_at for lbeObject in target.searchNewObjects(lbeObjectTemplate): # # TODO: take care about virtual/reference attributes # Example: an object where uid is computed as 'bbonfils', but backend value is 'asyd' lbeObject.synced_at = now backend.createObject(lbeObjectTemplate, lbeObject) lbeObjectTemplate.imported_at = now lbeObjectTemplate.save()
def handle(self, *args, **options): try: backend = BackendHelper() target = TargetHelper() except Exception as e: print >> sys.stderr, e sys.exit(1) for lbeObjectTemplate in LBEObjectTemplate.objects.all(): now = datetime.datetime.now(utc) # the searchNewObjectcs methods search for backend object where createTimestamp > objectTemplate.imported_at for lbeObject in target.searchNewObjects(lbeObjectTemplate): # # TODO: take care about virtual/reference attributes # Example: an object where uid is computed as 'bbonfils', but backend value is 'asyd' lbeObject.synced_at = now backend.createObject(lbeObjectTemplate, lbeObject) lbeObjectTemplate.imported_at = now lbeObjectTemplate.save()
class ReinitTarget(): def __init__(self): self.backend = BackendHelper() self.target = TargetHelper() def _remove(self, objectTemplate, objectInstance): try: self.target.delete(objectTemplate, objectInstance) except BaseException: # do not care if Target object does not exist pass def _create(self, objectTemplate, objectInstance): # replace attributes by changes set if exist if not objectInstance.changes['set'] == {}: # replace the _id value if needed: rdnAttributeName = objectTemplate.instanceNameAttribute.name if not objectInstance.attributes[rdnAttributeName][ 0] == objectInstance.changes['set'][rdnAttributeName][0]: self.backend.update_id( objectTemplate, objectInstance, objectInstance.changes['set'][rdnAttributeName][0]) # Replace changes['set'] to attributes objectInstance.attributes = objectInstance.changes['set'] self.target.create(objectTemplate, objectInstance) changes = {} changes['status'] = OBJECT_STATE_SYNCED changes['changes'] = {} changes['changes']['set'] = {} changes['changes']['type'] = -1 changes['synced_at'] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, objectInstance, changes) def start(self): for objectTemplate in LBEObjectTemplate.objects.all(): print " |-> Remove & recreate all objects from \033[91m'" + objectTemplate.name + "'\033[0m :" for objectInstance in self.backend.searchObjects(objectTemplate): # do not care about deleted (or in progress deleted) object if not objectInstance.status == OBJECT_CHANGE_DELETE_OBJECT and \ not objectInstance.status == OBJECT_STATE_DELETED: print " |-> \033[95m'" + objectInstance.displayName + "\033[0m' ..." self._remove(objectTemplate, objectInstance) self._create(objectTemplate, objectInstance)
class ReinitTarget(): def __init__(self): self.backend = BackendHelper() self.target = TargetHelper() def _remove(self, objectTemplate, objectInstance): try: self.target.delete(objectTemplate, objectInstance) except BaseException: # do not care if Target object does not exist pass def _create(self, objectTemplate, objectInstance): # replace attributes by changes set if exist if not objectInstance.changes['set'] == {}: # replace the _id value if needed: rdnAttributeName = objectTemplate.instanceNameAttribute.name if not objectInstance.attributes[rdnAttributeName][0] == objectInstance.changes['set'][rdnAttributeName][0]: self.backend.update_id(objectTemplate, objectInstance, objectInstance.changes['set'][rdnAttributeName][0]) # Replace changes['set'] to attributes objectInstance.attributes = objectInstance.changes['set'] self.target.create(objectTemplate, objectInstance) changes = {} changes['status'] = OBJECT_STATE_SYNCED changes['changes'] = {} changes['changes']['set'] = {} changes['changes']['type'] = -1 changes['synced_at'] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, objectInstance, changes) def start(self): for objectTemplate in LBEObjectTemplate.objects.all(): print " |-> Remove & recreate all objects from \033[91m'" + objectTemplate.name + "'\033[0m :" for objectInstance in self.backend.searchObjects(objectTemplate): # do not care about deleted (or in progress deleted) object if not objectInstance.status == OBJECT_CHANGE_DELETE_OBJECT and \ not objectInstance.status == OBJECT_STATE_DELETED: print " |-> \033[95m'" + objectInstance.displayName + "\033[0m' ..." self._remove(objectTemplate, objectInstance) self._create(objectTemplate, objectInstance)
def __init__(self): self.backend = BackendHelper() self.target = TargetHelper() self.start_date = django.utils.timezone.now()
class UpgradeTarget(): def __init__(self): self.backend = BackendHelper() self.target = TargetHelper() self.start_date = django.utils.timezone.now() def _changeClass(self, objectTemplate): objHelper = LBEObjectInstanceHelper(objectTemplate) try: scope = objHelper.callScriptClassMethod("scope_search") except BaseException: scope = 0 ob = self.backend.searchObjects(objectTemplate) for objectInstance in ob: try: oldClasses = self.target.getInstanceObjectClasses( objectTemplate, objectInstance, scope) except ldap.NO_SUCH_OBJECT: continue newClasses = objHelper.callScriptClassMethod("object_classes") if not sorted(oldClasses) == sorted(newClasses): print " |-> The object classes have changed for '\033[35m" + objectInstance.displayName + "\033[0m'" try: self.target.changeClass(objectTemplate, objectInstance, oldClasses, newClasses) except ldap.OBJECT_CLASS_VIOLATION as e: print " *\033[91mError to modify the object class " + str( newClasses) + ", skip it.\033[0m" print " *\033[91m" + e[0]['info'] + "\033[0m" def _changeRDN(self, objectTemplate): if objectTemplate.needReconciliationRDN: print " |-> Upgrade the RDN Target server for \033[35m" + objectTemplate.name + "\033[0m..." # Change the RDN Attribute to the Target Server ob = self.backend.searchObjects(objectTemplate) for o in ob: try: self.target.changeRDN( objectTemplate, o, objectTemplate.instanceNameBeforeAttribute.name, o.attributes[objectTemplate. instanceNameBeforeAttribute.name][0]) except BaseException: # if object does not exists into the Target Server pass self.backend.updateStatus(objectTemplate, o.name) # Update values for changing RDN attribute objectTemplate.instanceNameBeforeAttribute = None objectTemplate.needReconciliationRDN = False objectTemplate.save() print " |-> Done." def _createParent(self, lbeObjectTemplate, objService): base_dn = objService.callScriptClassMethod("base_dn") objectToCreate = base_dn.replace(settings.LDAP_SERVER['BASE_DN'], '')[:-1].split(',') objectToCreate.reverse() for i in range(0, len(objectToCreate)): dn = objectToCreate[i] + ',' for j in range(0, i): dn = dn + objectToCreate[j] + ',' dn = dn + settings.LDAP_SERVER['BASE_DN'] attrs = {} attrs['objectclass'] = ['top', 'organizationalUnit'] # do not care if the ou already exists try: self.target.createParent(dn, modlist.addModlist(attrs)) except BaseException: pass def _createObject(self, objectTemplate, objectInstance): self.target.create(objectTemplate, objectInstance) # Ok, the object is added, empty changes set, and update object status changes = {} changes['status'] = OBJECT_STATE_SYNCED changes['changes'] = {} changes['changes']['set'] = {} changes['changes']['type'] = -1 changes['synced_at'] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, objectInstance, changes) def _modifyObject(self, objectTemplate, objectInstance, SCOPE): rdnAttributeName = objectTemplate.instanceNameAttribute.name self.target.update(objectTemplate, objectInstance, SCOPE) if not objectInstance.attributes[rdnAttributeName][ 0] == objectInstance.changes['set'][rdnAttributeName][0]: self.backend.update_id( objectTemplate, objectInstance, objectInstance.changes['set'][rdnAttributeName][0]) # Update Backend value: changes = {} changes['status'] = OBJECT_STATE_SYNCED changes['changes'] = {} changes['changes']['set'] = {} changes['changes']['type'] = -1 changes['synced_at'] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, objectInstance, changes) def _deleteObject(self, objectTemplate, objectInstance): self.target.delete(objectTemplate, objectInstance) # Update Backend value: changes = {} changes['status'] = OBJECT_STATE_DELETED changes['changes'] = {} changes['changes']['set'] = {} changes['changes']['type'] = -1 changes['synced_at'] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, objectInstance, changes) def _getRDN(self, objectTemplate, listID): objectHelper = LBEObjectInstanceHelper(objectTemplate) baseDN = objectHelper.callScriptClassMethod('base_dn') listObjectID = [] for ID in listID: dn = objectTemplate.instanceNameAttribute.name + '=' + ID + ',' + baseDN listObjectID.append(dn) return listObjectID def _getID(self, listRDN): listID = [] for rdn in listRDN: listID.append(rdn.split('=')[1].split(',')[0]) return listID def start(self): print " Upgrade the Target server with the Backend server..." for objectTemplate in LBEObjectTemplate.objects.all(): # need to check if we need to change (before making reconciliation) the RDN attribute self._changeRDN(objectTemplate) # And the objects class self._changeClass(objectTemplate) # We're looking for all objects with state = OBJECT_STATE_AWAITING_SYNC for objectInstance in self.backend.searchObjectsToUpdate( objectTemplate): # First of all, applies all changes stored in backend [ such Virtual attributes ] # & create the parent DN if not exist: obj = LBEObjectInstanceHelper(objectTemplate, objectInstance) try: scope = obj.callScriptClassMethod("search_scope") except BaseException: scope = 0 self._createParent(objectTemplate, obj) #obj.compute(objectInstance) # then, upgrade: if objectInstance.changes[ 'type'] == OBJECT_CHANGE_CREATE_OBJECT: try: print " |-> Object '\033[35m" + objectInstance.displayName + "\033[0m' is \033[34mcreating\033[0m..." self._createObject(objectTemplate, objectInstance) # TODO: We should have a target exception rather ldap except ldap.ALREADY_EXISTS: print " |-> Object '\033[35m" + objectInstance.displayName + "'\033[0m already exists" changes = {} changes['status'] = OBJECT_STATE_SYNCED changes['changes'] = {} changes['changes']['set'] = {} changes['changes']['type'] = -1 changes['synced_at'] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, objectInstance, changes) pass elif objectInstance.changes[ 'type'] == OBJECT_CHANGE_DELETE_OBJECT: try: print " |-> Object '\033[35m" + objectInstance.displayName + "' is \033[33mdeleting\033[0m..." self._deleteObject(objectTemplate, objectInstance) except BaseException as e: print " |-> Object '\033[35m" + objectInstance.displayName + "'\033[0m does not exist." changes = {} changes['status'] = OBJECT_STATE_DELETED changes['changes'] = {} changes['changes']['set'] = {} changes['changes']['type'] = -1 changes['synced_at'] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, objectInstance, changes) pass elif objectInstance.changes[ 'type'] == OBJECT_CHANGE_UPDATE_OBJECT: try: print " |-> Object '\033[35m" + objectInstance.displayName + "'\033[0m is \033[36mupdating\033[0m..." # Group for group in LBEGroup.objects.all(): if group.objectTemplate.id == objectTemplate.id: GroupInstanceHelper(group).updateMember( obj.getObject(obj.instance.name)) self._modifyObject(objectTemplate, objectInstance, scope) except BaseException as e: print e print " |-> Object '\033[35m" + objectInstance.displayName + "' does not exist, being \033[34mcreated\033[0m..." # Create object if not exists: # Firstly, compute attributes values: # Then, create it: try: self._createObject(objectTemplate, objectInstance) except Exception as e: print e pass pass # Synced object: objectTemplate.synced_at = django.utils.timezone.now() objectTemplate.save() print '' print " Upgrade Groups Objects:" for groupTemplate in LBEGroup.objects.all(): for groupInstance in self.backend.searchObjectsToUpdate( groupTemplate): grp = GroupInstanceHelper(groupTemplate, groupInstance) try: scope = grp.callScriptClassMethod("search_scope") except BaseException: scope = 0 self._createParent(groupTemplate, grp) if groupInstance.changes[ 'type'] == OBJECT_CHANGE_CREATE_OBJECT: print " |-> Group '\033[35m" + groupInstance.displayName + "\033[0m' is \033[34mcreating\033[0m..." try: groupInstance.changes['set'][ grp.attributeName] = self._getRDN( groupTemplate.objectTemplate, groupInstance.changes['set'][ grp.attributeName]) self._createObject(groupTemplate, groupInstance) ############################################### if not groupInstance.changes['set'] == {}: groupInstance.changes['set'][ grp.attributeName] = self._getID( groupInstance.changes['set'][ grp.attributeName]) groupInstance.attributes[ 'cn'] = groupInstance.changes['set']['cn'] self.backend.updateObject( groupTemplate, groupInstance, { 'changes': { 'set': { 'cn': [groupInstance.displayName] }, 'type': -1 } }) ############################################### except ldap.ALREADY_EXISTS: print " |-> Group '\033[35m" + groupInstance.displayName + "'\033[0m already exists" elif groupInstance.changes[ 'type'] == OBJECT_CHANGE_UPDATE_OBJECT: try: print " |-> Group '\033[35m" + groupInstance.displayName + "'\033[0m is \033[36mupdating\033[0m..." groupInstance.changes['set'][ grp.attributeName] = self._getRDN( groupTemplate.objectTemplate, groupInstance.changes['set'][ grp.attributeName]) self._modifyObject(groupTemplate, groupInstance, scope) ############################################### groupInstance.changes['set'][ grp.attributeName] = self._getID( groupInstance.changes['set'][ grp.attributeName]) groupInstance.attributes['cn'] = groupInstance.changes[ 'set']['cn'] self.backend.updateObject( groupTemplate, groupInstance, { 'changes': { 'set': { 'cn': [groupInstance.displayName] }, 'type': -1 } }) ############################################### except BaseException as e: print e print " |-> Group '\033[35m" + groupInstance.displayName + "' does not exist, being \033[34mcreated\033[0m..." groupInstance.changes['set'][ grp.attributeName] = self._getRDN( groupTemplate.objectTemplate, groupInstance.changes['set'][ grp.attributeName]) self._createObject(groupTemplate, groupInstance) ############################################### groupInstance.changes['set'][ grp.attributeName] = self._getID( groupInstance.changes['set'][ grp.attributeName]) groupInstance.attributes['cn'] = groupInstance.changes[ 'set']['cn'] self.backend.updateObject( groupTemplate, groupInstance, { 'changes': { 'set': { 'cn': [groupInstance.displayName] }, 'type': -1 } }) ############################################### elif groupInstance.changes[ 'type'] == OBJECT_CHANGE_DELETE_OBJECT: print " |-> Group '\033[35m" + groupInstance.displayName + "' is \033[33mdeleting\033[0m..." self._deleteObject(groupTemplate, groupInstance) print " End."
def __init__(self): self.backend = BackendHelper() self.target = TargetHelper()
class ImportTarget(): def __init__(self): self.backend = BackendHelper() self.target = TargetHelper() self.start_date = django.utils.timezone.now() def _getID(self, listRDN): listID = [] for rdn in listRDN: listID.append(rdn.split('=')[1].split(',')[0]) return listID def save(self): print 'Checking for Objects which do not exist into LBE Backend but in LDAP Server:' for objectTemplate in LBEObjectTemplate.objects.all(): objectHelper = LBEObjectInstanceHelper(objectTemplate) try: scope = objectHelper.callScriptClassMethod("search_scope") except BaseException: scope = 0 filter = '(&' for oc in objectHelper.callScriptClassMethod('object_classes'): filter += '(objectClass=' + oc + ')' filter += ')' print '\033[91m' + objectTemplate.name + '\033[0m: (\033[95m' + objectHelper.callScriptClassMethod("base_dn") + '\033[0m) using \033[95m' + filter + '\033[0m' objTarget = self.target.searchObjects(objectTemplate, scope) objBackend = self.backend.searchObjects(objectTemplate) number = 0 for ot in objTarget: exist = False for ob in objBackend: if ot.name == ob.name: exist = True break if not exist: number += 1 print '=> Adding \033[95m' + ot.name + '\033[0m object into LBE Backend... ' print " values: " + str(ot.attributes) try: self.backend.createObject(objectTemplate, ot, True) print "\033[92mDone.\033[0m\n" except BaseException as e: print "\033[91mFail.\033[0m" print "''''''''" print e print "''''''''" if number == 0: print '<None>' # Synced object: objectTemplate.synced_at = django.utils.timezone.now() objectTemplate.save() print '.........................' print 'Checking for Groups which do not exist into LBE Backend but in Target:' for groupTemplate in LBEGroup.objects.all(): groupInstance = GroupInstanceHelper(groupTemplate) try: scope = groupInstance.callScriptClassMethod("search_scope") except BaseException: scope = 0 grpTarget = self.target.searchObjects(groupTemplate, scope) grpBackend = self.backend.searchObjects(groupTemplate) for gt in grpTarget: exist = False for gb in grpBackend: if gt.name == gb.name: exist = True break if not exist: # import only existing group into LBE config try: LBEGroup.objects.get(displayName__iexact=gt.displayName) except BaseException: continue print '=> Adding \033[95m' + gt.name + '\033[0m group into LBE Backend... ' print " values: " + str(gt.attributes) try: if groupInstance.attributeName in gt.attributes: gt.attributes[groupInstance.attributeName] = self._getID(gt.attributes[groupInstance.attributeName]) groupHelper = GroupInstanceHelper(groupTemplate, gt) groupHelper.createTemplate(True) #print " >\033[91mThis group does not exists in LBE Configuration Group.\033[0m" #print " >\033[91mIn order to see, manage it, please create it using some extra attribute:" #print " >\033[91m'Display Name': \033[95m" + gt.name + "\033[0m" #print " >\033[91mInto the Script file:" #print " >'DN Attribute': \033[95m" + groupHelper.callScriptClassMethod("base_dn") + "\033[91m" #print " >'Attribute Name' & 'Object Classes': as you wish.\033[0m" print "\033[92mDone.\033[0m\n" except BaseException as e: print "\033[91mFail.\033[0m\n" print "''''''''" print e print "''''''''" # Synced group: groupTemplate.synced_at = django.utils.timezone.now() groupTemplate.save() print "End."
class UpgradeBT(): def __init__(self): self.backend = BackendHelper() self.target = TargetHelper() self.start_date = django.utils.timezone.now() def _deleteORCreate(self, objectTemplate, ot): if objectTemplate.reconciliation_object_missing_policy == OBJECT_ADD_BACKEND: print " |-> Adding \033[95m'" + ot.displayName + "'\033[0m object into Backend... " try: self.backend.createObject(objectTemplate, ot, True) changes = {} changes['status'] = OBJECT_STATE_SYNCED changes['changes'] = {} changes['changes']['set'] = {} changes['changes']['type'] = -1 changes['synced_at'] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, ot, changes) except BaseException as e: print "''''''''" print e print "''''''''" elif objectTemplate.reconciliation_object_missing_policy == OBJECT_DELETE_TARGET: print " |-> Removing \033[95m'" + ot.displayName + "'\033[0m object from Target... " try: self.target.delete(objectTemplate, ot) except BaseException as e: print "''''''''" print e print "''''''''" def _upgradeObject(self, objectTemplate, objHelper, ot, ob): # check and replace ignore attributes: ignoreAttributes = objHelper.callScriptClassMethod("ignore_attributes") for key, values in ot.attributes.items(): if key in ignoreAttributes: ob.attributes[key] = ot.attributes[key] if not ot.attributes == ob.attributes: if objectTemplate.reconciliation_object_different_policy == TARGET: # check if values are empty []: # Then, skip it. numberEmpty = 0 for values in set(ot.attributes) ^ set(ob.attributes): try: # either empty or empty string: if ob.attributes[values] == [] or ob.attributes[values] == ['']: numberEmpty += 1 except BaseException: pass if not numberEmpty == 0 and numberEmpty == len(set(ot.attributes) ^ set(ob.attributes)): return print " |-> Upgrade Object '\033[35m" + ob.displayName + "\033[0m' into Target..." print " |-> -------------------------------------------- " print " ||-> Old Values: " + str(ot.attributes) print " ||-> New Values: " + str(ob.attributes) print " |-> -------------------------------------------- " # Remove & Add in order to add new attributes: try: # Remove: self.target.delete(objectTemplate, ob) # Add self.target.create(objectTemplate, ob) # Synced: changes = {} changes['status'] = OBJECT_STATE_SYNCED changes['changes'] = {} changes['changes']['set'] = {} changes['changes']['type'] = -1 changes['synced_at'] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, ob, changes) except BaseException as e: print e elif objectTemplate.reconciliation_object_different_policy == BACKEND: print " |-> Upgrade Object '\033[35m" + ob.displayName + "\033[0m' into Backend..." print " |-> -------------------------------------------- " print " ||-> Old Values: " + str(ob.attributes) print " ||-> New Values: " + str(ot.attributes) print " |-> -------------------------------------------- " try: self.backend.updateObject(objectTemplate, ob, ot) except BaseException as e: print e def start(self): print " Upgrade Server..." for objectTemplate in LBEObjectTemplate.objects.all(): print " |-> \033[91m" + objectTemplate.name + '\033[0m:' objHelper = LBEObjectInstanceHelper(objectTemplate) try: scope = objHelper.callScriptClassMethod("search_scope") except BaseException: scope = 0 objTarget = self.target.searchObjects(objectTemplate, scope) objBackend = self.backend.searchObjects(objectTemplate) # Target to Backend: for ot in objTarget: exist = False for ob in objBackend: if ot.name == ob.name: self._upgradeObject(objectTemplate, objHelper, ot, ob) exist = True break if not exist: self._deleteORCreate(objectTemplate, ot) # Synced object: objectTemplate.synced_at = django.utils.timezone.now() objectTemplate.save() print " End."
class UpgradeTarget: def __init__(self): self.backend = BackendHelper() self.target = TargetHelper() self.start_date = django.utils.timezone.now() def _changeClass(self, objectTemplate): objHelper = LBEObjectInstanceHelper(objectTemplate) try: scope = objHelper.callScriptClassMethod("scope_search") except BaseException: scope = 0 ob = self.backend.searchObjects(objectTemplate) for objectInstance in ob: try: oldClasses = self.target.getInstanceObjectClasses(objectTemplate, objectInstance, scope) except ldap.NO_SUCH_OBJECT: continue newClasses = objHelper.callScriptClassMethod("object_classes") if not sorted(oldClasses) == sorted(newClasses): print " |-> The object classes have changed for '\033[35m" + objectInstance.displayName + "\033[0m'" try: self.target.changeClass(objectTemplate, objectInstance, oldClasses, newClasses) except ldap.OBJECT_CLASS_VIOLATION as e: print " *\033[91mError to modify the object class " + str(newClasses) + ", skip it.\033[0m" print " *\033[91m" + e[0]["info"] + "\033[0m" def _changeRDN(self, objectTemplate): if objectTemplate.needReconciliationRDN: print " |-> Upgrade the RDN Target server for \033[35m" + objectTemplate.name + "\033[0m..." # Change the RDN Attribute to the Target Server ob = self.backend.searchObjects(objectTemplate) for o in ob: try: self.target.changeRDN( objectTemplate, o, objectTemplate.instanceNameBeforeAttribute.name, o.attributes[objectTemplate.instanceNameBeforeAttribute.name][0], ) except BaseException: # if object does not exists into the Target Server pass self.backend.updateStatus(objectTemplate, o.name) # Update values for changing RDN attribute objectTemplate.instanceNameBeforeAttribute = None objectTemplate.needReconciliationRDN = False objectTemplate.save() print " |-> Done." def _createParent(self, lbeObjectTemplate, objService): base_dn = objService.callScriptClassMethod("base_dn") objectToCreate = base_dn.replace(settings.LDAP_SERVER["BASE_DN"], "")[:-1].split(",") objectToCreate.reverse() for i in range(0, len(objectToCreate)): dn = objectToCreate[i] + "," for j in range(0, i): dn = dn + objectToCreate[j] + "," dn = dn + settings.LDAP_SERVER["BASE_DN"] attrs = {} attrs["objectclass"] = ["top", "organizationalUnit"] # do not care if the ou already exists try: self.target.createParent(dn, modlist.addModlist(attrs)) except BaseException: pass def _createObject(self, objectTemplate, objectInstance): self.target.create(objectTemplate, objectInstance) # Ok, the object is added, empty changes set, and update object status changes = {} changes["status"] = OBJECT_STATE_SYNCED changes["changes"] = {} changes["changes"]["set"] = {} changes["changes"]["type"] = -1 changes["synced_at"] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, objectInstance, changes) def _modifyObject(self, objectTemplate, objectInstance, SCOPE): rdnAttributeName = objectTemplate.instanceNameAttribute.name self.target.update(objectTemplate, objectInstance, SCOPE) if not objectInstance.attributes[rdnAttributeName][0] == objectInstance.changes["set"][rdnAttributeName][0]: self.backend.update_id(objectTemplate, objectInstance, objectInstance.changes["set"][rdnAttributeName][0]) # Update Backend value: changes = {} changes["status"] = OBJECT_STATE_SYNCED changes["changes"] = {} changes["changes"]["set"] = {} changes["changes"]["type"] = -1 changes["synced_at"] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, objectInstance, changes) def _deleteObject(self, objectTemplate, objectInstance): self.target.delete(objectTemplate, objectInstance) # Update Backend value: changes = {} changes["status"] = OBJECT_STATE_DELETED changes["changes"] = {} changes["changes"]["set"] = {} changes["changes"]["type"] = -1 changes["synced_at"] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, objectInstance, changes) def _getRDN(self, objectTemplate, listID): objectHelper = LBEObjectInstanceHelper(objectTemplate) baseDN = objectHelper.callScriptClassMethod("base_dn") listObjectID = [] for ID in listID: dn = objectTemplate.instanceNameAttribute.name + "=" + ID + "," + baseDN listObjectID.append(dn) return listObjectID def _getID(self, listRDN): listID = [] for rdn in listRDN: listID.append(rdn.split("=")[1].split(",")[0]) return listID def start(self): print " Upgrade the Target server with the Backend server..." for objectTemplate in LBEObjectTemplate.objects.all(): # need to check if we need to change (before making reconciliation) the RDN attribute self._changeRDN(objectTemplate) # And the objects class self._changeClass(objectTemplate) # We're looking for all objects with state = OBJECT_STATE_AWAITING_SYNC for objectInstance in self.backend.searchObjectsToUpdate(objectTemplate): # First of all, applies all changes stored in backend [ such Virtual attributes ] # & create the parent DN if not exist: obj = LBEObjectInstanceHelper(objectTemplate, objectInstance) try: scope = obj.callScriptClassMethod("search_scope") except BaseException: scope = 0 self._createParent(objectTemplate, obj) # obj.compute(objectInstance) # then, upgrade: if objectInstance.changes["type"] == OBJECT_CHANGE_CREATE_OBJECT: try: print " |-> Object '\033[35m" + objectInstance.displayName + "\033[0m' is \033[34mcreating\033[0m..." self._createObject(objectTemplate, objectInstance) # TODO: We should have a target exception rather ldap except ldap.ALREADY_EXISTS: print " |-> Object '\033[35m" + objectInstance.displayName + "'\033[0m already exists" changes = {} changes["status"] = OBJECT_STATE_SYNCED changes["changes"] = {} changes["changes"]["set"] = {} changes["changes"]["type"] = -1 changes["synced_at"] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, objectInstance, changes) pass elif objectInstance.changes["type"] == OBJECT_CHANGE_DELETE_OBJECT: try: print " |-> Object '\033[35m" + objectInstance.displayName + "' is \033[33mdeleting\033[0m..." self._deleteObject(objectTemplate, objectInstance) except BaseException as e: print " |-> Object '\033[35m" + objectInstance.displayName + "'\033[0m does not exist." changes = {} changes["status"] = OBJECT_STATE_DELETED changes["changes"] = {} changes["changes"]["set"] = {} changes["changes"]["type"] = -1 changes["synced_at"] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, objectInstance, changes) pass elif objectInstance.changes["type"] == OBJECT_CHANGE_UPDATE_OBJECT: try: print " |-> Object '\033[35m" + objectInstance.displayName + "'\033[0m is \033[36mupdating\033[0m..." # Group for group in LBEGroup.objects.all(): if group.objectTemplate.id == objectTemplate.id: GroupInstanceHelper(group).updateMember(obj.getObject(obj.instance.name)) self._modifyObject(objectTemplate, objectInstance, scope) except BaseException as e: print e print " |-> Object '\033[35m" + objectInstance.displayName + "' does not exist, being \033[34mcreated\033[0m..." # Create object if not exists: # Firstly, compute attributes values: # Then, create it: try: self._createObject(objectTemplate, objectInstance) except Exception as e: print e pass pass # Synced object: objectTemplate.synced_at = django.utils.timezone.now() objectTemplate.save() print "" print " Upgrade Groups Objects:" for groupTemplate in LBEGroup.objects.all(): for groupInstance in self.backend.searchObjectsToUpdate(groupTemplate): grp = GroupInstanceHelper(groupTemplate, groupInstance) try: scope = grp.callScriptClassMethod("search_scope") except BaseException: scope = 0 self._createParent(groupTemplate, grp) if groupInstance.changes["type"] == OBJECT_CHANGE_CREATE_OBJECT: print " |-> Group '\033[35m" + groupInstance.displayName + "\033[0m' is \033[34mcreating\033[0m..." try: groupInstance.changes["set"][grp.attributeName] = self._getRDN( groupTemplate.objectTemplate, groupInstance.changes["set"][grp.attributeName] ) self._createObject(groupTemplate, groupInstance) ############################################### if not groupInstance.changes["set"] == {}: groupInstance.changes["set"][grp.attributeName] = self._getID( groupInstance.changes["set"][grp.attributeName] ) groupInstance.attributes["cn"] = groupInstance.changes["set"]["cn"] self.backend.updateObject( groupTemplate, groupInstance, {"changes": {"set": {"cn": [groupInstance.displayName]}, "type": -1}}, ) ############################################### except ldap.ALREADY_EXISTS: print " |-> Group '\033[35m" + groupInstance.displayName + "'\033[0m already exists" elif groupInstance.changes["type"] == OBJECT_CHANGE_UPDATE_OBJECT: try: print " |-> Group '\033[35m" + groupInstance.displayName + "'\033[0m is \033[36mupdating\033[0m..." groupInstance.changes["set"][grp.attributeName] = self._getRDN( groupTemplate.objectTemplate, groupInstance.changes["set"][grp.attributeName] ) self._modifyObject(groupTemplate, groupInstance, scope) ############################################### groupInstance.changes["set"][grp.attributeName] = self._getID( groupInstance.changes["set"][grp.attributeName] ) groupInstance.attributes["cn"] = groupInstance.changes["set"]["cn"] self.backend.updateObject( groupTemplate, groupInstance, {"changes": {"set": {"cn": [groupInstance.displayName]}, "type": -1}}, ) ############################################### except BaseException as e: print e print " |-> Group '\033[35m" + groupInstance.displayName + "' does not exist, being \033[34mcreated\033[0m..." groupInstance.changes["set"][grp.attributeName] = self._getRDN( groupTemplate.objectTemplate, groupInstance.changes["set"][grp.attributeName] ) self._createObject(groupTemplate, groupInstance) ############################################### groupInstance.changes["set"][grp.attributeName] = self._getID( groupInstance.changes["set"][grp.attributeName] ) groupInstance.attributes["cn"] = groupInstance.changes["set"]["cn"] self.backend.updateObject( groupTemplate, groupInstance, {"changes": {"set": {"cn": [groupInstance.displayName]}, "type": -1}}, ) ############################################### elif groupInstance.changes["type"] == OBJECT_CHANGE_DELETE_OBJECT: print " |-> Group '\033[35m" + groupInstance.displayName + "' is \033[33mdeleting\033[0m..." self._deleteObject(groupTemplate, groupInstance) print " End."
class UpgradeBT(): def __init__(self): self.backend = BackendHelper() self.target = TargetHelper() self.start_date = django.utils.timezone.now() def _deleteORCreate(self, objectTemplate, ot): if objectTemplate.reconciliation_object_missing_policy == OBJECT_ADD_BACKEND: print " |-> Adding \033[95m'" + ot.displayName + "'\033[0m object into Backend... " try: self.backend.createObject(objectTemplate, ot, True) changes = {} changes['status'] = OBJECT_STATE_SYNCED changes['changes'] = {} changes['changes']['set'] = {} changes['changes']['type'] = -1 changes['synced_at'] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, ot, changes) except BaseException as e: print "''''''''" print e print "''''''''" elif objectTemplate.reconciliation_object_missing_policy == OBJECT_DELETE_TARGET: print " |-> Removing \033[95m'" + ot.displayName + "'\033[0m object from Target... " try: self.target.delete(objectTemplate, ot) except BaseException as e: print "''''''''" print e print "''''''''" def _upgradeObject(self, objectTemplate, objHelper, ot, ob): # check and replace ignore attributes: ignoreAttributes = objHelper.callScriptClassMethod("ignore_attributes") for key, values in ot.attributes.items(): if key in ignoreAttributes: ob.attributes[key] = ot.attributes[key] if not ot.attributes == ob.attributes: if objectTemplate.reconciliation_object_different_policy == TARGET: # check if values are empty []: # Then, skip it. numberEmpty = 0 for values in set(ot.attributes) ^ set(ob.attributes): try: # either empty or empty string: if ob.attributes[values] == [] or ob.attributes[ values] == ['']: numberEmpty += 1 except BaseException: pass if not numberEmpty == 0 and numberEmpty == len( set(ot.attributes) ^ set(ob.attributes)): return print " |-> Upgrade Object '\033[35m" + ob.displayName + "\033[0m' into Target..." print " |-> -------------------------------------------- " print " ||-> Old Values: " + str(ot.attributes) print " ||-> New Values: " + str(ob.attributes) print " |-> -------------------------------------------- " # Remove & Add in order to add new attributes: try: # Remove: self.target.delete(objectTemplate, ob) # Add self.target.create(objectTemplate, ob) # Synced: changes = {} changes['status'] = OBJECT_STATE_SYNCED changes['changes'] = {} changes['changes']['set'] = {} changes['changes']['type'] = -1 changes['synced_at'] = django.utils.timezone.now() self.backend.updateObject(objectTemplate, ob, changes) except BaseException as e: print e elif objectTemplate.reconciliation_object_different_policy == BACKEND: print " |-> Upgrade Object '\033[35m" + ob.displayName + "\033[0m' into Backend..." print " |-> -------------------------------------------- " print " ||-> Old Values: " + str(ob.attributes) print " ||-> New Values: " + str(ot.attributes) print " |-> -------------------------------------------- " try: self.backend.updateObject(objectTemplate, ob, ot) except BaseException as e: print e def start(self): print " Upgrade Server..." for objectTemplate in LBEObjectTemplate.objects.all(): print " |-> \033[91m" + objectTemplate.name + '\033[0m:' objHelper = LBEObjectInstanceHelper(objectTemplate) try: scope = objHelper.callScriptClassMethod("search_scope") except BaseException: scope = 0 objTarget = self.target.searchObjects(objectTemplate, scope) objBackend = self.backend.searchObjects(objectTemplate) # Target to Backend: for ot in objTarget: exist = False for ob in objBackend: if ot.name == ob.name: self._upgradeObject(objectTemplate, objHelper, ot, ob) exist = True break if not exist: self._deleteORCreate(objectTemplate, ot) # Synced object: objectTemplate.synced_at = django.utils.timezone.now() objectTemplate.save() print " End."
class DebugTarget(): def __init__(self): self.backend = BackendHelper() self.target = TargetHelper() """ Check which values' objects need to be sync and show them. """ def _needModification(self): print ' Objects need change:' number = 0 for objectTemplate in LBEObjectTemplate.objects.all(): # We're looking for all objects with state = OBJECT_STATE_AWAITING_SYNC for objectInstance in self.backend.searchObjectsToUpdate( objectTemplate): type = "" if objectInstance.changes[ 'type'] == OBJECT_CHANGE_CREATE_OBJECT: type += "\033[34mcreate" elif objectInstance.changes[ 'type'] == OBJECT_CHANGE_UPDATE_OBJECT: type += "\033[36mupdate" elif objectInstance.changes[ 'type'] == OBJECT_CHANGE_DELETE_OBJECT: type += "\033[33mdelete" type += "\033[0m" value = " " + type + ' \033[35m' + objectInstance.displayName + '\033[0m : ' valuesChanges = dict() for k in objectInstance.changes['set']: try: if objectInstance.attributes[ k] != objectInstance.changes['set'][k]: valuesChanges[k] = 'new Value: ' + str( objectInstance.changes['set'] [k]) + ' | old value: ' + str( objectInstance.attributes[k]) except KeyError: valuesChanges[k] = 'new Value: ' + str( objectInstance.changes['set'][k]) pass print value + str(valuesChanges) number += 1 if number == 0: print " \033[91m<None>\033[0m" print "" print ' Groups need change:' number = 0 for groupTemplate in LBEGroup.objects.all(): for groupInstance in self.backend.searchObjectsToUpdate( groupTemplate): number += 1 type = "" if groupInstance.changes[ 'type'] == OBJECT_CHANGE_CREATE_OBJECT: type += "\033[34mcreate" elif groupInstance.changes[ 'type'] == OBJECT_CHANGE_UPDATE_OBJECT: type += "\033[36mupdate" elif groupInstance.changes[ 'type'] == OBJECT_CHANGE_DELETE_OBJECT: type += "\033[33mdelete" type += "\033[0m" value = " " + type + ' \033[35m' + groupInstance.displayName + '\033[0m : ' valuesChanges = dict() for k in groupInstance.changes['set']: try: if groupInstance.attributes[ k] != groupInstance.changes['set'][k]: valuesChanges[k] = 'new Value: ' + str( groupInstance.changes['set'] [k]) + ' | old value: ' + str( groupInstance.attributes[k]) except KeyError: valuesChanges[k] = 'new Value: ' + str( groupInstance.changes['set'][k]) pass print value + str(valuesChanges) if number == 0: print " \033[91m<None>\033[0m" """ Show objects do not exist in LBE but LDAP. """ def _notExistObjectLBE(self): print ' Checking for Objects which do not exist into LBE but in LDAP Server:' for objectTemplate in LBEObjectTemplate.objects.all(): print " - \033[35m" + objectTemplate.name + '\033[0m...' objHelper = LBEObjectInstanceHelper(objectTemplate) try: scope = objHelper.callScriptClassMethod("search_scope") except BaseException: scope = 0 objTarget = self.target.searchObjects(objectTemplate, scope) objBackend = self.backend.searchObjects(objectTemplate) number = 0 for ot in objTarget: exist = False for ob in objBackend: if ot.name == ob.name: exist = True break if not exist: number += 1 print " " + ot.name if number == 0: print ' \033[91m<None>\033[0m' print "" print ' Checking for Groups which do not exist into LBE but in LDAP Server:' number = 0 for groupTemplate in LBEGroup.objects.all(): grpHelper = GroupInstanceHelper(groupTemplate) try: scope = grpHelper.callScriptClassMethod("search_scope") except BaseException: scope = 0 grpTarget = self.target.searchObjects( groupTemplate, scope, '(cn=' + groupTemplate.displayName + ')') grpBackend = self.backend.searchObjectsByPattern( groupTemplate, groupTemplate.displayName) if not grpBackend: print " - \033[36m" + groupTemplate.displayName + "\033[0m does not exists." number += 1 if number == 0: print ' \033[91m<None>\033[0m' def start(self): self._needModification() print "" print '\033[93m.........................\033[0m' print '\033[93m.........................\033[0m' print "" self._notExistObjectLBE()
class DebugTarget(): def __init__(self): self.backend = BackendHelper() self.target = TargetHelper() """ Check which values' objects need to be sync and show them. """ def _needModification(self): print ' Objects need change:' number = 0 for objectTemplate in LBEObjectTemplate.objects.all(): # We're looking for all objects with state = OBJECT_STATE_AWAITING_SYNC for objectInstance in self.backend.searchObjectsToUpdate(objectTemplate): type = "" if objectInstance.changes['type'] == OBJECT_CHANGE_CREATE_OBJECT: type += "\033[34mcreate" elif objectInstance.changes['type'] == OBJECT_CHANGE_UPDATE_OBJECT: type += "\033[36mupdate" elif objectInstance.changes['type'] == OBJECT_CHANGE_DELETE_OBJECT: type += "\033[33mdelete" type += "\033[0m" value = " " + type + ' \033[35m' + objectInstance.displayName + '\033[0m : ' valuesChanges = dict() for k in objectInstance.changes['set']: try: if objectInstance.attributes[k] != objectInstance.changes['set'][k]: valuesChanges[k] = 'new Value: ' + str( objectInstance.changes['set'][k]) + ' | old value: ' + str( objectInstance.attributes[k]) except KeyError: valuesChanges[k] = 'new Value: ' + str(objectInstance.changes['set'][k]) pass print value + str(valuesChanges) number += 1 if number == 0: print " \033[91m<None>\033[0m" print "" print ' Groups need change:' number = 0 for groupTemplate in LBEGroup.objects.all(): for groupInstance in self.backend.searchObjectsToUpdate(groupTemplate): number += 1 type = "" if groupInstance.changes['type'] == OBJECT_CHANGE_CREATE_OBJECT: type += "\033[34mcreate" elif groupInstance.changes['type'] == OBJECT_CHANGE_UPDATE_OBJECT: type += "\033[36mupdate" elif groupInstance.changes['type'] == OBJECT_CHANGE_DELETE_OBJECT: type += "\033[33mdelete" type += "\033[0m" value = " " + type + ' \033[35m' + groupInstance.displayName + '\033[0m : ' valuesChanges = dict() for k in groupInstance.changes['set']: try: if groupInstance.attributes[k] != groupInstance.changes['set'][k]: valuesChanges[k] = 'new Value: ' + str( groupInstance.changes['set'][k]) + ' | old value: ' + str( groupInstance.attributes[k]) except KeyError: valuesChanges[k] = 'new Value: ' + str(groupInstance.changes['set'][k]) pass print value + str(valuesChanges) if number == 0: print " \033[91m<None>\033[0m" """ Show objects do not exist in LBE but LDAP. """ def _notExistObjectLBE(self): print ' Checking for Objects which do not exist into LBE but in LDAP Server:' for objectTemplate in LBEObjectTemplate.objects.all(): print " - \033[35m" + objectTemplate.name + '\033[0m...' objHelper = LBEObjectInstanceHelper(objectTemplate) try: scope = objHelper.callScriptClassMethod("search_scope") except BaseException: scope = 0 objTarget = self.target.searchObjects(objectTemplate, scope) objBackend = self.backend.searchObjects(objectTemplate) number = 0 for ot in objTarget: exist = False for ob in objBackend: if ot.name == ob.name: exist = True break if not exist: number += 1 print " " + ot.name if number == 0: print ' \033[91m<None>\033[0m' print "" print ' Checking for Groups which do not exist into LBE but in LDAP Server:' number = 0 for groupTemplate in LBEGroup.objects.all(): grpHelper = GroupInstanceHelper(groupTemplate) try: scope = grpHelper.callScriptClassMethod("search_scope") except BaseException: scope = 0 grpTarget = self.target.searchObjects(groupTemplate, scope, '(cn=' + groupTemplate.displayName + ')') grpBackend = self.backend.searchObjectsByPattern(groupTemplate, groupTemplate.displayName) if not grpBackend: print " - \033[36m" + groupTemplate.displayName + "\033[0m does not exists." number += 1 if number == 0: print ' \033[91m<None>\033[0m' def start(self): self._needModification() print "" print '\033[93m.........................\033[0m' print '\033[93m.........................\033[0m' print "" self._notExistObjectLBE()
class ImportTarget(): def __init__(self): self.backend = BackendHelper() self.target = TargetHelper() self.start_date = django.utils.timezone.now() def _getID(self, listRDN): listID = [] for rdn in listRDN: listID.append(rdn.split('=')[1].split(',')[0]) return listID def save(self): print 'Checking for Objects which do not exist into LBE Backend but in LDAP Server:' for objectTemplate in LBEObjectTemplate.objects.all(): objectHelper = LBEObjectInstanceHelper(objectTemplate) try: scope = objectHelper.callScriptClassMethod("search_scope") except BaseException: scope = 0 filter = '(&' for oc in objectHelper.callScriptClassMethod('object_classes'): filter += '(objectClass=' + oc + ')' filter += ')' print '\033[91m' + objectTemplate.name + '\033[0m: (\033[95m' + objectHelper.callScriptClassMethod( "base_dn") + '\033[0m) using \033[95m' + filter + '\033[0m' objTarget = self.target.searchObjects(objectTemplate, scope) objBackend = self.backend.searchObjects(objectTemplate) number = 0 for ot in objTarget: exist = False for ob in objBackend: if ot.name == ob.name: exist = True break if not exist: number += 1 print '=> Adding \033[95m' + ot.name + '\033[0m object into LBE Backend... ' print " values: " + str(ot.attributes) try: self.backend.createObject(objectTemplate, ot, True) print "\033[92mDone.\033[0m\n" except BaseException as e: print "\033[91mFail.\033[0m" print "''''''''" print e print "''''''''" if number == 0: print '<None>' # Synced object: objectTemplate.synced_at = django.utils.timezone.now() objectTemplate.save() print '.........................' print 'Checking for Groups which do not exist into LBE Backend but in Target:' for groupTemplate in LBEGroup.objects.all(): groupInstance = GroupInstanceHelper(groupTemplate) try: scope = groupInstance.callScriptClassMethod("search_scope") except BaseException: scope = 0 grpTarget = self.target.searchObjects(groupTemplate, scope) grpBackend = self.backend.searchObjects(groupTemplate) for gt in grpTarget: exist = False for gb in grpBackend: if gt.name == gb.name: exist = True break if not exist: # import only existing group into LBE config try: LBEGroup.objects.get( displayName__iexact=gt.displayName) except BaseException: continue print '=> Adding \033[95m' + gt.name + '\033[0m group into LBE Backend... ' print " values: " + str(gt.attributes) try: if groupInstance.attributeName in gt.attributes: gt.attributes[ groupInstance.attributeName] = self._getID( gt.attributes[groupInstance.attributeName]) groupHelper = GroupInstanceHelper(groupTemplate, gt) groupHelper.createTemplate(True) #print " >\033[91mThis group does not exists in LBE Configuration Group.\033[0m" #print " >\033[91mIn order to see, manage it, please create it using some extra attribute:" #print " >\033[91m'Display Name': \033[95m" + gt.name + "\033[0m" #print " >\033[91mInto the Script file:" #print " >'DN Attribute': \033[95m" + groupHelper.callScriptClassMethod("base_dn") + "\033[91m" #print " >'Attribute Name' & 'Object Classes': as you wish.\033[0m" print "\033[92mDone.\033[0m\n" except BaseException as e: print "\033[91mFail.\033[0m\n" print "''''''''" print e print "''''''''" # Synced group: groupTemplate.synced_at = django.utils.timezone.now() groupTemplate.save() print "End."