Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ckan upgrade 2.8.0a - permit alternative DistinguishedName (dn) #49

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ Configuration of an LDAP client is always tricky. Unfortunately this really vari
The plugin provides the following **required** configuration items:

- `ckanext.ldap.uri`: The URI of the LDAP server, of the form _ldap://example.com_. You can use the URI to specify TLS (use 'ldaps' protocol), and the port number (suffix ':port');
- `ckanext.ldap.base_dn`: The base dn in which to perform the search. Example: 'ou=USERS,dc=example,dc=com';
- `ckanext.ldap.base_dn`: The base Dinstinguished Name (DN) in which to perform the search. Example: 'ou=USERS,dc=example,dc=com';
- `ckanext.ldap.base_dn_alt`: The alternative DN in case you have to look in more than one Organizational Unit (OU). This item is optional, use only if needed. Example: 'ou="Other OU",dc=example,dc=com';
- `ckanext.ldap.search.filter`: This is the search string that is sent to the LDAP server, in which '{login}' is replaced by the user name provided by the user. Example: 'sAMAccountName={login}'. The search performed here **must** return exactly 0 or 1 entry. See `ckanext.ldap.search.alt` to provide search on alternate fields;
- `ckanext.ldap.username`: The LDAP attribute that will be used as the CKAN username. This **must** be unique;
- `ckanext.ldap.email`: The LDAP attribute to map to the user's email address. This **must** be unique.
Expand Down
24 changes: 15 additions & 9 deletions ckanext/ldap/controllers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def _login_success(self, user_name, came_from):
'''
session[u'ckanext-ldap-user'] = user_name
session.save()
toolkit.redirect_to(u'user.logged_in', came_from=came_from)
toolkit.redirect_to(controller=u'user', action=u'logged_in', came_from=came_from)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reverts back to the old pylons syntax.

Suggested change
toolkit.redirect_to(controller=u'user', action=u'logged_in', came_from=came_from)
toolkit.redirect_to(u'user.logged_in', came_from=came_from)



def _get_user_dict(user_id):
Expand Down Expand Up @@ -287,23 +287,29 @@ def _find_ldap_user(login):

filter_str = config[u'ckanext.ldap.search.filter'].format(
login=ldap.filter.escape_filter_chars(login))
filter_str_alt = config[u'ckanext.ldap.search.alt'].format(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filter_str_alt = config[u'ckanext.ldap.search.alt'].format(
filter_str_alt = config.get(u'ckanext.ldap.search.alt', u'').format(

login=ldap.filter.escape_filter_chars(login))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ckanext.ldap.search.alt is optional so I'm pretty sure this will raise an exception if it's not defined in the config.

attributes = [config[u'ckanext.ldap.username']]
if u'ckanext.ldap.fullname' in config:
attributes.append(config[u'ckanext.ldap.fullname'])
if u'ckanext.ldap.email' in config:
attributes.append(config[u'ckanext.ldap.email'])

try:
ret = _ldap_search(cnx, filter_str, attributes, non_unique=u'log')
ret = _ldap_search(cnx, filter_str, attributes, config[u'ckanext.ldap.base_dn'], non_unique=u'log')
if ret is None and u'ckanext.ldap.search.alt' in config:
filter_str = config[u'ckanext.ldap.search.alt'].format(
login=ldap.filter.escape_filter_chars(login))
ret = _ldap_search(cnx, filter_str, attributes, non_unique=u'raise')
ret = _ldap_search(cnx, filter_str_alt, attributes, config[u'ckanext.ldap.base_dn'], non_unique=u'raise')
if ret is None and u'ckanext.ldap.base_dn_alt' in config:
ret = _ldap_search(cnx, filter_str, attributes, config[u'ckanext.ldap.base_dn_alt'], non_unique=u'log
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ret = _ldap_search(cnx, filter_str, attributes, config[u'ckanext.ldap.base_dn_alt'], non_unique=u'log
ret = _ldap_search(cnx, filter_str, attributes, config[u'ckanext.ldap.base_dn_alt'], non_unique=u'log')

if ret is None and u'ckanext.ldap.base_dn_alt' in config and u'ckanext.ldap.search.alt' in config:
ret = _ldap_search(cnx, filter_str_alt, attributes, config[u'ckanext.ldap.base_dn_alt'], non_unique=u'raise
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ret = _ldap_search(cnx, filter_str_alt, attributes, config[u'ckanext.ldap.base_dn_alt'], non_unique=u'raise
ret = _ldap_search(cnx, filter_str_alt, attributes, config[u'ckanext.ldap.base_dn_alt'], non_unique=u'raise')


finally:
cnx.unbind()
return ret


def _ldap_search(cnx, filter_str, attributes, non_unique=u'raise'):
def _ldap_search(cnx, filter_str, attributes, base_dn_str, non_unique=u'raise'):
'''Helper function to perform the actual LDAP search

:param cnx: The LDAP connection object
Expand All @@ -321,14 +327,14 @@ def _ldap_search(cnx, filter_str, attributes, non_unique=u'raise'):

'''
try:
res = cnx.search_s(config[u'ckanext.ldap.base_dn'], ldap.SCOPE_SUBTREE,
res = cnx.search_s(base_dn_str, ldap.SCOPE_SUBTREE,
filterstr=filter_str, attrlist=attributes)

except ldap.SERVER_DOWN:
log.error(u'LDAP server is not reachable')
return None
except ldap.OPERATIONS_ERROR as e:
log.error(
u'LDAP query failed. Maybe you need auth credentials for performing searches? Error returned by the server: ' + e.info)
log.error(u'LDAP query failed. Maybe you need auth credentials for performing searches?')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the error info be added back in? Or another way or getting more information about the error for the logs?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The e.info does not exist. I was getting a Notice in the error log, that says the info atribute is not available when it did enter in that except.
I don't know how I could get more information to de logs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ldap library's doc isn't all that clear but I think from my quick look the info attribute is optional and dependant on whether the server returned an extra info or not. Perhaps we could check if there is an info attribute available or not and then log it or not based on that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.error(u'LDAP query failed. Maybe you need auth credentials for performing searches?')
error_msg = u'LDAP query failed. Maybe you need auth credentials for performing searches?'
if hasattr(e, u'info'):
error_msg += u' Error returned by the server: ' + e.info
log.error(error_msg)

return None
except (ldap.NO_SUCH_OBJECT, ldap.REFERRAL) as e:
log.error(
Expand Down
3 changes: 3 additions & 0 deletions ckanext/ldap/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def configure(self, main_config):
u'ckanext.ldap.base_dn': {
u'required': True
},
u'ckanext.ldap.base_dn_alt': {
u'required': False
},
u'ckanext.ldap.search.filter': {
u'required': True
},
Expand Down