Skip to content

Commit

Permalink
adding key handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kakwa committed May 31, 2015
1 parent 2860f5a commit c9b971e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
3 changes: 2 additions & 1 deletion ldapcherry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ def _init_backends(self, config):
raise BackendModuleLoadingFail(module)
try:
attrslist = self.attributes.get_backend_attributes(backend)
self.backends[backend] = bc.Backend(params, cherrypy.log, backend, attrslist)
key = self.attributes.get_backend_key(backend)
self.backends[backend] = bc.Backend(params, cherrypy.log, backend, attrslist, key)
except MissingParameter as e:
raise e
except:
Expand Down
5 changes: 5 additions & 0 deletions ldapcherry/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def get_backend_attributes(self, backend):
raise WrongBackend(backend)
return self.backend_attributes[backend]

def get_backend_key(self, backend):
if backend not in self.backends:
raise WrongBackend(backend)
return self.attributes[self.key]['backends'][backend]

def get_attributes(self):
"""get the list of groups from roles"""
return self.self_attributes
19 changes: 16 additions & 3 deletions ldapcherry/backend/backendLdap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, user):

class Backend(ldapcherry.backend.Backend):

def __init__(self, config, logger, name, attrslist):
def __init__(self, config, logger, name, attrslist, key):
self.config = config
self._logger = logger
self.backend_name = name
Expand All @@ -38,6 +38,7 @@ def __init__(self, config, logger, name, attrslist):
self.search_filter_tmpl = self.get_param('search_filter_tmpl')
self.dn_user_attr = self.get_param('dn_user_attr')
self.objectclasses = []
self.key = key
for o in re.split('\W+', self.get_param('objectclasses')):
self.objectclasses.append(self._str(o))

Expand Down Expand Up @@ -216,12 +217,24 @@ def rm_from_group(self, username):
pass

def search(self, searchstring):
ret = {}

searchfilter = self.search_filter_tmpl % {
'searchstring': searchstring
}

return self._search(searchfilter, None, self.userdn)
for u in self._search(searchfilter, None, self.userdn):
attrs = {}
attrs_tmp = u[1]
for attr in attrs_tmp:
value_tmp = attrs_tmp[attr]
if len(value_tmp) == 1:
attrs[attr] = value_tmp[0]
else:
attrs[attr] = value_tmp

if self.key in attrs:
ret[attrs[self.key]] = attrs
return ret

def get_user(self, username):
ret = {}
Expand Down
2 changes: 1 addition & 1 deletion ldapcherry/backend/backendSamba4.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

class Backend(ldapcherry.backend.Backend):

