def test_bind_not_connected(self): client = ldapclient.LDAPClient() self.assertRaises( ldapclient.LDAPClientConnectionLostException, client.bind, "cn=foo,ou=baz,dc=example,dc=net", )
def setUp(self): db = inmemory.ReadOnlyInMemoryLDAPEntry('', {}) com = db.addChild('dc=com', {'objectClass': ['dcObject'], 'dc': ['com'], }) com.addChild('dc=example', {'objectClass': ['dcObject'], 'dc': ['example'], 'subschemaSubentry': ['cn=schema'], }) db.addChild('cn=schema', {'objectClass': ['TODO'], 'cn': ['schema'], 'attributeTypes': [test_schema.AttributeType_KnownValues.knownValues[0][0]], 'objectClasses': [test_schema.OBJECTCLASSES['organization'], test_schema.OBJECTCLASSES['organizationalUnit'], ], }) class LDAPServerFactory(protocol.ServerFactory): protocol = ldapserver.LDAPServer def __init__(self, root): self.root = root components.registerAdapter(lambda x: x.root, LDAPServerFactory, interfaces.IConnectedLDAPEntry) serverFactory = LDAPServerFactory(db) self.client = ldapclient.LDAPClient() server = serverFactory.buildProtocol(address.IPv4Address('TCP', 'localhost', '1024')) util.returnConnected(server, self.client)
def create_test_client(self): """ Create test client and transport. """ client = ldapclient.LDAPClient() transport = proto_helpers.StringTransport() client.makeConnection(transport) return client, transport
def test_send_not_connected(self): client = ldapclient.LDAPClient() op = self.create_test_search_req() self.assertRaises( ldapclient.LDAPClientConnectionLostException, client.send_multiResponse, op, None, )
def test_simple(self): c = ldapclient.LDAPClient() c.makeConnection(proto_helpers.StringTransport()) d1 = c.send(SillyMessage('foo')) d2 = c.send(SillyMessage('bar')) c.connectionLost(SillyError()) def eb(fail): fail.trap(SillyError) d1.addCallbacks(testutil.mustRaise, eb) d2.addCallbacks(testutil.mustRaise, eb) return defer.DeferredList([d1, d2], fireOnOneErrback=True)
def setUp(self): db = inmemory.ReadOnlyInMemoryLDAPEntry("", {}) com = db.addChild( "dc=com", { "objectClass": ["dcObject"], "dc": ["com"], }, ) com.addChild( "dc=example", { "objectClass": ["dcObject"], "dc": ["example"], "subschemaSubentry": ["cn=schema"], }, ) db.addChild( "cn=schema", { "objectClass": ["TODO"], "cn": ["schema"], "attributeTypes": [test_schema.AttributeType_KnownValues.knownValues[0][0]], "objectClasses": [ test_schema.OBJECTCLASSES["organization"], test_schema.OBJECTCLASSES["organizationalUnit"], ], }, ) class LDAPServerFactory(protocol.ServerFactory): protocol = ldapserver.LDAPServer def __init__(self, root): self.root = root components.registerAdapter(lambda x: x.root, LDAPServerFactory, interfaces.IConnectedLDAPEntry) serverFactory = LDAPServerFactory(db) self.client = ldapclient.LDAPClient() server = serverFactory.buildProtocol( address.IPv4Address("TCP", "localhost", "1024")) util.returnConnected(server, self.client)
def get_ldap_client(self): """ Returns a Deferred that fires with an asynchronous LDAP client. """ log = self.log log.debug( "{classname}: Entered `get_ldap_client()`. self.id == {instance_id}", classname=self.__class__.__name__, instance_id=id(self)) client = self.client_ if not client is None: self.clear_scheduled_unbind_() log.debug( "{classname}: Cleared scheduled UNBIND; returning existing LDAP client.", classname=self.__class__.__name__) returnValue(client) start_tls = self.start_tls endpoint_s = self.endpoint_s bind_dn = self.bind_dn bind_passwd = self.bind_passwd reactor = self.reactor ep = endpoints.clientFromString(reactor, endpoint_s) log.debug("LDAP endpoint: {endpoint}", endpoint=endpoint_s) client = yield endpoints.connectProtocol(ep, ldapclient.LDAPClient()) self.client_ = client log.debug("Client connection established to LDAP endpoint.") try: if start_tls: yield client.startTLS() log.debug("{classname}: LDAP client initiated StartTLS.", event_type='ldap_starttls', classname=self.__class__.__name__) if bind_dn and bind_passwd: yield client.bind(bind_dn, bind_passwd) log.debug("LDAP client BIND as '{bind_dn}'.", event_type='ldap_bind', bind_dn=bind_dn) except Exception: self.unbind_client_() raise self.clear_scheduled_unbind_() log.debug("'{classname}': Returning new LDAP client.", classname=self.__class__.__name__) returnValue(client)
def get_ldap_client_(self): """ Get an authenticated LDAP client. """ log = self.log endpoint_s = self.endpoint_s bind_dn = self.bind_dn bind_passwd = self.bind_passwd base_dn = self.base_dn reactor = self.reactor use_starttls = self.use_starttls starttls_trust_anchor = self.starttls_trust_anchor log.debug("Endpoint: {endpoint}", endpoint=endpoint_s) e = clientFromString(reactor, endpoint_s) client = yield connectProtocol(e, ldapclient.LDAPClient()) if use_starttls: log.debug("StartTLS trust anchor: {trust}", trust=starttls_trust_anchor) yield client.startTLS(starttls_trust_anchor) yield client.bind(bind_dn, bind_passwd) defer.returnValue(client)
def test_unbind_not_connected(self): client = ldapclient.LDAPClient() self.assertRaises(Exception, client.unbind)