Projet

Général

Profil

0002-ldap-display-server-error-on-technical-info-backoffi.patch

Paul Marillonnet, 17 juin 2022 12:09

Télécharger (4,29 ko)

Voir les différences:

Subject: [PATCH 2/2] ldap: display server error on technical info backoffice
 page (#65491)

 .../authentic2/manager/ldap_details.html          |  7 +++++--
 src/authentic2/manager/views.py                   | 15 ++++++++-------
 tests/test_ldap.py                                |  3 ++-
 3 files changed, 15 insertions(+), 10 deletions(-)
src/authentic2/manager/templates/authentic2/manager/ldap_details.html
1 1
{% load i18n %}
2 2
<h4>{% trans "Realm:" %} {{ ldap.realm }}</h4>
3 3
<div class="a2-manager-ldap-{{ ldap.realm }}">
4
{% if not error %}
4
{% if not ldap.error %}
5 5
<h5>{% blocktrans %}Base ldapsearch command:{% endblocktrans %}</h5>
6 6
 <pre>ldapsearch -v -H {{ ldap.ldap_uri }} -D "{{ ldap.binddn }}" -w "{{ ldap.bindpw }}" -b "{{ ldap.basedn }}"{% if ldap.user_filter or ldap.sync_ldap_users_filter %} "{% firstof ldap.sync_ldap_users_filter ldap.user_filter %}"{% endif %}</pre>
7 7
{% else %}
8 8
<div class="error">
9
{% blocktrans %}Error while attempting to connect to LDAP server, base ldapsearch command won't be displayed.{% endblocktrans %}
9
<p>{% blocktrans %}Error while attempting to connect to LDAP server, base ldapsearch command won't be displayed.{% endblocktrans %}</p>
10
{% if ldap.errmsg %}
11
<p>{% blocktrans %}Server error: {{ errmsg }}{% endblocktrans %}</p>
12
{% endif %}
10 13
</div>
11 14
{% endif %}
12 15
<h5>{% trans "Configuration:" %}</h5>
src/authentic2/manager/views.py
675 675
        backend = ldap_backend.LDAPBackend
676 676
        kwargs['ldap_list'] = []
677 677
        for block in backend.get_config():
678
            config = block.copy()
679
            conn, dummy = backend.get_connection(config)
678
            ldap_info = block.copy()
679
            conn, errmsg = backend.get_connection(ldap_info)
680 680
            if not conn:
681
                kwargs['error'] = True
681
                ldap_info['error'] = True
682
                ldap_info['errmsg'] = errmsg
682 683
            else:
683 684
                # retrieve ldap uri, not directly visible in configuration block
684
                config['ldap_uri'] = conn.get_option(ldap.OPT_URI)
685
                ldap_info['ldap_uri'] = conn.get_option(ldap.OPT_URI)
685 686
                # user filters need to be formatted to ldapsearch syntax
686
                config['user_filter'] = force_text(block.get('user_filter'), '').replace('%s', '*')
687
                config['sync_ldap_users_filter'] = (
687
                ldap_info['user_filter'] = force_text(block.get('user_filter'), '').replace('%s', '*')
688
                ldap_info['sync_ldap_users_filter'] = (
688 689
                    force_text(block.get('sync_ldap_users_filter'), '').replace('%s', '*').replace('%s', '*')
689 690
                )
690 691

  
691
            kwargs['ldap_list'].append(config)
692
            kwargs['ldap_list'].append(ldap_info)
692 693
        return super().get_context_data(**kwargs)
693 694

  
694 695

  
tests/test_ldap.py
2289 2289
        assert opt in ldap_config_text
2290 2290

  
2291 2291
    # mock a buggy connection
2292
    monkeypatch.setattr(ldap_backend.LDAPBackend, 'get_connection', lambda x: (None, None))
2292
    monkeypatch.setattr(ldap_backend.LDAPBackend, 'get_connection', lambda x: (None, 'some buggy connection error message'))
2293 2293
    resp = app.get(reverse('a2-manager-tech-info'))
2294 2294
    ldap_config_text = resp.pyquery('div#a2-manager-tech-info-ldap-list').text()
2295 2295

  
2296 2296
    assert 'Base ldapsearch command' not in ldap_config_text
2297 2297
    assert 'Error while attempting to connect to LDAP server' in ldap_config_text
2298
    assert 'some buggy connection error message' in ldap_config_text
2298 2299

  
2299 2300

  
2300 2301
class TestLookup:
2301
-