def __init__(self, config, logger, name, attrslist):
def __init__(self, config, logger, name, attrslist, key):
pass
40 changes: 20 additions & 20 deletions tests/test_BackendLdap.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def syslog_error(msg='', context='',
class TestError(object):

def testNominal(self):
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
inv = Backend(cfg, cherrypy.log, 'ldap', attr, 'uid')
return True

def testConnect(self):
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
inv = Backend(cfg, cherrypy.log, 'ldap', attr, 'uid')
ldap = inv._connect()
ldap.simple_bind_s(inv.binddn, inv.bindpassword)
return True
Expand All @@ -53,7 +53,7 @@ def testConnectSSL(self):
cfg2 = cfg.copy()
cfg2['uri'] = 'ldaps://ldap.ldapcherry.org:637'
cfg2['checkcert'] = 'on'
inv = Backend(cfg2, cherrypy.log, 'ldap', attr)
inv = Backend(cfg2, cherrypy.log, 'ldap', attr, 'uid')
ldap = inv._connect()
ldap.simple_bind_s(inv.binddn, inv.bindpassword)

Expand All @@ -62,7 +62,7 @@ def testLdapUnavaible(self):
cfg2['uri'] = 'ldaps://notaldap:637'
cfg2['checkcert'] = 'on'
cfg2['ca'] = './cfg/ca.crt'
inv = Backend(cfg2, cherrypy.log, 'ldap', attr)
inv = Backend(cfg2, cherrypy.log, 'ldap', attr, 'uid')
ldapc = inv._connect()
try:
ldapc.simple_bind_s(inv.binddn, inv.bindpassword)
Expand All @@ -76,7 +76,7 @@ def testConnectSSLWrongCA(self):
cfg2['uri'] = 'ldaps://ldap.ldapcherry.org:637'
cfg2['checkcert'] = 'on'
cfg2['ca'] = './cfg/wrong_ca.crt'
inv = Backend(cfg2, cherrypy.log, 'ldap', attr)
inv = Backend(cfg2, cherrypy.log, 'ldap', attr, 'uid')
ldapc = inv._connect()
try:
ldapc.simple_bind_s(inv.binddn, inv.bindpassword)
Expand All @@ -87,54 +87,54 @@ def testConnectSSLWrongCA(self):
# cfg2 = cfg.copy()
# cfg2['uri'] = 'ldaps://ldap.ldapcherry.org:637'
# cfg2['checkcert'] = 'off'
# inv = Backend(cfg2, cherrypy.log, 'ldap', attr)
# inv = Backend(cfg2, cherrypy.log, 'ldap', attr, 'uid')
# ldap = inv._connect()
# ldap.simple_bind_s(inv.binddn, inv.bindpassword)

def testAuthSuccess(self):
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
inv = Backend(cfg, cherrypy.log, 'ldap', attr, 'uid')
return True

def testAuthSuccess(self):
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
inv = Backend(cfg, cherrypy.log, 'ldap', attr, 'uid')
ret = inv.auth('jwatson', 'passwordwatson')
assert ret == True

def testAuthFailure(self):
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
inv = Backend(cfg, cherrypy.log, 'ldap', attr, 'uid')
res = inv.auth('notauser', 'password') or inv.auth('jwatson', 'notapassword')
assert res == False

def testMissingParam(self):
cfg2 = {}
return True
try:
inv = Backend(cfg2, cherrypy.log, 'ldap', attr)
inv = Backend(cfg2, cherrypy.log, 'ldap', attr, 'uid')
except MissingKey:
return
else:
raise AssertionError("expected an exception")

def testGetUser(self):
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
inv = Backend(cfg, cherrypy.log, 'ldap', attr, 'uid')
ret = inv.get_user('jwatson')
expected = {'uid': 'jwatson', 'cn': 'John Watson', 'sn': 'watson'}
assert ret == expected

def testGetUser(self):
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
inv = Backend(cfg, cherrypy.log, 'ldap', attr, 'uid')
ret = inv.get_groups('jwatson')
expected = ['cn=itpeople,ou=Groups,dc=example,dc=org']
assert ret == expected

def testSearchUser(self):
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
inv = Backend(cfg, cherrypy.log, 'ldap', attr, 'uid')
ret = inv.search('smith')
expected = [('cn=Sheri Smith,ou=People,dc=example,dc=org', {'uid': ['ssmith'], 'objectClass': ['inetOrgPerson'], 'carLicense': ['HERCAR 125'], 'sn': ['smith'], 'mail': ['s.smith@example.com', 'ssmith@example.com', 'sheri.smith@example.com'], 'homePhone': ['555-111-2225'], 'cn': ['Sheri Smith']}), ('cn=John Smith,ou=People,dc=example,dc=org', {'uid': ['jsmith'], 'objectClass': ['inetOrgPerson'], 'carLicense': ['HISCAR 125'], 'sn': ['Smith'], 'mail': ['j.smith@example.com', 'jsmith@example.com', 'jsmith.smith@example.com'], 'homePhone': ['555-111-2225'], 'cn': ['John Smith']})]
expected = {'ssmith': {'uid': 'ssmith', 'objectClass': 'inetOrgPerson', 'carLicense': 'HERCAR 125', 'sn': 'smith', 'mail': ['s.smith@example.com', 'ssmith@example.com', 'sheri.smith@example.com'], 'homePhone': '555-111-2225', 'cn': 'Sheri Smith'}, 'jsmith': {'uid': 'jsmith', 'objectClass': 'inetOrgPerson', 'carLicense': 'HISCAR 125', 'sn': 'Smith', 'mail': ['j.smith@example.com', 'jsmith@example.com', 'jsmith.smith@example.com'], 'homePhone': '555-111-2225', 'cn': 'John Smith'}}
assert ret == expected

def testAddUser(self):
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
inv = Backend(cfg, cherrypy.log, 'ldap', attr, 'uid')
user = {
'uid': 'test',
'sn': 'test',
Expand All @@ -148,7 +148,7 @@ def testAddUser(self):
inv.del_user('test')

def testAddUserDuplicate(self):
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
inv = Backend(cfg, cherrypy.log, 'ldap', attr, 'uid')
user = {
'uid': 'test',
'sn': 'test',
Expand All @@ -169,7 +169,7 @@ def testAddUserDuplicate(self):
raise AssertionError("expected an exception")

def testDelUserDontExists(self):
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
inv = Backend(cfg, cherrypy.log, 'ldap', attr, 'uid')
try:
inv.del_user('test')
inv.del_user('test')
Expand All @@ -179,13 +179,13 @@ def testDelUserDontExists(self):
raise AssertionError("expected an exception")

def testGetUser(self):
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
inv = Backend(cfg, cherrypy.log, 'ldap', attr, 'uid')
ret = inv.get_user('jwatson')
expected = {'sn': 'watson', 'uid': 'jwatson', 'cn': 'John Watson'}
assert ret == expected

def testAddUserMissingMustAttribute(self):
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
def testAddUserMissingMustattribute(self):
inv = Backend(cfg, cherrypy.log, 'ldap', attr, 'uid')
user = {
'uid': 'test',
'sn': 'test',
Expand Down

0 comments on commit c9b971e

Please sign in to comment.