def delete(self, baseDn): """ Delete entry @param baseDn: baseDn @type baseDn: string """ if not self.__connected: return # log disconnect event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_delete()) self.logSentEvent(shortEvt="delete", tplEvt=self.encapsule(ldap_event=tpl)) try: result = self.con.delete(baseDn) except Exception as e: # log error event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_error(details="%s" % e)) self.logRecvEvent(shortEvt="ldap delete - generic error", tplEvt=self.encapsule(ldap_event=tpl)) else: tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_response()) self.logRecvEvent(shortEvt="deleted", tplEvt=self.encapsule(ldap_event=tpl))
def disconnect(self): """ LDAP disconnection """ if not self.__connected: return # log disconnect event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_disconnect()) self.logSentEvent(shortEvt="disconnection", tplEvt=self.encapsule(ldap_event=tpl)) try: self.con.unbind() self.__connected = False except Exception as e: # log error event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_error(details="%s" % e)) self.logRecvEvent(shortEvt="ldap disconnect - generic error", tplEvt=self.encapsule(ldap_event=tpl)) else: # log disconnected event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_disconnected()) self.logRecvEvent(shortEvt="disconnected", tplEvt=self.encapsule(ldap_event=tpl))
def connect(self): """ LDAP connection """ # log connect event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_connect(dn=self.cfg['dn'])) self.logSentEvent(shortEvt="connection", tplEvt=self.encapsule(ldap_event=tpl)) try: if self.cfg["ssl-support"]: self.con = ldap.initialize('ldaps://%s:%s' % (self.cfg['ip'], self.cfg['port'])) else: self.con = ldap.initialize('ldap://%s:%s' % (self.cfg['ip'], self.cfg['port'])) self.con.bind_s(self.cfg['dn'], self.cfg['password']) self.__connected = True except ldap.INVALID_CREDENTIALS as e: # log error event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_error(details="%s" % e)) self.logRecvEvent(shortEvt="ldap connect - authentication error", tplEvt=self.encapsule(ldap_event=tpl)) except ldap.LDAPError as e: # log error event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_error(details="%s" % e)) self.logRecvEvent(shortEvt="ldap connect - error", tplEvt=self.encapsule(ldap_event=tpl)) except Exception as e: # log error event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_error(details="%s" % e)) self.logRecvEvent(shortEvt="ldap connect - generic error", tplEvt=self.encapsule(ldap_event=tpl)) else: # log connected event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_connected()) self.logRecvEvent(shortEvt="connected", tplEvt=self.encapsule(ldap_event=tpl))
def search(self, baseDn, filter="(objectClass=*)", attrs=[]): """ LDAP search query @param baseDn: baseDN for search @type baseDn: string @param filter: default (objectClass=*) @type filter: string @param attrs: attributes @type attrs: list """ if not self.__connected: return # log disconnect event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_search(bdn=baseDn, filter=filter)) self.logSentEvent(shortEvt="search", tplEvt=self.encapsule(ldap_event=tpl)) try: res = self.con.search_s(baseDn, ldap.SCOPE_SUBTREE, filter, attrs) except ldap.NO_SUCH_OBJECT: tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_error(details="%s" % e)) self.logRecvEvent(shortEvt="ldap search - no such object", tplEvt=self.encapsule(ldap_event=tpl)) except Exception as e: # log error event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_error(details="%s" % e)) self.logRecvEvent(shortEvt="ldap search - generic error", tplEvt=self.encapsule(ldap_event=tpl)) else: # log connected event r_tpl = TestTemplates.Template(parent=None) r_layer = r_tpl.prepareLayer(name="", data=res) tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_response(results=r_layer)) self.logRecvEvent(shortEvt="results (%s)" % len(res), tplEvt=self.encapsule(ldap_event=tpl))
def hasReceivedResponse(self, timeout=1.0): """ Wait ldap response event until the end of the timeout @param timeout: time max to wait to receive event in second (default=1s) @type timeout: float @return: an event matching with the template or None otherwise @rtype: templatemessage/none """ if not (isinstance(timeout, int) or isinstance(timeout, float)) or isinstance(timeout, bool): raise TestAdapterLib.ValueException( TestAdapterLib.caller(), "timeout argument is not a float or integer (%s)" % type(timeout)) # construct the expected template layer_ldap = templates.ldap() layer_ldap.addMore(more=templates.ldap_response()) expected = TestTemplates.TemplateMessage() expected.addLayer(layer=layer_ldap) # try to match the template evt = self.received(expected=expected, timeout=timeout) return evt
def add(self, baseDn, record=[]): """ LDAP add query @param baseDn: baseDN for search @type baseDn: string @param record: record to add @type record: list """ if not self.__connected: return r_tpl = TestTemplates.Template(parent=None) r_layer = r_tpl.prepareLayer(name="", data=record) # log disconnect event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_add(bdn=baseDn, record=r_layer)) self.logSentEvent(shortEvt="add", tplEvt=self.encapsule(ldap_event=tpl)) try: self.con.add_s(baseDn, record) except ldap.ALREADY_EXISTS as e: tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_error(details="%s" % str(e))) self.logRecvEvent(shortEvt="ldap add - entry already exits", tplEvt=self.encapsule(ldap_event=tpl)) except Exception as e: tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_error(details="%s" % str(e))) self.logRecvEvent(shortEvt="ldap add - generic error", tplEvt=self.encapsule(ldap_event=tpl)) else: tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_response()) self.logRecvEvent(shortEvt="add success", tplEvt=self.encapsule(ldap_event=tpl))
def addAttribute(self, baseDn, attrName, value): """ Add attribute @param baseDn: baseDn @type baseDn: string @param attrName: attrName @type attrName: string @param value: value to manipulate @type value: string """ if not self.__connected: return # log disconnect event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_add_attribute(bdn=baseDn, attr=attrName, value=value)) self.logSentEvent(shortEvt="add attribute", tplEvt=self.encapsule(ldap_event=tpl)) try: mod_attrs = [(ldap.MOD_ADD, attrName, value)] result = self.con.modify_s(baseDn, mod_attrs) except Exception as e: # log error event tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_error(details="%s" % e)) self.logRecvEvent(shortEvt="add attribute - generic error", tplEvt=self.encapsule(ldap_event=tpl)) else: tpl = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_response()) self.logRecvEvent(shortEvt="add attribute success", tplEvt=self.encapsule(ldap_event=tpl))
def isDisconnected(self, timeout=1.0): """ Waits to receive "disconnected" event until the end of the timeout @param timeout: time max to wait to receive event in second (default=1s) @type timeout: float @return: an event matching with the template or None otherwise @rtype: templatemessage """ if not (isinstance(timeout, int) or isinstance(timeout, float)) or isinstance(timeout, bool): raise TestAdapterLib.ValueException( TestAdapterLib.caller(), "timeout argument is not a float or integer (%s)" % type(timeout)) expected = templates.ldap(ip=self.cfg['ip'], port=self.cfg['port'], more=templates.ldap_disconnected()) evt = self.received(expected=self.encapsule(ldap_event=expected), timeout=timeout) return evt